Bläddra i källkod

更新团队统计功能,新增用户身份验证和角色判断,优化统计数据查询逻辑以支持按用户ID过滤统计结果。

wuyi 2 månader sedan
förälder
incheckning
41694ee337
3 ändrade filer med 16 tillägg och 4 borttagningar
  1. 9 1
      src/controllers/team.controller.ts
  2. 1 1
      src/routes/team.routes.ts
  3. 6 2
      src/services/team.service.ts

+ 9 - 1
src/controllers/team.controller.ts

@@ -99,7 +99,15 @@ export class TeamController {
 
   async getStatistics(request: FastifyRequest, reply: FastifyReply) {
     try {
-      const statistics = await this.teamService.getStatistics()
+      const user = request.user
+      if (!user) {
+        return reply.code(403).send({ message: '用户未登录' })
+      }
+      let userId: number | undefined
+      if (user.role === UserRole.TEAM) {
+        userId = user.id
+      }
+      const statistics = await this.teamService.getStatistics(userId)
       return reply.send(statistics)
     } catch (error) {
       return reply.code(500).send({ message: '获取统计数据失败' })

+ 1 - 1
src/routes/team.routes.ts

@@ -52,7 +52,7 @@ export default async function teamRoutes(fastify: FastifyInstance) {
   // 获取统计数据
   fastify.get(
     '/statistics/summary',
-    { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
+    { onRequest: [authenticate, hasAnyRole(UserRole.ADMIN, UserRole.TEAM)] },
     teamController.getStatistics.bind(teamController)
   )
 

+ 6 - 2
src/services/team.service.ts

@@ -118,15 +118,19 @@ export class TeamService {
     return this.teamRepository.save(team)
   }
 
-  async getStatistics(): Promise<{
+  async getStatistics(userId?: number): Promise<{
     totalTeams: number
     totalRevenue: number
     todayRevenue: number
     averageCommissionRate: number
     allTeams: Array<{ id: number; name: string; totalRevenue: number; todayRevenue: number }>
   }> {
-    // 获取所有团队信息
+    // 根据 userId 参数决定查询范围
+    const whereCondition = userId ? { userId } : {}
+    
+    // 获取团队信息(如果指定了 userId,只查询该用户的团队)
     const teams = await this.teamRepository.find({
+      where: whereCondition,
       select: ['id', 'name', 'userId', 'commissionRate']
     })