complaint-gateway.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { get, post } from '@/core/http/http-client'
  2. import { fetchUploadConfig, uploadImageFile } from '@/features/dynamics/publish-gateway'
  3. const ROUTE_REASONS = '/wanlshop/complaint/reasonlist'
  4. const ROUTE_ADD = '/wanlshop/complaint/add'
  5. const ROUTE_LISTS = '/wanlshop/complaint/lists'
  6. function unwrapListPageBody(data, raw) {
  7. const candidates = [data, raw?.data, raw?.res?.data]
  8. for (const c of candidates) {
  9. if (!c || typeof c !== 'object' || Array.isArray(c)) continue
  10. if (Array.isArray(c.data) || c.current_page != null || c.last_page != null) {
  11. return c
  12. }
  13. if (c.data && typeof c.data === 'object' && !Array.isArray(c.data)) {
  14. const inner = c.data
  15. if (Array.isArray(inner.data) || inner.current_page != null) return inner
  16. }
  17. }
  18. return { data: [], current_page: 1, last_page: 1, total: 0 }
  19. }
  20. function normalizeReasonRow(item) {
  21. if (!item || item.title == null) return null
  22. const code = Number(item.code_num)
  23. if (Number.isNaN(code)) return null
  24. return { code_num: code, title: String(item.title) }
  25. }
  26. /**
  27. * @param {Record<string, string>} option 路由 query
  28. * @param {(key: string) => string} t
  29. */
  30. export function buildComplaintDraft(option = {}, t) {
  31. const type = option.type != null && option.type !== '' ? Number(option.type) : 0
  32. const id = option.id != null && option.id !== '' ? Number(option.id) : 0
  33. const draft = {
  34. type: Number.isNaN(type) ? 0 : type,
  35. complaint_user_id: 0,
  36. complaint_shop_id: 0,
  37. complaint_goods_id: 0,
  38. images: [],
  39. content: '',
  40. reason: -1
  41. }
  42. let typeName = t('complaint.typeGeneral')
  43. if (draft.type === 0) {
  44. typeName = t('complaint.types.user')
  45. draft.complaint_user_id = id
  46. } else if (draft.type === 1) {
  47. typeName = t('complaint.types.product')
  48. draft.complaint_goods_id = id
  49. } else if (draft.type === 2) {
  50. typeName = t('complaint.types.shop')
  51. draft.complaint_shop_id = id
  52. } else if (draft.type === 3) {
  53. typeName = t('complaint.types.group')
  54. draft.complaint_goods_id = id
  55. }
  56. let content = ''
  57. if (option.content != null && String(option.content).trim() !== '') {
  58. content = String(option.content)
  59. try {
  60. content = decodeURIComponent(content)
  61. } catch (_) {}
  62. draft.content = content.trim()
  63. }
  64. return {
  65. draft,
  66. typeName,
  67. targetId: id
  68. }
  69. }
  70. export async function fetchComplaintReasons() {
  71. const { data, raw } = await get(ROUTE_REASONS)
  72. const list = Array.isArray(data) ? data : Array.isArray(raw?.data) ? raw.data : []
  73. return list.map(normalizeReasonRow).filter(Boolean)
  74. }
  75. export async function submitComplaint(payload) {
  76. const { data, msg } = await post(ROUTE_ADD, payload)
  77. return { data, msg }
  78. }
  79. export async function fetchComplaintListPage(page = 1) {
  80. const { data, raw } = await get(ROUTE_LISTS, { page })
  81. const body = unwrapListPageBody(data, raw)
  82. const rows = Array.isArray(body.data) ? body.data : []
  83. return {
  84. rows,
  85. total: Number(body.total || rows.length),
  86. currentPage: Number(body.current_page || page || 1),
  87. lastPage: Number(body.last_page || body.current_page || 1)
  88. }
  89. }
  90. export async function uploadComplaintImage(filePath) {
  91. const config = await fetchUploadConfig()
  92. return uploadImageFile(filePath, config)
  93. }