dynamics-gateway.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { get } from '@/core/http/http-client'
  2. import { buildRestPath } from '@/core/config/api-paths'
  3. import { extractFeedPage, normalizeClip } from '@/features/feed/clip-normalize'
  4. import { pickIsFollowFromRow } from '@/features/live/follow-state'
  5. const detailPath = () => buildRestPath('find/find/getDetails')
  6. const listPath = () => buildRestPath('find/find/getList')
  7. function ensureArray(value) {
  8. if (Array.isArray(value)) return value
  9. if (typeof value === 'string' && value.trim()) {
  10. try {
  11. const parsed = JSON.parse(value)
  12. if (Array.isArray(parsed)) return parsed
  13. } catch (_) {}
  14. return value.split(',').map((s) => s.trim()).filter(Boolean)
  15. }
  16. return []
  17. }
  18. /** 关注接口只认 user_no,不用 user_id / shop.id */
  19. function resolveAuthorUserNo(raw = {}) {
  20. if (raw.user_no != null && String(raw.user_no).trim() !== '') {
  21. return String(raw.user_no).trim()
  22. }
  23. const user = raw.user
  24. if (user && user.user_no != null && String(user.user_no).trim() !== '') {
  25. return String(user.user_no).trim()
  26. }
  27. const shop = raw.shop || {}
  28. const findUser = shop.find_user || shop.findUser || {}
  29. if (findUser.user_no != null && String(findUser.user_no).trim() !== '') {
  30. return String(findUser.user_no).trim()
  31. }
  32. return ''
  33. }
  34. function normalizePost(raw = {}) {
  35. const base = raw && typeof raw === 'object' ? raw : {}
  36. const userNo = resolveAuthorUserNo(base)
  37. return {
  38. ...base,
  39. user_no: userNo,
  40. isFollow: pickIsFollowFromRow(base),
  41. images: ensureArray(base.images),
  42. goods: ensureArray(base.goods),
  43. current: Number(base.current || 0)
  44. }
  45. }
  46. /** 详情接口返回可能是 post 本体,也可能多包一层 data */
  47. function unwrapDetail(payload) {
  48. if (!payload || typeof payload !== 'object') return {}
  49. if (payload.id != null) return payload
  50. if (payload.data && typeof payload.data === 'object') {
  51. if (payload.data.id != null) return payload.data
  52. }
  53. return payload
  54. }
  55. function pickDetailBody(data, raw) {
  56. const primary = unwrapDetail(data)
  57. if (primary.id != null) return primary
  58. const env = raw?.res?.data ?? raw?.data
  59. if (!env || typeof env !== 'object') return primary
  60. const fromEnv = unwrapDetail(env)
  61. if (fromEnv.id != null) return fromEnv
  62. if (env.data && typeof env.data === 'object') {
  63. const nested = unwrapDetail(env.data)
  64. if (nested.id != null) return nested
  65. }
  66. return primary
  67. }
  68. /**
  69. * 动态详情
  70. * GET /wanlshop/find/find/getDetails?id=
  71. */
  72. export async function fetchPostDetail(id) {
  73. const { data, raw } = await get(detailPath(), { id })
  74. return normalizePost(pickDetailBody(data, raw))
  75. }
  76. /**
  77. * 同作者更多动态(详情页下方推荐列表)
  78. * GET /wanlshop/find/find/getList?user_no=&page=&find_id=
  79. */
  80. export async function fetchAuthorPosts(userNo, page = 1, findId = '') {
  81. const { data, raw } = await get(listPath(), {
  82. user_no: userNo,
  83. page,
  84. find_id: findId || ''
  85. })
  86. let parsed = extractFeedPage(data)
  87. if (!parsed.rows.length) {
  88. const envelope = raw?.res?.data ?? raw?.data
  89. parsed = extractFeedPage(envelope)
  90. }
  91. return {
  92. rows: parsed.rows.map((row) => normalizePost(normalizeClip(row))),
  93. currentPage: parsed.currentPage,
  94. lastPage: parsed.lastPage
  95. }
  96. }