|
1 | 1 | <template> |
2 | | - <iframe :src="iframeUrl" width="100%" height="380px" frameborder="0" |
3 | | - style="margin-top: -30px"></iframe> |
| 2 | + <div id="wecom-qr" class="wecom-qr flex"></div> |
4 | 3 | </template> |
5 | 4 |
|
6 | 5 | <script lang="ts" setup> |
| 6 | +import {useRoute, useRouter} from 'vue-router' |
| 7 | +import * as ww from '@wecom/jssdk' |
| 8 | +import { |
| 9 | + WWLoginLangType, |
| 10 | + WWLoginPanelSizeType, |
| 11 | + WWLoginRedirectType, |
| 12 | + WWLoginType |
| 13 | +} from '@wecom/jssdk' |
7 | 14 | import {ref, nextTick, defineProps} from 'vue' |
| 15 | +import {MsgError} from '@/utils/message' |
| 16 | +import useStore from '@/stores' |
8 | 17 | import {getBrowserLang} from '@/locales' |
9 | | -import {useRoute} from "vue-router"; |
10 | 18 |
|
| 19 | +const router = useRouter() |
11 | 20 | const route = useRoute() |
| 21 | +const { |
| 22 | + params: {accessToken}, |
| 23 | +} = route as any |
| 24 | +const wwLogin = ref({}) |
| 25 | +const obj = ref<any>({isWeComLogin: false}) |
| 26 | +const {chatUser} = useStore() |
| 27 | +
|
12 | 28 | const props = defineProps<{ |
13 | 29 | config: { |
| 30 | + callback_url: string; |
14 | 31 | app_secret: string |
15 | 32 | app_key: string |
16 | 33 | corp_id?: string |
17 | 34 | agent_id?: string |
18 | | - callback_url: string |
19 | 35 | } |
20 | 36 | }>() |
21 | 37 |
|
22 | | -const { |
23 | | - params: {accessToken}, |
24 | | -} = route as any |
25 | | -
|
26 | | -const iframeUrl = ref('') |
27 | 38 | const init = async () => { |
28 | 39 | await nextTick() // 确保DOM已更新 |
29 | 40 | const data = { |
30 | 41 | corpId: props.config.corp_id, |
31 | | - agentId: props.config.agent_id, |
32 | | - redirectUri: props.config.callback_url, |
| 42 | + agentId: props.config.agent_id |
33 | 43 | } |
34 | | - let lang = localStorage.getItem('MaxKB-locale') || getBrowserLang() || 'en-US' |
35 | | - if (lang === 'en-US') { |
36 | | - lang = 'en' |
37 | | - } else { |
38 | | - lang = 'zh' |
| 44 | + const lang = localStorage.getItem('MaxKB-locale') || getBrowserLang() || 'en-US' |
| 45 | + const redirectUri = props.config.callback_url |
| 46 | + try { |
| 47 | + wwLogin.value = ww.createWWLoginPanel({ |
| 48 | + el: '#wecom-qr', |
| 49 | + params: { |
| 50 | + login_type: WWLoginType.corpApp, |
| 51 | + appid: data.corpId || '', |
| 52 | + agentid: data.agentId, |
| 53 | + redirect_uri: redirectUri, |
| 54 | + state: 'fit2cloud-wecom-qr', |
| 55 | + lang: lang === 'zh-CN' || lang === 'zh-Hant' ? WWLoginLangType.zh : WWLoginLangType.en, |
| 56 | + redirect_type: WWLoginRedirectType.callback, |
| 57 | + panel_size: WWLoginPanelSizeType.small |
| 58 | + }, |
| 59 | + onCheckWeComLogin: obj.value, |
| 60 | + async onLoginSuccess({code}: any) { |
| 61 | + chatUser.wecomCallback(code, accessToken).then(() => { |
| 62 | + setTimeout(() => { |
| 63 | + router.push({ |
| 64 | + name: 'chat', |
| 65 | + params: {accessToken: accessToken}, |
| 66 | + query: route.query, |
| 67 | + }) |
| 68 | + }) |
| 69 | + }) |
| 70 | + }, |
| 71 | + onLoginFail(err) { |
| 72 | + MsgError(`${err.errMsg}`) |
| 73 | + } |
| 74 | + }) |
| 75 | + } catch (error) { |
39 | 76 | } |
40 | | - const redirectUri = encodeURIComponent(data.redirectUri) |
41 | | - console.log('redirectUri', data.redirectUri) |
42 | | - // 手动构建生成二维码的url |
43 | | - iframeUrl.value = `https://login.work.weixin.qq.com/wwlogin/sso/login?login_type=CorpApp&appid=${data.corpId}&agentid=${data.agentId}&redirect_uri=${redirectUri}&state=${accessToken}&lang=${lang}&lang=${lang}&panel_size=small` |
44 | 77 | } |
45 | 78 |
|
46 | 79 | init() |
|
0 commit comments