WIP-CHECKPOINT[auth-refactor]: 固化工程师崩溃前部分成果 + 同树其他未提交WIP(仅源码,不含密钥/二进制)-- 待重激活工程师续作

This commit is contained in:
Simon
2026-07-07 21:52:11 +08:00
parent 242c1967ff
commit fab75760e0
203 changed files with 21504 additions and 3345 deletions
+321 -5
View File
@@ -1,11 +1,11 @@
<!--
=============================================================================
IT智能服务台 管理员登录页 (v1.1, 2026-07-04)
IT智能服务台 管理员登录页 (v1.2, 2026-07-06)
=============================================================================
说明从用户ID+姓名登录改为账号密码+OTP表单登录
说明智能检测企微登录状态提供三种登录方式
- PRD v1.5 §4.5: 企微免密登录/企微扫码登录/账号密码+OTP
- 复用坐席端登录 APIPOST /api/agents/login
- 登录后检查 role === 'admin' admin 提示"无管理权限"
- 支持账号密码+OTP认证
- 企微免密登录检测到企微登录账号且有管理员角色免密直接进入
-->
<template>
<div class="login-page">
@@ -25,6 +25,50 @@ IT智能服务台 — 管理员登录页 (v1.1, 2026-07-04)
<p class="login-subtitle">管理后台</p>
</div>
<!-- 登录方式选择企微已登录时显示 -->
<div v-if="showLoginOptions" class="login-options">
<p class="login-options-title">选择登录方式</p>
<div class="login-options-btns">
<el-button
type="primary"
size="large"
class="option-btn"
:loading="wecomQuickLoading"
@click="handleWecomQuickLogin"
>
<el-icon><Key /></el-icon>
企微免密登录
</el-button>
<el-button
size="large"
class="option-btn"
@click="showQrLogin"
>
<el-icon><Key /></el-icon>
企微扫码登录
</el-button>
<el-button
size="large"
class="option-btn"
@click="showPasswordLogin"
>
<el-icon><Lock /></el-icon>
账号密码登录
</el-button>
</div>
</div>
<!-- 企微扫码登录面板 -->
<div v-if="showQrPanel" class="qr-login">
<p class="qr-title">企微扫码登录</p>
<div class="qr-code">
<img v-if="qrCode" :src="qrCode" alt="扫码登录二维码" />
<div v-else class="qr-loading">加载中...</div>
</div>
<p class="qr-hint">请使用企业微信扫码登录</p>
<el-button link type="primary" @click="showPasswordLogin">其他登录方式</el-button>
</div>
<!-- 登录表单 -->
<el-form
ref="formRef"
@@ -105,7 +149,7 @@ IT智能服务台 — 管理员登录页 (v1.1, 2026-07-04)
// ==========================================================================
// 依赖导入
// ==========================================================================
import { ref, reactive } from 'vue'
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAdminStore } from '@/stores/admin'
@@ -138,6 +182,31 @@ const requireOtp = ref(false)
/** 错误信息 */
const errorMsg = ref<string>('')
// ==========================================================================
// 企微智能检测相关状态(PRD v1.5 §4.5
// ==========================================================================
/** 是否显示登录方式选择 */
const showLoginOptions = ref(false)
/** 是否显示扫码登录面板 */
const showQrPanel = ref(false)
/** 是否显示账号密码登录 */
const showPasswordPanel = ref(true)
/** 企微用户ID(用于免密登录) */
const wecomUserId = ref('')
/** 企微快捷登录loading */
const wecomQuickLoading = ref(false)
/** 企微二维码 */
const qrCode = ref('')
/** 企微检测中 */
const wecomChecking = ref(false)
/** 表单校验规则 */
const rules: FormRules = {
userId: [
@@ -156,6 +225,186 @@ const rules: FormRules = {
// 方法
// ==========================================================================
/**
* 检测企微客户端登录状态(PRD v1.5 §4.5
* 页面加载时自动检测
*/
async function checkWecomClient(): Promise<void> {
wecomChecking.value = true
try {
// 首先尝试使用 UA 检测
const isWxWork = /wxwork/i.test(navigator.userAgent)
// 不在企微环境,直接显示账号密码登录
if (!isWxWork) {
showLoginOptions.value = false
showPasswordPanel.value = true
wecomChecking.value = false
return
}
// 在企微客户端中,尝试使用企微 JS-SDK
if (typeof (window as any).wx !== 'undefined') {
try {
// 获取当前页面 URL
const currentUrl = window.location.href.split('#')[0]
// 调用后端接口获取 JS-SDK 签名配置
const apiClient = (await import('@/api')).default
const configResponse = await apiClient.get('/wecom/jsapi-config', {
params: { url: currentUrl }
})
const jsConfig = configResponse
console.log('[Admin Login] 获取到企微 JS-SDK 配置:', jsConfig)
// 使用 wx.config 配置企微 JS-SDK
await new Promise<void>((resolve) => {
(window as any).wx.config({
beta: true,
debug: false,
appId: jsConfig.corp_id,
timestamp: jsConfig.timestamp,
nonceStr: jsConfig.nonce_str,
signature: jsConfig.signature,
jsApiList: ['agentConfig']
})
resolve()
})
// 使用 wx.agentConfig 获取当前用户信息
const userInfo = await new Promise<any>((resolve, reject) => {
(window as any).wx.agentConfig({
corpid: jsConfig.corp_id,
agentid: jsConfig.agent_id,
timestamp: jsConfig.timestamp,
nonceStr: jsConfig.nonce_str,
signature: jsConfig.signature,
success: (res: any) => resolve(res),
fail: (res: any) => reject(new Error(res.errMsg || 'agentConfig 失败'))
})
})
// 获取用户ID
const userId = userInfo.userId
if (userId) {
console.log('[Admin Login] 获取到企微用户 ID:', userId)
wecomUserId.value = userId
// 检测角色
const roleResponse = await apiClient.get('/wecom/check-role', {
params: { userid: userId }
})
const roleData = roleResponse
console.log('[Admin Login] 用户角色:', roleData)
// 如果是管理员,显示免密登录选项
if (roleData.role === 'admin') {
showLoginOptions.value = true
showPasswordPanel.value = false
} else {
// 非管理员,显示普通登录
showLoginOptions.value = false
showPasswordPanel.value = true
}
}
} catch (e) {
console.warn('[Admin Login] 企微 JS-SDK 检测失败:', e)
// 检测失败,显示登录选项
showLoginOptions.value = true
showPasswordPanel.value = true
}
} else {
// 没有企微 JS-SDK
showLoginOptions.value = true
showPasswordPanel.value = true
}
} catch (error) {
console.error('企微客户端检测失败:', error)
showLoginOptions.value = false
showPasswordPanel.value = true
} finally {
wecomChecking.value = false
}
}
/**
* 企微免密登录
*/
async function handleWecomQuickLogin(): Promise<void> {
wecomQuickLoading.value = true
try {
const apiClient = (await import('@/api')).default
// 调用后端企微免密登录接口
const response = await apiClient.post('/auth_wecom/jsdk-login', {
userid: wecomUserId.value,
login_source: 'wecom_jsdk_admin'
})
const result = response
if (result?.token) {
const { token, employee_id, name } = result
// 存储 token
localStorage.setItem('admin_token', token)
if (employee_id) {
localStorage.setItem('admin_user_id', employee_id)
}
// 存储到 store
adminStore.token = token
adminStore.adminUserId = employee_id
ElMessage.success('登录成功')
router.push('/')
} else {
throw new Error('免密登录失败')
}
} catch (error) {
console.error('企微免密登录失败:', error)
ElMessage.error('免密登录失败,请使用其他方式')
} finally {
wecomQuickLoading.value = false
}
}
/**
* 显示扫码登录
*/
async function showQrLogin(): Promise<void> {
showLoginOptions.value = false
showQrPanel.value = true
showPasswordPanel.value = false
// 获取二维码
try {
const apiClient = (await import('@/api')).default
const response = await apiClient.post('/auth_qrcode/create')
const result = response
if (result.code === 0) {
qrCode.value = result.data.qrcode_png_base64
? `data:image/png;base64,${result.data.qrcode_png_base64}`
: result.data.qrcode_url
}
} catch (error) {
console.error('获取二维码失败:', error)
}
}
/**
* 显示账号密码登录
*/
function showPasswordLogin(): Promise<void> {
showLoginOptions.value = false
showQrPanel.value = false
showPasswordPanel.value = true
return Promise.resolve()
}
/**
* 处理登录
*/
@@ -185,6 +434,11 @@ async function handleLogin(): Promise<void> {
errorMsg.value = errMsg
}
}
// 页面加载时检测企微客户端状态
onMounted(() => {
checkWecomClient()
})
</script>
<style scoped>
@@ -274,4 +528,66 @@ async function handleLogin(): Promise<void> {
font-size: 12px;
color: var(--text-muted);
}
/* 登录方式选择 */
.login-options {
text-align: center;
padding: 20px 0;
}
.login-options-title {
font-size: 14px;
color: var(--text-secondary);
margin-bottom: 16px;
}
.login-options-btns {
display: flex;
flex-direction: column;
gap: 12px;
}
.option-btn {
width: 100%;
height: 44px;
font-size: 14px;
}
/* 扫码登录 */
.qr-login {
text-align: center;
padding: 20px 0;
}
.qr-title {
font-size: 16px;
color: var(--text-primary);
margin-bottom: 16px;
}
.qr-code {
width: 180px;
height: 180px;
margin: 0 auto 16px;
background: var(--bg-secondary);
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
}
.qr-code img {
max-width: 100%;
max-height: 100%;
}
.qr-loading {
color: var(--text-muted);
}
.qr-hint {
font-size: 13px;
color: var(--text-muted);
margin-bottom: 12px;
}
</style>