Skip to content

Commit 087f707

Browse files
committed
feat: update OAuth2 callback functions to include access token handling for DingTalk and Lark
1 parent 79fbc52 commit 087f707

File tree

3 files changed

+93
-14
lines changed

3 files changed

+93
-14
lines changed

ui/src/api/chat/chat.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ const getDingCallback: (code: string, accessToken: string, loading?: Ref<boolean
138138
return get('auth/dingtalk', {code, accessToken: accessToken}, loading)
139139
}
140140

141-
const getDingOauth2Callback: (code: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
141+
const getDingOauth2Callback: (code: string, accessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
142142
code,
143+
accessToken,
143144
loading,
144145
) => {
145-
return get('auth/dingtalk/oauth2', {code}, loading)
146+
return get('auth/dingtalk/oauth2', {code, accessToken: accessToken}, loading)
146147
}
147148

148149
const getWecomCallback: (code: string, accessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
@@ -152,11 +153,12 @@ const getWecomCallback: (code: string, accessToken: string, loading?: Ref<boolea
152153
) => {
153154
return get('auth/wecom', {code, accessToken: accessToken}, loading)
154155
}
155-
const getLarkCallback: (code: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
156+
const getLarkCallback: (code: string, accessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
156157
code,
158+
accessToken,
157159
loading,
158160
) => {
159-
return get('auth/lark/oauth2', {code}, loading)
161+
return get('auth/lark/oauth2', {code, accessToken: accessToken}, loading)
160162
}
161163

162164
/**

ui/src/stores/modules/chat-user.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { defineStore } from 'pinia'
1+
import {defineStore} from 'pinia'
22
import ChatAPI from '@/api/chat/chat'
3-
import type { ChatProfile, ChatUserProfile } from '@/api/type/chat'
4-
import type { LoginRequest } from '@/api/type/user'
5-
import type { Ref } from 'vue'
6-
import { getBrowserLang } from '@/locales/index'
3+
import type {ChatProfile, ChatUserProfile} from '@/api/type/chat'
4+
import type {LoginRequest} from '@/api/type/user'
5+
import type {Ref} from 'vue'
6+
import {getBrowserLang} from '@/locales/index'
77

88
interface ChatUser {
99
// 用户id
@@ -121,8 +121,8 @@ const useChatUserStore = defineStore('chat-user', {
121121
return this.token
122122
})
123123
},
124-
async dingOauth2Callback(code: string) {
125-
return ChatAPI.getDingOauth2Callback(code).then((ok) => {
124+
async dingOauth2Callback(code: string, accessToken: string) {
125+
return ChatAPI.getDingOauth2Callback(code, accessToken).then((ok) => {
126126
this.setToken(ok.data.token)
127127
return this.token
128128
})
@@ -133,8 +133,8 @@ const useChatUserStore = defineStore('chat-user', {
133133
return this.token
134134
})
135135
},
136-
async larkCallback(code: string) {
137-
return ChatAPI.getLarkCallback(code).then((ok) => {
136+
async larkCallback(code: string, accessToken: string) {
137+
return ChatAPI.getLarkCallback(code, accessToken).then((ok) => {
138138
this.setToken(ok.data.token)
139139
return this.token
140140
})

ui/src/views/chat/user-login/index.vue

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ import {useI18n} from 'vue-i18n'
179179
import QrCodeTab from '@/views/chat/user-login/scanCompinents/QrCodeTab.vue'
180180
import {MsgConfirm, MsgError} from '@/utils/message.ts'
181181
import PasswordAuth from '@/views/chat/auth/component/password.vue'
182-
import {isAppIcon} from '@/utils/common'
182+
import {isAppIcon, loadScript} from '@/utils/common'
183183
import forge from "node-forge";
184+
import * as dd from "dingtalk-jsapi";
184185
185186
useResize()
186187
const router = useRouter()
@@ -423,6 +424,82 @@ onBeforeMount(() => {
423424
}
424425
}
425426
})
427+
declare const window: any
428+
onMounted(() => {
429+
const route = useRoute()
430+
const currentUrl = ref(route.fullPath)
431+
const params = new URLSearchParams(currentUrl.value.split('?')[1])
432+
const client = params.get('client')
433+
434+
const handleDingTalk = () => {
435+
const code = params.get('corpId')
436+
if (code) {
437+
dd.runtime.permission.requestAuthCode({corpId: code}).then((res) => {
438+
console.log('DingTalk client request success:', res)
439+
chatUser.dingOauth2Callback(res.code, accessToken).then(() => {
440+
router.push({name: 'home'})
441+
})
442+
})
443+
}
444+
}
445+
446+
const handleLark = () => {
447+
const appId = params.get('appId')
448+
const callRequestAuthCode = () => {
449+
window.tt?.requestAuthCode({
450+
appId: appId,
451+
success: (res: any) => {
452+
chatUser.larkCallback(res.code, accessToken).then(() => {
453+
router.push({name: 'home'})
454+
})
455+
},
456+
fail: (error: any) => {
457+
MsgError(error)
458+
},
459+
})
460+
}
461+
462+
loadScript('https://lf-scm-cn.feishucdn.com/lark/op/h5-js-sdk-1.5.35.js', {
463+
jsId: 'lark-sdk',
464+
forceReload: true,
465+
})
466+
.then(() => {
467+
if (window.tt) {
468+
window.tt.requestAccess({
469+
appID: appId,
470+
scopeList: [],
471+
success: (res: any) => {
472+
chatUser.larkCallback(res.code, accessToken).then(() => {
473+
router.push({name: 'home'})
474+
})
475+
},
476+
fail: (error: any) => {
477+
const {errno} = error
478+
if (errno === 103) {
479+
callRequestAuthCode()
480+
}
481+
},
482+
})
483+
} else {
484+
callRequestAuthCode()
485+
}
486+
})
487+
.catch((error) => {
488+
console.error('SDK 加载失败:', error)
489+
})
490+
}
491+
492+
switch (client) {
493+
case 'dingtalk':
494+
handleDingTalk()
495+
break
496+
case 'lark':
497+
handleLark()
498+
break
499+
default:
500+
break
501+
}
502+
})
426503
</script>
427504
<style lang="scss" scoped>
428505
.user-login {

0 commit comments

Comments
 (0)