UNPKG

@choerodon/master

Version:
290 lines (236 loc) 11 kB
import "choerodon-ui/pro/lib/modal-provider/style"; import _ModalProvider from "choerodon-ui/pro/lib/modal-provider"; function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import React, { useState, useMemo } from 'react'; import { withRouter } from 'react-router-dom'; import { useEventListener, useLocalStorageState, useMount, useUpdateEffect } from 'ahooks'; import { QueryClient, QueryClientProvider } from 'react-query'; import { Provider } from 'mobx-react'; import { observer } from 'mobx-react-lite'; import { Container } from '@hzero-front-ui/core'; import { Loading, useQueryString } from '@choerodon/components'; import { authorizeC7n, getAccessToken, setAccessToken } from "./utils"; import Outward from "./containers/components/c7n/routes/outward"; import { asyncLocaleProvider, asyncRouter } from "./hoc"; import AppState from "./containers/stores/c7n/AppState"; import HeaderStore from "./containers/stores/c7n/HeaderStore"; import stores from "./containers/stores"; import Master from "./containers/components/c7n/master"; import "./containers/components/style"; import { enterprisesApi } from "./apis"; import { ENTERPRISE_ADDRESS } from "./constants"; import { useC7NThemeInit } from "./configs"; import { useC7NNotification, useSafariAdapter } from "./hooks"; var queryClient = new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false } } }); var language = AppState.currentLanguage; var UILocaleProviderAsync = asyncRouter(function () { return import('choerodon-ui/lib/locale-provider'); }, { locale: function locale() { return import("choerodon-ui/lib/locale-provider/".concat(language, ".js")); } }); var IntlProviderAsync = asyncLocaleProvider(language, function () { return import("./locale/".concat(language)); }); /** @type {boolean} 是否安装了敏捷模块 */ var HAS_AGILE_PRO = false; try { require.resolveWeak('@choerodon/agile-pro'); HAS_AGILE_PRO = true; } catch (error) {} var MasterIndex = function MasterIndex(props) { var location = props.location, history = props.history, AutoRouter = props.AutoRouter; var pathname = location.pathname, search = location.search; // 历史路径 var historyPath = sessionStorage.getItem('historyPath'); var _useLocalStorageState = useLocalStorageState('hasEnterpriseConfirmed', false), _useLocalStorageState2 = _slicedToArray(_useLocalStorageState, 2), hasEnterpriseConfirmed = _useLocalStorageState2[0], setEnterPriseConfirmed = _useLocalStorageState2[1]; // 用于多relogin的逻辑,多tab之间刷新页面 var _useLocalStorageState3 = useLocalStorageState('relogin', false), _useLocalStorageState4 = _slicedToArray(_useLocalStorageState3, 2), setReloginValue = _useLocalStorageState4[1]; var _useState = useState(true), _useState2 = _slicedToArray(_useState, 2), loading = _useState2[0], setLoading = _useState2[1]; // 获取url的params var params = useQueryString(); // 监听storage,作用在于如果有其他重新登录了,就触发刷新事件 useEventListener('storage', handleStorageChange); // 注入Notification授权的hook useC7NNotification(); // 初始化注入新UI的版本hook useC7NThemeInit(); // 为safari浏览器做适配的hook useSafariAdapter(); /** * 判断当前pathname是否存在于环境变量outward中,表明是否需要认证 * @return {boolean} */ var isInOutward = useMemo(function () { // "/knowledge/share,/knowledge/organizations/create,/knowledge/project/create,/iam/register-organization,/iam/invite-user" // eslint-disable-next-line no-underscore-dangle var injectOutward = window._env_.outward; if (injectOutward) { var splitArr = injectOutward.split(',').map(function (r) { return r.replace(/['"']/g, ''); }); splitArr.push('/unauthorized'); return splitArr.some(function (path) { return pathname.startsWith(path); }); } return false; }, [pathname]); /** * 其他tab页重新登录,刷新当前页面 * @param {*} e {StorageEvent} */ function handleStorageChange(e) { if (e.key === 'relogin') { window.location.reload(); } } /** * 开源版admin账号登录判断是否展示企业信息完善页面 */ function checkEnterprise() { return _checkEnterprise.apply(this, arguments); } /** * todo.... * @return {*} */ function _checkEnterprise() { _checkEnterprise = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { var res; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 3; return enterprisesApi.checkEnterpriseInfo(); case 3: res = _context.sent; if (res) { setEnterPriseConfirmed(true); } else { history.push(ENTERPRISE_ADDRESS); } _context.next = 10; break; case 7: _context.prev = 7; _context.t0 = _context["catch"](0); throw new Error(_context.t0); case 10: case "end": return _context.stop(); } } }, _callee, null, [[0, 7]]); })); return _checkEnterprise.apply(this, arguments); } function auth() { return _auth.apply(this, arguments); } function _auth() { _auth = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { var accessToken, tokenType, expiresIn; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: setLoading(true); accessToken = params.access_token, tokenType = params.token_type, expiresIn = params.expires_in; if (!accessToken) { _context2.next = 8; break; } setAccessToken(accessToken, tokenType, expiresIn); // 通知其他tab页刷新,在localstorage里头设置 setReloginValue(true); window.location.href = window.location.href.replace(/[&?]redirectFlag.*/g, ''); _context2.next = 11; break; case 8: if (getAccessToken()) { _context2.next = 11; break; } authorizeC7n(); return _context2.abrupt("return"); case 11: if (!(!HAS_AGILE_PRO && !(pathname === null || pathname === void 0 ? void 0 : pathname.startsWith(ENTERPRISE_ADDRESS)))) { _context2.next = 14; break; } _context2.next = 14; return checkEnterprise(); case 14: HeaderStore.axiosGetRoles(); AppState.loadModules(); AppState.loadDeployServices(); _context2.next = 19; return AppState.loadUserInfo(); case 19: setLoading(false); case 20: case "end": return _context2.stop(); } } }, _callee2); })); return _auth.apply(this, arguments); } useMount(function () { // 如果不存在历史地址则设置当前地址为跳转地址 !historyPath && sessionStorage.setItem('historyPath', pathname + search); // 不是就校验去登录 !isInOutward && auth(); }); useUpdateEffect(function () { if (!isInOutward) { if (loading) { auth(); } else if (pathname.startsWith(ENTERPRISE_ADDRESS) && !hasEnterpriseConfirmed && !HAS_AGILE_PRO) { checkEnterprise(); } } }, [pathname, loading, isInOutward]); var getContainer = useMemo(function () { var content = isInOutward ? Outward : Master; return /*#__PURE__*/React.createElement(content, { AutoRouter: AutoRouter }); }, [isInOutward, AutoRouter]); if (loading && !isInOutward) { return /*#__PURE__*/React.createElement(Loading, { style: { position: 'fixed', margin: 'auto', inset: 0 }, type: "c7n" }); } return /*#__PURE__*/React.createElement(QueryClientProvider, { client: queryClient }, /*#__PURE__*/React.createElement(UILocaleProviderAsync, null, /*#__PURE__*/React.createElement(IntlProviderAsync, null, /*#__PURE__*/React.createElement(_ModalProvider, { location: window.location }, /*#__PURE__*/React.createElement(Provider, stores, /*#__PURE__*/React.createElement(Container, null, getContainer)))))); }; export default withRouter(observer(MasterIndex));