فهرست منبع

📝 docs(prd): update story 3 description in multi-tenant integration document

- 修改Story 3描述,明确集成的租户模块包为@d8d/tenant-module-mt
- 更新功能点描述,包括租户管理路由、超级管理员认证和租户数据隔离

♻️ refactor(auth-management-ui-mt): optimize auth client implementation

- 重构authClient为单例模式的AuthClientManager,提供更好的实例管理
- 移除重复的类型定义,直接使用auth-module-mt提供的类型
- 调整依赖关系,使用@d8d/auth-module和@d8d/shared-types替代@d8d/auth-module-mt
- 更新AuthProvider中的API调用方式,使用authClientManager确保客户端实例正确初始化

🔧 chore(types): fix order type export in order-management-ui packages

- 将order类型从命名导出改为类型导出,避免类型污染和命名冲突
yourname 1 ماه پیش
والد
کامیت
21b7d1a7e6

+ 1 - 1
docs/prd/epic-008-server-web-multi-tenant-integration.md

@@ -100,7 +100,7 @@ packages/
 
 ### 阶段 2: 租户模块和UI包集成
 
-3. **Story 3:** 租户模块集成到server - 将多租户模块包集成到server中,包括租户管理、租户上下文传递和租户数据隔离功能,确保server能够正确处理多租户请求和数据隔离
+3. **Story 3:** 租户模块集成到server - 将租户模块包(@d8d/tenant-module-mt)集成到server中,包括租户管理路由、超级管理员认证和租户数据隔离功能,确保server能够支持租户管理操作
 
 4. **Story 4:** 租户UI包集成到Web - 将多租户UI包集成到Web应用中,包括租户管理界面、租户切换功能和租户感知的UI组件,确保Web应用能够支持多租户操作和界面展示
 

+ 2 - 1
packages/auth-management-ui-mt/package.json

@@ -40,7 +40,8 @@
     "typecheck": "tsc --noEmit"
   },
   "dependencies": {
-    "@d8d/auth-module-mt": "workspace:*",
+    "@d8d/auth-module": "workspace:*",
+    "@d8d/shared-types": "workspace:*",
     "@d8d/shared-ui-components": "workspace:*",
     "@hookform/resolvers": "^5.2.1",
     "@tanstack/react-query": "^5.90.9",

+ 36 - 58
packages/auth-management-ui-mt/src/api/authClient.ts

@@ -1,66 +1,44 @@
-import { hc } from 'hono/client';
-import type { AppType } from '@d8d/auth-module-mt';
+import { authRoutes } from '@d8d/auth-module-mt';
+import { rpcClient } from '@d8d/shared-ui-components/utils/hc';
 
-/**
- * 认证API客户端
- * 基于Hono Client的RPC调用,提供类型安全的API调用
- */
-export const authClient = hc<AppType>('/');
+class AuthClientManager {
+  private static instance: AuthClientManager;
+  private client: ReturnType<typeof rpcClient<typeof authRoutes>> | null = null;
 
-/**
- * 认证API端点配置
- */
-export const authEndpoints = {
-  login: '/auth/login',
-  logout: '/auth/logout',
-  me: '/auth/me',
-  register: '/auth/register'
-} as const;
+  private constructor() {}
 
-/**
- * 认证错误类型
- */
-export type AuthError = {
-  message: string;
-  code?: string;
-  status?: number;
-};
+  public static getInstance(): AuthClientManager {
+    if (!AuthClientManager.instance) {
+      AuthClientManager.instance = new AuthClientManager();
+    }
+    return AuthClientManager.instance;
+  }
 
-/**
- * 登录请求参数
- */
-export interface LoginRequest {
-  username: string;
-  password: string;
-}
+  // 初始化客户端
+  public init(baseUrl: string = '/'): ReturnType<typeof rpcClient<typeof authRoutes>> {
+    return this.client = rpcClient<typeof authRoutes>(baseUrl);
+  }
+
+  // 获取客户端实例
+  public get(): ReturnType<typeof rpcClient<typeof authRoutes>> {
+    if (!this.client) {
+      return this.init()
+    }
+    return this.client;
+  }
 
-/**
- * 登录响应
- */
-export interface LoginResponse {
-  token: string;
-  user: {
-    id: number;
-    username: string;
-    email: string;
-    role: {
-      id: number;
-      name: string;
-    };
-  };
+  // 重置客户端(用于测试或重新初始化)
+  public reset(): void {
+    this.client = null;
+  }
 }
 
-/**
- * 用户信息
- */
-export interface UserInfo {
-  id: number;
-  username: string;
-  email: string;
-  role: {
-    id: number;
-    name: string;
-  };
-  createdAt: string;
-  updatedAt: string;
+// 导出单例实例
+const authClientManager = AuthClientManager.getInstance();
+
+// 导出默认客户端实例(延迟初始化)
+export const authClient = authClientManager.get()
+
+export {
+  authClientManager
 }

+ 1 - 2
packages/auth-management-ui-mt/src/api/index.ts

@@ -1,4 +1,3 @@
 // API导出入口
 
-export { authClient, authEndpoints } from './authClient';
-export type { AuthError, LoginRequest, LoginResponse, UserInfo } from './authClient';
+export { authClient, authClientManager } from './authClient';

+ 4 - 4
packages/auth-management-ui-mt/src/hooks/AuthProvider.tsx

@@ -9,7 +9,7 @@ import 'dayjs/locale/zh-cn';
 import type {
   AuthContextType
 } from '@d8d/shared-types';
-import { authClient } from '../api/authClient';
+import { authClient , authClientManager} from '../api/authClient';
 import type { InferResponseType } from 'hono/client';
 
 type User = InferResponseType<typeof authClient.me.$get, 200>;
@@ -36,7 +36,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
     try {
       // 如果已登录,调用登出API
       if (token) {
-        await authClient.logout.$post();
+        await authClientManager.get().logout.$post();
       }
     } catch (error) {
       console.error('登出请求失败:', error);
@@ -68,7 +68,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
         // 设置全局默认请求头
         axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
         // 使用API验证当前用户
-        const res = await authClient.me.$get();
+        const res = await authClientManager.get().me.$get();
         if (res.status !== 200) {
           const result = await res.json();
           throw new Error(result.message)
@@ -104,7 +104,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
       }
 
       // 使用AuthAPI登录
-      const response = await authClient.login.$post(requestParams)
+      const response = await authClientManager.get().login.$post(requestParams)
       if (response.status !== 200) {
         const result = await response.json()
         throw new Error(result.message);

+ 1 - 2
packages/auth-management-ui-mt/src/index.ts

@@ -8,8 +8,7 @@ export { AuthManagement } from './components/AuthManagement';
 export { AuthProvider, useAuth } from './hooks/AuthProvider';
 
 // 导出API客户端
-export { authClient, authEndpoints } from './api/authClient';
-export type { AuthError, LoginRequest, LoginResponse, UserInfo } from './api/authClient';
+export { authClient, authClientManager } from './api/authClient';
 
 // 导出类型定义
 export type { AuthContextType, AuthStatus, AuthConfig, AuthEvent, UseAuthReturn } from './types/auth';

+ 1 - 1
packages/order-management-ui-mt/src/types/order.ts

@@ -12,7 +12,7 @@ import {
 } from '@d8d/orders-module-mt';
 
 // 重新导出订单模块的类型
-export {
+export type {
   OrderStatusType,
   PayStatusType,
   OrderTypeType,

+ 1 - 1
packages/order-management-ui/src/types/order.ts

@@ -12,7 +12,7 @@ import {
 } from '@d8d/orders-module';
 
 // 重新导出订单模块的类型
-export {
+export type {
   OrderStatusType,
   PayStatusType,
   OrderTypeType,

+ 5 - 2
pnpm-lock.yaml

@@ -1035,9 +1035,12 @@ importers:
 
   packages/auth-management-ui-mt:
     dependencies:
-      '@d8d/auth-module-mt':
+      '@d8d/auth-module':
         specifier: workspace:*
-        version: link:../auth-module-mt
+        version: link:../auth-module
+      '@d8d/shared-types':
+        specifier: workspace:*
+        version: link:../shared-types
       '@d8d/shared-ui-components':
         specifier: workspace:*
         version: link:../shared-ui-components