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:
parent
0b70ef0471
commit
173ad579e5
|
|
@ -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>(),
|
||||
|
|
|
|||
|
|
@ -79,6 +79,11 @@ public class AssessmentResultDto
|
|||
/// 发展建议
|
||||
/// </summary>
|
||||
public List<string> Suggestions { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 报告PDF地址
|
||||
/// </summary>
|
||||
public string? ReportUrl { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -19,4 +19,9 @@ public class ResultStatusDto
|
|||
/// 状态描述信息
|
||||
/// </summary>
|
||||
public string? Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 报告PDF地址
|
||||
/// </summary>
|
||||
public string? ReportUrl { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user