oms/frontend/src/api/wechat.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

74 lines
2.0 KiB
TypeScript

import { get, post, del } from '@/utils/request'
/** 二维码数据(后端申请临时码后返回,前端据此渲染二维码) */
export interface WechatQrcode {
scene: 'login' | 'bind'
/** 扫码会话标识,轮询状态时回传 */
state: string
/** 网关临时码(一次性,不用于展示) */
temp_code: string
/** 二维码内容:网关扫码页地址 */
page_url: string
/** 二维码有效秒数 */
expires_in: number
}
export interface WechatLoginStatus {
status: 'pending' | 'success' | 'unbound' | 'disabled'
message?: string
token?: string
user_id?: number
expires_in?: number
}
export interface WechatBindStatus {
status: 'pending' | 'success' | 'conflict' | 'failed'
message?: string
}
export interface WechatBindInfo {
bound: boolean
/** 脱敏后的 openid */
openid: string
/** 脱敏后的 unionid */
unionid: string
bind_scene: string
bound_at: string | null
last_login_at: string | null
}
/** 微信登录是否可用(服务端是否已配置网关应用) */
export function getWechatEnabled() {
return get<{ enabled: boolean }>('/auth/wechat/enabled')
}
/** 申请微信登录二维码(无需登录) */
export function createWechatLoginCode() {
return post<WechatQrcode>('/auth/wechat/login/code')
}
/** 轮询微信登录结果(结果一次性,取到即失效) */
export function getWechatLoginStatus(state: string) {
return get<WechatLoginStatus>('/auth/wechat/login/status', { state })
}
/** 申请微信绑定二维码(需登录) */
export function createWechatBindCode() {
return post<WechatQrcode>('/auth/wechat/bind/code')
}
/** 轮询微信绑定结果(需登录) */
export function getWechatBindStatus(state: string) {
return get<WechatBindStatus>('/auth/wechat/bind/status', { state })
}
/** 查询当前账号的微信绑定信息 */
export function getWechatBind() {
return get<WechatBindInfo>('/auth/wechat/bind')
}
/** 解绑当前账号的微信 */
export function unbindWechat() {
return del<{ unbound: boolean }>('/auth/wechat/bind')
}