refactor(user): 禁用 loginByOpenId 静默登录并优化登录状态控制
- 移除 UserContext 和 useUser 中的 loginByOpenId 调用及相关降级逻辑 - 无本地 token 时保持未登录状态,不再自动触发静默登录 - 清理无用导入 TenantId 和 loginByOpenId - 订单列表页增加登录校验,未登录用户跳转登录页并阻止渲染订单 - 商城首页价格改为 Price 组件,未登录游客显示登录遮罩,单位仅登录后可见
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import React, { createContext, useContext, useState, useEffect, type ReactNode } from 'react'
|
||||
import Taro from '@tarojs/taro'
|
||||
import type { User } from '@/api/system/user/model'
|
||||
import { getUserInfo, loginByOpenId } from '@/api/layout'
|
||||
import { getUserInfo } from '@/api/layout'
|
||||
import { saveStorageByLoginUser, clearStorageByLoginUser } from '@/utils/server'
|
||||
import { TenantId } from '@/config/app'
|
||||
|
||||
interface UserContextType {
|
||||
user: User | null
|
||||
@@ -42,36 +41,12 @@ export const UserProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
setLoading(false)
|
||||
return
|
||||
} catch {
|
||||
// token无效,清除并尝试静默登录
|
||||
// token无效,清除
|
||||
clearStorageByLoginUser()
|
||||
}
|
||||
}
|
||||
|
||||
// 情况3: 没有本地token,尝试静默登录(老用户自动登录)
|
||||
try {
|
||||
const loginResult = await new Promise<{ access_token?: string; user?: User }>((resolve, reject) => {
|
||||
Taro.login({
|
||||
success: (res) => {
|
||||
if (res.code) {
|
||||
loginByOpenId({ code: res.code, tenantId: TenantId })
|
||||
.then(resolve)
|
||||
.catch(reject)
|
||||
} else {
|
||||
reject(new Error('no code'))
|
||||
}
|
||||
},
|
||||
fail: reject,
|
||||
})
|
||||
})
|
||||
// 静默登录成功(老用户),自动完成登录
|
||||
if (loginResult?.access_token && loginResult?.user) {
|
||||
saveStorageByLoginUser(loginResult.access_token, loginResult.user)
|
||||
setUser(loginResult.user)
|
||||
}
|
||||
} catch {
|
||||
// 静默登录失败(非老用户或网络问题),保持未登录状态
|
||||
}
|
||||
|
||||
// 无本地token,保持未登录状态(已禁用静默登录)
|
||||
setLoading(false)
|
||||
}
|
||||
init()
|
||||
|
||||
@@ -2,8 +2,6 @@ import { useUserContext } from '@/contexts/UserContext'
|
||||
import { useEffect, useState } from 'react'
|
||||
import Taro from '@tarojs/taro'
|
||||
import type { User } from '@/api/system/user/model'
|
||||
import { loginByOpenId } from '@/api/layout'
|
||||
import { TenantId } from '@/config/app'
|
||||
|
||||
/**
|
||||
* useUser - 兼容性 Hook
|
||||
@@ -57,24 +55,8 @@ const useUserFallback = () => {
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
const hasLocal = loadFromStorage()
|
||||
if (!hasLocal) {
|
||||
try {
|
||||
const data = await new Promise<any>((resolve, reject) => {
|
||||
Taro.login({
|
||||
success: (res) => loginByOpenId({ code: res.code, tenantId: TenantId }).then(resolve).catch(reject),
|
||||
fail: reject,
|
||||
})
|
||||
})
|
||||
if (data?.access_token && data?.user) {
|
||||
loginUser(data.access_token, data.user)
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
setLoading(false)
|
||||
}
|
||||
init()
|
||||
loadFromStorage()
|
||||
setLoading(false)
|
||||
}, [])
|
||||
|
||||
return { user, isLoggedIn: !!user, loading, loginUser, logoutUser, refreshUser, syncFromStorage }
|
||||
|
||||
@@ -31,7 +31,7 @@ interface TabState {
|
||||
}
|
||||
|
||||
const OrderListPage: React.FC = () => {
|
||||
const { user } = useUser()
|
||||
const { user, isLoggedIn } = useUser()
|
||||
const initTab = Taro.getCurrentInstance().router?.params?.tab
|
||||
?? Taro.getStorageSync('order_tab')
|
||||
// 读取后清除,避免影响下次进入
|
||||
@@ -104,10 +104,14 @@ const OrderListPage: React.FC = () => {
|
||||
|
||||
// 切换 tab 时加载数据
|
||||
useEffect(() => {
|
||||
if (!isLoggedIn) {
|
||||
Taro.navigateTo({ url: '/passport/login' })
|
||||
return
|
||||
}
|
||||
if (!tabStates[tabIndex].initialized) {
|
||||
loadOrders(tabIndex, 1)
|
||||
}
|
||||
}, [tabIndex])
|
||||
}, [tabIndex, isLoggedIn])
|
||||
|
||||
const handleLoadMore = () => {
|
||||
if (!currentState.finished && !currentState.loading) {
|
||||
@@ -121,6 +125,10 @@ const OrderListPage: React.FC = () => {
|
||||
}
|
||||
}
|
||||
|
||||
if (!isLoggedIn) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<View className='min-h-screen bg-gray-50 flex flex-col'>
|
||||
{/* 自定义 Tabs 标题栏 */}
|
||||
|
||||
@@ -13,6 +13,7 @@ import { requireLogin } from '@/utils/login-guard'
|
||||
import { useShare } from '@/hooks/useShare'
|
||||
import EmptyState from '@/components/common/EmptyState'
|
||||
import LoadMore from '@/components/common/LoadMore'
|
||||
import Price from '@/components/common/Price'
|
||||
|
||||
definePageConfig({
|
||||
navigationBarTitleText: '商品分类',
|
||||
@@ -305,24 +306,21 @@ const ShopPage: React.FC = () => {
|
||||
{/* 价格 + 销量 + 加购 */}
|
||||
<View className='flex items-end justify-between'>
|
||||
<View className='flex-1 min-w-0'>
|
||||
<Text className='text-sm text-red-500 font-medium'>
|
||||
¥{getDisplayPrice(item)}
|
||||
</Text>
|
||||
{item.unitName ? (
|
||||
<Price
|
||||
price={getDisplayPrice(item)}
|
||||
original={
|
||||
isVipMember() && item.dealerPrice
|
||||
? item.price
|
||||
: item.salePrice && item.salePrice !== item.price
|
||||
? item.salePrice
|
||||
: undefined
|
||||
}
|
||||
size='small'
|
||||
loginMask
|
||||
/>
|
||||
{item.unitName && !isGuest() ? (
|
||||
<Text className='text-xs text-gray-400'>/{item.unitName}</Text>
|
||||
) : null}
|
||||
{/* VIP 划掉原价 */}
|
||||
{isVipMember() && item.dealerPrice ? (
|
||||
<Text className='text-xs text-gray-400 line-through ml-1'>
|
||||
¥{item.price}
|
||||
</Text>
|
||||
) : item.salePrice && item.salePrice !== item.price ? (
|
||||
isGuest() ? null : (
|
||||
<Text className='text-xs text-gray-400 line-through ml-1'>
|
||||
¥{item.salePrice}
|
||||
</Text>
|
||||
)
|
||||
) : null}
|
||||
{/* 销量 */}
|
||||
{item.sales !== undefined && item.sales > 0 && (
|
||||
<Text className='text-xs text-gray-400 block mt-0.5'>
|
||||
|
||||
Reference in New Issue
Block a user