initConfig.js 5.12 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
  // 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
48

49
  // }
50
  let client = params.getParams('client') || Cookies.get('client');
邓晓峰's avatar
邓晓峰 committed
51
    client = client && client !== 'undefined' ? client : 'city';
52 53 54 55 56 57 58 59 60

    const queryConfig = (gateWayConfig = {}) => {
      appService
        .queryConfig({
          client: client || 'city',
          ignoreSite: true,
        })
        .then(res => {
          if (res) {
61
            const { data } = res;
62 63 64
            if (!data.client) {
              data.client = client;
            }
65
            // Cookies.set('client', data.client);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            store.dispatch(
              actionCreators.getConfig(
                Object.assign(
                  {},
                  data,
                  {
                    token: '',
                    access_token: '',
                    userInfo: null,
                  },
                  gateWayConfig,
                ),
              ),
            );

81
            // Cookies.set('client', params.getParams('client'))
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96
            // if (data.loginTemplate === '新春 - 智联.html') {
            //   updateTheme('#ff9600');
            // }
            const products = (data.products || []).map(item => {
              if (item.PackageName === 'civweb4') {
                return 'web4_console'
              }
              return `${item.PackageName}-main`;
            });
            if(products && products.length > 0) {
              Object.keys(products).forEach(item => {
                window[products[item]] && window[products[item]] && window[products[item]].unmount && window[products[item]].unmount({ store: store });
              });
              initMicroApps();
97 98 99 100 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 133 134
            }
            // eslint-disable-next-line no-new
            if (getToken()) {
              // eslint-disable-next-line no-new
              new Login(
                {
                  global: Object.assign({}, data,
                    {
                      token: getToken(),
                    },
                    gateWayConfig,
                  ),
                  // eslint-disable-next-line no-shadow
                  updateConfig: data => store.dispatch(actionCreators.getConfig(data)),
                  isInit: false,
                  logout: () => store.dispatch(actionCreators.logout()),
                },
                () => {
                  (async () => {
                    (await (getToken() &&
                      window.globalConfig &&
                      window.globalConfig.token)) &&
                      initMicroApps(loader, store);
                  })();
                },
                true,
              );
            }
          }
          return res;
        })
        .then(res => {
          render({ appContent: '', loading: true });
        })
        .catch(error => {
          store.dispatch(actionCreators.getConfigError(error));
        });
    }
邓晓峰's avatar
邓晓峰 committed
135 136
    appService
      .getWateWayConfig({
137
        ignoreSite: true,
邓晓峰's avatar
邓晓峰 committed
138 139 140 141
      })
      .then(res => {
        /* eslint-disable */
        const hasGateWay = !res || !res.data ? false : _.isString(res.data) ? JSON.parse(res.data) : typeof res.data === 'boolean' ? res.data : false;
142 143
        return {
          hasGateWay,
崔佳豪's avatar
崔佳豪 committed
144
          apiGatewayDomain: `${window.location.origin}${hasGateWay ? '/PandaCore/GateWay' : ''}`,
邓晓峰's avatar
邓晓峰 committed
145 146 147
        };
      })
      .then(gateWayConfig => {
148 149 150 151
        queryConfig(gateWayConfig);
      })
      .catch(err => {
        queryConfig();
152
      });
邓晓峰's avatar
邓晓峰 committed
153
};