From d3eb97eb8b8ba8a4a27a6cb97f295a6aeaadf41b Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Mon, 23 Mar 2026 12:59:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9AH5=20=E5=B7=A5?= =?UTF-8?q?=E5=8D=95=E9=A1=B5=E4=BC=81=E5=BE=AE=20OAuth2=20=E8=AE=A4?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 页面打开时检查 URL 中的 code,有则换 userid, 无则跳转企微授权页。未配置企微参数时跳过(开发模式)。 --- apps/web-antd/src/views/work-order/index.vue | 55 ++++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/apps/web-antd/src/views/work-order/index.vue b/apps/web-antd/src/views/work-order/index.vue index a587bd78c..169aac56f 100644 --- a/apps/web-antd/src/views/work-order/index.vue +++ b/apps/web-antd/src/views/work-order/index.vue @@ -41,6 +41,48 @@ const uploading = ref(false); // API 基础地址(vsp-service) const API_BASE = import.meta.env.VITE_VSP_SERVICE_URL || 'http://124.221.55.225:8000'; +// 企微 OAuth2 配置 +const CORP_ID = import.meta.env.VITE_WECHAT_CORP_ID || ''; +const AGENT_ID = import.meta.env.VITE_WECHAT_AGENT_ID || ''; + +// 当前认证用户 +const currentUserId = ref(''); +const authChecked = ref(false); + +/** 企微 OAuth2 认证 */ +async function checkAuth() { + // 检查 URL 中是否有 code 参数(OAuth2 回调带回) + const urlParams = new URLSearchParams(window.location.search); + const code = urlParams.get('code'); + + if (code) { + // 用 code 换 userid + try { + const resp = await fetch(`${API_BASE}/api/work-order/auth?code=${code}`); + const data = await resp.json(); + if (data.code === 0 && data.data?.userId) { + currentUserId.value = data.data.userId; + authChecked.value = true; + return true; + } + } catch { + // code 可能已过期 + } + } + + // 没有 code 或 code 无效:跳转企微 OAuth2 授权 + if (CORP_ID) { + const redirectUri = encodeURIComponent(window.location.href.split('?')[0] + '?' + urlParams.toString()); + const oauthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${CORP_ID}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&agentid=${AGENT_ID}&state=workorder#wechat_redirect`; + window.location.href = oauthUrl; + return false; + } + + // 未配置企微参数,跳过认证(开发模式) + authChecked.value = true; + return true; +} + /** 加载工单详情 */ async function loadDetail() { loading.value = true; @@ -124,14 +166,19 @@ function removeImage(index: number) { uploadedImages.value.splice(index, 1); } -onMounted(() => { +onMounted(async () => { alarmId.value = (route.query.alarmId as string) || ''; - if (alarmId.value) { - loadDetail(); - } else { + if (!alarmId.value) { loading.value = false; error.value = '缺少告警ID参数'; + return; } + + // 企微 OAuth2 认证 + const authed = await checkAuth(); + if (!authed) return; // 正在跳转授权页 + + loadDetail(); });