zhouxiantao
8 days ago 03193b2a27a2c23e10f3a2f298de9c1142116780
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/**
 * @author 韩天尊
 * @time 2024-01-15
 * @version 1.0.0
 * @description API服务层 - 统一管理所有后端接口调用
 */
import axios, { AxiosResponse } from 'axios';
import { ApiResponse, PointsOverview, CommunityPointsOverview, VolunteerRegisterForm, QRCodeScanResult } from '../types';
import { APP_CONFIG } from '../config/appConfig';
 
// API基础配置
const HUB_BASE_URL = APP_CONFIG.API.HUB_BASE_URL;
const SYS_BASE_URL = APP_CONFIG.API.SYS_BASE_URL;
 
// 创建主要业务API的axios实例 (grid-hub)
const apiClient = axios.create({
  baseURL: HUB_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
});
 
// 创建系统管理API的axios实例 (grid-sys)
const sysApiClient = axios.create({
  baseURL: SYS_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
});
 
// 请求拦截器 - 添加token (主要业务API)
apiClient.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
 
// 请求拦截器 - 添加token (系统管理API)
sysApiClient.interceptors.request.use(
  (config) => {
    // 如果请求已经设置了Authorization header,则不覆盖
    if (!config.headers.Authorization) {
      const token = localStorage.getItem('token');
      if (token) {
        config.headers.Authorization = token;
      }
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
 
// 响应拦截器 - 统一处理响应 (主要业务API)
apiClient.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    console.error('API请求错误:', error);
    if (error.response?.status === 401) {
      // token过期,清除所有本地存储并跳转到登录页
      localStorage.removeItem('fwmPhone');
      localStorage.removeItem('token');
      localStorage.removeItem('user');
      localStorage.removeItem('adminInfo');
      window.location.href = '/login';
    }
    return Promise.reject(error);
  }
);
 
// 响应拦截器 - 统一处理响应 (系统管理API)
sysApiClient.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    console.error('系统API请求错误:', error);
    if (error.response?.status === 401) {
      // token过期,清除所有本地存储并跳转到登录页
      localStorage.removeItem('fwmPhone');
      localStorage.removeItem('token');
      localStorage.removeItem('user');
      localStorage.removeItem('adminInfo');
      window.location.href = '/login';
    }
    return Promise.reject(error);
  }
);
 
// 用户认证相关接口
export const authAPI = {
  // 用户登录
  login: (phone: string, password: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/users/login', {
      phone,
      password,
    }, {
      headers: {
        'Authorization': 'sys'
      }
    });
  },
 
  // 用户注册
  register: (name: string, phone: string, password: string, idCard: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/users/register', {
      name,
      phone,
      password,
      idCard,
    }, {
      headers: {
        'Authorization': 'sys'
      }
    });
  },
 
  // 根据手机号获取用户信息
  getUserByPhone: (phone: string): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/users/getByPhone', {
      params: { phone }
    });
  },
 
  // 退出登录
  logout: (): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/users/logout');
  },
};
 
// 志愿者注册相关接口
export const volunteerAPI = {
  // 志愿者注册申请 - 使用sys认证,不需要登录
  register: (formData: VolunteerRegisterForm): Promise<ApiResponse> => {
    return sysApiClient.post('/dingTalk/api/vol/register', formData, {
      headers: {
        'Authorization': 'sys'
      }
    });
  },
 
  // 获取志愿者注册状态
  getStatus: (): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/vol/status');
  },
};
 
// 活动相关接口
export const activityAPI = {
  // 获取活动列表
  getList: (params: {
    page?: number;
    size?: number;
    status?: string;
    category?: string;
    keyword?: string;
    community?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/activities/list', { params });
  },
 
  // 获取活动详情
  getDetail: (id: number): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/activities/detail/${id}`);
  },
 
  // 报名活动
  join: (activityId: number): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/activities/join', {
      activityId,
    });
  },
 
  // 取消报名
  cancel: (activityId: number): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/activities/cancel', {
      activityId,
    });
  },
};
 
// 签到相关接口
export const checkinAPI = {
  // 扫码签到
  scanCheckin: (activityId: number, qrCode: string, latitude: string, longitude: string, location: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/checkin/scan', {
      activityId,
      qrCode,
      latitude,
      longitude,
      location,
    });
  },
 
  // 签退
  checkout: (activityId: number, latitude: string, longitude: string, location: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/checkin/checkout', {
      activityId,
      latitude,
      longitude,
      location,
    });
  },
 
  // 获取签到记录
  getRecords: (params: {
    page?: number;
    size?: number;
    activityId?: number;
    status?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/checkin/records', { params });
  },
 
  // 签到/签退接口
  scanCheck: (activityId: number, latitude?: number, longitude?: number, location?: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/checkin/scanCheck', {
      activityId,
      latitude,
      longitude,
      location,
    });
  },
};
 
// 积分相关接口
export const pointsAPI = {
  // 提交积分申报
  declare: (data: {
    category: string;
    categoryDesc: string;
    secondary: string;
    activityName: string;
    startTime: string;
    endTime: string;
    serviceHours: number;
    expectedPoints: number;
    ownerId: string;
  }): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/points/declare', data);
  },
 
  // 获取积分申报列表
  getDeclarations: (params: {
    page?: number;
    size?: number;
    status?: string;
    category?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/points/declarations', { params });
  },
 
  // 获取积分申报详情
  getDeclarationDetail: (id: number): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/points/declarations/${id}`);
  },
 
  // 获取积分概览
  getOverview: (): Promise<ApiResponse<PointsOverview | CommunityPointsOverview>> => {
    return apiClient.get('/dingTalk/api/points/overview');
  },
 
  // 获取积分记录
  getRecords: (params: {
    page?: number;
    size?: number;
    type?: string;
    startTime?: string;
    endTime?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/points/records', { params });
  },
 
  // 生成核销二维码
  generateRedemptionQR: (points: number, productNum: number, productName: string, description: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/points/redemption/qrcode', {
      points,
      productNum,
      productName,
      description,
    });
  },
 
  // 处理积分核销
  processRedemption: (qrCode: string, points: number, productNum: number, productName: string, description: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/points/redemption/process', {
      qrCode,
      points,
      productNum,
      productName,
      description,
    });
  },
 
  // 扫描二维码获取用户信息
  scanQRCodeUser: (communityCode: string): Promise<ApiResponse<QRCodeScanResult>> => {
    return apiClient.post('/dingTalk/api/points/redemption/qrcodeUser', {
      communityCode
    });
  },
 
  // 获取核销记录
  getRedemptionRecords: (params: {
    page?: number;
    size?: number;
    startTime?: string;
    endTime?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/points/redemption/records', { params });
  },
};
 
// 消息相关接口
export const messageAPI = {
  // 获取消息列表
  getList: (params?: {
    page?: number;
    size?: number;
    type?: string;
    isRead?: string;
    userId?: number;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/messages/list', { params });
  },
 
  // 获取消息详情
  getDetail: (id: number): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/messages/detail/${id}`);
  },
 
  // 标记消息为已读
  markAsRead: (messageId: number): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/messages/read', { messageId });
  },
};
 
// 管理员相关接口
export const adminAPI = {
  // 管理员登录
  login: (username: string, password: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/admin/login', {
      username,
      password,
    });
  },
 
  // 获取管理员信息
  getAdminInfo: (phone: string): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/admin/users/info/${phone}`);
  },
 
  // 获取管理端统计数据
  getDashboardStats: (): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/admin/stats/dashboard');
  },
 
  // 获取活动统计
  getActivityStats: (params: {
    startDate?: string;
    endDate?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/admin/stats/activities', { params });
  },
 
  // 获取积分统计
  getPointsStats: (params: {
    startDate?: string;
    endDate?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/admin/stats/points', { params });
  },
 
  // 获取活动列表(管理员)
  getActivityList: (params: {
    page?: number;
    size?: number;
    status?: string;
    communityCode?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/admin/activities/list', { params });
  },
 
  // 获取活动详情(管理员)
  getActivityDetail: (id: number): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/admin/activities/detail/${id}`);
  },
 
  // 创建活动
  createActivity: (data: {
    title: string;
    category: string;
    categoryDesc: string;
    startTime: string;
    endTime: string;
    location: string;
    content: string;
    maxParticipants: number;
    deadline: string;
    points: number;
    ownerId: string;
    img?: string;
    communityCode: string;
    communityName: string;
    subdistrictCode: string;
    subdistrictName: string;
    districtCode: string;
    districtName: string;
    creatorId: number;
    creatorName: string;
  }): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/admin/activities/create', data);
  },
 
  // 更新活动状态
  updateActivityStatus: (activityId: number, status: string): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/admin/activities/updateStatus', {
      activityId,
      status,
    });
  },
 
  // 生成活动签到二维码
  generateActivityQRCode: (id: number): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/admin/activities/qrcode/${id}`);
  },
 
  // 获取积分审核列表
  getPointsReviewList: (params: {
    page?: number;
    size?: number;
    status?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/admin/points/review/list', { params });
  },
 
  // 获取积分审核详情
  getPointsReviewDetail: (id: number): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/admin/points/review/detail/${id}`);
  },
 
  // 批量审核积分申报
  batchReviewPointsDeclaration: (data: {
    declarationIds: number[];
    action: string;
    comment: string;
  }): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/admin/points/review', data);
  },
 
  // 单个审核积分申报
  singleReviewPointsDeclaration: (data: {
    declarationId: number;
    action: string;
    comment: string;
  }): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/admin/points/review/single', data);
  },
 
  // 获取志愿者审核列表
  getVolunteerReviewList: (params: {
    page?: number;
    size?: number;
    status?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/admin/volunteer/review/list', { params });
  },
 
  // 获取志愿者审核详情
  getVolunteerReviewDetail: (id: number): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/admin/volunteer/review/detail/${id}`);
  },
 
  // 批量审核志愿者申请
  batchReviewVolunteerApplication: (data: {
    applicationIds: number[];
    action: string;
    comment: string;
  }): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/admin/volunteer/review', data);
  },
 
  // 单个审核志愿者申请
  singleReviewVolunteerApplication: (data: {
    applicationId: number;
    action: string;
    comment: string;
  }): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/admin/volunteer/review/single', data);
  },
 
  // 扫码核销积分
  redeemPoints: (data: {
    qrCode: string;
    communityCode: string;
    volunteerId: string;
    points: number;
    productNum: number;
    productName: string;
    description: string;
  }): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/admin/points/redemption', data);
  },
 
  // 根据二维码参数获取用户信息
  getQRCodeUserInfo: (data: {
    communityCode: string;
    userId: string;
  }): Promise<ApiResponse> => {
    return apiClient.post('/dingTalk/api/points/redemption/qrcodeUser', data);
  },
 
  // 获取核销记录
  getRedemptionRecords: (params: {
    page?: number;
    size?: number;
    startDate?: string;
    endDate?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/admin/points/redemption/records', { params });
  },
 
  // 获取用户列表
  getUserList: (params: {
    page?: number;
    size?: number;
    keyword?: string;
    communityCode?: string;
  }): Promise<ApiResponse> => {
    return apiClient.get('/dingTalk/api/admin/users/list', { params });
  },
 
  // 获取用户详情
  getUserDetail: (id: number): Promise<ApiResponse> => {
    return apiClient.get(`/dingTalk/api/admin/users/detail/${id}`);
  },
 
  // 上传文件
  uploadFile: (formData: FormData, ownerId: string, ownerType: string = '35001'): Promise<ApiResponse> => {
    return sysApiClient.post(`/dingTalk/api/fileInfo/upload?ownerId=${ownerId}&ownerType=${ownerType}`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
  },
 
  // 删除文件
  deleteFile: (fileId: string): Promise<ApiResponse> => {
    return sysApiClient.get(`/dingTalk/api/fileInfo/deleteById?id=${fileId}`);
  },
};
 
export default apiClient;