Browse Source

Merge branch 'test' of Web/web-server into master

huangzhichao 1 day ago
parent
commit
e2ff96f4e5

+ 3 - 1
src/app.module.ts

@@ -4,7 +4,9 @@ import { AgentServerModule } from '@/module/agent-server/agent-server.module'
 import { SqlAgentModule } from '@/module/sql-agent/sql-agent.module'
 import { SqlAgentModule } from '@/module/sql-agent/sql-agent.module'
 import { TencentCloudModule } from '@/module/tencent-cloud/tencent-cloud.module'
 import { TencentCloudModule } from '@/module/tencent-cloud/tencent-cloud.module'
 
 
+import { ChatManagementModule } from '@/module/chat-management/chat-management.module'
+
 @Module({
 @Module({
-  imports: [SqlAgentModule, TencentCloudModule, AgentServerModule]
+  imports: [SqlAgentModule, TencentCloudModule, AgentServerModule, ChatManagementModule]
 })
 })
 export class AppModule {}
 export class AppModule {}

+ 61 - 0
src/dto/chat-management.ts

@@ -0,0 +1,61 @@
+import { IsString, IsNotEmpty, IsNumber, IsOptional } from 'class-validator'
+
+export class GetStaffSessionSummaryDto {
+  @IsString()
+  @IsOptional()
+  staffId: string
+
+  @IsString()
+  @IsOptional()
+  status: string
+
+  @IsNumber()
+  @IsOptional()
+  page: number
+
+  @IsNumber()
+  @IsOptional()
+  size: number
+}
+
+export class GetStaffListDto {
+  @IsNumber()
+  @IsOptional()
+  page: number
+
+  @IsNumber()
+  @IsOptional()
+  size: number
+}
+
+export class GetStaffSessionListDto {
+  @IsString()
+  @IsNotEmpty({ message: 'staffId不能为空' })
+  staffId: string
+
+  @IsNumber()
+  @IsOptional()
+  page: number
+
+  @IsNumber()
+  @IsOptional()
+  pageSize: number
+}
+
+export class GetConversationListDto {
+  @IsString()
+  @IsNotEmpty({ message: 'customerId不能为空' })
+  customerId: string
+
+  @IsString()
+  @IsNotEmpty({ message: 'staffId不能为空' })
+  staffId: string
+
+  @IsNumber()
+  @IsOptional()
+  status: number
+
+  @IsNumber()
+  @IsOptional()
+  page: number
+}

+ 29 - 0
src/interface/chat-management.interface.ts

@@ -0,0 +1,29 @@
+import { ServiceResponse } from '../response/response.interface'
+
+export interface IChatManagementService {
+  getStaffSessionSummary(
+    staffId?: string,
+    status?: string,
+    page?: number,
+    size?: number
+  ): Promise<ServiceResponse<StaffSummary[]>>
+
+  getStaffList(
+    page?: number,
+    size?: number
+  ): Promise<ServiceResponse<ChatStaff[]>>
+
+  getStaffSessionList(
+    staffId: string,
+    page?: number,
+    pageSize?: number
+  ): Promise<ServiceResponse<StaffSession[]>>
+
+  getConversationList(
+    customerId: string,
+    staffId: string,
+    status?: number,
+    page?: number,
+    size?: number
+  ): Promise<ServiceResponse<ChatConversation[]>>
+}

+ 61 - 0
src/module/chat-management/chat-management.controller.ts

@@ -0,0 +1,61 @@
+import { Controller, Get, Query } from '@nestjs/common'
+
+import { ApiOperation, ApiTags } from '@nestjs/swagger'
+
+import {
+  GetStaffSessionSummaryDto,
+  GetStaffListDto,
+  GetStaffSessionListDto,
+  GetConversationListDto
+} from '@/dto/chat-management'
+
+import { ChatManagementService } from './service/chat-management.service'
+
+@ApiTags('chat-management')
+@Controller('chat-management')
+export class ChatManagementController {
+  constructor(private readonly chatManagementService: ChatManagementService) {}
+
+  @Get('staff-session-summary')
+  @ApiOperation({ summary: '获取客服会话总览' })
+  async getStaffSessionSummary(@Query() query: GetStaffSessionSummaryDto) {
+    const { staffId, status, page, size } = query
+    console.log(staffId, status, page, size)
+    return this.chatManagementService.getStaffSessionSummary(
+      staffId,
+      status,
+      page,
+      size
+    )
+  }
+
+  @Get('staff-list')
+  @ApiOperation({ summary: '获取客服列表' })
+  async getStaffList(@Query() query: GetStaffListDto) {
+    const { page, size } = query
+    return this.chatManagementService.getStaffList(page, size)
+  }
+
+  @Get('staff-session-list')
+  @ApiOperation({ summary: '获取客服会话列表' })
+  async getStaffSessionList(@Query() query: GetStaffSessionListDto) {
+    const { staffId, page, pageSize } = query
+    return this.chatManagementService.getStaffSessionList(
+      staffId,
+      page,
+      pageSize
+    )
+  }
+
+  @Get('conversation-list')
+  @ApiOperation({ summary: '获取会话列表' })
+  async getConversationList(@Query() query: GetConversationListDto) {
+    const { customerId, staffId, status, page } = query
+    return this.chatManagementService.getConversationList(
+      customerId,
+      staffId,
+      status,
+      page
+    )
+  }
+}

+ 18 - 0
src/module/chat-management/chat-management.module.ts

@@ -0,0 +1,18 @@
+import { HttpModule } from '@nestjs/axios'
+import { Module } from '@nestjs/common'
+
+import { ChatManagementController } from './chat-management.controller'
+import { ChatManagementHttpService } from './service/chat-management-http.service'
+import { ChatManagementService } from './service/chat-management.service'
+
+@Module({
+  imports: [
+    HttpModule.register({
+      timeout: 600000,
+      maxRedirects: 3
+    })
+  ],
+  controllers: [ChatManagementController],
+  providers: [ChatManagementHttpService, ChatManagementService]
+})
+export class ChatManagementModule {}

+ 112 - 0
src/module/chat-management/service/chat-management-http.service.ts

@@ -0,0 +1,112 @@
+import { HttpService } from '@nestjs/axios'
+import { Injectable } from '@nestjs/common'
+import { lastValueFrom } from 'rxjs'
+
+import { ServiceResponse } from '@/response/response.interface'
+import { HttpStatusCode } from '@/response/status-code.enum'
+
+@Injectable()
+export class ChatManagementHttpService {
+  private readonly baseUrl = 'http://192.168.206.189:8083/api'
+
+  constructor(private readonly httpService: HttpService) {}
+
+  private async makeRequest<T>(
+    endpoint: string,
+    params?: Record<string, any>
+  ): Promise<ServiceResponse<T>> {
+    try {
+      const url = `${this.baseUrl}/${endpoint}`
+      const responseObservable = await this.httpService.get<ServiceResponse<T>>(
+        url,
+        { params }
+      )
+      const { data } = await lastValueFrom(responseObservable)
+      return data
+    } catch (error) {
+      return {
+        code: HttpStatusCode.INTERNAL_SERVER_ERROR,
+        msg: error.message,
+        data: null as T
+      }
+    }
+  }
+
+  async makeRequestPost<T>(
+    endpoint: string,
+    params?: Record<string, any>
+  ): Promise<ServiceResponse<T>> {
+    try {
+      const url = `${this.baseUrl}/${endpoint}`
+      const responseObservable = await this.httpService.post<
+        ServiceResponse<T>
+      >(url, params, {
+        headers: {
+          'Content-Type': 'application/json'
+        }
+      })
+      const { data } = await lastValueFrom(responseObservable)
+      return data
+    } catch (error) {
+      return {
+        code: HttpStatusCode.INTERNAL_SERVER_ERROR,
+        msg: error.message,
+        data: null as T
+      }
+    }
+  }
+
+  // 获取对话总览列表
+  async getStaffSessionSummary(
+    staff_id?: string,
+    status?: string,
+    page?: number,
+    size?: number
+  ): Promise<ServiceResponse<StaffSummary[]>> {
+    return this.makeRequest<StaffSummary[]>('getStaffSessionSummary', {
+      staff_id,
+      status,
+      page,
+      size
+    })
+  }
+
+  // 获取客服人员列表
+  async getStaffList(
+    page?: number,
+    size?: number
+  ): Promise<ServiceResponse<ChatStaff[]>> {
+    return this.makeRequest<ChatStaff[]>('getStaffList', {
+      page,
+      size
+    })
+  }
+
+  // 获取客服对话列表
+  async getStaffSessionList(
+    staff_id: string,
+    page_id?: number,
+    page_size?: number
+  ): Promise<ServiceResponse<StaffSession[]>> {
+    return this.makeRequest<StaffSession[]>('getStaffSessionList', {
+      staff_id,
+      page_id,
+      page_size
+    })
+  }
+
+  // 获取客服微信对话详情列表
+  async getConversationList(
+    user_id: string,
+    staff_id: string,
+    status?: number,
+    page_id?: number
+  ): Promise<ServiceResponse<ChatConversation[]>> {
+    return this.makeRequest<ChatConversation[]>('getConversationList', {
+      user_id,
+      staff_id,
+      status,
+      page_id
+    })
+  }
+}

+ 110 - 0
src/module/chat-management/service/chat-management.service.ts

@@ -0,0 +1,110 @@
+import { Injectable } from '@nestjs/common'
+
+import { IChatManagementService } from '@/interface/chat-management.interface'
+import { ServiceResponse } from '@/response/response.interface'
+import { BusinessCode, HttpStatusCode } from '@/response/status-code.enum'
+
+import { ChatManagementHttpService } from './chat-management-http.service'
+
+@Injectable()
+export class ChatManagementService implements IChatManagementService {
+  constructor(private readonly httpService: ChatManagementHttpService) {}
+  // 获取对话总览列表
+  async getStaffSessionSummary(
+    staffId?: string,
+    status?: string,
+    page?: number,
+    size?: number
+  ): Promise<ServiceResponse<StaffSummary[]>> {
+    const { code, data, msg } = await this.httpService.getStaffSessionSummary(
+      staffId,
+      status,
+      page,
+      size
+    )
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取列表成功',
+      data: data
+    }
+  }
+
+  // 获取客服列表
+  async getStaffList(
+    page?: number,
+    size?: number
+  ): Promise<ServiceResponse<ChatStaff[]>> {
+    const { code, data, msg } = await this.httpService.getStaffList(page, size)
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取客服列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取客服列表成功',
+      data: data
+    }
+  }
+
+  // 获取微信对话列表
+  async getStaffSessionList(
+    staffId: string,
+    page?: number,
+    pageSize?: number
+  ): Promise<ServiceResponse<StaffSession[]>> {
+    const { code, data, msg } = await this.httpService.getStaffSessionList(
+      staffId,
+      page,
+      pageSize
+    )
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取微信对话列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取微信对话列表成功',
+      data: data
+    }
+  }
+
+  // 获取微信对话详情
+  async getConversationList(
+    customerId: string,
+    staffId: string,
+    status?: number,
+    page?: number
+  ): Promise<ServiceResponse<ChatConversation[]>> {
+    const { code, data, msg } = await this.httpService.getConversationList(
+      customerId,
+      staffId,
+      status,
+      page
+    )
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取微信对话详情失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取微信对话详情成功',
+      data: data
+    }
+  }
+}

+ 29 - 0
src/module/chat-management/type.d.ts

@@ -0,0 +1,29 @@
+type StaffSession = {
+  avatar: string
+  customer_id: string
+  customer_name: string
+  timestamp: number
+}
+
+type ChatStaff = {
+  avatar: string
+  staff_id: string
+  staff_name: string
+}
+
+type ChatConversation = {
+  avatar: string
+  content: string
+  message_type: number
+  role: string
+  sender_id: string
+  sender_name: string
+  timestamp: number
+}
+
+type StaffSummary = {
+  active_sessions: number
+  human_intervention_sessions: number
+  staff_id: string
+  staff_name: string
+}