initConfig.js 4.88 KB
Newer Older
邓晓峰's avatar
邓晓峰 committed
1
import Cookies from 'js-cookie';
邓晓峰's avatar
邓晓峰 committed
2
import { params, Storeage } from '@wisdom-utils/utils/lib/helpers';
3 4 5
import { appService } from './api';
import { initMicroApps } from './micro';
import { actionCreators } from './containers/App/store';
邓晓峰's avatar
邓晓峰 committed
6
import { getToken } from './utils/utils';
7 8
import loader, { render } from './render';
import store from './stores';
9
import Login from './pages/user/login/login';
邓晓峰's avatar
邓晓峰 committed
10
// eslint-disable-next-line no-restricted-globals
11 12
const namespace = `__PANDA_STORE__${location.hostname}`;
window.createStoreage = new Storeage(namespace);
13
export const initGlobalConfig = () => {
邓晓峰's avatar
邓晓峰 committed
14 15 16 17 18 19 20 21
  /* eslint-disable */
  createStoreage.remove(`__PANDA_STORE__${location.hostname}`);
  window.globalConfig = {};
  window.__INITIAL_STATE__ = {};
  let config = window.globalConfig || {};
  // createStoreage.get('globalConfig') || {};
  store.dispatch(actionCreators.updateComplexConfig({}));
  if (!getToken() || config.token == null) {
22
    // eslint-disable-next-line no-undef
邓晓峰's avatar
邓晓峰 committed
23 24 25 26
    createStoreage.remove(namespace);
    config = {};
  }
  if (!params.getParams('client', window.location.search) && config) {
27
    // eslint-disable-next-line no-undef
邓晓峰's avatar
邓晓峰 committed
28 29
    createStoreage.remove(namespace);
  }
邓晓峰's avatar
邓晓峰 committed
30

邓晓峰's avatar
邓晓峰 committed
31 32 33 34 35 36 37 38
  // eslint-disable-next-line no-undef
  if (!createStoreage.get('globalConfig')) {
    window.createStoreage = new Storeage(namespace);
  }
  if (!getToken()) {
    localStorage.removeItem('loginSite');
  }
  /* eslint-disable */
39 40 41 42 43 44 45 46 47 48 49 50
  // if (window.globalConfig.token !== null && Object.keys(window.globalConfig).length > 0) {
  //   store.dispatch(actionCreators.getConfig(window.globalConfig));
  //   // render({ appContent: '', loading: true });
  //   initMicroApps(loader, store);
  //   if (config.isNewYear) {
  //     updateTheme('#ff9600');
  //   }
  // } else {
  //   // eslint-disable-next-line react-hooks/rules-of-hooks
    
  // }
  let client = params.getParams('client') || Cookies.get('city');
邓晓峰's avatar
邓晓峰 committed
51 52 53
    client = client && client !== 'undefined' ? client : 'city';
    appService
      .getWateWayConfig({
54
        ignoreSite: true,
邓晓峰's avatar
邓晓峰 committed
55 56 57 58
      })
      .then(res => {
        /* eslint-disable */
        const hasGateWay = !res || !res.data ? false : _.isString(res.data) ? JSON.parse(res.data) : typeof res.data === 'boolean' ? res.data : false;
59 60
        return {
          hasGateWay,
崔佳豪's avatar
崔佳豪 committed
61
          apiGatewayDomain: `${window.location.origin}${hasGateWay ? '/PandaCore/GateWay' : ''}`,
邓晓峰's avatar
邓晓峰 committed
62 63 64
        };
      })
      .then(gateWayConfig => {
65 66 67 68 69 70 71 72 73 74 75 76 77 78
        appService
          .queryConfig({
            client: client || 'city',
            ignoreSite: true,
          })
          .then(res => {
            if (res) {
              const data = res;
              if (!data.client) {
                data.client = client;
              }
              // Cookies.set('city', data.client);
              store.dispatch(
                actionCreators.getConfig(
邓晓峰's avatar
邓晓峰 committed
79 80 81 82 83 84 85 86 87 88
                  Object.assign(
                    {},
                    data,
                    {
                      token: '',
                      access_token: '',
                      userInfo: null,
                    },
                    gateWayConfig,
                  ),
89 90
                ),
              );
邓晓峰's avatar
邓晓峰 committed
91

邓晓峰's avatar
邓晓峰 committed
92
              // Cookies.set('city', params.getParams('client'))
邓晓峰's avatar
邓晓峰 committed
93

94 95 96
              if (data.loginTemplate === '新春 - 智联.html') {
                updateTheme('#ff9600');
              }
97
              // eslint-disable-next-line no-new
98 99 100 101
              if (getToken()) {
                // eslint-disable-next-line no-new
                new Login(
                  {
102 103 104 105 106 107
                    global: Object.assign({}, data,
                      {
                        token: getToken(),
                      },
                      gateWayConfig,
                    ),
108
                    // eslint-disable-next-line no-shadow
邓晓峰's avatar
邓晓峰 committed
109 110
                    updateConfig: data =>
                      store.dispatch(actionCreators.getConfig(data)),
111 112 113 114 115 116 117
                    isInit: false,
                    logout: () => store.dispatch(actionCreators.logout()),
                  },
                  () => {
                    (async () => {
                      (await (getToken() &&
                        window.globalConfig &&
邓晓峰's avatar
邓晓峰 committed
118 119
                        window.globalConfig.token)) &&
                        initMicroApps(loader, store);
120 121 122 123 124
                    })();
                  },
                  true,
                );
              }
邓晓峰's avatar
邓晓峰 committed
125

126
              // eslint-disable-next-line no-shadow
127
            }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            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));
          });
      });
邓晓峰's avatar
邓晓峰 committed
143
};