From 7b7013bc8d642f01b026b73bffa9e107b3edaeef Mon Sep 17 00:00:00 2001 From: jinglv Date: Fri, 30 Sep 2022 19:44:22 +0800 Subject: [PATCH] feat(bc-qiankun) add qiankun in emqx-dashboard as subapp --- .eslintrc.js | 22 ++- package.json | 3 + script/config.json | 12 ++ src/common/forToBeSubApp.js | 4 + src/common/http.js | 64 +++++-- src/common/utils.js | 13 ++ src/components/Breadcrumb.vue | 22 ++- src/components/Layout.vue | 7 +- src/components/LeftBar.vue | 35 +++- src/components/NavHeader.vue | 9 +- src/i18n/Modules.js | 8 +- src/main.js | 171 +++++++++++++++++- src/public-path.js | 4 + src/stores/index.js | 34 +++- src/views/Base/Login.vue | 4 +- src/views/Clients/ClientDetails.vue | 6 +- src/views/Clients/Clients.vue | 2 +- src/views/Modules/ModuleDetail.vue | 4 +- src/views/Modules/Modules.vue | 7 +- src/views/Modules/SelectModules.vue | 2 +- .../components/Lwm2mProtocol/LwClients.vue | 2 +- .../components/SlowQuery/SlowQuery.vue | 2 +- src/views/Overview/Overview.vue | 18 +- .../Overview/components/NodeBasicCard.vue | 4 +- .../Overview/components/PercentageCards.vue | 2 +- .../Overview/components/PolylineCards.vue | 4 +- src/views/Plugins/Plugins.vue | 16 +- src/views/RuleEngine/ResourceView.vue | 6 +- src/views/RuleEngine/Resources.vue | 3 +- src/views/RuleEngine/RuleCreate.vue | 9 +- src/views/RuleEngine/RuleView.vue | 2 +- src/views/RuleEngine/Rules.vue | 10 +- src/views/Schemas/Schemas.vue | 36 ++-- src/views/Schemas/SchemasDetails.vue | 9 +- src/views/Settings/ClusterSettings.vue | 2 +- vue.config.js | 30 +++ yarn.lock | 14 +- 37 files changed, 492 insertions(+), 110 deletions(-) create mode 100644 src/common/forToBeSubApp.js create mode 100755 src/public-path.js diff --git a/.eslintrc.js b/.eslintrc.js index 07e6a372..2fb0f04f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,20 +1,18 @@ +const path = require('path') + module.exports = { root: true, env: { node: true, }, - extends: [ - 'plugin:vue/essential', - '@vue/airbnb', - '@vue/prettier', - ], + extends: ['plugin:vue/essential', '@vue/airbnb', '@vue/prettier', 'plugin:vue/base'], rules: { 'prettier/prettier': 'error', 'no-console': 'off', 'no-debugger': 'off', 'import/extensions': 0, 'vue/max-attributes-per-line': [ - 4, + 2, { singleline: 6, multiline: { @@ -39,4 +37,16 @@ module.exports = { parserOptions: { parser: 'babel-eslint', }, + settings: { + 'import/resolver': { + alias: { + map: [['@', path.resolve(__dirname, 'src')]], + extensions: ['.js', '.jsx'], + }, + node: { + paths: ['src'], + extensions: ['.js', '.ts', '.d.ts', '.tsx'], + }, + }, + }, } diff --git a/package.json b/package.json index eebf489e..690dd38c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "scripts": { "dev-cloud": "export VUE_APP_BUILD_ENV=cloud && yarn serve", "serve": "vue-cli-service serve", + "serve:sub-app": "VUE_APP_BUILD_ENV=sub-app && vue-cli-service serve", "build": "vue-cli-service build", + "build:sub-app": "VUE_SUB_APP_BUILD_ENV=sub-app vue-cli-service build", "build:config": "node ./script/build.config.js", "lint": "vue-cli-service lint", "format": "prettier --write \"src/**/*.js\" \"src/**/*.vue\" \"src/**/*.scss\"" @@ -20,6 +22,7 @@ "dateformat": "^3.0.3", "echarts": "^4.2.1", "element-ui": "^2.9.1", + "eslint-import-resolver-alias": "^1.1.2", "highlight.js": "^9.15.8", "js-sql-parser": "^1.3.0", "jsonwebtoken": "^8.5.1", diff --git a/script/config.json b/script/config.json index 8a3ff3ce..a50061ca 100644 --- a/script/config.json +++ b/script/config.json @@ -34,5 +34,17 @@ "rules.schema" ] } + }, + "sub-app": { + "baseURL": "/api/v4", + "httpTimeout": 15000, + "useRelativeResourcePath": true, + "publicPath": "/", + "hide": { + "routes": [ + "usersAcl" + ], + "children": ["monitor.license"] + } } } \ No newline at end of file diff --git a/src/common/forToBeSubApp.js b/src/common/forToBeSubApp.js new file mode 100644 index 00000000..cc94d786 --- /dev/null +++ b/src/common/forToBeSubApp.js @@ -0,0 +1,4 @@ +export const SING_NODE_VALUE = 'single-node' +export const isSingNode = (node) => node === SING_NODE_VALUE + +export const isSubApp = window.__POWERED_BY_QIANKUN__ diff --git a/src/common/http.js b/src/common/http.js index f98b2337..2033f10b 100644 --- a/src/common/http.js +++ b/src/common/http.js @@ -7,6 +7,7 @@ import 'nprogress/nprogress.css' import { getBasicAuthInfo, toLogin } from '@/common/utils' import store from '@/stores' import router from '@/routes' +import { isSubApp } from '../common/forToBeSubApp.js' let timer = 0 @@ -33,6 +34,7 @@ const httpCode = { 115: '主题错误', '-1': '需要登录', '-2': '相关插件未开启', + '-3': 'dashbard 没有权限', }, en: { 0: 'Success', @@ -53,6 +55,7 @@ const httpCode = { 115: 'Bad topic', '-1': 'Login Required', '-2': 'Plugin not started', + '-3': 'dashbard not allowed', }, } @@ -60,30 +63,49 @@ const httpMap = httpCode[lang] const pluginPages = ['schemas', 'rules', 'resources', 'setting'] -Object.assign(axios.defaults, { - headers: { - 'Content-Type': 'application/json', - 'Cache-Control': 'no-cache', - }, - baseURL: store.getters.httpBaseUrl, - timeout: store.state.httpTimeout, - auth: {}, -}) +if (!isSubApp) { + Object.assign(axios.defaults, { + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'no-cache', + }, + baseURL: store.getters.httpBaseUrl, + timeout: store.state.httpTimeout, + auth: {}, + }) +} else { + Object.assign(axios.defaults, { + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'no-cache', + }, + baseURL: '', + timeout: store.state.httpTimeout, + }) +} axios.interceptors.request.use( (config) => { + if (store.state.isSubAppDestroyed) { + // console.log('是否退出=========') + throw new axios.Cancel('cancel') + } const user = getBasicAuthInfo() config.params = config.params || {} + // 如果是 subapp const { params: { _t: tokenRequired = true }, } = config - if (!user.username && tokenRequired) { + if (isSubApp) { + // do nothing + } else if (!user.username && tokenRequired) { toLogin() throw new Error(httpMap['-1']) } - config.auth.username = user.username - config.auth.password = user.password - + if (!isSubApp) { + config.auth.username = user.username + config.auth.password = user.password + } store.dispatch('LOADING', true) // lwm2m observe if (!config.url.includes('?msgType=observe&path=')) { @@ -111,6 +133,7 @@ function handleError(error) { config: { params: { _m: showMessage = true }, }, + headers: {}, }, } = error if (selfError) { @@ -120,7 +143,18 @@ function handleError(error) { } const { name: currentPage, fullPath } = router.history.current if (status === 401) { - toLogin() + if (!isSubApp) { + toLogin() + } else { + for (let i = 0; i <= timer; i++) { + clearTimeout(i) + } + Message.closeAll() + Message.error(httpMap['-3']) + if (window.orgId && window.projectId && window.deployId) { + router.push({ path: `/deploy/${window.orgId}/${window.projectId}/${window.deployId}/overview` }) + } + } } else if (status === 404 && pluginPages.includes(currentPage)) { Message.error(httpMap['-2']) } else if (showMessage) { @@ -149,6 +183,7 @@ axios.interceptors.response.use((response) => { let error = '' let selfError = '' if (typeof res === 'object') { + console.log(res, 'res') const { status } = response const { code = -100, meta, message } = response.data let { data } = response.data @@ -162,7 +197,6 @@ axios.interceptors.response.use((response) => { } res = { data, status } } - clearTimeout(timer) timer = setTimeout(() => { store.dispatch('LOADING', false) diff --git a/src/common/utils.js b/src/common/utils.js index 23267b0b..fb5ca90a 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -11,9 +11,22 @@ import { omit, cloneDeep, isObject } from 'lodash' import { enDocsLink, zhDocsLink, pluginsZh, pluginsEn } from '@/common/link_urls' +import { isSubApp } from './forToBeSubApp.js' + const locale = store.state.lang const VueI18n = lang[locale] +const isSubAppIn = isSubApp +//qiankun 处理路由 +export function getPathForEmqxDashBoardPage(routePath, path) { + if (isSubAppIn) { + const pathPre = routePath.split('/').splice(0, 8).join('/') + return `${pathPre}${path}` + } else { + return path + } +} + /** * 获取基础的验证信息 * @param null diff --git a/src/components/Breadcrumb.vue b/src/components/Breadcrumb.vue index 396cabbd..7b1d3131 100644 --- a/src/components/Breadcrumb.vue +++ b/src/components/Breadcrumb.vue @@ -18,7 +18,7 @@ - + {{ $t('components.monitor') }} @@ -27,6 +27,9 @@