people-reading/miniprogram/pages/report/report.js
2026-05-11 23:26:11 +08:00

129 lines
4.0 KiB
JavaScript

const { API_BASE_URL, authHeader, request } = require('../../utils/request')
Page({
data: {
id: '',
report: null,
data: null,
qualityPercent: 0,
dimensionCount: 0,
shareLoading: false,
shareStatusText: ''
},
onLoad(query) {
this.setData({ id: query.id })
this.loadReport()
},
async loadReport() {
try {
const report = await request({ url: `/reports/${this.data.id}` })
const data = report.report_data || {}
if (data.dimensions) {
data.dimensions = data.dimensions.map((item) => ({
...item,
confidencePercent: Math.round((item.confidence || 0) * 100)
}))
}
const handSideText = {
left: '左手',
right: '右手',
unknown: '未知手'
}[report.hand_side] || '未知手'
this.setData({
report: {
...report,
handSideText,
createdDate: (report.created_at || '').replace('T', ' ').slice(0, 16)
},
data,
qualityPercent: Math.round(((data.quality_check && data.quality_check.confidence) || 0) * 100),
dimensionCount: data.dimensions ? data.dimensions.length : 0
})
} catch (error) {
wx.showToast({ title: error.message || '加载失败', icon: 'none' })
}
},
deleteReport() {
wx.showModal({
title: '删除报告',
content: '删除后将无法恢复,关联照片也会被清理。',
success: async (res) => {
if (!res.confirm) return
try {
await request({ url: `/reports/${this.data.id}`, method: 'DELETE' })
wx.showToast({ title: '已删除' })
wx.switchTab({ url: '/pages/history/history' })
} catch (error) {
wx.showToast({ title: error.message || '删除失败', icon: 'none' })
}
}
})
},
generateShareImage() {
this.setData({ shareLoading: true, shareStatusText: '分享图生成中,通常需要 30-120 秒。你可以先继续查看报告。' })
request({
url: `/reports/${this.data.id}/share-image-jobs`,
method: 'POST'
})
.then((job) => this.pollShareImageJob(job.id, 0))
.catch((error) => {
this.setData({ shareLoading: false, shareStatusText: '' })
wx.showToast({ title: error.message || '创建任务失败', icon: 'none' })
})
},
pollShareImageJob(jobId, count) {
if (count > 80) {
this.setData({ shareLoading: false, shareStatusText: '' })
wx.showToast({ title: '生成时间较长,请稍后再试', icon: 'none' })
return
}
request({ url: `/reports/share-image-jobs/${jobId}` })
.then((job) => {
if (job.status === 'completed') {
this.downloadShareImage(jobId)
return
}
if (job.status === 'failed') {
this.setData({ shareLoading: false, shareStatusText: '' })
wx.showToast({ title: job.error_message || '分享图生成失败', icon: 'none' })
return
}
setTimeout(() => this.pollShareImageJob(jobId, count + 1), 2000)
})
.catch((error) => {
this.setData({ shareLoading: false, shareStatusText: '' })
wx.showToast({ title: error.message || '查询任务失败', icon: 'none' })
})
},
downloadShareImage(jobId) {
wx.downloadFile({
url: `${API_BASE_URL}/reports/share-image-jobs/${jobId}/image`,
header: authHeader(),
success: (res) => {
if (res.statusCode >= 200 && res.statusCode < 300) {
wx.previewImage({ current: res.tempFilePath, urls: [res.tempFilePath] })
} else {
wx.showToast({ title: '分享图下载失败', icon: 'none' })
}
},
fail: () => wx.showToast({ title: '分享图下载失败', icon: 'none' }),
complete: () => {
this.setData({ shareLoading: false, shareStatusText: '' })
}
})
},
onShareAppMessage() {
return {
title: '我在赛博先生生成了一份手相报告',
path: '/pages/index/index'
}
}
})