|
|
@@ -2,6 +2,8 @@
|
|
|
import Vue from 'vue'
|
|
|
import BASE_URL from './config.js'
|
|
|
import {mapActions} from 'vuex'
|
|
|
+ import loginService from '@/api/auth/loginService'
|
|
|
+ import * as dd from 'dingtalk-jsapi'
|
|
|
export default {
|
|
|
onLaunch: function() {
|
|
|
uni.getSystemInfo({
|
|
|
@@ -114,8 +116,12 @@
|
|
|
console.log('App Hide')
|
|
|
},
|
|
|
created: async function() {
|
|
|
+
|
|
|
+ const urlParams = this.getUrlParams();
|
|
|
+ if (await this.tryDingTalkAutoLogin(urlParams)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
// 获取 URL 中的 code 参数
|
|
|
- const urlParams = new URLSearchParams(window.location.search);
|
|
|
const code = urlParams.get('code');
|
|
|
|
|
|
console.log('传入 code -------');
|
|
|
@@ -192,6 +198,9 @@
|
|
|
// 获取 URL 中的 'flow' 参数
|
|
|
const flowParam = this.$route.query.flow;
|
|
|
// 将 URL 编码的 'flow' 参数解析为 JSON 对象
|
|
|
+ if (!flowParam) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
const flowObject = JSON.parse(decodeURIComponent(flowParam));
|
|
|
console.log("flowObject.openId---> ", flowObject.openId)
|
|
|
if (flowObject.openId) {
|
|
|
@@ -212,6 +221,93 @@
|
|
|
},
|
|
|
methods: {
|
|
|
...mapActions(['refreshUserInfo']),
|
|
|
+ getUrlParams() {
|
|
|
+ const params = new URLSearchParams(window.location.search || '');
|
|
|
+ const hash = window.location.hash || '';
|
|
|
+ const hashQueryIndex = hash.indexOf('?');
|
|
|
+ if (hashQueryIndex > -1) {
|
|
|
+ const hashParams = new URLSearchParams(hash.substring(hashQueryIndex + 1));
|
|
|
+ hashParams.forEach((value, key) => {
|
|
|
+ if (!params.has(key)) {
|
|
|
+ params.set(key, value);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return params;
|
|
|
+ },
|
|
|
+ isDingTalkEnv() {
|
|
|
+ return /DingTalk/i.test(window.navigator.userAgent || '');
|
|
|
+ },
|
|
|
+ loadDingTalkJs() {
|
|
|
+ return Promise.resolve(dd);
|
|
|
+ },
|
|
|
+ requestDingTalkAuthCode(corpId) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ if (!dd ) {
|
|
|
+ reject(new Error('钉钉资源未加载成功,请联系管理员'));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ dd.getAuthCode({
|
|
|
+ corpId: corpId,
|
|
|
+ onSuccess: (res) => resolve(res.code || res.authCode),
|
|
|
+ onFail: reject
|
|
|
+ });
|
|
|
+ dd.error(reject);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ async handleDingTalkLoginSuccess(data) {
|
|
|
+ this.$store.commit('SET_TOKEN', data.token);
|
|
|
+ await this.refreshUserInfo();
|
|
|
+ },
|
|
|
+ async tryDingTalkAutoLogin(urlParams) {
|
|
|
+ if (!this.isDingTalkEnv()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (this.$store.state.user.token) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ const tenantId = urlParams.get('tenantId') || uni.getStorageSync('dingTalkTenantId') || '10009';
|
|
|
+
|
|
|
+ try {
|
|
|
+ const clientConfig = await loginService.dingTalkClientConfig(tenantId);
|
|
|
+
|
|
|
+ if (!clientConfig || !clientConfig.corpId) {
|
|
|
+
|
|
|
+ throw new Error('DingTalk corpId is missing');
|
|
|
+ }
|
|
|
+
|
|
|
+ await this.loadDingTalkJs();
|
|
|
+
|
|
|
+ const authCode = await this.requestDingTalkAuthCode(clientConfig.corpId);
|
|
|
+
|
|
|
+ const loginResult = await loginService.dingTalkLogin({
|
|
|
+ authCode: authCode,
|
|
|
+ tenantId: tenantId
|
|
|
+ });
|
|
|
+
|
|
|
+ if (loginResult.bindRequired) {
|
|
|
+ uni.setStorageSync('dingTalkBindKey', loginResult.bindKey);
|
|
|
+ uni.setStorageSync('dingTalkTenantId', loginResult.tenantId || tenantId);
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/login/login?bind=dingtalk'
|
|
|
+ });
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ await this.handleDingTalkLoginSuccess(loginResult);
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/index/index'
|
|
|
+ });
|
|
|
+ } catch (e) {
|
|
|
+ if(!e.isTrusted){
|
|
|
+ uni.showToast({
|
|
|
+ title: '钉钉异常,请联系管理员',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
}
|