|
|
@ -1,7 +1,9 @@ |
|
|
|
import { useState, useEffect } from 'react'; |
|
|
|
import Taro from '@tarojs/taro'; |
|
|
|
import { User } from '@/api/system/user/model'; |
|
|
|
import { getUserInfo, updateUserInfo } from '@/api/layout'; |
|
|
|
import { getUserInfo, updateUserInfo, loginByOpenId } from '@/api/layout'; |
|
|
|
import { TenantId } from '@/config/app'; |
|
|
|
import {getStoredInviteParams, handleInviteRelation} from '@/utils/invite'; |
|
|
|
|
|
|
|
// 用户Hook
|
|
|
|
export const useUser = () => { |
|
|
@ -9,8 +11,62 @@ export const useUser = () => { |
|
|
|
const [isLoggedIn, setIsLoggedIn] = useState(false); |
|
|
|
const [loading, setLoading] = useState(true); |
|
|
|
|
|
|
|
// 自动登录(通过OpenID)
|
|
|
|
const autoLoginByOpenId = async () => { |
|
|
|
try { |
|
|
|
const res = await new Promise<any>((resolve, reject) => { |
|
|
|
Taro.login({ |
|
|
|
success: (loginRes) => { |
|
|
|
loginByOpenId({ |
|
|
|
code: loginRes.code, |
|
|
|
tenantId: TenantId |
|
|
|
}).then(async (data) => { |
|
|
|
if (data) { |
|
|
|
// 保存登录信息
|
|
|
|
saveUserToStorage(data.access_token, data.user); |
|
|
|
setUser(data.user); |
|
|
|
setIsLoggedIn(true); |
|
|
|
|
|
|
|
// 处理邀请关系
|
|
|
|
if (data.user?.userId) { |
|
|
|
try { |
|
|
|
const inviteSuccess = await handleInviteRelation(data.user.userId); |
|
|
|
if (inviteSuccess) { |
|
|
|
console.log('自动登录时邀请关系建立成功'); |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('自动登录时处理邀请关系失败:', error); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
resolve(data.user); |
|
|
|
} else { |
|
|
|
reject(new Error('自动登录失败')); |
|
|
|
} |
|
|
|
}).catch(_ => { |
|
|
|
// 首次注册,跳转到邀请注册页面
|
|
|
|
const pages = Taro.getCurrentPages(); |
|
|
|
const currentPage = pages[pages.length - 1]; |
|
|
|
const inviteParams = getStoredInviteParams() |
|
|
|
if (currentPage?.route !== 'dealer/apply/add' && inviteParams?.inviter) { |
|
|
|
return Taro.navigateTo({ |
|
|
|
url: '/dealer/apply/add' |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
fail: reject |
|
|
|
}); |
|
|
|
}); |
|
|
|
return res; |
|
|
|
} catch (error) { |
|
|
|
console.error('自动登录失败:', error); |
|
|
|
return null; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
// 从本地存储加载用户数据
|
|
|
|
const loadUserFromStorage = () => { |
|
|
|
const loadUserFromStorage = async () => { |
|
|
|
try { |
|
|
|
const token = Taro.getStorageSync('access_token'); |
|
|
|
const userData = Taro.getStorageSync('User'); |
|
|
@ -26,8 +82,13 @@ export const useUser = () => { |
|
|
|
setIsLoggedIn(true); |
|
|
|
setUser({ userId, tenantId } as User); |
|
|
|
} else { |
|
|
|
setUser(null); |
|
|
|
setIsLoggedIn(false); |
|
|
|
// 没有本地登录信息,尝试自动登录
|
|
|
|
console.log('没有本地登录信息,尝试自动登录...'); |
|
|
|
const autoLoginResult = await autoLoginByOpenId(); |
|
|
|
if (!autoLoginResult) { |
|
|
|
setUser(null); |
|
|
|
setIsLoggedIn(false); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('加载用户数据失败:', error); |
|
|
@ -43,9 +104,24 @@ export const useUser = () => { |
|
|
|
try { |
|
|
|
Taro.setStorageSync('access_token', token); |
|
|
|
Taro.setStorageSync('User', userInfo); |
|
|
|
Taro.setStorageSync('UserId', userInfo.userId); |
|
|
|
Taro.setStorageSync('TenantId', userInfo.tenantId); |
|
|
|
Taro.setStorageSync('Phone', userInfo.phone); |
|
|
|
|
|
|
|
// 确保关键字段不为空时才保存,避免覆盖现有数据
|
|
|
|
if (userInfo.userId) { |
|
|
|
Taro.setStorageSync('UserId', userInfo.userId); |
|
|
|
} |
|
|
|
if (userInfo.tenantId) { |
|
|
|
Taro.setStorageSync('TenantId', userInfo.tenantId); |
|
|
|
} |
|
|
|
if (userInfo.phone) { |
|
|
|
Taro.setStorageSync('Phone', userInfo.phone); |
|
|
|
} |
|
|
|
// 保存头像和昵称信息
|
|
|
|
if (userInfo.avatar) { |
|
|
|
Taro.setStorageSync('Avatar', userInfo.avatar); |
|
|
|
} |
|
|
|
if (userInfo.nickname) { |
|
|
|
Taro.setStorageSync('Nickname', userInfo.nickname); |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('保存用户数据失败:', error); |
|
|
|
} |
|
|
@ -114,9 +190,16 @@ export const useUser = () => { |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
const updatedUser = { ...user, ...userData }; |
|
|
|
// 先获取最新的用户信息,确保我们有完整的数据
|
|
|
|
const latestUserInfo = await getUserInfo(); |
|
|
|
|
|
|
|
// 合并最新的用户信息和要更新的数据
|
|
|
|
const updatedUser = { ...latestUserInfo, ...userData }; |
|
|
|
|
|
|
|
// 调用API更新用户信息
|
|
|
|
await updateUserInfo(updatedUser); |
|
|
|
|
|
|
|
// 更新本地状态
|
|
|
|
setUser(updatedUser); |
|
|
|
|
|
|
|
// 更新本地存储
|
|
|
@ -216,7 +299,10 @@ export const useUser = () => { |
|
|
|
|
|
|
|
// 初始化时加载用户数据
|
|
|
|
useEffect(() => { |
|
|
|
loadUserFromStorage(); |
|
|
|
loadUserFromStorage().catch(error => { |
|
|
|
console.error('初始化用户数据失败:', error); |
|
|
|
setLoading(false); |
|
|
|
}); |
|
|
|
}, []); |
|
|
|
|
|
|
|
return { |
|
|
@ -231,6 +317,7 @@ export const useUser = () => { |
|
|
|
fetchUserInfo, |
|
|
|
updateUser, |
|
|
|
loadUserFromStorage, |
|
|
|
autoLoginByOpenId, |
|
|
|
|
|
|
|
// 工具方法
|
|
|
|
hasPermission, |
|
|
|