fix(login): 手机号授权透传新版 code 令牌,兼容 iv/encryptedData 两种回传形态

This commit is contained in:
ozh 2026-09-16 15:19:09 +08:00
parent 10905df143
commit 026578dfbd
3 changed files with 21 additions and 9 deletions

View File

@ -5,8 +5,9 @@ export interface WxMiniappLoginParams {
appId: string // 小程序 appId appId: string // 小程序 appId
code: string // Taro.login() 一次性 code,5 分钟有效 code: string // Taro.login() 一次性 code,5 分钟有效
openId?: string // 首次登录响应返回,后续登录/解密回传 openId?: string // 首次登录响应返回,后续登录/解密回传
iv?: string // 加密资料初始向量(getUserProfile 授权后补传) iv?: string // 加密资料初始向量(getUserProfile/getPhoneNumber 授权后补传)
encryptedData?: string // 加密用户资料(getUserProfile 授权后补传) encryptedData?: string // 加密用户资料(getUserProfile/getPhoneNumber 授权后补传)
phoneCode?: string // 手机号授权新版动态令牌(getPhoneNumber 回调 detail.code,后端可直连微信换取手机号)
} }
// 首次登录(needUserInfo)响应:仅含绑定凭证,无 token/用户信息 // 首次登录(needUserInfo)响应:仅含绑定凭证,无 token/用户信息

View File

@ -83,9 +83,14 @@ export default function Login() {
// noMobile:手机号授权按钮回调(Button open-type=getPhoneNumber) // noMobile:手机号授权按钮回调(Button open-type=getPhoneNumber)
const handleGetPhoneNumber = async (e: { const handleGetPhoneNumber = async (e: {
detail: { iv: string; encryptedData: string; errMsg?: string } detail: { iv: string; encryptedData: string; code?: string; errMsg?: string }
}) => { }) => {
console.log('[login-page] 手机号授权回调', e.detail.errMsg) console.log('[login-page] 手机号授权回调', {
errMsg: e.detail.errMsg,
// 两种手机号回传形态:新版 code(动态令牌)或旧版 iv/encryptedData(加密数据)
hasCode: Boolean(e.detail.code),
hasEncrypted: Boolean(e.detail.encryptedData)
})
if (loading || !openId) return if (loading || !openId) return
setLoading(true) setLoading(true)
try { try {

View File

@ -45,7 +45,7 @@ export async function performWxLogin(): Promise<WxMiniappLoginResponse> {
// 重调登录接口的公共尾部:换新 code(一次性)→ 组装参数 → 调用 → 缓存 openId // 重调登录接口的公共尾部:换新 code(一次性)→ 组装参数 → 调用 → 缓存 openId
async function reloginWith( async function reloginWith(
openId: string, openId: string,
extra: { iv?: string; encryptedData?: string } extra: { iv?: string; encryptedData?: string; phoneCode?: string }
): Promise<WxMiniappLoginResponse> { ): Promise<WxMiniappLoginResponse> {
let code: string let code: string
try { try {
@ -74,13 +74,19 @@ export async function completeUserInfoLogin(openId: string): Promise<WxMiniappLo
return reloginWith(openId, profile) return reloginWith(openId, profile)
} }
// noMobile 三次授权:手机号授权按钮回调(open-type=getPhoneNumber)拿 iv/encryptedData → 重调登录接口 // noMobile 三次授权:手机号授权按钮回调(open-type=getPhoneNumber)→ 重调登录接口
// detail 两种形态:新版含 code(动态令牌,后端可直连微信换取手机号);
// 旧版含 iv/encryptedData(需与 session_key 解密,依赖同一 code 会话)
export async function completeMobileLogin( export async function completeMobileLogin(
openId: string, openId: string,
detail: { iv: string; encryptedData: string; errMsg?: string } detail: { iv: string; encryptedData: string; code?: string; errMsg?: string }
): Promise<WxMiniappLoginResponse> { ): Promise<WxMiniappLoginResponse> {
if (!detail.iv || !detail.encryptedData) { if (!detail.iv && !detail.code) {
throw new Error(detail.errMsg || '手机号授权失败') throw new Error(detail.errMsg || '手机号授权失败')
} }
return reloginWith(openId, { iv: detail.iv, encryptedData: detail.encryptedData }) return reloginWith(openId, {
iv: detail.iv,
encryptedData: detail.encryptedData,
phoneCode: detail.code
})
} }