experience-maps.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { loadBackendAuthTokens, loadBackendBaseUrl } from '../../utils/backendAuth'
  2. import { getExperienceMaps, getPublicExperienceMaps, type BackendExperienceMapSummary } from '../../utils/backendApi'
  3. import { reportBackendClientLog } from '../../utils/backendClientLogs'
  4. type ExperienceMapCardView = {
  5. mapId: string
  6. placeText: string
  7. mapText: string
  8. summaryText: string
  9. coverUrl: string
  10. defaultExperienceText: string
  11. disabled: boolean
  12. }
  13. type ExperienceMapsPageData = {
  14. loading: boolean
  15. statusText: string
  16. cards: ExperienceMapCardView[]
  17. }
  18. function getAccessToken(): string | null {
  19. const app = getApp<IAppOption>()
  20. const tokens = app.globalData && app.globalData.backendAuthTokens
  21. ? app.globalData.backendAuthTokens
  22. : loadBackendAuthTokens()
  23. return tokens && tokens.accessToken ? tokens.accessToken : null
  24. }
  25. function buildCardView(item: BackendExperienceMapSummary): ExperienceMapCardView {
  26. const mapId = item.mapId || ''
  27. const defaultExperienceCount = typeof item.defaultExperienceCount === 'number' ? item.defaultExperienceCount : 0
  28. return {
  29. mapId,
  30. placeText: item.placeName || item.placeId || '地点待确认',
  31. mapText: item.mapName || item.mapId || '地图待确认',
  32. summaryText: item.summary || '当前暂无地图摘要',
  33. coverUrl: item.coverUrl || '',
  34. defaultExperienceText: defaultExperienceCount > 0 ? `默认体验 ${defaultExperienceCount} 个` : '当前暂无默认体验活动',
  35. disabled: !mapId,
  36. }
  37. }
  38. Page({
  39. data: {
  40. loading: false,
  41. statusText: '准备加载地图体验列表',
  42. cards: [],
  43. } as ExperienceMapsPageData,
  44. onLoad() {
  45. this.loadMaps()
  46. },
  47. onShow() {
  48. this.loadMaps()
  49. },
  50. async loadMaps() {
  51. const accessToken = getAccessToken()
  52. this.setData({
  53. loading: true,
  54. statusText: '正在加载地图体验列表',
  55. })
  56. try {
  57. const baseUrl = loadBackendBaseUrl()
  58. const result = accessToken
  59. ? await getExperienceMaps({
  60. baseUrl,
  61. accessToken,
  62. })
  63. : await getPublicExperienceMaps({
  64. baseUrl,
  65. })
  66. reportBackendClientLog({
  67. level: 'info',
  68. category: 'experience-maps',
  69. message: 'experience maps loaded',
  70. details: {
  71. guestMode: !accessToken,
  72. mapCount: result.length,
  73. mapIds: result.map((item) => item.mapId || ''),
  74. mapsWithDefaultExperience: result.filter((item) => {
  75. return typeof item.defaultExperienceCount === 'number' && item.defaultExperienceCount > 0
  76. }).length,
  77. },
  78. })
  79. const cards = result.map(buildCardView)
  80. this.setData({
  81. loading: false,
  82. statusText: cards.length ? '地图体验列表加载完成' : '当前没有可体验地图',
  83. cards,
  84. })
  85. } catch (error) {
  86. const message = error && (error as { message?: string }).message ? (error as { message: string }).message : '未知错误'
  87. this.setData({
  88. loading: false,
  89. statusText: `地图体验列表加载失败:${message}`,
  90. cards: [],
  91. })
  92. }
  93. },
  94. handleRefresh() {
  95. this.loadMaps()
  96. },
  97. handleOpenMap(event: WechatMiniprogram.TouchEvent) {
  98. const mapId = event.currentTarget.dataset.mapId as string | undefined
  99. reportBackendClientLog({
  100. level: 'info',
  101. category: 'experience-maps',
  102. message: 'experience map clicked',
  103. details: {
  104. clickedMapId: mapId || '',
  105. },
  106. })
  107. if (!mapId) {
  108. wx.showToast({
  109. title: '该地图暂无详情入口',
  110. icon: 'none',
  111. })
  112. return
  113. }
  114. wx.navigateTo({
  115. url: `/pages/experience-map/experience-map?mapId=${encodeURIComponent(mapId)}`,
  116. })
  117. },
  118. })