From 4ac7edad8a646f77da099db0c09ece51903ce2a9 Mon Sep 17 00:00:00 2001 From: fengchenchen Date: Fri, 1 Mar 2024 15:29:56 +0800 Subject: [PATCH] =?UTF-8?q?[add]=E9=92=89=E9=92=89=E5=8D=95=E7=82=B9?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=89=8D=E7=AB=AF=E4=BB=A3=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 3 ++- .env.production | 1 + .env.test | 3 ++- package.json | 2 +- src/api/login.js | 8 +++++++ src/main.js | 7 ++++++ src/store/modules/user.js | 36 +++++++++++++++++++++++++++++-- src/utils/request.js | 45 +++++++++++++++++++++++---------------- src/utils/util.js | 30 ++++++++++++++++++++++++++ 9 files changed, 112 insertions(+), 23 deletions(-) diff --git a/.env.development b/.env.development index 8f73c83..a6b99f3 100644 --- a/.env.development +++ b/.env.development @@ -1,4 +1,5 @@ NODE_ENV=development VUE_APP_TAG_FLAG=0 VUE_APP_LOGIN_PAGE='/user/login' -VUE_APP_CURRENT_SYSTEM='inner' \ No newline at end of file +VUE_APP_CURRENT_SYSTEM='inner' +VUE_APP_DING_CORP_ID='ding7e68cba137504bb7f2c783f7214b6d69' \ No newline at end of file diff --git a/.env.production b/.env.production index d3decf4..5293cec 100644 --- a/.env.production +++ b/.env.production @@ -2,3 +2,4 @@ NODE_ENV=production VUE_APP_TAG_FLAG=1 VUE_APP_LOGIN_PAGE='/user/login' VUE_APP_CURRENT_SYSTEM='inner' +VUE_APP_DING_CORP_ID='ding7e68cba137504bb7f2c783f7214b6d69' diff --git a/.env.test b/.env.test index a4f3781..3d2e074 100644 --- a/.env.test +++ b/.env.test @@ -4,4 +4,5 @@ VUE_APP_CAS_BASE_URL=http://cas.example.org:8443/cas VUE_APP_ONLINE_BASE_URL=http://fileview.jero.com/onlinePreview VUE_APP_TAG_FLAG=1 VUE_APP_LOGIN_PAGE='/user/login' -VUE_APP_CURRENT_SYSTEM='inner' \ No newline at end of file +VUE_APP_CURRENT_SYSTEM='inner' +VUE_APP_DING_CORP_ID='ding9ec7f291faadef5dee0f45d8e4f7c288' \ No newline at end of file diff --git a/package.json b/package.json index ce53745..ea044d4 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "ant-design-vue": "1.7.2", "axios": "^0.21.1", "core-js": "^3.8.3", - "dingtalk-jsapi": "^3.0.29", + "dingtalk-jsapi": "^3.0.31", "js-base64": "^3.6.0", "jsencrypt": "^3.0.0-rc.1", "md5": "^2.2.1", diff --git a/src/api/login.js b/src/api/login.js index cb9c894..b6ed7e7 100644 --- a/src/api/login.js +++ b/src/api/login.js @@ -19,6 +19,14 @@ export function login (parameter) { data: parameter }) } +// 钉钉单点登录 需要修改路径。 +export function dingLogin (parameter) { + return axios({ + url: '/hiwork/login', + method: 'post', + data: parameter + }) +} export function phoneLogin (parameter) { return axios({ diff --git a/src/main.js b/src/main.js index 2748210..5248b12 100644 --- a/src/main.js +++ b/src/main.js @@ -65,6 +65,13 @@ new Vue({ mounted () { store.commit('SET_TOKEN', Vue.ls.get(ACCESS_TOKEN)) store.commit('SET_INFO', Vue.ls.get(USER_INFO)) + // 判断是否有token,没有的话就直接单点登录 + if (!Vue.ls.get(ACCESS_TOKEN)) { + console.log('store.dispatch(\'DingLogin\')') + store.dispatch('DingLogin').catch((e) => { + Toast(e.message) + }) + } }, render: h => h(App) }).$mount('#app') diff --git a/src/store/modules/user.js b/src/store/modules/user.js index a173eb9..d7bcae6 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,8 +1,8 @@ import Vue from 'vue' -import { login, logout, phoneLogin, thirdLogin } from '@/api/login' +import {login, logout, phoneLogin, thirdLogin, dingLogin} from '@/api/login' import { ACCESS_TOKEN, USER_NAME, USER_INFO, USER_AUTH, SYS_BUTTON_AUTH, UI_CACHE_DB_DICT_DATA, TENANT_ID, CACHE_INCLUDED_ROUTES } from '@/store/mutation-types' import store from '@/store' -import { welcome } from '@/utils/util' +import { welcome, getDingCode } from '@/utils/util' import { queryPermissionsByUser } from '@/api/api' import { getAction } from '@/api/manage' @@ -157,6 +157,38 @@ const user = { }) }) }, + // 钉钉单点登录 + async DingLogin ({ commit }) { + const code = await getDingCode() + console.log('--getDingCode---', code) + // 先使用钉钉本身的api获取当前登录人的coode码 然后传给后端调用单点登录的接口 + return new Promise((resolve, reject) => { + if (code) { + // 使用code调用后端hiwork登录的接口 + dingLogin({ code }).then(response => { + if (response.code + '' === '200') { + const result = response.result + const userInfo = result.userInfo + Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000) + Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000) + Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000) + Vue.ls.set(UI_CACHE_DB_DICT_DATA, result.sysAllDictItems, 7 * 24 * 60 * 60 * 1000) + commit('SET_TOKEN', result.token) + commit('SET_INFO', userInfo) + commit('SET_NAME', { username: userInfo.username, realname: userInfo.realname, welcome: welcome() }) + commit('SET_AVATAR', userInfo.avatar) + resolve(response) + } else { + reject(response) + } + }).catch(error => { + reject(error) + }) + } else { + reject() + } + }) + }, // 登出 Logout ({ commit, state }) { diff --git a/src/utils/request.js b/src/utils/request.js index 7e76bf5..052ace2 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -41,24 +41,33 @@ const err = (error) => { // update-end- --- author:liusq ------ date:20200910 ---- for:处理Blob情况---- if (data.includes('Token失效')) { // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转---- - Dialog.alert({ - title: '登录已过期', - message: '很抱歉,登录已过期,请重新登录', - confirmButtonText: '重新登录', - theme: 'round-button' - }).then(() => { - store.dispatch('Logout').then(() => { - Vue.ls.remove(ACCESS_TOKEN) - try { - const path = window.document.location.pathname - console.log('location pathname -> ' + path) - if (path !== '/' && path.indexOf('/user/login') === -1) { - window.location.reload() - } - } catch (e) { - window.location.reload() - } - }) + // 需要接hiwork 所以如果hiwork 登录失效的话 直接免密登录即可。 + store.dispatch('DingLogin').then((res) => { + }).catch((e) => { + // 不是钉钉登录的环境,走原来的方法 退出登录 + if (!e) { + Dialog.alert({ + title: '登录已过期', + message: '很抱歉,登录已过期,请重新登录', + confirmButtonText: '重新登录', + theme: 'round-button' + }).then(() => { + store.dispatch('Logout').then(() => { + Vue.ls.remove(ACCESS_TOKEN) + try { + const path = window.document.location.pathname + console.log('location pathname -> ' + path) + if (path !== '/' && path.indexOf('/user/login') === -1) { + window.location.reload() + } + } catch (e) { + window.location.reload() + } + }) + }) + } else { + Toast(e.message) + } }) // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转---- } diff --git a/src/utils/util.js b/src/utils/util.js index 57223cc..7db6324 100644 --- a/src/utils/util.js +++ b/src/utils/util.js @@ -1,6 +1,7 @@ import Vue from 'vue' import * as api from '@/api/api' import { ACCESS_TOKEN } from '@/store/mutation-types' +import * as dd from 'dingtalk-jsapi' // export function timeFix () { const time = new Date() @@ -524,3 +525,32 @@ export function urlToParams (url) { }) return paramsObj } + +// 获取钉钉 免登授权码 +export function getDingCode () { + return new Promise((resolve, reject) => { + // 请求的相关信息,直接用dd也可以。 + console.log('dd.env.platform', dd.env.platform) + if (dd.env.platform !== 'notInDingTalk') { + dd.ready(function (err) { + console.log(1111111, err) + dd.runtime.permission.requestAuthCode({ + // corpId: 'ding7e68cba137504bb7f2c783f7214b6d69', // Zoey测试 这个企业id 是固定的,正式系统和测试系统应该不一样。 + corpId: process.env.VUE_APP_DING_CORP_ID, // 使用环境变量做的区分 + // corpId: 'ding9ec7f291faadef5dee0f45d8e4f7c288', // 重汽测试环境 + onSuccess: function (info) { + const code = info.code // 通过该免登授权码可以获取用户身份 + resolve(code) + // 然后把这个code传给后端做相关的验证。 + }, + onFail: function (e) { + console.log(1111111, e) + resolve('') + } + }) + }) + } else { + resolve('') + } + }) +}