功能:H5 工单页企微 OAuth2 认证
页面打开时检查 URL 中的 code,有则换 userid, 无则跳转企微授权页。未配置企微参数时跳过(开发模式)。
This commit is contained in:
@@ -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();
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user