app.js 7.55 KB
Newer Older
dengxiaofeng's avatar
dengxiaofeng committed
1 2
import '!file-loader?name=[name].[ext]!./images/favicon.ico';
import './global.less';
3
// import '@babel/polyfill';
dengxiaofeng's avatar
dengxiaofeng committed
4
import 'animate.css/animate.css';
5
// import 'antd/dist/antd.less';
dengxiaofeng's avatar
dengxiaofeng committed
6 7 8
import 'file-loader?name=.htaccess!./.htaccess'; // eslint-disable-line import/extensions
import 'kit_utils/lib/format';
import 'sanitize.css/sanitize.css';
9 10 11 12

// import './locales/zh-CN';
// import './loader';
// import './vm';
dengxiaofeng's avatar
dengxiaofeng committed
13 14
import React from 'react';
import ReactDOM from 'react-dom';
15

邓晓峰's avatar
邓晓峰 committed
16
import { Button, message, notification } from 'antd';
dengxiaofeng's avatar
dengxiaofeng committed
17 18
import { ConnectedRouter } from 'connected-react-router/immutable';
import Immutable from 'immutable';
19
import Cookies from 'js-cookie';
邓晓峰's avatar
邓晓峰 committed
20
import { params, Storeage } from 'kit_utils';
dengxiaofeng's avatar
dengxiaofeng committed
21
import { Provider } from 'react-redux';
22 23 24 25 26

import { useIntl } from '@/locales/localeExports';
import { ErrorBoundary } from '@ant-design/pro-utils';
import { history } from '@wisdom-utils/runtime';

邓晓峰's avatar
邓晓峰 committed
27
import defaultSettings from '../config/defaultSetting';
28 29 30
// import themePluginConfig from '../config/themePluginConfig';
import { appService } from './api';
import { updateTheme } from './common/helpers/settingColor';
邓晓峰's avatar
邓晓峰 committed
31
import Container from './components/Container';
dengxiaofeng's avatar
dengxiaofeng committed
32 33 34
import configureStore from './configureStore';
import App from './containers/App';
import { actionCreators } from './containers/App/store';
35
import { LocaleContainer } from './locales/locale';
邓晓峰's avatar
邓晓峰 committed
36
import { initMicroApps } from './micro';
邓晓峰's avatar
邓晓峰 committed
37
import Login from './pages/user/login/login';
邓晓峰's avatar
邓晓峰 committed
38
import { getToken, isString } from './utils/utils';
39

邓晓峰's avatar
邓晓峰 committed
40 41 42 43
const isHttps = document.location.protocol === 'https:';
const { pwa } = defaultSettings;
// eslint-disable-next-line no-restricted-globals
const namespace = `__PANDA_STORE__${location.hostname}`;
邓晓峰's avatar
邓晓峰 committed
44
debugger
邓晓峰's avatar
邓晓峰 committed
45
window.createStoreage = new Storeage(namespace);
邓晓峰's avatar
邓晓峰 committed
46
// eslint-disable-next-line no-underscore-dangle
dengxiaofeng's avatar
dengxiaofeng committed
47 48
const initialState = Immutable.Map();
const store = configureStore(initialState, history);
49 50 51
const MOUNT_NODE = document.getElementById('root');

// window.umi_plugin_ant_themeVar = themePluginConfig.theme;
dengxiaofeng's avatar
dengxiaofeng committed
52
const render = () => {
邓晓峰's avatar
邓晓峰 committed
53
  // eslint-disable-next-line react-hooks/rules-of-hooks
dengxiaofeng's avatar
dengxiaofeng committed
54 55 56
  ReactDOM.render(
    <Provider store={store}>
      <ConnectedRouter history={history}>
57
        <LocaleContainer>
邓晓峰's avatar
邓晓峰 committed
58 59 60 61 62
          <ErrorBoundary>
            <Container>
              <App />
            </Container>
          </ErrorBoundary>
63
        </LocaleContainer>
dengxiaofeng's avatar
dengxiaofeng committed
64 65 66 67 68
      </ConnectedRouter>
    </Provider>,
    MOUNT_NODE,
  );
};
邓晓峰's avatar
邓晓峰 committed
69

dengxiaofeng's avatar
dengxiaofeng committed
70 71 72
const loader = (appContent, loading) => render({ appContent, loading });

const initLocale = () => {
邓晓峰's avatar
邓晓峰 committed
73 74
  localStorage.setItem('umi_locale', 'zh-CN');
};
dengxiaofeng's avatar
dengxiaofeng committed
75 76

const initGlobalConfig = () => {
邓晓峰's avatar
邓晓峰 committed
77 78
  // eslint-disable-next-line no-debugger
  debugger
邓晓峰's avatar
邓晓峰 committed
79
  // eslint-disable-next-line no-undef
邓晓峰's avatar
邓晓峰 committed
80 81
  let config = createStoreage.get('globalConfig') || {};
  store.dispatch(actionCreators.updateComplexConfig({}));
82
  if (!getToken() || config.token == null) {
邓晓峰's avatar
邓晓峰 committed
83
    // eslint-disable-next-line no-undef
邓晓峰's avatar
邓晓峰 committed
84
    createStoreage.remove(namespace);
85
    config = {};
dengxiaofeng's avatar
dengxiaofeng committed
86
  }
邓晓峰's avatar
邓晓峰 committed
87
  if (!params.getParams('client', window.location.search) && config) {
邓晓峰's avatar
邓晓峰 committed
88
    // eslint-disable-next-line no-undef
邓晓峰's avatar
邓晓峰 committed
89
    createStoreage.remove(namespace);
dengxiaofeng's avatar
dengxiaofeng committed
90 91
  }

邓晓峰's avatar
邓晓峰 committed
92
  // eslint-disable-next-line no-undef
邓晓峰's avatar
邓晓峰 committed
93
  if (!createStoreage.get('globalConfig')) {
邓晓峰's avatar
邓晓峰 committed
94
    window.createStoreage = new Storeage(namespace);
dengxiaofeng's avatar
dengxiaofeng committed
95
  }
96
  if (!getToken()) {
邓晓峰's avatar
邓晓峰 committed
97 98
    localStorage.removeItem('loginSite');
  }
99

邓晓峰's avatar
邓晓峰 committed
100
  if (config.token !== null && Object.keys(config).length > 0) {
邓晓峰's avatar
邓晓峰 committed
101
    store.dispatch(actionCreators.getConfig(config));
邓晓峰's avatar
邓晓峰 committed
102 103
    // render({ appContent: '', loading: true });
    initMicroApps(loader, store);
104 105 106
    if (config.isNewYear) {
      updateTheme('#ff9600');
    }
dengxiaofeng's avatar
dengxiaofeng committed
107
  } else {
邓晓峰's avatar
邓晓峰 committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121
    appService.getWateWayConfig().then(res => {
      const hasGateWay =
        res && res.data && isString(res.data) ? JSON.parse(res.data) : res.data;
      if (res.code === 0) {
        store.dispatch(
          actionCreators.getConfig(
            Object.assign({}, window.globalConfig, {
              hasGateWay,
              apiGatewayDomain: `${window.location.origin}/Publish/GateWay`,
            }),
          ),
        );
      }
    });
122
    // eslint-disable-next-line react-hooks/rules-of-hooks
邓晓峰's avatar
邓晓峰 committed
123
    appService
邓晓峰's avatar
邓晓峰 committed
124
      .queryConfig({ client: params.getParams('client') || 'city' })
邓晓峰's avatar
邓晓峰 committed
125
      .then(res => {
邓晓峰's avatar
邓晓峰 committed
126 127
        if (res) {
          const data = res;
邓晓峰's avatar
邓晓峰 committed
128
          if (!data.client) {
129
            data.client = 'city';
邓晓峰's avatar
邓晓峰 committed
130
          }
邓晓峰's avatar
邓晓峰 committed
131 132 133 134 135
          store.dispatch(
            actionCreators.getConfig(
              Object.assign({}, window.globalConfig, data),
            ),
          );
136 137 138 139

          if (data.loginTemplate === '新春 - 智联.html') {
            updateTheme('#ff9600');
          }
邓晓峰's avatar
邓晓峰 committed
140
          // eslint-disable-next-line no-new
141
          if (getToken()) {
142 143 144 145
            // eslint-disable-next-line no-new
            new Login(
              {
                global: Object.assign({}, data, {
146
                  token: getToken(),
147 148 149 150 151 152 153
                }),
                // eslint-disable-next-line no-shadow
                updateConfig: data =>
                  store.dispatch(actionCreators.getConfig(data)),
              },
              () => {
                (async () => {
154
                  (await (getToken() &&
155
                    window.globalConfig &&
156
                    window.globalConfig.token)) && initMicroApps(loader, store);
157 158 159 160 161 162
                })();
              },
              true,
            );
          }

邓晓峰's avatar
邓晓峰 committed
163
          // eslint-disable-next-line no-shadow
邓晓峰's avatar
邓晓峰 committed
164
        }
邓晓峰's avatar
邓晓峰 committed
165
        return res;
邓晓峰's avatar
邓晓峰 committed
166
      })
邓晓峰's avatar
邓晓峰 committed
167
      // eslint-disable-next-line no-shadow
邓晓峰's avatar
邓晓峰 committed
168 169 170 171 172 173
      .then(res => {
        render({ appContent: '', loading: true });
      })
      .catch(error => {
        store.dispatch(actionCreators.getConfigError(error));
      });
dengxiaofeng's avatar
dengxiaofeng committed
174
  }
邓晓峰's avatar
邓晓峰 committed
175
};
叶飞's avatar
叶飞 committed
176

邓晓峰's avatar
邓晓峰 committed
177 178
initGlobalConfig();
initLocale();
邓晓峰's avatar
邓晓峰 committed
179
window.share &&
邓晓峰's avatar
邓晓峰 committed
180 181
  window.share.event &&
  window.share.event.on('triggerMicro', () => {
182
    initMicroApps(loader, store);
邓晓峰's avatar
邓晓峰 committed
183
  });
邓晓峰's avatar
邓晓峰 committed
184

邓晓峰's avatar
邓晓峰 committed
185 186

if (pwa) {
187
  // const appPWA = window.i18n.getI18n('app');
邓晓峰's avatar
邓晓峰 committed
188
  window.addEventListener('sw.offline', () => {
189
    message.warning(useIntl().formatMessage({ id: 'app.pwa.offline' }));
邓晓峰's avatar
邓晓峰 committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  });

  window.addEventListener('sw.updated', event => {
    const e = event;
    const reloadSW = async () => {
      const worker = e.detail && e.detail.waiting;
      if (!worker) {
        return true;
      }
      await new Promise((resolve, reject) => {
        const channel = new MessageChannel();
        channel.port1.onmessage = msgEvent => {
          if (msgEvent.data.error) {
            reject(msgEvent.data.error);
          } else {
            resolve(msgEvent.data);
          }
        };
        worker.postMessage({ type: 'skip-waiting' }, [channel.port2]);
      });
      window.location.reload(true);
      return true;
    };
    const key = `open${Date.now()}`;
    const btn = (
      <Button
        type="primary"
        onClick={() => {
          notification.close(key);
          reloadSW();
        }}
      >
222
        {useIntl().formatMessage({ id: 'app.pwa.serviceworker.updated.ok' })}
邓晓峰's avatar
邓晓峰 committed
223 224 225
      </Button>
    );
    notification.open({
226 227 228 229
      message: useIntl().formatMessage({ id: 'pwa.serviceworker.updated' }),
      description: useIntl().formatMessage({
        id: 'pwa.serviceworker.updated.hint',
      }),
邓晓峰's avatar
邓晓峰 committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
      btn,
      key,
      onClose: async () => null,
    });
  });
} else if ('serviceWorker' in navigator && isHttps) {
  const { serviceWorker } = navigator;
  if (serviceWorker.getRegistrations) {
    serviceWorker.getRegistrations().then(sws => {
      sws.forEach(sw => {
        sw.unregister();
      });
    });
  }
  serviceWorker.getRegistration().then(sw => {
    if (sw) sw.unregister();
  });

  if (window.caches && window.caches.keys) {
    caches.keys().then(keys => {
      keys.forEach(key => {
        caches.delete(key);
      });
    });
  }
}