oms/frontend/src/store/auth.ts
liufumingren 1847e17ba7 feat(auth): 接入微信扫码登录与微信绑定
- 新增网关客户端与通知验签(sha256(appSecret) 派生 HMAC-SHA256,5 分钟防重放),
  扫码会话与一次性结果存 Redis,openid 仅服务端保存
- 新增 user_wechat_binds 表与迁移:openid 唯一(一个微信只绑一个账号)、
  user_id 唯一(重新绑定覆盖旧微信)
- 新增 /api/auth/wechat/* 接口与 /api/wx/notify 网关回调;登录二维码按 IP 限流,
  未配置网关时相关接口返回 400 提示,不影响账号密码 / 邮箱验证码登录
- 前端:「微信登录」tab(二维码 + 倒计时 + 轮询)与「我的」页微信绑定 / 解绑
- 开发与生产各出一份 WX_GATEWAY_* 配置样板,生产权威副本走 shared-config 覆盖
- 同步补充 swagger 契约与请求日志脱敏字段(bind_code / sign)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-01 23:17:11 +08:00

124 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import router from '@/router'
import {
login as loginApi,
register as registerApi,
sendEmailCode as sendEmailCodeApi,
sendLoginCode as sendLoginCodeApi,
loginByEmailCode as loginByEmailCodeApi,
sendResetCode as sendResetCodeApi,
resetPasswordByCode as resetPasswordByCodeApi,
getProfile,
type UserInfo,
type RegisterPayload,
} from '@/api/auth'
import { getToken, setToken, clearToken } from '@/utils/request'
import { useMenuStore } from '@/store/menu'
import { usePrincipalStore } from '@/store/principal'
export const useAuthStore = defineStore('auth', () => {
const token = ref<string>(getToken())
const user = ref<UserInfo | null>(null)
/** 是否已有所属主体(多租户隔离的开关) */
const hasPrincipal = computed(
() => Array.isArray(user.value?.principals) && user.value!.principals.length > 0,
)
/** 拉取并缓存当前用户信息 */
async function fetchProfile() {
user.value = await getProfile()
// 尚未在 localStorage 选择主体时,默认选中第一个所属主体
const ps = usePrincipalStore()
if (!ps.currentId && user.value?.principals?.length) {
ps.setCurrentId(user.value.principals[0].id)
}
}
/** 账号登录 */
async function login(account: string, password: string) {
const res = await loginApi({ account, password })
applyToken(res.token)
await fetchProfile()
await useMenuStore().fetchMenus()
return res
}
/** 邮箱验证码登录 */
async function loginByEmailCode(email: string, code: string) {
const res = await loginByEmailCodeApi({ email, code })
applyToken(res.token)
await fetchProfile()
await useMenuStore().fetchMenus()
return res
}
/** 微信扫码登录:扫码结果由后端轮询接口换成 token,这里只负责落登录态 */
async function loginByWechatToken(token: string) {
applyToken(token)
await fetchProfile()
await useMenuStore().fetchMenus()
}
/** 注册账号 */
async function register(payload: RegisterPayload) {
const res = await registerApi(payload)
applyToken(res.token)
await fetchProfile()
await useMenuStore().fetchMenus()
return res
}
/** 发送邮箱验证码(注册场景,6 位) */
async function sendEmailCode(email: string) {
return sendEmailCodeApi({ email })
}
/** 发送邮箱登录验证码(6 位) */
async function sendLoginCode(email: string) {
return sendLoginCodeApi(email)
}
/** 发送密码重置验证码(4 位) */
async function sendResetCode(email: string) {
return sendResetCodeApi(email)
}
/** 通过邮箱验证码重置密码 */
async function resetPasswordByCode(data: { email: string; code: string; password: string }) {
return resetPasswordByCodeApi(data)
}
/** 退出登录 */
function logout() {
clearToken()
token.value = ''
user.value = null
ElMessage.success('已退出登录')
router.replace('/login')
}
function applyToken(value: string) {
token.value = value
setToken(value)
}
return {
token,
user,
hasPrincipal,
login,
loginByEmailCode,
loginByWechatToken,
register,
sendEmailCode,
sendLoginCode,
sendResetCode,
resetPasswordByCode,
fetchProfile,
logout,
}
})