WithAuthToPageBaseHandler.java 4.55 KB
package cn.csbr.app.gui.handler;

import cn.csbr.app.auth.LoginService;
import cn.csbr.app.config.FxConfigure;
import cn.csbr.app.gui.GUIContext;
import cn.csbr.app.gui.LoginPage;
import cn.csbr.app.gui.page.MainPage;
import cn.csbr.app.gui.page.Page;
import cn.csbr.app.gui.page.WelcomePage;
import cn.csbr.app.gui.util.DialogUtils;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * 这个类实现eventhandler。
 * 需要实现页面跳转的handler都要继承自这个类。
 * 类中在跳转前会做权限查询。 并且提供了一个基础方法
 * @param <T>
 */
public abstract class WithAuthToPageBaseHandler<T extends Page> implements EventHandler{
    @Autowired
    private GUIContext context;
    @Autowired
    private LoginService loginService;
    @Autowired
    private MainPage mainPage;
    @Autowired
    private WelcomePage welcomePage;
    @Autowired
    private FxConfigure fxConfigure;
    @Autowired
    private LoginPage loginPage;
    @Value("${PiccMode}")
    private String PiccMode;
    @Override
    public void handle(Event event) {

        if(GUIContext.isreturn){
            GUIContext.isreturn=false;
            this.context.setCurrentPage(null);
            T page = createPage();
            toPre(page);
            page.refresh();
            context.updateMainContent(page.getContent());
            toEnd(page);
            return;
        }
        int i=0;
        if (ifAuthCheck() && !fxConfigure.isNodatabase()) {
            String menucode = getMenuCode();
            //   System.out.println(menucode);
            if("h_zw".equals(menucode)||"h_card".equals(menucode)) {
                if(GUIContext.isOK==1){
                    this.context.setCurrentPage(null);
                    T page = createPage();
                    toPre(page);
                    page.refresh();
                    context.updateMainContent(page.getContent());
                    toEnd(page);
                }
                return;
            }
            if ("h_reject".equals(menucode)||"h_query".equals(menucode)|| "h_setting".equals(menucode)||"h_banlance".equals(menucode)||"h_check".equals(menucode) ) {//退货设置操作
                if ("h_reject".equals(menucode)||"h_query".equals(menucode) || "h_setting".equals(menucode)||"h_banlance".equals(menucode))
                {
                    //  if (context.getLoginUserVali() == null) {
                    loginPage.start(context.getPrimaryStage());
                    //}
                }
                else
                {
                    if(PiccMode.equals("1")) {
                        if ("h_check".equals(menucode) || "h_query".equals(menucode)) {//退货设置操作
                            //  if (context.getLoginUserVali() == null) {
                            loginPage.start(context.getPrimaryStage());
                            //}
                        }else {
                            GUIContext.isOK=1;
                        }
                    }
                    else {
                        GUIContext.isOK=1;
                    }
                }


            }else {

                try {
                    boolean exist = loginService.menusExist(menucode);
                    if (!exist) {
                        Platform.runLater(() -> {
                            DialogUtils.showHeaderTextInformationAlert("您无此操作权限!", context.getPrimaryStage());
                        });
                        return;
                    }
                } catch (Exception e) {
                    Platform.runLater(() -> {
                        DialogUtils.showAlert(e.getMessage(), v -> {
                            context.updateMainContent(welcomePage.getContent());
                        });
                    });
                    return;
                }
            }

        }

        if(GUIContext.isOK==1){
            this.context.setCurrentPage(null);
            T page = createPage();
            toPre(page);
            page.refresh();
            context.updateMainContent(page.getContent());
            toEnd(page);
        }


    }

    protected boolean ifAuthCheck() {
        return true;
    }

    abstract protected String getMenuCode();

    abstract protected T createPage();

    abstract protected void toPre(T page);

    abstract protected void toEnd(T page);
    public void setMenuCode(String code){

    }

}