save
This commit is contained in:
parent
dc24d5b6ed
commit
cd11b355bb
25 changed files with 3632 additions and 186 deletions
75
client/src/utils/auth.ts
Normal file
75
client/src/utils/auth.ts
Normal file
|
@ -0,0 +1,75 @@
|
|||
import Taro from '@tarojs/taro'
|
||||
|
||||
// 授权状态接口
|
||||
export interface AuthStatus {
|
||||
hasPhone: boolean
|
||||
hasUserInfo: boolean
|
||||
phone?: string
|
||||
userInfo?: any
|
||||
}
|
||||
|
||||
// 检查用户授权状态
|
||||
export const checkAuthStatus = (): AuthStatus => {
|
||||
const phone = Taro.getStorageSync('userPhone')
|
||||
const userInfo = Taro.getStorageSync('userInfo')
|
||||
|
||||
return {
|
||||
hasPhone: !!phone,
|
||||
hasUserInfo: !!userInfo,
|
||||
phone,
|
||||
userInfo
|
||||
}
|
||||
}
|
||||
|
||||
// 保存用户授权信息
|
||||
export const saveAuthInfo = (data: { phone?: string, userInfo?: any }) => {
|
||||
if (data.phone) {
|
||||
Taro.setStorageSync('userPhone', data.phone)
|
||||
}
|
||||
if (data.userInfo) {
|
||||
Taro.setStorageSync('userInfo', data.userInfo)
|
||||
}
|
||||
}
|
||||
|
||||
// 清除用户授权信息
|
||||
export const clearAuthInfo = () => {
|
||||
Taro.removeStorageSync('userPhone')
|
||||
Taro.removeStorageSync('userInfo')
|
||||
}
|
||||
|
||||
// 检查是否需要授权
|
||||
export const isAuthRequired = (): boolean => {
|
||||
const status = checkAuthStatus()
|
||||
return !status.hasPhone || !status.hasUserInfo
|
||||
}
|
||||
|
||||
// 获取用户显示名称
|
||||
export const getUserDisplayName = (): string => {
|
||||
const status = checkAuthStatus()
|
||||
if (status.userInfo && status.userInfo.nickName) {
|
||||
return status.userInfo.nickName
|
||||
}
|
||||
return '用户昵称'
|
||||
}
|
||||
|
||||
// 获取用户头像
|
||||
export const getUserAvatar = (): string => {
|
||||
const status = checkAuthStatus()
|
||||
if (status.userInfo && status.userInfo.avatarUrl) {
|
||||
return status.userInfo.avatarUrl
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
// 格式化手机号显示
|
||||
export const formatPhoneDisplay = (): string => {
|
||||
const status = checkAuthStatus()
|
||||
if (status.phone) {
|
||||
// 如果是完整手机号,进行脱敏处理
|
||||
if (status.phone.length === 11) {
|
||||
return status.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
|
||||
}
|
||||
return status.phone
|
||||
}
|
||||
return '未授权'
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue