channelCode.ts
9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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,
};
}
function readString(value: unknown) {
return typeof value === 'string' ? value : '';
}
function readRecord(value: unknown) {
return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {};
}
function resolveQRCodePositionName(item: Partial<ChannelQRCodeDetail>) {
const raw = item as Record<string, unknown>;
const position = readRecord(raw.position);
const positionTag = readRecord(raw.position_tag);
return readString(item.position_name)
|| readString(raw.positionName)
|| readString(raw.position_tag_name)
|| readString(position.name)
|| readString(position.position_name)
|| readString(positionTag.name)
|| readString(positionTag.position_name)
|| readString(item.position_code);
}
function fillQRCodeDetail(item: Partial<ChannelQRCodeDetail>): ChannelQRCodeDetail {
return {
qrcode_id: item.qrcode_id ?? 0,
employee_id: item.employee_id ?? '',
qrcode_name: item.qrcode_name ?? '',
employee_name: item.employee_name ?? '',
position_code: item.position_code ?? '',
position_name: resolveQRCodePositionName(item),
channel_code: item.channel_code ?? '',
qrcode_url: item.qrcode_url ?? '',
agent_ids: item.agent_ids ?? [],
agent_names: item.agent_names ?? [],
status: item.status ?? 'enable',
remark: item.remark,
welcome_message: item.welcome_message,
created_at: item.created_at ?? '',
created_by: item.created_by ?? '',
};
}
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> {
const response = await apiClient.get<ChannelQRCodeDetail>(`/agent-group/qrcode/${qrcodeId}`);
return fillQRCodeDetail(response);
}
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');
}