Просмотр исходного кода

refactor(auth): 将小程序认证系统转为只支持企业用户

- 修改 auth.tsx 使用 enterpriseAuthClient 替代 authClient
- 更新存储键名从 mini_token/userInfo 到 enterprise_token/enterpriseUserInfo
- 企业用户注册和更新功能提示联系管理员(企业账户由管理员创建)
- 修复 enterprise-auth.tsx 中多余的 QueryClient 创建
- 所有测试通过,兼容原有页面

🤖 Generated with [Claude Code](https://claude.com/claude-code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname 1 неделя назад
Родитель
Сommit
1fafbe7f95
2 измененных файлов с 25 добавлено и 38 удалено
  1. 25 35
      mini/src/utils/auth.tsx
  2. 0 3
      mini/src/utils/enterprise-auth.tsx

+ 25 - 35
mini/src/utils/auth.tsx

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

+ 0 - 3
mini/src/utils/enterprise-auth.tsx

@@ -18,8 +18,6 @@ interface EnterpriseAuthContextType {
 
 const EnterpriseAuthContext = createContext<EnterpriseAuthContextType | undefined>(undefined)
 
-const queryClient = new QueryClient()
-
 export const EnterpriseAuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
   const queryClient = useQueryClient()
 
@@ -120,4 +118,3 @@ export const useEnterpriseAuth = () => {
   return context
 }
 
-export { queryClient }