Files
xuetang/TUICallKit-Vue3/native/tang-detective/pages/report/report.js
T
2026-09-09 14:47:29 +08:00

86 lines
2.1 KiB
JavaScript

const memoryCards = require('../../data/memoryCards')
const { chapterRoute } = require('../../utils/chapterRoute')
const MEMORY_CARD_ID_PATTERN = /^S\d{2}-C\d{2}-MC\d{2}$/
const INVALID_CARD_MESSAGE = '这回生活观察暂时没有找到。回到封面,还可以慢慢翻一回。'
function normalizeCardId(value) {
const raw = String(value || '').trim()
if (!raw || raw.length > 64) return ''
let decoded = raw
try {
decoded = decodeURIComponent(raw)
} catch (error) {
return ''
}
return MEMORY_CARD_ID_PATTERN.test(decoded) ? decoded : ''
}
function findReportCard(value) {
const cardId = normalizeCardId(value)
if (!cardId) return null
return memoryCards.find((card) => card.cardId === cardId) || null
}
function twoDigits(value) {
return String(value).padStart(2, '0')
}
Page({
data: {
reportCard: null,
chapterLabel: '',
invalidCard: false,
invalidCardMessage: INVALID_CARD_MESSAGE,
},
onLoad(options = {}) {
const reportCard = findReportCard(options.cardId)
if (!reportCard) {
this.setData({
reportCard: null,
chapterLabel: '',
invalidCard: true,
})
return
}
this.setData({
reportCard: { ...reportCard },
chapterLabel: `第${twoDigits(reportCard.chapterNumber)}回`,
invalidCard: false,
})
},
backToChapter() {
const reportCard = this.data.reportCard
if (!reportCard || !reportCard.chapterNumber) {
this.goHome()
return
}
const pages = typeof getCurrentPages === 'function'
? getCurrentPages()
: []
const previous = pages[pages.length - 2]
if (
previous
&& String(previous.route || '').endsWith('/pages/chapter/chapter')
&& typeof wx.navigateBack === 'function'
) {
wx.navigateBack({ delta: 1 })
return
}
wx.navigateTo({
url: chapterRoute(reportCard.chapterNumber),
})
},
goHome() {
const target = { url: '/pages/home/home' }
if (typeof wx.reLaunch === 'function') {
wx.reLaunch(target)
return
}
wx.redirectTo(target)
},
})