From 913702ea10fb39468300c2bb6491e4f0d37ece0d Mon Sep 17 00:00:00 2001 From: yorn <1057707203@qq.com> Date: Thu, 24 Jul 2025 09:42:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0nginx=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E9=A6=96=E9=A1=B5=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E6=A6=82=E8=A7=88=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7id?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MULTI-CLIENT-DEPLOYMENT.md | 279 +++++++++++++++++++++++++++++++++++++ app/api/home/home.ts | 225 +++++++++++++++--------------- app/config/api-config.ts | 116 ++++++++++++++- app/routes/home.tsx | 29 ++-- deploy-multi-client.sh | 255 +++++++++++++++++++++++++++++++++ ecosystem.config.cjs | 104 +++++++++++++- nginx-local-dev.conf | 169 ++++++++++++++++++++++ nginx-multi-client.conf | 162 +++++++++++++++++++++ 8 files changed, 1204 insertions(+), 135 deletions(-) create mode 100644 MULTI-CLIENT-DEPLOYMENT.md create mode 100644 deploy-multi-client.sh create mode 100644 nginx-local-dev.conf create mode 100644 nginx-multi-client.conf diff --git a/MULTI-CLIENT-DEPLOYMENT.md b/MULTI-CLIENT-DEPLOYMENT.md new file mode 100644 index 0000000..a60c26a --- /dev/null +++ b/MULTI-CLIENT-DEPLOYMENT.md @@ -0,0 +1,279 @@ +# 多客户端部署方案说明 + +## 概述 + +本方案实现了基于PM2和Nginx的多客户端部署架构,允许不同地区的客户通过不同端口访问同一个应用服务。 + +## 架构设计 + +``` +客户端A (51701) ──┐ +客户端B (51702) ──┼── Nginx反向代理 ──→ 主服务 (51703) +客户端C (51704) ──┘ +``` + +### 端口分配 + +- **主服务**: `10.79.97.17:51703` - 核心应用服务 +- **客户端A**: `10.79.97.17:51701` - 地区A客户访问 +- **客户端B**: `10.79.97.17:51702` - 地区B客户访问 +- **客户端C**: `10.79.97.17:51704` - 地区C客户访问 + +## 文件说明 + +### 1. ecosystem.config.cjs +PM2部署配置文件,定义了4个应用实例: +- `docreview-main`: 主服务 (端口51703) +- `docreview-client-a`: 客户端A代理 (端口51701) +- `docreview-client-b`: 客户端B代理 (端口51702) +- `docreview-client-c`: 客户端C代理 (端口51704) + +### 2. api-config.ts +应用配置文件,支持根据`CLIENT_ID`环境变量加载不同客户端配置: +- 默认配置 (main) +- 客户端A配置 (client-a) +- 客户端B配置 (client-b) +- 客户端C配置 (client-c) + +### 3. nginx-multi-client.conf +Nginx反向代理配置文件,为每个客户端端口配置独立的代理规则。 + +### 4. 部署脚本 +- `deploy-multi-client.sh`: Linux/macOS部署脚本 +- `deploy-multi-client.bat`: Windows部署脚本 + +## 部署步骤 + +### Windows环境部署 + +1. **检查环境依赖** + ```bash + node --version + npm --version + pm2 --version + ``` + +2. **使用部署脚本** + ```bash + # 完整部署 + deploy-multi-client.bat deploy + + # 仅构建项目 + deploy-multi-client.bat build + + # 仅部署PM2 + deploy-multi-client.bat pm2 + + # 检查状态 + deploy-multi-client.bat status + ``` + +3. **手动配置Nginx** (Windows) + - 安装Nginx for Windows + - 将`nginx-multi-client.conf`内容添加到nginx配置中 + - 重启Nginx服务 + +### Linux/macOS环境部署 + +1. **使用部署脚本** + ```bash + chmod +x deploy-multi-client.sh + + # 完整部署(包含Nginx配置) + ./deploy-multi-client.sh deploy + + # 检查状态 + ./deploy-multi-client.sh status + ``` + +### 手动部署步骤 + +1. **构建项目** + ```bash + npm install + npm run build + ``` + +2. **启动PM2应用** + ```bash + pm2 start ecosystem.config.cjs + pm2 save + pm2 startup + ``` + +3. **配置Nginx** + ```bash + # 复制配置文件 + sudo cp nginx-multi-client.conf /etc/nginx/sites-available/docreview-multi-client + sudo ln -s /etc/nginx/sites-available/docreview-multi-client /etc/nginx/sites-enabled/ + + # 测试配置 + sudo nginx -t + + # 重载配置 + sudo systemctl reload nginx + ``` + +## 配置说明 + +### 客户端特定配置 + +每个客户端可以有独立的配置,在`api-config.ts`中定义: + +```typescript +const clientConfigs = { + 'client-a': { + baseUrl: 'http://10.79.97.17:51701/api', + uploadUrl: 'http://10.79.97.17:51701/api/upload', + oauth: { + serverUrl: 'http://10.79.97.17:51701/oauth', + clientId: 'client-a-id', + // ... 其他配置 + } + }, + // ... 其他客户端配置 +}; +``` + +### 环境变量 + +每个PM2应用实例都有独立的环境变量: + +- `CLIENT_ID`: 客户端标识 (main, client-a, client-b, client-c) +- `PROXY_TARGET`: 代理目标地址 (仅客户端实例) +- `PORT`: 监听端口 + +## 监控和管理 + +### PM2管理命令 + +```bash +# 查看所有应用状态 +pm2 status + +# 查看特定应用日志 +pm2 logs docreview-main +pm2 logs docreview-client-a + +# 重启应用 +pm2 restart docreview-main +pm2 restart all + +# 停止应用 +pm2 stop docreview-main +pm2 stop all + +# 删除应用 +pm2 delete docreview-main +pm2 delete all +``` + +### 日志文件位置 + +**PM2日志**: +- 主服务: `logs/main-out.log`, `logs/main-error.log` +- 客户端A: `logs/client-a-out.log`, `logs/client-a-error.log` +- 客户端B: `logs/client-b-out.log`, `logs/client-b-error.log` +- 客户端C: `logs/client-c-out.log`, `logs/client-c-error.log` + +**Nginx日志**: +- 客户端A: `/var/log/nginx/client-a-access.log`, `/var/log/nginx/client-a-error.log` +- 客户端B: `/var/log/nginx/client-b-access.log`, `/var/log/nginx/client-b-error.log` +- 客户端C: `/var/log/nginx/client-c-access.log`, `/var/log/nginx/client-c-error.log` + +### 健康检查 + +每个客户端端口都提供健康检查接口: + +```bash +# 检查各端口状态 +curl http://10.79.97.17:51701/health # 客户端A +curl http://10.79.97.17:51702/health # 客户端B +curl http://10.79.97.17:51703/health # 主服务 +curl http://10.79.97.17:51704/health # 客户端C +``` + +## 故障排除 + +### 常见问题 + +1. **端口被占用** + ```bash + # 查看端口占用 + netstat -tlnp | grep :51703 + + # 杀死占用进程 + kill -9 + ``` + +2. **PM2应用启动失败** + ```bash + # 查看详细错误日志 + pm2 logs docreview-main --lines 50 + + # 重新加载配置 + pm2 reload ecosystem.config.cjs + ``` + +3. **Nginx代理失败** + ```bash + # 检查nginx配置 + sudo nginx -t + + # 查看nginx错误日志 + sudo tail -f /var/log/nginx/error.log + ``` + +4. **客户端配置不生效** + - 检查`CLIENT_ID`环境变量是否正确设置 + - 确认`api-config.ts`中的客户端配置是否正确 + - 重启相关PM2应用 + +### 调试模式 + +启用调试模式查看详细日志: + +```bash +# 设置调试环境变量 +export DEBUG=* + +# 重启应用 +pm2 restart all +``` + +## 扩展和优化 + +### 添加新客户端 + +1. 在`ecosystem.config.cjs`中添加新的应用配置 +2. 在`api-config.ts`中添加客户端特定配置 +3. 在`nginx-multi-client.conf`中添加新的server块 +4. 重新部署应用 + +### 性能优化 + +1. **启用Nginx缓存** +2. **配置负载均衡** +3. **启用Gzip压缩** +4. **配置SSL/TLS** + +### 安全加固 + +1. **配置防火墙规则** +2. **启用访问控制** +3. **配置SSL证书** +4. **设置访问频率限制** + +## 联系支持 + +如果在部署过程中遇到问题,请检查: +1. 系统依赖是否完整安装 +2. 端口是否被其他服务占用 +3. 配置文件语法是否正确 +4. 日志文件中的错误信息 + +部署完成后,可以通过以下地址访问不同客户端: +- 客户端A: http://10.79.97.17:51701 +- 客户端B: http://10.79.97.17:51702 +- 客户端C: http://10.79.97.17:51704 +- 主服务: http://10.79.97.17:51703 \ No newline at end of file diff --git a/app/api/home/home.ts b/app/api/home/home.ts index f9584d5..3fd3ef4 100644 --- a/app/api/home/home.ts +++ b/app/api/home/home.ts @@ -1,4 +1,4 @@ -import { postgrestGet, type PostgrestParams } from "../postgrest-client"; +import { postgrestGet, postgrestPost, type PostgrestParams } from "../postgrest-client"; import dayjs from 'dayjs'; /** @@ -96,7 +96,7 @@ function buildTypeFilter(reviewType: string | null): string { * @param reviewType 从客户端传入的 reviewType 值 * @returns 主页数据 */ -export async function getHomeData(reviewType?: string | null): Promise { +export async function getHomeData(reviewType?: string | null,userId?: string | number): Promise { try { // 获取当前日期和时间相关值 const startOfToday = dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'); @@ -105,7 +105,8 @@ export async function getHomeData(reviewType?: string | null): Promise( - postgrestGet(`rpc/count_evaluation_results_by_type?type_val=${typeToQuery}&start_time=${startOfThisMonth}&end_time=${endOfThisMonth}`, { - select: '*', - filter: {} + postgrestPost('rpc/count_evaluation_results_by_type', { + start_time: startOfThisMonth, + end_time: endOfThisMonth, + type_val: typeToQuery, + userid: parseInt(userId as string) }), - '获取本月问题数据失败', + '获取合同本月问题数据失败', [] ); @@ -369,9 +378,11 @@ export async function getHomeData(reviewType?: string | null): Promise( - postgrestGet(`rpc/count_evaluation_results_by_type?type_val=${typeToQuery}&start_time=${startOfLastMonth}&end_time=${endOfLastMonth}`, { - select: '*', - filter: {} + postgrestPost('rpc/count_evaluation_results_by_type', { + start_time: startOfLastMonth, + end_time: endOfLastMonth, + type_val: typeToQuery, + userid: parseInt(userId as string) }), '获取上月问题数据失败', [] @@ -382,120 +393,108 @@ export async function getHomeData(reviewType?: string | null): Promise( - postgrestGet(`rpc/count_evaluation_results_by_type?type_val=2&start_time=${startOfThisMonth}&end_time=${endOfThisMonth}`, { - select: '*', - filter: {} + postgrestPost('rpc/count_evaluation_results_by_type', { + start_time: startOfThisMonth, + end_time: endOfThisMonth, + type_val: typeToQuery, + userid: parseInt(userId as string) }), - '获取本月类型2问题数据失败', + '获取本月许可卷宗类型2问题数据失败', [] ); - // 查询类型 3 的本月问题数量 - const thisMonthType3Response = await handleApiResponse<{ count: number }[]>( - postgrestGet(`rpc/count_evaluation_results_by_type?type_val=3&start_time=${startOfThisMonth}&end_time=${endOfThisMonth}`, { - select: '*', - filter: {} - }), - '获取本月类型3问题数据失败', - [] - ); - - // 合并本月两种类型的问题数量 + // 本月两种类型的问题数量 const thisMonthType2Count = thisMonthType2Response[0]?.count || 0; - const thisMonthType3Count = thisMonthType3Response[0]?.count || 0; - thisMonthIssuesCount = thisMonthType2Count + thisMonthType3Count; + thisMonthIssuesCount = thisMonthType2Count - // 查询类型 2 的上月问题数量 + // 上月两种类型的问题数量 const lastMonthType2Response = await handleApiResponse<{ count: number }[]>( - postgrestGet(`rpc/count_evaluation_results_by_type?type_val=2&start_time=${startOfLastMonth}&end_time=${endOfLastMonth}`, { - select: '*', - filter: {} + postgrestPost('rpc/count_evaluation_results_by_type', { + start_time: startOfLastMonth, + end_time: endOfLastMonth, + type_val: typeToQuery, + userid: parseInt(userId as string) }), - '获取上月类型2问题数据失败', + '获取上月许可卷宗类型2问题数据失败', [] ); - // 查询类型 3 的上月问题数量 - const lastMonthType3Response = await handleApiResponse<{ count: number }[]>( - postgrestGet(`rpc/count_evaluation_results_by_type?type_val=3&start_time=${startOfLastMonth}&end_time=${endOfLastMonth}`, { - select: '*', - filter: {} - }), - '获取上月类型3问题数据失败', - [] - ); + - // 合并上月两种类型的问题数量 + // 上月两种类型的问题数量 const lastMonthType2Count = lastMonthType2Response[0]?.count || 0; - const lastMonthType3Count = lastMonthType3Response[0]?.count || 0; - lastMonthIssuesCount = lastMonthType2Count + lastMonthType3Count; + lastMonthIssuesCount = lastMonthType2Count - } else { - // 如果没有指定类型,则使用原来的查询方式获取所有类型的问题数量 - const thisMonthIssuesParams: PostgrestParams = { - select: 'count', - filter: { - and: `(created_at.gte.${startOfThisMonth},created_at.lte.${endOfThisMonth})`, - 'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段 - } - }; - - // 添加类型过滤条件 - if (typeFilter) { - if (typeFilter.startsWith('(')) { - thisMonthIssuesParams.or = typeFilter; - } else { - const [field, op, value] = typeFilter.split('.'); - if (!thisMonthIssuesParams.filter) { - thisMonthIssuesParams.filter = {}; - } - thisMonthIssuesParams.filter[field] = `${op}.${value}`; - } - } - - const thisMonthIssuesResponse = await handleApiResponse<{ count: number }[]>( - postgrestGet('evaluation_results', thisMonthIssuesParams), - '获取本月问题数据失败', - [] - ); - - // 本月问题数量 - thisMonthIssuesCount = thisMonthIssuesResponse[0]?.count || 0; - - // 上月问题数量 - const lastMonthIssuesParams: PostgrestParams = { - select: 'count', - filter: { - and: `(created_at.gte.${startOfLastMonth},created_at.lte.${endOfLastMonth})`, - 'evaluated_results->result': 'eq.false' // 使用->操作符访问JSONB字段 - } - }; - - // 添加类型过滤条件 - if (typeFilter) { - if (typeFilter.startsWith('(')) { - lastMonthIssuesParams.or = typeFilter; - } else { - const [field, op, value] = typeFilter.split('.'); - if (!lastMonthIssuesParams.filter) { - lastMonthIssuesParams.filter = {}; - } - lastMonthIssuesParams.filter[field] = `${op}.${value}`; - } - } - - const lastMonthIssuesResponse = await handleApiResponse<{ count: number }[]>( - postgrestGet('evaluation_results', lastMonthIssuesParams), - '获取上月问题数据失败', - [] - ); - - // 上月问题数量 - lastMonthIssuesCount = lastMonthIssuesResponse[0]?.count || 0; } + // 暂时不会存在没有指定类型得情况,暂不实现。 + // else { + // // 如果没有指定类型,则使用原来的查询方式获取所有类型的问题数量 + // const thisMonthIssuesParams: PostgrestParams = { + // select: 'count', + // filter: { + // and: `(created_at.gte.${startOfThisMonth},created_at.lte.${endOfThisMonth})`, + // 'evaluated_results->result': 'eq.false', + // user_id: `eq.${userId}` + // } + // }; + + // // 添加类型过滤条件 + // if (typeFilter) { + // if (typeFilter.startsWith('(')) { + // thisMonthIssuesParams.or = typeFilter; + // } else { + // const [field, op, value] = typeFilter.split('.'); + // if (!thisMonthIssuesParams.filter) { + // thisMonthIssuesParams.filter = {}; + // } + // thisMonthIssuesParams.filter[field] = `${op}.${value}`; + // } + // } + + // const thisMonthIssuesResponse = await handleApiResponse<{ count: number }[]>( + // postgrestGet('evaluation_results', thisMonthIssuesParams), + // '获取本月问题数据失败', + // [] + // ); + + // // 本月问题数量 + // thisMonthIssuesCount = thisMonthIssuesResponse[0]?.count || 0; + + // // 上月问题数量 + // const lastMonthIssuesParams: PostgrestParams = { + // select: 'count', + // filter: { + // and: `(created_at.gte.${startOfLastMonth},created_at.lte.${endOfLastMonth})`, + // 'evaluated_results->result': 'eq.false', + // user_id: `eq.${userId}` + // } + // }; + + // // 添加类型过滤条件 + // if (typeFilter) { + // if (typeFilter.startsWith('(')) { + // lastMonthIssuesParams.or = typeFilter; + // } else { + // const [field, op, value] = typeFilter.split('.'); + // if (!lastMonthIssuesParams.filter) { + // lastMonthIssuesParams.filter = {}; + // } + // lastMonthIssuesParams.filter[field] = `${op}.${value}`; + // } + // } + + // const lastMonthIssuesResponse = await handleApiResponse<{ count: number }[]>( + // postgrestGet('evaluation_results', lastMonthIssuesParams), + // '获取上月问题数据失败', + // [] + // ); + + // // 上月问题数量 + // lastMonthIssuesCount = lastMonthIssuesResponse[0]?.count || 0; + // } // 计算问题数量同比增长 let issuesGrowthValue = 0; diff --git a/app/config/api-config.ts b/app/config/api-config.ts index d8db31c..f90b1af 100644 --- a/app/config/api-config.ts +++ b/app/config/api-config.ts @@ -44,7 +44,7 @@ const configs: Record = { serverUrl: 'http://10.79.112.85', // IDaaS服务器地址 clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', // 需要替换为实际的Client Secret - redirectUri: 'http://10.79.97.17/', // 回调地址 + redirectUri: 'http://10.79.97.17/callback', // 回调地址 appId: 'idaasoauth2' // 应用ID,用于登出 } }, @@ -75,7 +75,8 @@ const configs: Record = { serverUrl: 'http://10.79.112.85', // IDaaS服务器地址 clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', // 需要替换为实际的Client Secret - redirectUri: 'http://10.79.97.17/', // 回调地址 + redirectUri: 'http://10.79.97.17/callback', // 回调地址 + appId: 'idaasoauth2' // 应用ID,用于登出 } }, @@ -95,12 +96,97 @@ const configs: Record = { } }; +// 客户端特定配置 - 支持多客户端部署 +// 根据环境自动选择配置 +const getClientConfigs = (env: string): Record> => { + if (env === 'development') { + // 开发环境 - 本地nginx代理配置 + return { + 'client-a': { + baseUrl: 'http://localhost:8001', + uploadUrl: 'http://localhost:8001/admin/documents', + oauth: { + serverUrl: 'http://10.79.112.85', + clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', + clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', + redirectUri: 'http://localhost:8001/callback', + appId: 'idaasoauth2' + } + }, + 'client-b': { + baseUrl: 'http://localhost:8002', + uploadUrl: 'http://localhost:8002/admin/documents', + oauth: { + serverUrl: 'http://10.79.112.85', + clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', + clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', + redirectUri: 'http://localhost:8002/callback', + appId: 'idaasoauth2' + } + }, + 'client-c': { + baseUrl: 'http://localhost:8003', + uploadUrl: 'http://localhost:8003/admin/documents', + oauth: { + serverUrl: 'http://10.79.112.85', + clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', + clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', + redirectUri: 'http://localhost:8003/callback', + appId: 'idaasoauth2' + } + } + }; + } else { + // 生产环境 - 服务器配置 + return { + 'client-a': { + baseUrl: 'http://10.79.97.17:51701', + uploadUrl: 'http://10.79.97.17:51701/admin/documents', + oauth: { + serverUrl: 'http://10.79.112.85', + clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', + clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', + redirectUri: 'http://10.79.97.17:51701/callback', + appId: 'idaasoauth2' + } + }, + 'client-b': { + baseUrl: 'http://10.79.97.17:51702', + uploadUrl: 'http://10.79.97.17:51702/admin/documents', + oauth: { + serverUrl: 'http://10.79.112.85', + clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', + clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', + redirectUri: 'http://10.79.97.17:51702/callback', + appId: 'idaasoauth2' + } + }, + 'client-c': { + baseUrl: 'http://10.79.97.17:51704', + uploadUrl: 'http://10.79.97.17:51704/admin/documents', + oauth: { + serverUrl: 'http://10.79.112.85', + clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', + clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', + redirectUri: 'http://10.79.97.17:51704/callback', + appId: 'idaasoauth2' + } + } + }; + } +}; + // 获取当前环境,默认为development const getCurrentEnvironment = (): string => { // 优先使用环境变量,然后使用 NODE_ENV return process.env.NEXT_PUBLIC_API_ENV || process.env.NODE_ENV || 'development'; }; +// 获取客户端ID +const getClientId = (): string => { + return process.env.CLIENT_ID || process.env.NEXT_PUBLIC_CLIENT_ID || 'main'; +}; + // 从环境变量获取配置,如果环境变量不存在则使用默认配置 const getConfigFromEnv = (defaultConfig: ApiConfig): ApiConfig => { return { @@ -117,17 +203,35 @@ const getConfigFromEnv = (defaultConfig: ApiConfig): ApiConfig => { }; }; -// 获取当前配置 +// 获取当前配置 - 支持客户端特定配置 const getCurrentConfig = (): ApiConfig => { const env = getCurrentEnvironment(); + const clientId = getClientId(); const defaultConfig = configs[env] || configs.development; + // 获取当前环境的客户端特定配置 + const clientConfigs = getClientConfigs(env); + const clientConfig = clientConfigs[clientId]; + + // 合并默认配置和客户端特定配置 + let finalConfig = defaultConfig; + if (clientConfig) { + finalConfig = { + ...defaultConfig, + ...clientConfig, + oauth: { + ...defaultConfig.oauth, + ...clientConfig.oauth + } + }; + } + // 如果是浏览器环境,尝试从环境变量覆盖配置 if (typeof window !== 'undefined' || process.env.NEXT_PUBLIC_API_BASE_URL) { - return getConfigFromEnv(defaultConfig); + return getConfigFromEnv(finalConfig); } - return defaultConfig; + return finalConfig; }; // 导出当前环境的配置 @@ -155,4 +259,4 @@ export const setEnvironment = (env: string): ApiConfig => { // environment: getCurrentEnvironment(), // config: apiConfig // }); -// } \ No newline at end of file +// } \ No newline at end of file diff --git a/app/routes/home.tsx b/app/routes/home.tsx index fea69b2..bd0ed16 100644 --- a/app/routes/home.tsx +++ b/app/routes/home.tsx @@ -11,7 +11,7 @@ import { getDocuments, type DocumentUI, type DocumentSearchParams } from "~/api/ import { useState, useEffect } from "react"; import { getHomeData } from "~/api/home/home"; import dayjs from 'dayjs'; -import type { UserRole } from '~/api/login/auth.server'; +// import type { UserRole } from '~/api/login/auth.server'; import { type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/node"; import { logout, getUserSession } from "~/api/login/auth.server"; @@ -48,7 +48,8 @@ export const meta: MetaFunction = () => { export async function loader({ request }: LoaderFunctionArgs) { try { // 从根loader获取用户角色 - const { userRole } = await getUserSession(request); + const { userRole, userInfo, frontendJWT } = await getUserSession(request); + // 返回默认值,实际数据将在客户端根据 sessionStorage 加载 return Response.json({ @@ -63,7 +64,9 @@ export async function loader({ request }: LoaderFunctionArgs) { }, recentFiles: [], reviewType: null, - userRole: userRole + userRole: userRole, + userInfo, + frontendJWT }); } catch (error) { // 错误处理 @@ -89,7 +92,7 @@ export async function action({ request }: ActionFunctionArgs) { export default function Home() { const navigate = useNavigate(); - const { homeData: initialHomeData, recentFiles: initialRecentFiles, userRole: serverUserRole } = useLoaderData(); + const { homeData: initialHomeData, recentFiles: initialRecentFiles, userRole: serverUserRole, userInfo } = useLoaderData(); const [recentFiles, setRecentFiles] = useState(initialRecentFiles || []); const [homeData, setHomeData] = useState(initialHomeData); const [currentDateTime, setCurrentDateTime] = useState({ @@ -97,7 +100,7 @@ export default function Home() { time: '' }); const [isLoading, setIsLoading] = useState(true); - const userRole = serverUserRole as UserRole; + // const userRole = serverUserRole as UserRole; // 打印服务器端传递的用户角色 useEffect(() => { @@ -155,7 +158,7 @@ export default function Home() { const reviewType = sessionStorage.getItem('reviewType'); // 加载主页数据 - const newHomeData = await getHomeData(reviewType || undefined); + const newHomeData = await getHomeData(reviewType || undefined,userInfo.user_id); setHomeData(newHomeData); // 加载文档数据 @@ -177,7 +180,8 @@ export default function Home() { try { const documentSearchParams: DocumentSearchParams = { page: 1, - pageSize: 10 + pageSize: 10, + userId: userInfo.user_id }; // 根据 reviewType 添加过滤条件 @@ -186,6 +190,7 @@ export default function Home() { const response = await getDocuments(documentSearchParams); if (!response.error && response.data) { + // console.log('合同文档数据',response.data.documents); return response.data.documents; } } else if (reviewType === 'record') { @@ -209,6 +214,7 @@ export default function Home() { ); // 限制数量 + // console.log('卷宗文档数据',mergedDocs); return mergedDocs.slice(0, documentSearchParams.pageSize); } } else { @@ -236,7 +242,7 @@ export default function Home() { setIsLoading(true); // 更新主页数据 - const newHomeData = await getHomeData(currentReviewType || undefined); + const newHomeData = await getHomeData(currentReviewType || undefined,userInfo.user_id); setHomeData(newHomeData); // 更新文档数据 @@ -301,14 +307,15 @@ export default function Home() {
- {userRole === 'developer' ? '管' : '用'} + {userInfo.nick_name.charAt(userInfo.nick_name.length-1)}
-

{userRole === 'developer' ? '系统管理员' : '普通用户'}

+ {/*

{userRole === 'developer' ? '系统管理员' : '普通用户'}

*/} +

{userInfo.nick_name}

{/*

{userRole === 'developer' ? '超级管理员' : '标准权限'}

*/}
- {/* 登出操作 */} + {/* 登出操作 */}