Skip to content

Commit c8072d1

Browse files
committed
fix: refactor wecomQrCode component to use dynamic iframe creation and improve cleanup logic
1 parent 4ef20a9 commit c8072d1

File tree

3 files changed

+78
-28
lines changed

3 files changed

+78
-28
lines changed

ui/src/locales/lang/zh-CN/workflow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export default {
138138
fileUploadType: {
139139
label: '上传的文件类型',
140140
documentText: '需要使用“文档内容提取”节点解析文档内容',
141-
imageText: '需要使用“视觉模型”节点解析图片内容',
141+
imageText: '需要使用“图片理解”节点解析图片内容',
142142
audioText: '需要使用“语音转文本”节点解析音频内容',
143143
videoText: '需要使用“视频理解”节点解析视频内容',
144144
otherText: '需要自行解析该类型文件',
Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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'
1213
const 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+
2227
const {
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+
2761
const 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+
4698
init()
4799
</script>
48100

49101
<style scoped lang="scss">
50102
#wecom-qr {
51103
margin-top: -20px;
52-
height: 331px;
104+
height: 360px;
53105
justify-content: center;
54-
55106
}
56107
</style>

ui/src/views/login/scanCompinents/wecomQrCode.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const init = async () => {
3232
lang = 'zh'
3333
}
3434
const redirectUri = encodeURIComponent(data.redirectUri)
35-
console.log('redirectUri', data.redirectUri)
3635
// 手动构建生成二维码的url
3736
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`
3837
}

0 commit comments

Comments
 (0)