Jelajahi Sumber

chore: 移除无用的企业认证相关文件

- 删除 mini/src/utils/enterprise-auth.tsx(主 auth.tsx 已处理企业认证)
- 删除 mini/src/utils/enterprise-auth-guard.ts(未使用)
- 更新测试文件类型定义和导入
- 所有测试通过

🤖 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 Minggu lalu
induk
melakukan
75366f5058

+ 0 - 83
mini/src/utils/enterprise-auth-guard.ts

@@ -1,83 +0,0 @@
-import Taro from '@tarojs/taro'
-import { useEnterpriseAuth } from './enterprise-auth'
-
-/**
- * 企业用户认证守卫钩子
- * 检查用户是否已登录,未登录则跳转到登录页面
- */
-export const useEnterpriseAuthGuard = () => {
-  const { user, isLoading } = useEnterpriseAuth()
-
-  const checkAuth = (): boolean => {
-    if (isLoading) {
-      return false
-    }
-
-    if (!user) {
-      Taro.showToast({
-        title: '请先登录企业账号',
-        icon: 'none',
-        duration: 2000,
-      })
-      setTimeout(() => {
-        Taro.redirectTo({ url: '/pages/yongren/login/index' })
-      }, 1500)
-      return false
-    }
-
-    return true
-  }
-
-  const requireAuth = <T extends any[]>(fn: (...args: T) => void) => {
-    return (...args: T) => {
-      if (!checkAuth()) {
-        return
-      }
-      return fn(...args)
-    }
-  }
-
-  return {
-    checkAuth,
-    requireAuth,
-    user,
-    isLoading,
-    isAuthenticated: !!user,
-  }
-}
-
-/**
- * 企业用户角色权限检查
- * 可以根据企业用户角色进行权限验证
- */
-export const useEnterpriseRoleGuard = () => {
-  const { user } = useEnterpriseAuth()
-
-  const hasRole = (role: string): boolean => {
-    if (!user) {
-      return false
-    }
-    // 根据实际企业用户角色结构进行调整
-    return user.roles?.some(r => r.name === role) || false
-  }
-
-  const hasAnyRole = (roles: string[]): boolean => {
-    if (!user) {
-      return false
-    }
-    return user.roles?.some(r => roles.includes(r.name)) || false
-  }
-
-  const hasAllRoles = (roles: string[]): boolean => {
-    if (!user) {
-      return false
-    }
-    return roles.every(role => user.roles?.some(r => r.name === role)) || false
-  }
-
-  return {
-    hasRole,
-    hasAnyRole,
-    hasAllRoles,
-  }
-}

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

@@ -1,120 +0,0 @@
-import { createContext, useContext, PropsWithChildren } from 'react'
-import Taro from '@tarojs/taro'
-import { enterpriseAuthClient } from '../api'
-import { InferResponseType, InferRequestType } from 'hono'
-import { QueryClient, useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
-
-// 企业用户类型定义
-export type EnterpriseUser = InferResponseType<typeof enterpriseAuthClient.me.$get, 200>
-type EnterpriseLoginRequest = InferRequestType<typeof enterpriseAuthClient.login.$post>['json']
-
-interface EnterpriseAuthContextType {
-  user: EnterpriseUser | null
-  login: (data: EnterpriseLoginRequest) => Promise<EnterpriseUser>
-  logout: () => Promise<void>
-  isLoading: boolean
-  isLoggedIn: boolean
-}
-
-const EnterpriseAuthContext = createContext<EnterpriseAuthContextType | undefined>(undefined)
-
-export const EnterpriseAuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
-  const queryClient = useQueryClient()
-
-  const { data: user, isLoading } = useQuery<EnterpriseUser | null, Error>({
-    queryKey: ['currentEnterpriseUser'],
-    queryFn: async () => {
-      const token = Taro.getStorageSync('enterprise_token')
-      if (!token) {
-        return null
-      }
-      try {
-        const response = await enterpriseAuthClient.me.$get({})
-        if (response.status !== 200) {
-          throw new Error('获取企业用户信息失败')
-        }
-        const user = await response.json()
-        Taro.setStorageSync('enterpriseUserInfo', JSON.stringify(user))
-        return user
-      } catch (error) {
-        Taro.removeStorageSync('enterprise_token')
-        Taro.removeStorageSync('enterpriseUserInfo')
-        return null
-      }
-    },
-    staleTime: Infinity,
-    refetchOnWindowFocus: false,
-    refetchOnReconnect: false,
-  })
-
-  const loginMutation = useMutation<EnterpriseUser, Error, EnterpriseLoginRequest>({
-    mutationFn: async (data) => {
-      const response = await enterpriseAuthClient.login.$post({ json: data })
-      if (response.status !== 200) {
-        throw new Error('企业登录失败')
-      }
-      const { token, user } = await response.json()
-      Taro.setStorageSync('enterprise_token', token)
-      Taro.setStorageSync('enterpriseUserInfo', JSON.stringify(user))
-      return user
-    },
-    onSuccess: (newUser) => {
-      queryClient.setQueryData(['currentEnterpriseUser'], newUser)
-    },
-    onError: (error) => {
-      Taro.showToast({
-        title: error.message || '企业登录失败,请检查手机号和密码',
-        icon: 'none',
-        duration: 2000,
-      })
-    },
-  })
-
-  const logoutMutation = useMutation<void, Error>({
-    mutationFn: async () => {
-      try {
-        const response = await enterpriseAuthClient.logout.$post({})
-        if (response.status !== 200) {
-          throw new Error('企业登出失败')
-        }
-      } catch (error) {
-        console.error('Enterprise logout error:', error)
-      } finally {
-        Taro.removeStorageSync('enterprise_token')
-        Taro.removeStorageSync('enterpriseUserInfo')
-      }
-    },
-    onSuccess: () => {
-      queryClient.setQueryData(['currentEnterpriseUser'], null)
-      Taro.redirectTo({ url: '/pages/yongren/login/index' })
-    },
-    onError: (error) => {
-      Taro.showToast({
-        title: error.message || '企业登出失败',
-        icon: 'none',
-        duration: 2000,
-      })
-    },
-  })
-
-
-
-  const value = {
-    user: user || null,
-    login: loginMutation.mutateAsync,
-    logout: logoutMutation.mutateAsync,
-    isLoading: isLoading || loginMutation.isPending || logoutMutation.isPending,
-    isLoggedIn: !!user,
-  }
-
-  return <EnterpriseAuthContext.Provider value={value}>{children}</EnterpriseAuthContext.Provider>
-}
-
-export const useEnterpriseAuth = () => {
-  const context = useContext(EnterpriseAuthContext)
-  if (context === undefined) {
-    throw new Error('useEnterpriseAuth must be used within an EnterpriseAuthProvider')
-  }
-  return context
-}
-

+ 6 - 1
mini/tests/setup.ts

@@ -1,5 +1,11 @@
 import '@testing-library/jest-dom'
 
+// 扩展全局类型以支持 Taro 配置测试
+declare global {
+  // eslint-disable-next-line no-var
+  var defineAppConfig: (config: any) => any
+}
+
 /* eslint-disable react/display-name */
 
 // 设置环境变量
@@ -8,7 +14,6 @@ process.env.TARO_PLATFORM = 'web'
 process.env.SUPPORT_TARO_POLYFILL = 'disabled'
 
 // 定义 defineAppConfig 全局函数用于测试 Taro 配置文件
-// @ts-ignore
 global.defineAppConfig = (config: any) => config
 
 // Mock Taro 组件

+ 1 - 0
mini/tests/yongren-components.test.tsx

@@ -1,3 +1,4 @@
+// @ts-ignore
 import React from 'react'
 import { render, screen } from '@testing-library/react'
 import { YongrenTabBarLayout } from '../src/layouts/yongren-tab-bar-layout'