Bläddra i källkod

fix(report): prevent stale pdf preview

Developer 1 månad sedan
förälder
incheckning
0f6d0ee6ca

+ 5 - 3
pages/report.vue

@@ -47,6 +47,7 @@
 
 
 <script>
 <script>
 	import pageEmpty from '@/components/pageEmpty/index.vue'
 	import pageEmpty from '@/components/pageEmpty/index.vue'
+	import { reportArtifactUrl } from '@/utils/reportArtifactLinks.js'
 	import { canEditEvaluation, EVALUATION_PROCESSING_PROMPT, normalizeEvaluationForm } from '@/utils/reportEvaluationEdit.js'
 	import { canEditEvaluation, EVALUATION_PROCESSING_PROMPT, normalizeEvaluationForm } from '@/utils/reportEvaluationEdit.js'
 	export default {
 	export default {
 		components:{ pageEmpty },
 		components:{ pageEmpty },
@@ -107,9 +108,10 @@
 				}).finally(()=>{ this.editSubmitting = false; });
 				}).finally(()=>{ this.editSubmitting = false; });
 			},
 			},
 			viewReport(item){
 			viewReport(item){
-				if(!item.fileUrl) return this.$showToast('报告pdf为空,请联系教练。')
-				uni.navigateTo({
-					url:`/pages/reportDetail?pdfUrl=${item.fileUrl}&reportId=${item.reportId}&fileName=${item.programName}-${item.reportName}`
+				const pdfUrl = reportArtifactUrl(item, 'public')
+				if(!pdfUrl) return this.$showToast('报告pdf为空,请联系教练。')
+				uni.navigateTo({
+					url:`/pages/reportDetail?pdfUrl=${pdfUrl}&reportId=${item.reportId}&fileName=${item.programName}-${item.reportName}`
 				})
 				})
 			}
 			}
 		}
 		}

+ 4 - 4
pagesHome/components/report/generateList.vue

@@ -85,7 +85,7 @@
 
 
 <script>
 <script>
 	import { getPlatformInfo } from '@/utils/platform.js'; 
 	import { getPlatformInfo } from '@/utils/platform.js'; 
-	import { reportArtifactUrl } from '@/utils/reportArtifactLinks.js'
+	import { reportArtifactUrl, reportArtifactDownloadUrl } from '@/utils/reportArtifactLinks.js'
 	import { canEditEvaluation, EVALUATION_PROCESSING_PROMPT, normalizeEvaluationForm } from '@/utils/reportEvaluationEdit.js'
 	import { canEditEvaluation, EVALUATION_PROCESSING_PROMPT, normalizeEvaluationForm } from '@/utils/reportEvaluationEdit.js'
 	import CusSelectTeamUser from '@/components/CusSelectTeamUser/index.vue'
 	import CusSelectTeamUser from '@/components/CusSelectTeamUser/index.vue'
 	export default {
 	export default {
@@ -275,12 +275,12 @@
 				}
 				}
 			},
 			},
 			async openReportDocument(fileUrl) {
 			async openReportDocument(fileUrl) {
-			  try {
+			  try {
 				// 1. 显示加载提示
 				// 1. 显示加载提示
 				uni.showLoading({ title: '加载中...' });
 				uni.showLoading({ title: '加载中...' });
 				// 2. 先下载PDF文件到本地(解决安卓直接预览网络文件慢的问题)
 				// 2. 先下载PDF文件到本地(解决安卓直接预览网络文件慢的问题)
-				const downloadRes = await uni.downloadFile({
-				  url: fileUrl,
+				const downloadRes = await uni.downloadFile({
+				  url: reportArtifactDownloadUrl(fileUrl),
 				  // 可选:设置超时时间,避免长时间等待
 				  // 可选:设置超时时间,避免长时间等待
 				  timeout: 30000
 				  timeout: 30000
 				});
 				});

+ 7 - 6
pagesHome/reportDetail.vue

@@ -8,8 +8,9 @@
 	</view>
 	</view>
 </template>
 </template>
 
 
-<script>
-	export default {
+<script>
+	import { reportArtifactDownloadUrl } from '@/utils/reportArtifactLinks.js'
+	export default {
 		data(){
 		data(){
 			return {
 			return {
 				pdfUrl:'',
 				pdfUrl:'',
@@ -22,9 +23,9 @@
 			this.showPdf(this.pdfUrl)
 			this.showPdf(this.pdfUrl)
 		},
 		},
 		methods:{
 		methods:{
-			showPdf(url){
-				wx.downloadFile({
-				  url,
+			showPdf(url){
+				wx.downloadFile({
+				  url: reportArtifactDownloadUrl(url),
 				  success: function (res) {
 				  success: function (res) {
 				    const filePath = res.tempFilePath
 				    const filePath = res.tempFilePath
 				    wx.openDocument({
 				    wx.openDocument({
@@ -61,4 +62,4 @@
 			background: #FFFFFF;
 			background: #FFFFFF;
 		}
 		}
 	}
 	}
-</style>
+</style>

+ 3 - 1
tests/reportArtifactLinks.test.js

@@ -1,9 +1,11 @@
 const assert = require('assert')
 const assert = require('assert')
-const { reportArtifactUrl, reportArtifactLinks } = require('../utils/reportArtifactLinks')
+const { reportArtifactUrl, reportArtifactLinks, reportArtifactDownloadUrl } = require('../utils/reportArtifactLinks')
 
 
 assert.equal(reportArtifactUrl({ backgroundReportUrl: 'background.docx' }, 'background'), 'background.docx')
 assert.equal(reportArtifactUrl({ backgroundReportUrl: 'background.docx' }, 'background'), 'background.docx')
 assert.equal(reportArtifactUrl({ professionalReportUrl: 'professional.docx' }, 'professional'), 'professional.docx')
 assert.equal(reportArtifactUrl({ professionalReportUrl: 'professional.docx' }, 'professional'), 'professional.docx')
 assert.equal(reportArtifactUrl({ publicReportUrl: '', fileUrl: 'public.pdf' }, 'public'), 'public.pdf')
 assert.equal(reportArtifactUrl({ publicReportUrl: '', fileUrl: 'public.pdf' }, 'public'), 'public.pdf')
+assert.match(reportArtifactDownloadUrl('public.pdf'), /^public\.pdf\?_download=\d+$/)
+assert.match(reportArtifactDownloadUrl('public.pdf?source=report'), /^public\.pdf\?source=report&_download=\d+$/)
 assert.deepEqual(reportArtifactLinks({
 assert.deepEqual(reportArtifactLinks({
 	backgroundReportUrl: 'background.docx',
 	backgroundReportUrl: 'background.docx',
 	professionalReportUrl: 'professional.docx',
 	professionalReportUrl: 'professional.docx',

+ 7 - 1
utils/reportArtifactLinks.js

@@ -13,4 +13,10 @@ function reportArtifactLinks(item = {}) {
 	}
 	}
 }
 }
 
 
-module.exports = { reportArtifactUrl, reportArtifactLinks }
+function reportArtifactDownloadUrl(url) {
+	if (!url) return ''
+	const separator = url.indexOf('?') >= 0 ? '&' : '?'
+	return `${url}${separator}_download=${Date.now()}`
+}
+
+module.exports = { reportArtifactUrl, reportArtifactLinks, reportArtifactDownloadUrl }