channelCode.ts 7.88 KB
import { apiClient, type PaginatedResponse } from './client';

export type ChannelQRCodeStatus = 'enable' | 'disable';

export interface ChannelQRCodeListItem extends Record<string, unknown> {
  qrcode_id: number;
  qrcode_name: string;
  employee_name: string;
  position_name: string;
  channel_code: string;
  qrcode_url: string;
  agent_count: number;
  agent_names_preview: string[];
  status: ChannelQRCodeStatus;
  created_at: string;
  operations: string[];
  description?: string;
  remark?: string;
  welcome_message?: string;
}

export interface ChannelQRCodeListQuery {
  employee_name?: string;
  position_code?: string;
  status?: ChannelQRCodeStatus;
  keyword?: string;
  page: number;
  page_size: number;
}

export interface ChannelQRCodeListResponse extends PaginatedResponse<ChannelQRCodeListItem> {}

export interface ChannelQRCodeDetail extends Record<string, unknown> {
  qrcode_id: number;
  employee_id: string;
  qrcode_name: string;
  employee_name: string;
  position_code: string;
  position_name: string;
  channel_code: string;
  qrcode_url: string;
  agent_ids: number[];
  agent_names: string[];
  status: ChannelQRCodeStatus;
  remark?: string;
  welcome_message?: string;
  created_at: string;
  created_by: string;
}

export interface CreateChannelQRCodePayload {
  qrcode_name: string;
  position_code: string;
  employee_id: string;
  wechat_user_id: string;
  status: ChannelQRCodeStatus;
  remark?: string;
  welcome_message?: string;
}

export interface CreateChannelQRCodeResponse {
  qrcode_id: number;
  qrcode_name: string;
  qrcode_url: string;
  channel_code: string;
  status: ChannelQRCodeStatus;
  agent_ids: number[];
  agent_names: string[];
  created_at: string;
}

export interface UpdateChannelQRCodePayload {
  qrcode_name: string;
  status?: ChannelQRCodeStatus;
  remark?: string;
  welcome_message?: string;
}

export interface UpdateChannelQRCodeResponse {
  success: boolean;
  welcome_message?: string;
  updated_at: string;
}

export interface UpdateChannelQRCodeStatusPayload {
  reason?: string;
}

export interface UpdateChannelQRCodeStatusResponse {
  success: boolean;
  disabled_at?: string;
  previous_status: ChannelQRCodeStatus;
}

export interface BatchUpdateChannelQRCodeStatusPayload {
  qrcode_ids: number[];
  target_status: ChannelQRCodeStatus;
  reason?: string;
}

export interface BatchUpdateChannelQRCodeStatusFailedItem {
  qrcode_id: number;
  reason: string;
}

export interface BatchUpdateChannelQRCodeStatusResponse {
  success_count: number;
  failed_count: number;
  failed_list: BatchUpdateChannelQRCodeStatusFailedItem[];
}

export interface ChannelQRCodeExportPayload {
  filter_conditions?: {
    employee_name?: string;
    position_code?: string;
    status?: ChannelQRCodeStatus;
    keyword?: string;
  };
  export_fields: string[];
}

export interface ChannelQRCodeExportResponse {
  file_url: string;
  file_name: string;
  total_count: number;
  expires_at: string;
}

export interface DeleteChannelQRCodeResponse {
  success: boolean;
  deleted_at: string;
}

export interface PositionEmployeeItem {
  employee_id: string;
  employee_name: string;
  wechat_user_id: string;
  department: string;
  status: string;
}

export interface PositionEmployeesResponse {
  position_name: string;
  employee_count: number;
  employees: PositionEmployeeItem[];
}

export interface EmployeeConfiguredQRCodeItem {
  qrcode_id: number;
  position_name: string;
  channel_code: string;
  status: ChannelQRCodeStatus;
  created_at: string;
}

export interface EmployeeConfiguredQRCodesResponse {
  employee_id: string;
  employee_name: string;
  employee_role_text: string;
  qrcodes: EmployeeConfiguredQRCodeItem[];
}

export interface EmployeeMountItem {
  employee_id: string;
  wechat_user_id: string;
  employee_name: string;
  employee_role_text: string;
  position_code: string;
  position_name: string;
  qrcode_count: number;
  qrcodes: EmployeeConfiguredQRCodeItem[];
  agent_names: string[];
}

export interface EmployeeMountsResponse {
  employee_count: number;
  employees: EmployeeMountItem[];
}

/**
 * 根据状态推断渠道活码可用操作
 * - enable 状态: 可查看、编辑、停用
 * - disable 状态: 可查看、编辑、启用、删除
 */
function inferQRCodeOperations(status: ChannelQRCodeStatus): string[] {
  return status === 'enable'
    ? ['view', 'edit', 'disable']
    : ['view', 'edit', 'enable', 'delete'];
}

/**
 * 为列表项填充默认的 operations 字段
 * 如果后端返回空数组,前端也会根据状态推断
 */
function fillQRCodeListItem(item: Partial<ChannelQRCodeListItem>): ChannelQRCodeListItem {
  const status = item.status ?? 'enable';
  const operations = Array.isArray(item.operations) && item.operations.length > 0
    ? item.operations
    : inferQRCodeOperations(status);
  return {
    qrcode_id: item.qrcode_id ?? 0,
    qrcode_name: item.qrcode_name ?? '',
    employee_name: item.employee_name ?? '',
    position_name: item.position_name ?? '',
    channel_code: item.channel_code ?? '',
    qrcode_url: item.qrcode_url ?? '',
    agent_count: item.agent_count ?? 0,
    agent_names_preview: item.agent_names_preview ?? [],
    status,
    created_at: item.created_at ?? '',
    operations,
    description: item.description,
    remark: item.remark,
    welcome_message: item.welcome_message,
  };
}

export async function getChannelQRCodeList(query: ChannelQRCodeListQuery): Promise<ChannelQRCodeListResponse> {
  const response = await apiClient.get<ChannelQRCodeListResponse>('/agent-group/qrcode/list', { params: query });
  return {
    ...response,
    list: response.list.map(fillQRCodeListItem),
  };
}

export async function getChannelQRCodeDetail(qrcodeId: number): Promise<ChannelQRCodeDetail> {
  return apiClient.get<ChannelQRCodeDetail>(`/agent-group/qrcode/${qrcodeId}`);
}

export async function createChannelQRCode(payload: CreateChannelQRCodePayload): Promise<CreateChannelQRCodeResponse> {
  return apiClient.post<CreateChannelQRCodeResponse>('/agent-group/qrcode/create', payload);
}

export async function updateChannelQRCode(qrcodeId: number, payload: UpdateChannelQRCodePayload): Promise<UpdateChannelQRCodeResponse> {
  return apiClient.put<UpdateChannelQRCodeResponse>(`/agent-group/qrcode/${qrcodeId}`, payload);
}

export async function enableChannelQRCode(qrcodeId: number): Promise<UpdateChannelQRCodeStatusResponse> {
  return apiClient.put<UpdateChannelQRCodeStatusResponse>(`/agent-group/qrcode/${qrcodeId}/enable`, undefined);
}

export async function disableChannelQRCode(qrcodeId: number, payload?: UpdateChannelQRCodeStatusPayload): Promise<UpdateChannelQRCodeStatusResponse> {
  return apiClient.put<UpdateChannelQRCodeStatusResponse>(`/agent-group/qrcode/${qrcodeId}/disable`, payload);
}

export async function batchUpdateChannelQRCodeStatus(payload: BatchUpdateChannelQRCodeStatusPayload): Promise<BatchUpdateChannelQRCodeStatusResponse> {
  return apiClient.post<BatchUpdateChannelQRCodeStatusResponse>('/agent-group/qrcode/batch-status', payload);
}

export async function exportChannelQRCode(payload: ChannelQRCodeExportPayload): Promise<ChannelQRCodeExportResponse> {
  return apiClient.post<ChannelQRCodeExportResponse>('/agent-group/qrcode/export', payload);
}

export async function deleteChannelQRCode(qrcodeId: number): Promise<DeleteChannelQRCodeResponse> {
  return apiClient.delete<DeleteChannelQRCodeResponse>(`/agent-group/qrcode/${qrcodeId}`, undefined);
}

export async function getPositionEmployees(positionCode: string): Promise<PositionEmployeesResponse> {
  return apiClient.get<PositionEmployeesResponse>(`/agent-group/position/${positionCode}/employees`);
}

export async function getEmployeeConfiguredQRCodes(employeeId: string): Promise<EmployeeConfiguredQRCodesResponse> {
  return apiClient.get<EmployeeConfiguredQRCodesResponse>(`/agent-group/employee/${employeeId}/qrcodes`);
}

export async function getEmployeeMounts(): Promise<EmployeeMountsResponse> {
  return apiClient.get<EmployeeMountsResponse>('/agent-group/employees/mounts');
}