auditRowMapping.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // 审核中心:服务角色申请与各 Tab 原有列表行的统一映射/合并工具
  2. const ROLE_LABELS = {
  3. planner: '规划师',
  4. nutritionist: '营养师',
  5. butler: '管家',
  6. vendor: '服务商',
  7. activity_provider: '活动提供商',
  8. article_admin: '知识管理员'
  9. }
  10. const SERVICE_ROLE_STATUS = { 0: 'pending', 1: 'approved', 2: 'rejected' }
  11. // 解析后端 JSON 字符串字段(roles / images),容错返回数组
  12. export function parseJsonArray(value) {
  13. if (Array.isArray(value)) return value
  14. if (!value) return []
  15. try {
  16. const arr = JSON.parse(value)
  17. return Array.isArray(arr) ? arr : []
  18. } catch (e) {
  19. return []
  20. }
  21. }
  22. export function parseRoles(roles) {
  23. return parseJsonArray(roles)
  24. }
  25. export function roleLabel(role, roleLabels = {}) {
  26. const labels = { ...ROLE_LABELS, ...roleLabels }
  27. return labels[role] || role
  28. }
  29. // 服务角色申请 → 统一审核行
  30. export function mapServiceRoleRow(app, roleLabels = {}) {
  31. if (!app) return null
  32. const roles = parseRoles(app.roles)
  33. return {
  34. source: 'service_role',
  35. applyId: app.id,
  36. id: app.id,
  37. userId: app.userId,
  38. nickname: app.userName,
  39. realName: app.realName,
  40. phone: app.phone,
  41. roles,
  42. roleLabel: roles.map(r => roleLabel(r, roleLabels)).join('、'),
  43. status: SERVICE_ROLE_STATUS[app.status] || 'pending',
  44. rejected: app.status === 2,
  45. rejectReason: app.rejectReason,
  46. submittedAt: app.createdAt,
  47. reviewedAt: app.reviewedAt,
  48. idCard: app.idCard,
  49. idCardImages: parseJsonArray(app.idCardImages),
  50. certificateImages: parseJsonArray(app.certificateImages),
  51. province: app.province,
  52. city: app.city,
  53. district: app.district,
  54. street: app.street,
  55. addressDetail: app.addressDetail,
  56. raw: app
  57. }
  58. }
  59. function submittedTime(row) {
  60. const t = row.submittedAt || row.createdAt || 0
  61. const parsed = new Date(t).getTime()
  62. return isNaN(parsed) ? 0 : parsed
  63. }
  64. // 合并原 Tab 行与服务角色申请行:同 userId 去重(服务角色优先),按提交时间倒序
  65. export function mergeAuditRows(userRows = [], serviceRoleRows = []) {
  66. const map = new Map()
  67. for (const row of userRows) {
  68. if (!row) continue
  69. const key = String(row.userId != null ? row.userId : row.id)
  70. map.set(key, { source: 'user', ...row })
  71. }
  72. for (const row of serviceRoleRows) {
  73. if (!row) continue
  74. const key = String(row.userId != null ? row.userId : row.applyId)
  75. map.set(key, { source: 'service_role', ...row })
  76. }
  77. return Array.from(map.values()).sort((a, b) => submittedTime(b) - submittedTime(a))
  78. }