登录小窗修改为使用window.open (#7889)

* feat: 登录小窗修改为使用window.open

* chore: 本地开发支持模拟检查token并跳转到登录页

* feat: 前置检查登录态未登录则跳转至登录页
This commit is contained in:
yangyy
2024-04-17 12:03:36 +08:00
committed by GitHub
parent f3d1284b9e
commit 9b027be523
9 changed files with 82 additions and 37 deletions

3
.gitignore vendored
View File

@@ -14,3 +14,6 @@ src/web*/cmdb_*
.vscode .vscode
.pre-commit-config.yaml .pre-commit-config.yaml
pre-*-bkcodeai

30
bkcodeai.json Normal file
View File

@@ -0,0 +1,30 @@
{
"crignore_file_patterns": [
"out/",
"dist/",
"assets/",
"images/",
"package.json",
"package-lock.json",
"yarn.lock",
".env.*",
"*.env",
"*.map",
"*.yml",
"*.yaml",
"*.min.js",
"libs/",
"static/",
"doc/",
"docs/",
"*.mo",
"*.po",
"*.ini",
"*.toml",
"*.json",
"*.xml",
"*.lock",
"*.md",
"*.txt"
]
}

View File

@@ -95,7 +95,9 @@ const dev = {
useMock: false, useMock: false,
grabAPI: !!argv.grabapi grabAPI: !!argv.grabapi,
checkToken: ''
} }
const customDevConfigPath = path.resolve(__dirname, `index.dev.${argv.env || 'ee'}.js`) const customDevConfigPath = path.resolve(__dirname, `index.dev.${argv.env || 'ee'}.js`)

View File

@@ -13,6 +13,7 @@
const path = require('path') const path = require('path')
const MockJS = require('mockjs') const MockJS = require('mockjs')
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser');
const { pathToRegexp } = require('path-to-regexp') const { pathToRegexp } = require('path-to-regexp')
const mock = require('../../mock/index') const mock = require('../../mock/index')
@@ -27,6 +28,8 @@ module.exports = config => ({
} }
if (config.dev.useMock) { if (config.dev.useMock) {
devServer.app.use(cookieParser())
// parse application/x-www-form-urlencoded // parse application/x-www-form-urlencoded
devServer.app.use(bodyParser.urlencoded({ extended: true })) devServer.app.use(bodyParser.urlencoded({ extended: true }))

View File

@@ -16,8 +16,8 @@
"lint-fix": "eslint --fix --ext .js,.vue ./src" "lint-fix": "eslint --fix --ext .js,.vue ./src"
}, },
"dependencies": { "dependencies": {
"@blueking/login-modal": "^1.0.0",
"@blueking/notice-component-vue2": "^2.0.1-beta.2", "@blueking/notice-component-vue2": "^2.0.1-beta.2",
"@blueking/paas-login": "^0.0.11",
"@blueking/user-selector": "^1.0.6", "@blueking/user-selector": "^1.0.6",
"@icon-cool/bk-icon-cmdb-colorful": "0.0.1", "@icon-cool/bk-icon-cmdb-colorful": "0.0.1",
"await-to-js": "^3.0.0", "await-to-js": "^3.0.0",
@@ -87,6 +87,7 @@
"browserify-zlib": "^0.2.0", "browserify-zlib": "^0.2.0",
"buffer": "^6.0.3", "buffer": "^6.0.3",
"chalk": "^4.1.0", "chalk": "^4.1.0",
"cookie-parser": "^1.4.6",
"copy-webpack-plugin": "^8.1.1", "copy-webpack-plugin": "^8.1.1",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"crypto-browserify": "^3.12.0", "crypto-browserify": "^3.12.0",

View File

@@ -26,18 +26,12 @@
<the-header></the-header> <the-header></the-header>
<router-view class="views-layout" :name="topView" ref="topView"></router-view> <router-view class="views-layout" :name="topView" ref="topView"></router-view>
<the-permission-modal ref="permissionModal"></the-permission-modal> <the-permission-modal ref="permissionModal"></the-permission-modal>
<the-login-modal ref="loginModal"
v-if="loginUrl"
:login-url="loginUrl"
:success-url="loginSuccessUrl">
</the-login-modal>
</div> </div>
</template> </template>
<script> <script>
import theHeader from '@/components/layout/header' import theHeader from '@/components/layout/header'
import thePermissionModal from '@/components/modal/permission' import thePermissionModal from '@/components/modal/permission'
import theLoginModal from '@blueking/paas-login'
import theNotice from '@/components/notice' import theNotice from '@/components/notice'
import { addResizeListener, removeResizeListener } from '@/utils/resize-events' import { addResizeListener, removeResizeListener } from '@/utils/resize-events'
import { MENU_INDEX } from '@/dictionary/menu-symbol' import { MENU_INDEX } from '@/dictionary/menu-symbol'
@@ -47,14 +41,12 @@
components: { components: {
theNotice, theNotice,
theHeader, theHeader,
thePermissionModal, thePermissionModal
theLoginModal
}, },
data() { data() {
const showBrowserTips = window.navigator.userAgent.toLowerCase().indexOf('chrome') === -1 const showBrowserTips = window.navigator.userAgent.toLowerCase().indexOf('chrome') === -1
return { return {
showBrowserTips, showBrowserTips,
loginSuccessUrl: `${window.location.origin}/static/login_success.html`,
showNotice: false, showNotice: false,
noticeHeight: 0 noticeHeight: 0
} }
@@ -72,14 +64,6 @@
const [topRoute] = this.$route.matched const [topRoute] = this.$route.matched
return (topRoute && topRoute.meta.view) || 'default' return (topRoute && topRoute.meta.view) || 'default'
}, },
loginUrl() {
const siteLoginUrl = this.$Site.login || ''
const [loginBaseUrl] = siteLoginUrl.split('?')
if (loginBaseUrl) {
return `${loginBaseUrl}plain/`
}
return ''
},
enableNotice() { enableNotice() {
if (window.Site.enableNotification === false) { if (window.Site.enableNotification === false) {
return false return false
@@ -95,7 +79,6 @@
mounted() { mounted() {
addResizeListener(this.$el, this.calculateAppHeight) addResizeListener(this.$el, this.calculateAppHeight)
window.permissionModal = this.$refs.permissionModal window.permissionModal = this.$refs.permissionModal
window.loginModal = this.$refs.loginModal
// 在body标签添加语言标识属性用于插入到body下的内容进行国际化处理 // 在body标签添加语言标识属性用于插入到body下的内容进行国际化处理
document.body.setAttribute('lang', this.$i18n.locale) document.body.setAttribute('lang', this.$i18n.locale)

View File

@@ -12,11 +12,12 @@
import Axios from 'axios' import Axios from 'axios'
import md5 from 'md5' import md5 from 'md5'
import CachedPromise from './_cached-promise' import has from 'has'
import RequestQueue from './_request-queue'
import { $error } from '@/magicbox' import { $error } from '@/magicbox'
import i18n, { language } from '@/i18n' import i18n, { language } from '@/i18n'
import has from 'has' import { showLoginModal } from '@blueking/login-modal'
import CachedPromise from './_cached-promise'
import RequestQueue from './_request-queue'
import customHeaders from './custom-header' import customHeaders from './custom-header'
// axios实例 // axios实例
@@ -185,12 +186,18 @@ function handleReject(error, config) {
const { status, data } = error.response const { status, data } = error.response
const nextError = { message: error.message, status } const nextError = { message: error.message, status }
if (status === 401) { if (status === 401) {
if (window.loginModal) { const successUrl = `${window.location.origin}/static/login_success.html`
window.loginModal.show()
} else { const siteLoginUrl = window.Site.login || ''
// 接口401需要拿html中定义的Site if (!siteLoginUrl) {
window.Site.login && (window.location.href = window.Site.login) console.error('Login URL not configured!')
return
} }
const [loginBaseUrl] = siteLoginUrl.split('?')
const loginUrl = `${loginBaseUrl}plain?c_url=${encodeURIComponent(successUrl)}`
showLoginModal({ loginUrl })
} else if (data && data.bk_error_msg) { } else if (data && data.bk_error_msg) {
nextError.message = data.bk_error_msg nextError.message = data.bk_error_msg
} else if (status === 403) { } else if (status === 403) {

View File

@@ -47,14 +47,29 @@ Vue.prototype.$http = api
Vue.prototype.$tools = tools Vue.prototype.$tools = tools
Vue.prototype.$routerActions = routerActions Vue.prototype.$routerActions = routerActions
window.CMDB_APP = new Vue({ api.get(`${window.API_HOST}is_login`).then(() => {
window.CMDB_APP = new Vue({
el: '#app', el: '#app',
router, router,
store, store,
i18n, i18n,
components: { App }, components: { App },
template: '<App/>' template: '<App/>'
})
}) })
.catch(() => {
if (!window.Site.login) {
console.error('The login URL is not configured!')
return
}
try {
const loginURL = new URL(window.Site.login)
loginURL.searchParams.set('c_url', location.href)
location.href = loginURL.href
} catch (_) {
console.error('The login URL invalid!')
}
})
if (process.env.COMMIT_ID) { if (process.env.COMMIT_ID) {
window.CMDB_COMMIT_ID = process.env.COMMIT_ID window.CMDB_COMMIT_ID = process.env.COMMIT_ID

View File

@@ -14,10 +14,11 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Document</title> <title>Login Success</title>
</head> </head>
<script> <script>
window.parent.location.reload() window.opener && window.opener.location.reload()
window.close()
</script> </script>
<body> <body>
</body> </body>