From 087f70700282372bc50245cda0d5e0637203ac6e Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Fri, 17 Oct 2025 11:14:03 +0800 Subject: [PATCH] feat: update OAuth2 callback functions to include access token handling for DingTalk and Lark --- ui/src/api/chat/chat.ts | 10 ++-- ui/src/stores/modules/chat-user.ts | 18 +++--- ui/src/views/chat/user-login/index.vue | 79 +++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/ui/src/api/chat/chat.ts b/ui/src/api/chat/chat.ts index 26c5aab6c..dde551501 100644 --- a/ui/src/api/chat/chat.ts +++ b/ui/src/api/chat/chat.ts @@ -138,11 +138,12 @@ const getDingCallback: (code: string, accessToken: string, loading?: Ref) => Promise> = ( +const getDingOauth2Callback: (code: string, accessToken: string, loading?: Ref) => Promise> = ( code, + accessToken, loading, ) => { - return get('auth/dingtalk/oauth2', {code}, loading) + return get('auth/dingtalk/oauth2', {code, accessToken: accessToken}, loading) } const getWecomCallback: (code: string, accessToken: string, loading?: Ref) => Promise> = ( @@ -152,11 +153,12 @@ const getWecomCallback: (code: string, accessToken: string, loading?: Ref { return get('auth/wecom', {code, accessToken: accessToken}, loading) } -const getLarkCallback: (code: string, loading?: Ref) => Promise> = ( +const getLarkCallback: (code: string, accessToken: string, loading?: Ref) => Promise> = ( code, + accessToken, loading, ) => { - return get('auth/lark/oauth2', {code}, loading) + return get('auth/lark/oauth2', {code, accessToken: accessToken}, loading) } /** diff --git a/ui/src/stores/modules/chat-user.ts b/ui/src/stores/modules/chat-user.ts index a4efbda98..125b9afd7 100644 --- a/ui/src/stores/modules/chat-user.ts +++ b/ui/src/stores/modules/chat-user.ts @@ -1,9 +1,9 @@ -import { defineStore } from 'pinia' +import {defineStore} from 'pinia' import ChatAPI from '@/api/chat/chat' -import type { ChatProfile, ChatUserProfile } from '@/api/type/chat' -import type { LoginRequest } from '@/api/type/user' -import type { Ref } from 'vue' -import { getBrowserLang } from '@/locales/index' +import type {ChatProfile, ChatUserProfile} from '@/api/type/chat' +import type {LoginRequest} from '@/api/type/user' +import type {Ref} from 'vue' +import {getBrowserLang} from '@/locales/index' interface ChatUser { // 用户id @@ -121,8 +121,8 @@ const useChatUserStore = defineStore('chat-user', { return this.token }) }, - async dingOauth2Callback(code: string) { - return ChatAPI.getDingOauth2Callback(code).then((ok) => { + async dingOauth2Callback(code: string, accessToken: string) { + return ChatAPI.getDingOauth2Callback(code, accessToken).then((ok) => { this.setToken(ok.data.token) return this.token }) @@ -133,8 +133,8 @@ const useChatUserStore = defineStore('chat-user', { return this.token }) }, - async larkCallback(code: string) { - return ChatAPI.getLarkCallback(code).then((ok) => { + async larkCallback(code: string, accessToken: string) { + return ChatAPI.getLarkCallback(code, accessToken).then((ok) => { this.setToken(ok.data.token) return this.token }) diff --git a/ui/src/views/chat/user-login/index.vue b/ui/src/views/chat/user-login/index.vue index 68034cb2c..be1e0ad92 100644 --- a/ui/src/views/chat/user-login/index.vue +++ b/ui/src/views/chat/user-login/index.vue @@ -179,8 +179,9 @@ import {useI18n} from 'vue-i18n' import QrCodeTab from '@/views/chat/user-login/scanCompinents/QrCodeTab.vue' import {MsgConfirm, MsgError} from '@/utils/message.ts' import PasswordAuth from '@/views/chat/auth/component/password.vue' -import {isAppIcon} from '@/utils/common' +import {isAppIcon, loadScript} from '@/utils/common' import forge from "node-forge"; +import * as dd from "dingtalk-jsapi"; useResize() const router = useRouter() @@ -423,6 +424,82 @@ onBeforeMount(() => { } } }) +declare const window: any +onMounted(() => { + const route = useRoute() + const currentUrl = ref(route.fullPath) + const params = new URLSearchParams(currentUrl.value.split('?')[1]) + const client = params.get('client') + + const handleDingTalk = () => { + const code = params.get('corpId') + if (code) { + dd.runtime.permission.requestAuthCode({corpId: code}).then((res) => { + console.log('DingTalk client request success:', res) + chatUser.dingOauth2Callback(res.code, accessToken).then(() => { + router.push({name: 'home'}) + }) + }) + } + } + + const handleLark = () => { + const appId = params.get('appId') + const callRequestAuthCode = () => { + window.tt?.requestAuthCode({ + appId: appId, + success: (res: any) => { + chatUser.larkCallback(res.code, accessToken).then(() => { + router.push({name: 'home'}) + }) + }, + fail: (error: any) => { + MsgError(error) + }, + }) + } + + loadScript('https://lf-scm-cn.feishucdn.com/lark/op/h5-js-sdk-1.5.35.js', { + jsId: 'lark-sdk', + forceReload: true, + }) + .then(() => { + if (window.tt) { + window.tt.requestAccess({ + appID: appId, + scopeList: [], + success: (res: any) => { + chatUser.larkCallback(res.code, accessToken).then(() => { + router.push({name: 'home'}) + }) + }, + fail: (error: any) => { + const {errno} = error + if (errno === 103) { + callRequestAuthCode() + } + }, + }) + } else { + callRequestAuthCode() + } + }) + .catch((error) => { + console.error('SDK 加载失败:', error) + }) + } + + switch (client) { + case 'dingtalk': + handleDingTalk() + break + case 'lark': + handleLark() + break + default: + break + } +})