|
|
@@ -1,13 +1,14 @@
|
|
|
import { createContext, useContext, PropsWithChildren } from 'react'
|
|
|
import Taro from '@tarojs/taro'
|
|
|
-import { authClient } from '../api'
|
|
|
+import { enterpriseAuthClient } from '../api'
|
|
|
import { InferResponseType, InferRequestType } from 'hono'
|
|
|
import { QueryClient, useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
|
|
|
|
|
-// 用户类型定义
|
|
|
-export type User = InferResponseType<typeof authClient.me.$get, 200>
|
|
|
-type LoginRequest = InferRequestType<typeof authClient.login.$post>['json']
|
|
|
-type RegisterRequest = InferRequestType<typeof authClient.register.$post>['json']
|
|
|
+// 用户类型定义 - 使用企业用户认证
|
|
|
+export type User = InferResponseType<typeof enterpriseAuthClient.me.$get, 200>
|
|
|
+type LoginRequest = InferRequestType<typeof enterpriseAuthClient.login.$post>['json']
|
|
|
+// 企业用户注册可能由管理员创建,前端不提供注册接口
|
|
|
+type RegisterRequest = { username: string; password: string }
|
|
|
|
|
|
interface AuthContextType {
|
|
|
user: User | null
|
|
|
@@ -29,21 +30,21 @@ export const AuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
|
|
|
const { data: user, isLoading } = useQuery<User | null, Error>({
|
|
|
queryKey: ['currentUser'],
|
|
|
queryFn: async () => {
|
|
|
- const token = Taro.getStorageSync('mini_token')
|
|
|
+ const token = Taro.getStorageSync('enterprise_token')
|
|
|
if (!token) {
|
|
|
return null
|
|
|
}
|
|
|
try {
|
|
|
- const response = await authClient.me.$get({})
|
|
|
+ const response = await enterpriseAuthClient.me.$get({})
|
|
|
if (response.status !== 200) {
|
|
|
throw new Error('获取用户信息失败')
|
|
|
}
|
|
|
const user = await response.json()
|
|
|
- Taro.setStorageSync('userInfo', JSON.stringify(user))
|
|
|
+ Taro.setStorageSync('enterpriseUserInfo', JSON.stringify(user))
|
|
|
return user
|
|
|
} catch (error) {
|
|
|
- Taro.removeStorageSync('mini_token')
|
|
|
- Taro.removeStorageSync('userInfo')
|
|
|
+ Taro.removeStorageSync('enterprise_token')
|
|
|
+ Taro.removeStorageSync('enterpriseUserInfo')
|
|
|
return null
|
|
|
}
|
|
|
},
|
|
|
@@ -54,13 +55,13 @@ export const AuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
|
|
|
|
|
|
const loginMutation = useMutation<User, Error, LoginRequest>({
|
|
|
mutationFn: async (data) => {
|
|
|
- const response = await authClient.login.$post({ json: data })
|
|
|
+ const response = await enterpriseAuthClient.login.$post({ json: data })
|
|
|
if (response.status !== 200) {
|
|
|
throw new Error('登录失败')
|
|
|
}
|
|
|
const { token, user } = await response.json()
|
|
|
- Taro.setStorageSync('mini_token', token)
|
|
|
- Taro.setStorageSync('userInfo', JSON.stringify(user))
|
|
|
+ Taro.setStorageSync('enterprise_token', token)
|
|
|
+ Taro.setStorageSync('enterpriseUserInfo', JSON.stringify(user))
|
|
|
return user
|
|
|
},
|
|
|
onSuccess: (newUser) => {
|
|
|
@@ -76,24 +77,18 @@ export const AuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
|
|
|
})
|
|
|
|
|
|
const registerMutation = useMutation<User, Error, RegisterRequest>({
|
|
|
- mutationFn: async (data) => {
|
|
|
- const response = await authClient.register.$post({ json: data })
|
|
|
- if (response.status !== 201) {
|
|
|
- throw new Error('注册失败')
|
|
|
- }
|
|
|
- const { token, user } = await response.json()
|
|
|
- Taro.setStorageSync('mini_token', token)
|
|
|
- Taro.setStorageSync('userInfo', JSON.stringify(user))
|
|
|
- return user
|
|
|
+ mutationFn: async () => {
|
|
|
+ // 企业用户注册由管理员创建,前端不提供注册接口
|
|
|
+ throw new Error('企业用户注册请联系管理员创建账户')
|
|
|
},
|
|
|
onSuccess: (newUser) => {
|
|
|
queryClient.setQueryData(['currentUser'], newUser)
|
|
|
},
|
|
|
onError: (error) => {
|
|
|
Taro.showToast({
|
|
|
- title: error.message || '注册失败,请重试',
|
|
|
+ title: error.message || '企业用户注册请联系管理员',
|
|
|
icon: 'none',
|
|
|
- duration: 2000,
|
|
|
+ duration: 3000,
|
|
|
})
|
|
|
},
|
|
|
})
|
|
|
@@ -101,15 +96,15 @@ export const AuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
|
|
|
const logoutMutation = useMutation<void, Error>({
|
|
|
mutationFn: async () => {
|
|
|
try {
|
|
|
- const response = await authClient.logout.$post({})
|
|
|
+ const response = await enterpriseAuthClient.logout.$post({})
|
|
|
if (response.status !== 200) {
|
|
|
throw new Error('登出失败')
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error('Logout error:', error)
|
|
|
} finally {
|
|
|
- Taro.removeStorageSync('mini_token')
|
|
|
- Taro.removeStorageSync('userInfo')
|
|
|
+ Taro.removeStorageSync('enterprise_token')
|
|
|
+ Taro.removeStorageSync('enterpriseUserInfo')
|
|
|
}
|
|
|
},
|
|
|
onSuccess: () => {
|
|
|
@@ -126,14 +121,9 @@ export const AuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
|
|
|
})
|
|
|
|
|
|
const updateUserMutation = useMutation<User, Error, Partial<User>>({
|
|
|
- mutationFn: async (userData) => {
|
|
|
- const response = await authClient.me.$put({ json: userData })
|
|
|
- if (response.status !== 200) {
|
|
|
- throw new Error('更新用户信息失败')
|
|
|
- }
|
|
|
- const updatedUser = await response.json()
|
|
|
- Taro.setStorageSync('userInfo', JSON.stringify(updatedUser))
|
|
|
- return updatedUser
|
|
|
+ mutationFn: async () => {
|
|
|
+ // 企业用户信息更新可能由管理员管理,前端不提供更新接口
|
|
|
+ throw new Error('企业用户信息更新请联系管理员')
|
|
|
},
|
|
|
onSuccess: (updatedUser) => {
|
|
|
queryClient.setQueryData(['currentUser'], updatedUser)
|