feat(assessment): 小程序测评结果页增加PDF下载功能

- ResultStatusDto/AssessmentResultDto 增加 ReportUrl 属性
- AssessmentService 的 GetResultStatusAsync/GetResultAsync 返回 ReportUrl
- loading 页面修复状态码映射(4=已完成/5=失败),传递 reportUrl 到结果页
- result 页面实现 PDF 下载:uni.downloadFile + uni.openDocument
This commit is contained in:
zpc 2026-03-17 23:33:07 +08:00
parent 0b70ef0471
commit 173ad579e5
5 changed files with 75 additions and 26 deletions

View File

@ -103,7 +103,8 @@ public class AssessmentService : IAssessmentService
.Select(r => new ResultStatusDto
{
Status = r.Status,
IsCompleted = r.Status == 4
IsCompleted = r.Status == 4,
ReportUrl = r.ReportUrl
})
.FirstOrDefaultAsync();
@ -386,6 +387,7 @@ public class AssessmentService : IAssessmentService
AssessmentName = assessmentType.Name,
Name = record.Name,
TestDate = record.CompleteTime?.ToString("yyyy-MM-dd") ?? record.CreateTime.ToString("yyyy-MM-dd"),
ReportUrl = record.ReportUrl,
Intelligences = new List<IntelligenceDto>(),
Traits = new List<TraitDto>(),
Abilities = new List<AbilityDto>(),

View File

@ -79,6 +79,11 @@ public class AssessmentResultDto
/// 发展建议
/// </summary>
public List<string> Suggestions { get; set; } = new();
/// <summary>
/// 报告PDF地址
/// </summary>
public string? ReportUrl { get; set; }
}
/// <summary>

View File

@ -19,4 +19,9 @@ public class ResultStatusDto
/// 状态描述信息
/// </summary>
public string? Message { get; set; }
/// <summary>
/// 报告PDF地址
/// </summary>
public string? ReportUrl { get; set; }
}

View File

@ -106,23 +106,25 @@ async function checkStatus() {
if (res && res.code === 0 && res.data) {
const status = res.data.status
// 1- 2- 3-
if (status === 2) {
// 3- 4- 5-
if (status === 4) {
//
stopPolling()
stopTipRotation()
// reportUrl
const reportUrl = res.data.reportUrl || ''
uni.redirectTo({
url: `/pages/assessment/result/index?recordId=${recordId.value}`
url: `/pages/assessment/result/index?recordId=${recordId.value}&reportUrl=${encodeURIComponent(reportUrl)}`
})
} else if (status === 3) {
} else if (status === 5) {
//
stopPolling()
stopTipRotation()
uni.showModal({
title: '提示',
content: res.data.message || '报告生成失败,请重新测评',
content: res.data.message || '报告生成失败,请联系客服',
showCancel: false,
success: () => {
uni.switchTab({
@ -131,7 +133,7 @@ async function checkStatus() {
}
})
}
// status === 1
// status === 3
}
} catch (error) {
console.error('查询状态失败:', error)

View File

@ -21,6 +21,7 @@ const userStore = useUserStore()
//
const recordId = ref('')
const reportUrl = ref('')
//
const pageLoading = ref(true)
@ -104,6 +105,10 @@ async function loadResult() {
if (res && res.code === 0 && res.data) {
resultData.value = res.data
// reportUrl
if (res.data.reportUrl) {
reportUrl.value = res.data.reportUrl
}
} else {
// 使
resultData.value = generateMockData()
@ -258,43 +263,68 @@ function getProgressColor(score) {
}
/**
* 保存到本地PDF格式
* 保存到本地下载PDF报告
*/
async function handleSaveToLocal() {
if (saving.value) return
if (!reportUrl.value) {
uni.showToast({
title: 'PDF报告暂未生成',
icon: 'none'
})
return
}
saving.value = true
try {
// PDF
uni.showLoading({
title: '正在生成PDF...'
title: '正在下载PDF...'
})
// PDF
await new Promise(resolve => setTimeout(resolve, 1500))
// PDF
const downloadRes = await new Promise((resolve, reject) => {
uni.downloadFile({
url: reportUrl.value,
success: resolve,
fail: reject
})
})
uni.hideLoading()
//
uni.showModal({
title: '提示',
content: 'PDF报告已生成是否保存到本地',
confirmText: '保存',
success: (res) => {
if (res.confirm) {
//
if (downloadRes.statusCode === 200) {
// 使 openDocument PDF
uni.openDocument({
filePath: downloadRes.tempFilePath,
fileType: 'pdf',
showMenu: true,
success: () => {
uni.showToast({
title: '保存成功',
title: '打开成功',
icon: 'success'
})
},
fail: (err) => {
console.error('打开PDF失败:', err)
uni.showToast({
title: '打开失败,请重试',
icon: 'none'
})
}
}
})
})
} else {
uni.showToast({
title: '下载失败,请重试',
icon: 'none'
})
}
} catch (error) {
console.error('保存失败:', error)
uni.hideLoading()
console.error('下载PDF失败:', error)
uni.showToast({
title: '保存失败,请重试',
title: '下载失败,请重试',
icon: 'none'
})
} finally {
@ -308,6 +338,11 @@ async function handleSaveToLocal() {
onLoad((options) => {
recordId.value = options.recordId || ''
// loading reportUrl
if (options.reportUrl) {
reportUrl.value = decodeURIComponent(options.reportUrl)
}
//
userStore.restoreFromStorage()
@ -321,7 +356,7 @@ onLoad((options) => {
<Navbar title="测评报告" :showBack="true">
<template #right>
<view class="save-btn" @click="handleSaveToLocal">
<text>{{ saving ? '保存中...' : '保存到本地' }}</text>
<text>{{ saving ? '下载中...' : '下载PDF' }}</text>
</view>
</template>
</Navbar>