11<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 >
43</template >
54
65<script lang="ts" setup>
7- import {ref , nextTick , defineProps } from ' vue'
8- import {getBrowserLang } from ' @/locales'
9- import {useRoute } from " vue-router" ;
6+ import { nextTick , defineProps , onBeforeUnmount } from ' vue'
7+ import { useRoute , useRouter } from ' vue-router'
8+ import { getBrowserLang } from ' @/locales'
9+ import useStore from ' @/stores'
1010
11- const route = useRoute ()
11+ const WE_COM_ORIGIN = ' https://login.work.weixin.qq.com'
12+ const LOGIN_STATE = ' fit2cloud-wecom-qr'
1213const props = defineProps <{
1314 config: {
1415 app_secret: string
@@ -19,38 +20,88 @@ const props = defineProps<{
1920 }
2021}>()
2122
23+ const router = useRouter ()
24+ const route = useRoute ()
25+ const { chatUser } = useStore ()
26+
2227const {
23- params : {accessToken},
28+ params : { accessToken },
2429} = route as any
2530
26- const iframeUrl = ref (' ' )
31+ let iframe: HTMLIFrameElement | null = null
32+
33+ function createTransparentIFrame(el : string ) {
34+ const container = document .querySelector (el )
35+ if (! container ) return null
36+
37+ const iframeEl = document .createElement (' iframe' )
38+ iframeEl .style .cssText = `
39+ display: block;
40+ border: none;
41+ background: transparent;
42+ `
43+ iframeEl .referrerPolicy = ' origin'
44+ iframeEl .setAttribute (' frameborder' , ' 0' )
45+ iframeEl .setAttribute (' allowtransparency' , ' true' )
46+ iframeEl .setAttribute (' allow' , ' local-network-access' )
47+ container .appendChild (iframeEl )
48+ return iframeEl
49+ }
50+
51+ function getLang() {
52+ const lang = localStorage .getItem (' MaxKB-locale' ) || getBrowserLang ()
53+ return lang === ' en-US' ? ' en' : ' zh'
54+ }
55+
56+ function cleanup() {
57+ iframe ?.remove ()
58+ iframe = null
59+ }
60+
2761const init = async () => {
28- await nextTick () // 确保DOM已更新
29- const data = {
30- corpId: props .config .corp_id ,
31- agentId: props .config .agent_id ,
32- redirectUri: props .config .callback_url ,
33- }
34- let lang = localStorage .getItem (' MaxKB-locale' ) || getBrowserLang () || ' en-US'
35- if (lang === ' en-US' ) {
36- lang = ' en'
37- } else {
38- lang = ' zh'
39- }
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 `
62+ await nextTick ()
63+
64+ iframe = createTransparentIFrame (' #wecom-qr' )
65+ if (! iframe ) return
66+
67+ const redirectUri = encodeURIComponent (props .config .callback_url )
68+
69+ iframe .src =
70+ ` ${WE_COM_ORIGIN }/wwlogin/sso/login ` +
71+ ` ?login_type=CorpApp ` +
72+ ` &appid=${props .config .corp_id } ` +
73+ ` &agentid=${props .config .agent_id } ` +
74+ ` &redirect_uri=${redirectUri } ` +
75+ ` &state=${accessToken } ` +
76+ ` &lang=${getLang ()} ` +
77+ ` &panel_size=small ` +
78+ ` &redirect_type=self `
79+ iframe .addEventListener (' load' , (e ) => {
80+ if (iframe ?.contentWindow ) {
81+ iframe .contentWindow .postMessage (' getToken' , ' *' )
82+ }
83+ })
84+ window .addEventListener (' message' , (event ) => {
85+ if (event .data .type === ' token' ) {
86+ chatUser .setToken (event .data .value )
87+ router .push ({
88+ name: ' chat' ,
89+ params: { accessToken },
90+ query: route .query ,
91+ })
92+ }
93+ })
4494}
4595
96+ onBeforeUnmount (cleanup )
97+
4698init ()
4799 </script >
48100
49101<style scoped lang="scss">
50102#wecom-qr {
51103 margin-top : -20px ;
52- height : 331 px ;
104+ height : 360 px ;
53105 justify-content : center ;
54-
55106}
56107 </style >
0 commit comments