SEC_Warehouse/src/api/auth.ts

58 lines
2.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 { request } from '@/http'
import { USER, THIRD_PLATFORM } from '@/constants/endpoints'
// 微信小程序登录请求参数(2026-09-14 文档版,openId 为二次登录回传字段)
export interface WxMiniappLoginParams {
appId: string // 小程序 appId
code: string // Taro.login() 一次性 code,5 分钟有效
openId?: string // 首次登录响应返回,后续登录/解密回传
iv?: string // 加密资料初始向量(getUserProfile/getPhoneNumber 授权后补传)
encryptedData?: string // 加密用户资料(getUserProfile/getPhoneNumber 授权后补传)
phoneCode?: string // 手机号授权新版动态令牌(getPhoneNumber 回调 detail.code,后端可直连微信换取手机号)
}
// 首次登录(needUserInfo)响应:仅含绑定凭证,无 token/用户信息
export interface WxNeedUserInfoResult {
openId: string
bindKey: string
}
// 用户信息(补授权后完整登录返回;联调前字段按文档版,实际字段名待确认)
export interface WxUserInfo {
userId: string
userType: number
avatar: string
nickName: string
mobile: string
gender: number
region: string
loginFrom: string
}
// 完整登录响应(needUserInfo=false 时)
export interface WxLoginResult {
userInfo: WxUserInfo
code: string | null // 后端文档写登录凭证,联调实测恒为 null(token 走响应头)
openId?: string // 首次登录返回,需本地缓存
token?: string // http 层从响应头 Token 提取附加(仅登录接口返回)
}
// 登录响应联合体:Message 子状态由 http 层附加到 message 字段
// ''/undefined = 完整登录;'needUserInfo' = 需头像昵称授权;'noMobile' = 需手机号授权
export type WxMiniappLoginResponse = WxLoginResult &
Partial<WxNeedUserInfoResult> & { message?: string }
export function wxMiniappLogin(params: WxMiniappLoginParams): Promise<WxMiniappLoginResponse> {
return request.post<WxLoginResult>(
THIRD_PLATFORM.wxMiniappLogin,
params as unknown as Record<string, unknown>,
{ needAuth: false }
)
}
// 冷启动登录态校验:token 有效返回登录信息,过期返回 202 由 http 层统一踢登录页。
// 响应 Content 结构(ResponseData<UserInfo>)联调实测后修正字段
export function getLoginInfo(): Promise<WxUserInfo> {
return request.get(USER.getLoginInfo)
}