import Cookies from 'js-cookie'; import { params, Storeage } from '@wisdom-utils/utils/lib/helpers'; import { appService } from './api'; import { initMicroApps } from './micro'; import { actionCreators } from './containers/App/store'; import { getToken } from './utils/utils'; import loader, { render } from './render'; import store from './stores'; import Login from './pages/user/login/login'; // eslint-disable-next-line no-restricted-globals const namespace = `__PANDA_STORE__${location.hostname}`; window.createStoreage = new Storeage(namespace); export const initGlobalConfig = () => { /* 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) { // 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'); } /* eslint-disable */ // 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('client'); client = client && client !== 'undefined' ? client : 'city'; const queryConfig = (gateWayConfig = {}) => { appService .queryConfig({ client: client || 'city', ignoreSite: true, }) .then(res => { if (res) { const { data } = res; if (!data.client) { data.client = client; } // Cookies.set('client', data.client); store.dispatch( actionCreators.getConfig( Object.assign( {}, data, { token: '', access_token: '', userInfo: null, }, gateWayConfig, ), ), ); // Cookies.set('client', params.getParams('client')) // 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(); } // 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)); }); } appService .getWateWayConfig({ ignoreSite: true, }) .then(res => { /* eslint-disable */ const hasGateWay = !res || !res.data ? false : _.isString(res.data) ? JSON.parse(res.data) : typeof res.data === 'boolean' ? res.data : false; return { hasGateWay, apiGatewayDomain: `${window.location.origin}${hasGateWay ? '/PandaCore/GateWay' : ''}`, }; }) .then(gateWayConfig => { queryConfig(gateWayConfig); }) .catch(err => { queryConfig(); }); };