feedback-gateway.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { get, post } from '@/core/http/http-client'
  2. import { fetchUploadConfig, uploadImageFile } from '@/features/dynamics/publish-gateway'
  3. import appSettings from '@/core/config/app-settings'
  4. const ROUTE_ADD = '/wanlshop/feedback/add'
  5. const ROUTE_LISTS = '/wanlshop/feedback/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. export function buildDeviceInfo() {
  21. const sys = uni.getSystemInfoSync()
  22. const device = {
  23. language: sys.language,
  24. brand: sys.brand,
  25. model: sys.model,
  26. system: sys.system
  27. }
  28. // #ifdef MP-ALIPAY
  29. device.platform = 'alipay'
  30. // #endif
  31. // #ifdef MP-BAIDU
  32. device.platform = 'baidu'
  33. // #endif
  34. // #ifdef MP-QQ
  35. device.platform = 'qq'
  36. // #endif
  37. // #ifdef MP-TOUTIAO
  38. device.platform = 'toutiao'
  39. // #endif
  40. // #ifdef MP-WEIXIN
  41. device.platform = 'weixin'
  42. // #endif
  43. // #ifdef H5
  44. device.platform = 'h5'
  45. // #endif
  46. // #ifdef APP-PLUS
  47. device.platform = sys.platform
  48. try {
  49. device.version = plus.runtime.version
  50. device.versionCode = plus.runtime.versionCode
  51. } catch (_) {}
  52. // #endif
  53. return device
  54. }
  55. export async function submitFeedback(payload) {
  56. const { data, msg } = await post(ROUTE_ADD, payload)
  57. return { data, msg }
  58. }
  59. function normalizeFeedbackRow(item) {
  60. if (!item || item.id == null) return null
  61. let images = item.images
  62. if (typeof images === 'string') {
  63. try {
  64. images = JSON.parse(images)
  65. } catch (_) {
  66. images = []
  67. }
  68. }
  69. return {
  70. ...item,
  71. score: Number(item.score) || 0,
  72. images: Array.isArray(images) ? images : []
  73. }
  74. }
  75. export async function fetchFeedbackListPage(page = 1) {
  76. const { data, raw } = await get(ROUTE_LISTS, { page })
  77. const body = unwrapListPageBody(data, raw)
  78. const rows = (Array.isArray(body.data) ? body.data : [])
  79. .map(normalizeFeedbackRow)
  80. .filter(Boolean)
  81. return {
  82. rows,
  83. total: Number(body.total || rows.length),
  84. currentPage: Number(body.current_page || page || 1),
  85. lastPage: Number(body.last_page || body.current_page || 1)
  86. }
  87. }
  88. export async function uploadFeedbackImage(filePath) {
  89. const config = await fetchUploadConfig()
  90. return uploadImageFile(filePath, config)
  91. }