diff --git a/src/api/user.js b/src/api/user.js index 69c7ecb..66a5ec6 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -37,6 +37,21 @@ export const loginBySocial = (tenantId, source, code, state) => request({ } }) +export const loginBySso = (state, code) => request({ + url: '/api/blade-auth/oauth/token', + method: 'post', + headers: { + 'Tenant-Id': state + }, + params: { + tenantId: state, + code, + grant_type: "authorization_code", + scope: "all", + redirect_uri: website.redirectUri, + } +}) + export const refreshToken = (refresh_token, tenantId, deptId, roleId) => request({ url: '/api/blade-auth/oauth/token', method: 'post', diff --git a/src/config/website.js b/src/config/website.js index c469633..7563ced 100644 --- a/src/config/website.js +++ b/src/config/website.js @@ -44,4 +44,8 @@ export default { authUrl: 'http://localhost/blade-auth/oauth/render', // 报表设计器地址(cloud端口为8108,boot端口为80) reportUrl: 'http://localhost:8108/ureport', + // 单点登录系统认证(blade-auth服务的地) + ssoUrl: 'http://localhost:8100/oauth/authorize?client_id=saber&response_type=code&redirect_uri=', + // 单点登录回调地址(Saber服务的地址) + redirectUri: 'http://localhost:1888', } diff --git a/src/lang/en.js b/src/lang/en.js index 47da297..f149c91 100644 --- a/src/lang/en.js +++ b/src/lang/en.js @@ -81,6 +81,7 @@ export default { userLogin: 'userLogin', phoneLogin: 'phoneLogin', thirdLogin: 'thirdLogin', + ssoLogin: 'ssoLogin', msgText: 'send code', msgSuccess: 'reissued code', }, diff --git a/src/lang/zh.js b/src/lang/zh.js index 71471b7..6e85659 100644 --- a/src/lang/zh.js +++ b/src/lang/zh.js @@ -81,6 +81,7 @@ export default { userLogin: '账号密码登录', phoneLogin: '手机号登录', thirdLogin: '第三方系统登录', + ssoLogin: '单点系统登录', msgText: '发送验证码', msgSuccess: '秒后重发', }, diff --git a/src/page/login/index.vue b/src/page/login/index.vue index d83c8b6..ddb6e42 100644 --- a/src/page/login/index.vue +++ b/src/page/login/index.vue @@ -45,6 +45,7 @@ {{ $t('login.userLogin') }} {{ $t('login.thirdLogin') }} + {{ $t('login.ssoLogin') }} @@ -108,6 +109,7 @@ handleLogin() { const topUrl = getTopUrl(); const redirectUrl = "/oauth/redirect/"; + const ssoCode = "?code="; this.socialForm.source = getQueryString("source"); this.socialForm.code = getQueryString("code"); this.socialForm.state = getQueryString("state"); @@ -116,7 +118,7 @@ source = source.split(redirectUrl)[1]; this.socialForm.source = source; } - if (!validatenull(this.socialForm.source) && !validatenull(this.socialForm.code) && !validatenull(this.socialForm.state)) { + if (topUrl.includes(redirectUrl) && !validatenull(this.socialForm.source) && !validatenull(this.socialForm.code) && !validatenull(this.socialForm.state)) { const loading = this.$loading({ lock: true, text: '第三方系统登录中,请稍后。。。', @@ -129,6 +131,19 @@ }).catch(() => { loading.close(); }); + } else if (!topUrl.includes(redirectUrl) && !validatenull(this.socialForm.code) && !validatenull(this.socialForm.state)) { + const loading = this.$loading({ + lock: true, + text: '单点系统登录中,请稍后。。。', + spinner: "el-icon-loading" + }); + this.$store.dispatch("LoginBySso", this.socialForm).then(() => { + window.location.href = topUrl.split(ssoCode)[0]; + this.$router.push({path: this.tagWel.value}); + loading.close(); + }).catch(() => { + loading.close(); + }); } } } diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 5fe9fd0..f92f525 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -4,7 +4,7 @@ import {setStore, getStore} from '@/util/store' import {isURL, validatenull} from '@/util/validate' import {deepClone} from '@/util/util' import website from '@/config/website' -import {loginByUsername, loginBySocial, getUserInfo, logout, refreshToken, getButtons} from '@/api/user' +import {loginByUsername, loginBySocial, loginBySso, getUserInfo, logout, refreshToken, getButtons} from '@/api/user' import {getTopMenu, getRoutes} from '@/api/system/menu' import md5 from 'js-md5' @@ -95,6 +95,29 @@ const user = { commit('SET_TOKEN', data.access_token); commit('SET_REFRESH_TOKEN', data.refresh_token); commit('SET_USER_INFO', data); + commit('SET_TENANT_ID', data.tenant_id); + commit('DEL_ALL_TAG'); + commit('CLEAR_LOCK'); + } + resolve(); + }) + }) + }, + //根据单点信息登录 + LoginBySso({commit}, userInfo) { + return new Promise((resolve) => { + loginBySso(userInfo.state, userInfo.code).then(res => { + const data = res.data; + if (data.error_description) { + Message({ + message: data.error_description, + type: 'error' + }) + } else { + commit('SET_TOKEN', data.access_token); + commit('SET_REFRESH_TOKEN', data.refresh_token); + commit('SET_USER_INFO', data); + commit('SET_TENANT_ID', data.tenant_id); commit('DEL_ALL_TAG'); commit('CLEAR_LOCK'); }