fix(login): 恢复手机号快捷登录功能,避免带旧Token导致失败
- 将 login.tsx 和 register.tsx 中登录请求从封装的 request 工具改回 Taro.request - 取消登录请求中自动注入 Authorization 头,避免携带过期/旧 token 导致认证失败 - 调整登录接口响应数据解析,适配 Taro.request 的数据结构 - fetchUserInfo 改回使用 Taro.request,手动携带 Authorization 头部 - 添加全局 getPhoneNumber 调用冷却机制,防止频繁调用导致微信报错 - 登录页新增手机授权冷却
This commit is contained in:
23
src/app.tsx
23
src/app.tsx
@@ -26,21 +26,20 @@ function App(props: AppProps) {
|
||||
}
|
||||
|
||||
// 微信隐私协议(基础库 3.16.1+ 强制)
|
||||
// 必须使用 open-type="agreePrivacyAuthorization" 的 Button 让用户点击同意,
|
||||
// Taro.showModal 的按钮无法被微信识别为隐私授权,会导致后续敏感 API 仍被拒绝。
|
||||
// getPhoneNumber 等敏感能力必须由用户点击 agreePrivacyAuthorization 按钮授权。
|
||||
const wxAny: any = Taro
|
||||
if (typeof wxAny.getPrivacySetting === 'function') {
|
||||
wxAny.getPrivacySetting({
|
||||
success: (res: any) => {
|
||||
if (res?.privacyContractName) {
|
||||
privacyManager.setPrivacyContractName(res.privacyContractName)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
if (typeof wxAny.onNeedPrivacyAuthorization === 'function') {
|
||||
wxAny.onNeedPrivacyAuthorization((resolve: any) => {
|
||||
wxAny.getPrivacySetting({
|
||||
success: (setting: any) => {
|
||||
privacyManager.setPrivacyContractName(setting?.privacyContractName)
|
||||
privacyManager.show(resolve)
|
||||
},
|
||||
fail: () => {
|
||||
// 获取设置失败时,默认 resolve 同意,避免流程阻塞
|
||||
resolve({ event: 'agree', button: 'agree' })
|
||||
},
|
||||
})
|
||||
privacyManager.show(resolve)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -34,12 +34,14 @@ const PrivacyModal = () => {
|
||||
</Text>
|
||||
<View className='privacy-modal__footer'>
|
||||
<Button
|
||||
id='privacy-disagree-btn'
|
||||
className='privacy-modal__btn'
|
||||
onClick={() => privacyManager.disagree()}
|
||||
>
|
||||
拒绝
|
||||
</Button>
|
||||
<Button
|
||||
id='privacy-agree-btn'
|
||||
className='privacy-modal__btn privacy-modal__btn--primary'
|
||||
openType='agreePrivacyAuthorization'
|
||||
onAgreePrivacyAuthorization={() => privacyManager.agree()}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import Taro from '@tarojs/taro'
|
||||
import { View, Image, Text, Button } from '@tarojs/components'
|
||||
import { TenantId } from '@/config/app'
|
||||
@@ -7,6 +7,11 @@ import { getUserInfo } from '@/api/layout'
|
||||
import { saveStorageByLoginUser, SERVER_API_URL } from '@/utils/server'
|
||||
import { isUserDisabled } from '@/utils/auth'
|
||||
import request from '@/utils/request'
|
||||
import {
|
||||
getPhoneAuthCooldownRemaining,
|
||||
isPhoneAuthCoolingDown,
|
||||
markPhoneAuthCalled,
|
||||
} from '@/utils/phoneAuth'
|
||||
import {
|
||||
checkAndHandleInviteRelation,
|
||||
hasPendingInvite,
|
||||
@@ -73,21 +78,22 @@ async function ensureWxOpenIdSaved() {
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取用户信息(使用临时 token,避免本地 storage 尚未写入) */
|
||||
/** 获取用户信息 */
|
||||
async function fetchUserInfo(token: string) {
|
||||
try {
|
||||
const res: any = await request.get(
|
||||
`${SERVER_API_URL}/auth/user`,
|
||||
{},
|
||||
{
|
||||
header: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
showError: false,
|
||||
}
|
||||
)
|
||||
if (res?.code === 0 && res?.data) {
|
||||
return res.data
|
||||
const res: any = await Taro.request({
|
||||
url: `${SERVER_API_URL}/auth/user`,
|
||||
method: 'GET',
|
||||
header: {
|
||||
Authorization: token,
|
||||
'content-type': 'application/json',
|
||||
TenantId: String(TenantId),
|
||||
},
|
||||
timeout: 15000,
|
||||
})
|
||||
|
||||
if (res.data?.code === 0 && res.data?.data) {
|
||||
return res.data.data
|
||||
}
|
||||
return null
|
||||
} catch (e) {
|
||||
@@ -99,16 +105,39 @@ async function fetchUserInfo(token: string) {
|
||||
const Login = () => {
|
||||
const [isAgree, setIsAgree] = useState(false)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [phoneAuthCooling, setPhoneAuthCooling] = useState(isPhoneAuthCoolingDown())
|
||||
const [showContent, setShowContent] = useState(false)
|
||||
const phoneAuthInFlightRef = useRef(false)
|
||||
const phoneAuthTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
|
||||
const router = Taro.getCurrentInstance().router
|
||||
const isWeapp = IS_WEAPP
|
||||
const canUsePhoneAuth = isAgree && !loading && !phoneAuthCooling
|
||||
|
||||
/** 页面加载动画 */
|
||||
useEffect(() => {
|
||||
setTimeout(() => setShowContent(true), 100)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (phoneAuthTimerRef.current) {
|
||||
clearTimeout(phoneAuthTimerRef.current)
|
||||
phoneAuthTimerRef.current = null
|
||||
}
|
||||
}
|
||||
}, [])
|
||||
|
||||
const startPhoneAuthCooldown = () => {
|
||||
const remaining = Math.max(getPhoneAuthCooldownRemaining(), 300)
|
||||
setPhoneAuthCooling(true)
|
||||
if (phoneAuthTimerRef.current) clearTimeout(phoneAuthTimerRef.current)
|
||||
phoneAuthTimerRef.current = setTimeout(() => {
|
||||
phoneAuthTimerRef.current = null
|
||||
setPhoneAuthCooling(false)
|
||||
}, remaining)
|
||||
}
|
||||
|
||||
/** 解析 redirect 参数 */
|
||||
const redirectUrl = (() => {
|
||||
const raw = (router?.params as Record<string, string> | undefined)?.redirect
|
||||
@@ -201,10 +230,19 @@ const Login = () => {
|
||||
Taro.showToast({ title: '请先勾选同意协议', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (loading) return
|
||||
if (loading || phoneAuthInFlightRef.current || isPhoneAuthCoolingDown()) {
|
||||
startPhoneAuthCooldown()
|
||||
Taro.showToast({ title: '请稍后再试', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
const { code: phoneCode, errMsg } = detail || {}
|
||||
phoneAuthInFlightRef.current = true
|
||||
markPhoneAuthCalled()
|
||||
startPhoneAuthCooldown()
|
||||
|
||||
const { code: phoneCode, encryptedData, iv, errMsg } = detail || {}
|
||||
if (!phoneCode || (errMsg && errMsg.includes('fail'))) {
|
||||
phoneAuthInFlightRef.current = false
|
||||
showPhoneAuthFailedModal(errMsg)
|
||||
return
|
||||
}
|
||||
@@ -219,12 +257,19 @@ const Login = () => {
|
||||
`${SERVER_API_URL}/wx-login/loginByMpWxPhone`,
|
||||
{
|
||||
code: phoneCode,
|
||||
encryptedData,
|
||||
iv,
|
||||
notVerifyPhone: true,
|
||||
refereeId,
|
||||
sceneType: 'save_referee',
|
||||
tenantId: Number(TenantId),
|
||||
},
|
||||
{ showError: false }
|
||||
{
|
||||
timeout: 20000,
|
||||
retry: 0,
|
||||
showError: false,
|
||||
returnRaw: true,
|
||||
}
|
||||
)
|
||||
|
||||
if (res?.code === 0 && res?.data?.access_token) {
|
||||
@@ -266,6 +311,7 @@ const Login = () => {
|
||||
console.error('微信登录失败:', e)
|
||||
Taro.showToast({ title: e?.message || '登录失败', icon: 'none' })
|
||||
} finally {
|
||||
phoneAuthInFlightRef.current = false
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
@@ -309,9 +355,9 @@ const Login = () => {
|
||||
margin: 0,
|
||||
padding: 0,
|
||||
}}
|
||||
openType='getPhoneNumber'
|
||||
openType={canUsePhoneAuth ? 'getPhoneNumber' : undefined}
|
||||
onGetPhoneNumber={handleGetPhoneNumber}
|
||||
disabled={!isAgree || loading}
|
||||
disabled={!canUsePhoneAuth}
|
||||
loading={loading}
|
||||
>
|
||||
手机号快捷登录
|
||||
|
||||
@@ -4,9 +4,8 @@ import { View, Text } from '@tarojs/components'
|
||||
import { Button, Checkbox } from '@nutui/nutui-react-taro'
|
||||
import { TenantId } from '@/config/app'
|
||||
import { getUserInfo, getWxOpenId } from '@/api/layout'
|
||||
import { saveStorageByLoginUser, SERVER_API_URL } from '@/utils/server'
|
||||
import { saveStorageByLoginUser } from '@/utils/server'
|
||||
import { isUserDisabled } from '@/utils/auth'
|
||||
import request from '@/utils/request'
|
||||
import {
|
||||
getStoredInviteParams,
|
||||
parseInviteParams,
|
||||
@@ -26,6 +25,17 @@ interface GetPhoneNumberEvent {
|
||||
detail: GetPhoneNumberDetail
|
||||
}
|
||||
|
||||
interface LoginResponse {
|
||||
data: {
|
||||
code?: number
|
||||
message?: string
|
||||
data?: {
|
||||
access_token: string
|
||||
user: any
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function getWeappLoginCode(): Promise<string | undefined> {
|
||||
try {
|
||||
const res = await new Promise<{ code?: string }>((resolve, reject) => {
|
||||
@@ -181,9 +191,10 @@ const Register = () => {
|
||||
// 获取小程序登录 code(用于后续绑定 openid)
|
||||
const wxLoginCode = await getWeappLoginCode()
|
||||
|
||||
const res: any = await request.post(
|
||||
`${SERVER_API_URL}/wx-login/loginByMpWxPhone`,
|
||||
{
|
||||
const res = (await Taro.request({
|
||||
url: 'https://shop-api.websoft.top/api/wx-login/loginByMpWxPhone',
|
||||
method: 'POST',
|
||||
data: {
|
||||
code: phoneCode,
|
||||
encryptedData,
|
||||
iv,
|
||||
@@ -192,16 +203,19 @@ const Register = () => {
|
||||
sceneType: 'save_referee',
|
||||
tenantId: TenantId,
|
||||
},
|
||||
{ showError: false }
|
||||
)
|
||||
header: {
|
||||
'content-type': 'application/json',
|
||||
TenantId,
|
||||
},
|
||||
})) as unknown as LoginResponse
|
||||
|
||||
if (res?.code === 1) {
|
||||
Taro.showToast({ title: res.message || '登录失败', icon: 'none' })
|
||||
if ((res as any)?.data?.code === 1) {
|
||||
Taro.showToast({ title: res.data.message || '登录失败', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
const token = res?.data?.access_token
|
||||
const user = res?.data?.user
|
||||
const token = res?.data?.data?.access_token
|
||||
const user = res?.data?.data?.user
|
||||
if (!token || !user?.userId) {
|
||||
Taro.showToast({ title: '登录失败,请重试', icon: 'none' })
|
||||
return
|
||||
|
||||
35
src/utils/phoneAuth.ts
Normal file
35
src/utils/phoneAuth.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* getPhoneNumber 全局频率控制
|
||||
*
|
||||
* 微信原生层对 getPhoneNumber API 有调用频率限制,短时间内多次调用会报:
|
||||
* [渲染层错误] invoke getPhoneNumber too frequently
|
||||
*
|
||||
* 本模块提供模块级(跨页面)的时间戳锁,配合各页面的 useRef 同步锁 + 条件渲染使用。
|
||||
*/
|
||||
|
||||
/** 上次 getPhoneNumber 回调触发的时间戳(ms) */
|
||||
let lastPhoneAuthTime = 0
|
||||
|
||||
/** 冷却期(ms),在此期间不允许再次触发 getPhoneNumber */
|
||||
const PHONE_AUTH_COOLDOWN = 3000
|
||||
|
||||
/**
|
||||
* 检查当前是否在冷却期内。
|
||||
* 在渲染 getPhoneNumber 按钮前调用此函数,若在冷却期内则不渲染 openType。
|
||||
*/
|
||||
export function isPhoneAuthCoolingDown(): boolean {
|
||||
if (lastPhoneAuthTime === 0) return false
|
||||
return Date.now() - lastPhoneAuthTime < PHONE_AUTH_COOLDOWN
|
||||
}
|
||||
|
||||
/** 记录一次 getPhoneNumber 回调已触发(在 onGetPhoneNumber 回调入口调用) */
|
||||
export function markPhoneAuthCalled(): void {
|
||||
lastPhoneAuthTime = Date.now()
|
||||
}
|
||||
|
||||
/** 获取冷却期剩余时间(ms),用于设置 unlock 延迟 */
|
||||
export function getPhoneAuthCooldownRemaining(): number {
|
||||
if (lastPhoneAuthTime === 0) return 0
|
||||
const remaining = PHONE_AUTH_COOLDOWN - (Date.now() - lastPhoneAuthTime)
|
||||
return remaining > 0 ? remaining : 0
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
* 必须在页面中渲染 <PrivacyModal /> 并绑定本管理器。
|
||||
*/
|
||||
|
||||
type PrivacyResolve = (result: { event: 'agree' | 'disagree'; button: string }) => void
|
||||
type PrivacyResolve = (result: { event: 'agree' | 'disagree'; buttonId: string }) => void
|
||||
|
||||
let currentResolve: PrivacyResolve | null = null
|
||||
let showCallback: ((show: boolean) => void) | null = null
|
||||
@@ -31,16 +31,22 @@ export const privacyManager = {
|
||||
showCallback?.(true)
|
||||
},
|
||||
|
||||
/** 隐私协议授权流程已结束,隐藏弹窗 */
|
||||
hide() {
|
||||
currentResolve = null
|
||||
showCallback?.(false)
|
||||
},
|
||||
|
||||
/** 用户点击同意(由 open-type=agreePrivacyAuthorization 的 button 触发) */
|
||||
agree() {
|
||||
currentResolve?.({ event: 'agree', button: 'agree' })
|
||||
currentResolve?.({ event: 'agree', buttonId: 'privacy-agree-btn' })
|
||||
currentResolve = null
|
||||
showCallback?.(false)
|
||||
},
|
||||
|
||||
/** 用户点击拒绝 */
|
||||
disagree() {
|
||||
currentResolve?.({ event: 'disagree', button: 'disagree' })
|
||||
currentResolve?.({ event: 'disagree', buttonId: 'privacy-disagree-btn' })
|
||||
currentResolve = null
|
||||
showCallback?.(false)
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user