refactor(user): 禁用 loginByOpenId 静默登录并优化登录状态控制

- 移除 UserContext 和 useUser 中的 loginByOpenId 调用及相关降级逻辑
- 无本地 token 时保持未登录状态,不再自动触发静默登录
- 清理无用导入 TenantId 和 loginByOpenId
- 订单列表页增加登录校验,未登录用户跳转登录页并阻止渲染订单
- 商城首页价格改为 Price 组件,未登录游客显示登录遮罩,单位仅登录后可见
This commit is contained in:
2026-07-14 01:36:58 +08:00
parent 93a46bdae7
commit fd03ed80fd
5 changed files with 55 additions and 66 deletions

View File

@@ -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()