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