initConfig.js 4.53 KB
Newer Older
1 2 3 4 5
import { appService } from './api';
import { initMicroApps } from './micro';
import { params, Storeage } from 'kit_utils';
import { actionCreators } from './containers/App/store';
import { getToken, isString } from './utils/utils';
6 7
import loader, { render } from './render';
import store from './stores';
8
import Login from './pages/user/login/login';
9 10
const namespace = `__PANDA_STORE__${location.hostname}`;
window.createStoreage = new Storeage(namespace);
11
export const initGlobalConfig = () => {
邓晓峰's avatar
邓晓峰 committed
12
  
13 14
    // eslint-disable-next-line no-debugger
    // eslint-disable-next-line no-undef
15
    createStoreage.remove(`__PANDA_STORE__${location.hostname}`);
16
    window.globalConfig = {};
17 18 19
    window.__INITIAL_STATE__ = {};
    let config = window.globalConfig || {};
    //createStoreage.get('globalConfig') || {};
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    store.dispatch(actionCreators.updateComplexConfig({}));
    if (!getToken() || config.token == null) {
      // eslint-disable-next-line no-undef
      createStoreage.remove(namespace);
      config = {};
    }
    if (!params.getParams('client', window.location.search) && config) {
      // eslint-disable-next-line no-undef
      createStoreage.remove(namespace);
    }
  
    // eslint-disable-next-line no-undef
    if (!createStoreage.get('globalConfig')) {
      window.createStoreage = new Storeage(namespace);
    }
    if (!getToken()) {
      localStorage.removeItem('loginSite');
    }
  
39 40
    if (window.globalConfig.token !== null && Object.keys(window.globalConfig).length > 0) {
      store.dispatch(actionCreators.getConfig(window.globalConfig));
41
     // render({ appContent: '', loading: true });
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      initMicroApps(loader, store);
      if (config.isNewYear) {
        updateTheme('#ff9600');
      }
    } else {
      appService.getWateWayConfig().then(res => {
        const hasGateWay =
          res && res.data && isString(res.data) ? JSON.parse(res.data) : res.data;
        if (res.code === 0 && res.data === 'true') {
          store.dispatch(
            actionCreators.getConfig(
              Object.assign({}, window.globalConfig, {
                hasGateWay,
                apiGatewayDomain: `${window.location.origin}/PandaCore`,
              }),
            ),
          );
        }
      });
      // eslint-disable-next-line react-hooks/rules-of-hooks
62
    const client = params.getParams('client')
63 64
    appService
        .queryConfig({
65
          client: client || 'city',
66 67 68
        })
        .then(res => {
          if (res) {
69
           
70
            store.dispatch(actionCreators.getConfig(Object.assign({})));
71 72 73 74 75 76 77
            const data = res;
            if (!data.client) {
              data.client = params.getParams('client') || 'city';
            }
            // Cookies.set('city', data.client);
            store.dispatch(
              actionCreators.getConfig(
邓晓峰's avatar
邓晓峰 committed
78 79 80
                Object.assign({}, data, {
                  token: '', 
                  access_token: '',
81
                  userInfo: null
邓晓峰's avatar
邓晓峰 committed
82
                }),
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
              ),
            );
            
            //Cookies.set('city', params.getParams('client'))
  
            if (data.loginTemplate === '新春 - 智联.html') {
              updateTheme('#ff9600');
            }
            // eslint-disable-next-line no-new
            if (getToken()) {
              // eslint-disable-next-line no-new
              new Login(
                {
                  global: Object.assign({}, data, {
                    token: getToken(),
                  }),
                  // eslint-disable-next-line no-shadow
邓晓峰's avatar
邓晓峰 committed
100
                  updateConfig: data =>store.dispatch(actionCreators.getConfig(data)),
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                  isInit: false,
                  logout: () => store.dispatch(actionCreators.logout()),
                },
                () => {
                  (async () => {
                    (await (getToken() &&
                      window.globalConfig &&
                      window.globalConfig.token)) && initMicroApps(loader, store);
                  })();
                },
                true,
              );
            }
  
            // eslint-disable-next-line no-shadow
          }
          return res;
        })
        // eslint-disable-next-line no-shadow
        .then(res => {
          // eslint-disable-next-line no-use-before-define
          // initSensorType();
          // eslint-disable-next-line no-use-before-define
          // initIsMock();
          render({ appContent: '', loading: true });
        })
        .catch(error => {
          console.log(error);
          store.dispatch(actionCreators.getConfigError(error));
        });
    }
  };