result.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { loadBackendAuthTokens, loadBackendBaseUrl } from '../../utils/backendAuth'
  2. import { getSessionResult } from '../../utils/backendApi'
  3. import type { MapEngineResultSnapshot } from '../../engine/map/mapEngine'
  4. import type { GameLaunchEnvelope } from '../../utils/gameLaunch'
  5. type ResultPageData = {
  6. sessionId: string
  7. eventId: string
  8. guestMode: boolean
  9. statusText: string
  10. sessionTitleText: string
  11. sessionSubtitleText: string
  12. activitySummaryText: string
  13. listButtonText: string
  14. rows: Array<{ label: string; value: string }>
  15. }
  16. function getAccessToken(): string | null {
  17. const app = getApp<IAppOption>()
  18. const tokens = app.globalData && app.globalData.backendAuthTokens
  19. ? app.globalData.backendAuthTokens
  20. : loadBackendAuthTokens()
  21. return tokens && tokens.accessToken ? tokens.accessToken : null
  22. }
  23. function formatValue(value: unknown): string {
  24. if (value === null || value === undefined || value === '') {
  25. return '--'
  26. }
  27. return String(value)
  28. }
  29. function formatRouteSummary(input: {
  30. variantName?: string | null
  31. routeCode?: string | null
  32. }): string {
  33. if (input.variantName && input.routeCode) {
  34. return `${input.variantName} / ${input.routeCode}`
  35. }
  36. if (input.variantName) {
  37. return input.variantName
  38. }
  39. if (input.routeCode) {
  40. return input.routeCode
  41. }
  42. return '默认赛道'
  43. }
  44. function formatRuntimeValue(...candidates: Array<string | null | undefined>): string {
  45. for (let index = 0; index < candidates.length; index += 1) {
  46. const value = candidates[index]
  47. if (typeof value === 'string' && value.trim().length > 0) {
  48. return value.trim()
  49. }
  50. }
  51. return '--'
  52. }
  53. function appendRuntimeRows(
  54. rows: Array<{ label: string; value: string }>,
  55. options: {
  56. runtime?: {
  57. runtimeBindingId?: string | null
  58. placeId?: string | null
  59. placeName?: string | null
  60. mapId?: string | null
  61. mapName?: string | null
  62. tileReleaseId?: string | null
  63. courseSetId?: string | null
  64. courseVariantId?: string | null
  65. routeCode?: string | null
  66. } | null
  67. variantName?: string | null
  68. routeCode?: string | null
  69. },
  70. ) {
  71. if (!options.runtime) {
  72. return rows
  73. }
  74. return rows.concat([
  75. { label: '运行绑定', value: formatRuntimeValue(options.runtime.runtimeBindingId) },
  76. { label: '地点', value: formatRuntimeValue(options.runtime.placeName, options.runtime.placeId) },
  77. { label: '地图', value: formatRuntimeValue(options.runtime.mapName, options.runtime.mapId) },
  78. { label: '赛道集', value: formatRuntimeValue(options.runtime.courseSetId) },
  79. { label: '赛道版本', value: formatRuntimeValue(options.runtime.courseVariantId, options.variantName) },
  80. { label: 'RouteCode', value: formatRuntimeValue(options.runtime.routeCode, options.routeCode) },
  81. { label: '瓦片版本', value: formatRuntimeValue(options.runtime.tileReleaseId) },
  82. ])
  83. }
  84. function loadPendingResultLaunchEnvelope(): GameLaunchEnvelope | null {
  85. const app = getApp<IAppOption>()
  86. return app.globalData && app.globalData.pendingResultLaunchEnvelope
  87. ? app.globalData.pendingResultLaunchEnvelope
  88. : null
  89. }
  90. Page({
  91. data: {
  92. sessionId: '',
  93. eventId: '',
  94. guestMode: false,
  95. statusText: '准备加载结果',
  96. sessionTitleText: '结果页',
  97. sessionSubtitleText: '未加载',
  98. activitySummaryText: '你可以查看本局结果,也可以回到活动继续查看详情。',
  99. listButtonText: '查看历史结果',
  100. rows: [],
  101. } as ResultPageData,
  102. onLoad(query: { sessionId?: string }) {
  103. const sessionId = query && query.sessionId ? decodeURIComponent(query.sessionId) : ''
  104. this.setData({ sessionId })
  105. this.applyPendingResultSnapshot()
  106. if (sessionId) {
  107. this.loadSingleResult(sessionId)
  108. return
  109. }
  110. this.setData({
  111. statusText: '未提供单局会话,已跳转历史结果',
  112. })
  113. wx.redirectTo({
  114. url: '/pages/results/results',
  115. })
  116. },
  117. applyPendingResultSnapshot() {
  118. const app = getApp<IAppOption>()
  119. const snapshot = app.globalData && app.globalData.pendingResultSnapshot
  120. ? app.globalData.pendingResultSnapshot as MapEngineResultSnapshot
  121. : null
  122. if (!snapshot) {
  123. return
  124. }
  125. const pendingLaunchEnvelope = loadPendingResultLaunchEnvelope()
  126. this.setData({
  127. statusText: '正在加载结果',
  128. sessionTitleText: snapshot.title,
  129. sessionSubtitleText: snapshot.subtitle,
  130. guestMode: !getAccessToken(),
  131. eventId: pendingLaunchEnvelope && pendingLaunchEnvelope.business && pendingLaunchEnvelope.business.eventId
  132. ? pendingLaunchEnvelope.business.eventId
  133. : '',
  134. activitySummaryText: pendingLaunchEnvelope && pendingLaunchEnvelope.business && pendingLaunchEnvelope.business.eventId
  135. ? (!getAccessToken()
  136. ? '本局游客体验已结束,你可以回到活动继续查看,或返回地图体验。'
  137. : '本局结果已生成,你可以继续查看详情,或回到活动页。')
  138. : (!getAccessToken() ? '本局游客体验已结束,你可以返回地图体验。' : '本局结果已生成,你可以继续查看历史结果。'),
  139. listButtonText: getAccessToken() ? '查看历史结果' : '返回地图体验',
  140. rows: appendRuntimeRows([
  141. { label: snapshot.heroLabel, value: snapshot.heroValue },
  142. ...snapshot.rows.map((row) => ({
  143. label: row.label,
  144. value: row.value,
  145. })),
  146. ], {
  147. runtime: pendingLaunchEnvelope && pendingLaunchEnvelope.runtime ? pendingLaunchEnvelope.runtime : null,
  148. variantName: pendingLaunchEnvelope && pendingLaunchEnvelope.variant ? pendingLaunchEnvelope.variant.variantName : null,
  149. routeCode: pendingLaunchEnvelope && pendingLaunchEnvelope.variant ? pendingLaunchEnvelope.variant.routeCode : null,
  150. }),
  151. })
  152. if (app.globalData) {
  153. app.globalData.pendingResultSnapshot = null
  154. }
  155. },
  156. async loadSingleResult(sessionId: string) {
  157. const accessToken = getAccessToken()
  158. if (!accessToken) {
  159. this.setData({
  160. guestMode: true,
  161. statusText: '游客模式当前不加载后端单局结果,先展示本地结果摘要',
  162. listButtonText: '返回地图体验',
  163. })
  164. return
  165. }
  166. this.setData({
  167. statusText: '正在加载单局结果',
  168. })
  169. try {
  170. const result = await getSessionResult({
  171. baseUrl: loadBackendBaseUrl(),
  172. accessToken,
  173. sessionId,
  174. })
  175. const pendingLaunchEnvelope = loadPendingResultLaunchEnvelope()
  176. this.setData({
  177. statusText: '单局结果加载完成',
  178. eventId: result.session.eventId || '',
  179. sessionTitleText: result.session.eventName || result.session.eventDisplayName || result.session.eventId || result.session.id || result.session.sessionId,
  180. sessionSubtitleText: `${result.session.status || result.session.sessionStatus} / ${result.result.status} / ${formatRouteSummary(result.session)}`,
  181. activitySummaryText: result.session.eventId
  182. ? '你可以继续查看这场活动的详情,或回看历史结果。'
  183. : '你可以继续回看历史结果。',
  184. rows: appendRuntimeRows([
  185. { label: '赛道版本', value: formatRouteSummary(result.session) },
  186. { label: '最终得分', value: formatValue(result.result.finalScore) },
  187. { label: '最终用时(秒)', value: formatValue(result.result.finalDurationSec) },
  188. { label: '完成点数', value: formatValue(result.result.completedControls) },
  189. { label: '总点数', value: formatValue(result.result.totalControls) },
  190. { label: '累计里程(m)', value: formatValue(result.result.distanceMeters) },
  191. { label: '平均速度(km/h)', value: formatValue(result.result.averageSpeedKmh) },
  192. { label: '最大心率', value: formatValue(result.result.maxHeartRateBpm) },
  193. ], {
  194. runtime: result.session.runtime || (pendingLaunchEnvelope && pendingLaunchEnvelope.runtime ? pendingLaunchEnvelope.runtime : null),
  195. variantName: result.session.variantName || (pendingLaunchEnvelope && pendingLaunchEnvelope.variant ? pendingLaunchEnvelope.variant.variantName : null),
  196. routeCode: result.session.routeCode || (pendingLaunchEnvelope && pendingLaunchEnvelope.variant ? pendingLaunchEnvelope.variant.routeCode : null),
  197. }),
  198. })
  199. const app = getApp<IAppOption>()
  200. if (app.globalData) {
  201. app.globalData.pendingResultLaunchEnvelope = null
  202. }
  203. } catch (error) {
  204. const message = error && (error as { message?: string }).message ? (error as { message: string }).message : '未知错误'
  205. this.setData({
  206. statusText: `结果加载失败:${message}`,
  207. })
  208. }
  209. },
  210. handleBackToList() {
  211. if (this.data.guestMode) {
  212. wx.redirectTo({
  213. url: '/pages/experience-maps/experience-maps',
  214. })
  215. return
  216. }
  217. wx.redirectTo({
  218. url: '/pages/results/results',
  219. })
  220. },
  221. handleBackToEvent() {
  222. if (!this.data.eventId) {
  223. wx.showToast({
  224. title: '当前结果未关联活动',
  225. icon: 'none',
  226. })
  227. return
  228. }
  229. wx.redirectTo({
  230. url: `/pages/event/event?eventId=${encodeURIComponent(this.data.eventId)}`,
  231. })
  232. },
  233. })