Selaa lähdekoodia

fix(wx): show readable report preview names

Developer 1 viikko sitten
vanhempi
commit
238f325662

+ 38 - 9
pagesHome/components/report/generateList.vue

@@ -52,9 +52,9 @@
 			<view class="kr-box adffcac report-selector-box">
 				<view class="kr-title">查看报告</view>
 				<view class="report-selector-tip">请选择要查看的报告</view>
-				<view class="report-selector-btn" :class="{disabled: reportSelectorUnavailable('background')}" @click="selectReportArtifact('background')">背景报告</view>
-				<view class="report-selector-btn" :class="{disabled: reportSelectorUnavailable('professional')}" @click="selectReportArtifact('professional')">教练报告</view>
-				<view class="report-selector-btn" @click="selectReportArtifact('public')">团队报告</view>
+				<view class="report-selector-btn" :class="{disabled: reportSelectorUnavailable('background')}" @click="selectReportArtifact('background')">{{reportSelectorLabel(reportSelectorItem, 'background')}}</view>
+				<view class="report-selector-btn" :class="{disabled: reportSelectorUnavailable('professional')}" @click="selectReportArtifact('professional')">{{reportSelectorLabel(reportSelectorItem, 'professional')}}</view>
+				<view class="report-selector-btn" @click="selectReportArtifact('public')">{{reportSelectorLabel(reportSelectorItem, 'public')}}</view>
 				<view class="report-selector-cancel" @click="closeReportSelector">取消</view>
 			</view>
 		</view>
@@ -100,7 +100,12 @@
 
 <script>
 	import { getPlatformInfo } from '@/utils/platform.js'; 
-	import { reportArtifactUrl, reportArtifactDownloadUrl } from '@/utils/reportArtifactLinks.js'
+	import {
+		reportArtifactUrl,
+		reportArtifactDownloadUrl,
+		reportPreviewName,
+		reportSelectorLabel as artifactSelectorLabel
+	} from '@/utils/reportArtifactLinks.js'
 	import { canEditEvaluation, isReportGenerating, EVALUATION_PROCESSING_PROMPT, normalizeEvaluationForm } from '@/utils/reportEvaluationEdit.js'
 	import CusSelectTeamUser from '@/components/CusSelectTeamUser/index.vue'
 	export default {
@@ -238,6 +243,9 @@
 				return this.reportSelectorItem && String(this.reportSelectorItem.type) === '1'
 					&& ['background', 'professional'].indexOf(kind) !== -1;
 			},
+			reportSelectorLabel(item, kind){
+				return artifactSelectorLabel(item, kind);
+			},
 			selectReportArtifact(kind){
 				const item = this.reportSelectorItem;
 				this.closeReportSelector();
@@ -324,13 +332,13 @@
 				if (isPC) {
 					this.$showModal('请在手机端该微信小程序中查看报告')
 				} else if (isMobile||platform==='devtools') {
-					this.openReportDocument(url)
+					this.openReportDocument(url, reportPreviewName(item, kind))
 					// uni.navigateTo({
 					// 	url:'/pages/webView?src='+item.fileUrl
 					// })
 				}
 			},
-			async openReportDocument(fileUrl) {
+			async openReportDocument(fileUrl, previewName) {
 			  try {
 				// 1. 显示加载提示
 				uni.showLoading({ title: '加载中...' });
@@ -349,9 +357,11 @@
 				  return;
 				}
 		
-				// 3. 按实际扩展名使用微信原生文档预览
-				const openRes = await uni.openDocument({
-				  filePath: dres.tempFilePath,
+				// 3. 微信下载路径使用随机名;复制为业务名称后再预览,便于原生查看器展示可读标题
+				const previewPath = this.createNamedPreviewFile(dres.tempFilePath, previewName);
+				// 4. 按实际扩展名使用微信原生文档预览
+				await uni.openDocument({
+				  filePath: previewPath,
 				  fileType: this.documentType(fileUrl),
 				  showMenu: false
 				});
@@ -362,6 +372,25 @@
 				uni.showToast({ title: '加载失败,请重试', icon: 'none' });
 				}
 			},
+			createNamedPreviewFile(tempFilePath, previewName) {
+				if (!tempFilePath || !previewName || typeof wx === 'undefined'
+					|| !wx.env || !wx.env.USER_DATA_PATH || !wx.getFileSystemManager) {
+					return tempFilePath;
+				}
+				const previewPath = `${wx.env.USER_DATA_PATH}/${previewName}`;
+				try {
+					const fileSystemManager = wx.getFileSystemManager();
+					const previousPath = uni.getStorageSync('perillReportPreviewPath');
+					if (previousPath) {
+						try { fileSystemManager.unlinkSync(previousPath); } catch (error) {}
+					}
+					fileSystemManager.copyFileSync(tempFilePath, previewPath);
+					uni.setStorageSync('perillReportPreviewPath', previewPath);
+					return previewPath;
+				} catch (error) {
+					return tempFilePath;
+				}
+			},
 			documentType(fileUrl) {
 				const path = (fileUrl || '').split('?')[0].toLowerCase()
 				return path.endsWith('.docx') ? 'docx' : 'pdf'

+ 31 - 1
tests/reportArtifactLinks.test.js

@@ -1,5 +1,10 @@
 const assert = require('assert')
-const { reportArtifactUrl, reportArtifactLinks, reportArtifactDownloadUrl } = require('../utils/reportArtifactLinks')
+const {
+	reportArtifactUrl,
+	reportArtifactLinks,
+	reportArtifactDownloadUrl,
+	reportPreviewName
+} = require('../utils/reportArtifactLinks')
 
 assert.equal(reportArtifactUrl({ backgroundReportUrl: 'background.docx' }, 'background'), 'background.docx')
 assert.equal(reportArtifactUrl({ professionalReportUrl: 'professional.docx' }, 'professional'), 'professional.docx')
@@ -16,4 +21,29 @@ assert.deepEqual(reportArtifactLinks({
 	public: 'public.pdf'
 })
 
+assert.equal(
+	reportPreviewName({ teamName: 'TSCore', type: 2 }, 'background'),
+	'TSCore_团队背景信息报告'
+)
+assert.equal(
+	reportPreviewName({ teamName: 'TSCore', type: 2 }, 'professional'),
+	'TSCore_PERILL评估报告_教练版'
+)
+assert.equal(
+	reportPreviewName({ teamName: 'TSCore', type: 2 }, 'public'),
+	'TSCore_PERILL评估报告_团队版'
+)
+assert.equal(
+	reportPreviewName({ teamName: 'TSCore', type: 1 }, 'public'),
+	'TSCore_PERILL评估报告_个人版'
+)
+assert.equal(
+	reportPreviewName({ teamName: ' 研发/平台:*? ' , type: 2 }, 'background'),
+	'研发_平台____团队背景信息报告'
+)
+assert.equal(
+	reportPreviewName({ teamName: '   ', type: 1 }, 'public'),
+	'未命名团队_PERILL评估报告_个人版'
+)
+
 console.log('reportArtifactLinks tests passed')

+ 5 - 11
tests/reportButtonLabels.test.js

@@ -1,15 +1,9 @@
 const assert = require('node:assert/strict')
-const fs = require('node:fs')
-const path = require('node:path')
+const { reportSelectorLabel } = require('../utils/reportArtifactLinks')
 
-const source = fs.readFileSync(
-	path.join(__dirname, '../pagesHome/components/report/generateList.vue'),
-	'utf8'
-)
-
-assert.match(source, />教练报告<\/view>/)
-assert.match(source, />团队报告<\/view>/)
-assert.doesNotMatch(source, />专业版<\/view>/)
-assert.doesNotMatch(source, />公开版<\/view>/)
+assert.equal(reportSelectorLabel({ type: 1 }, 'background'), '背景报告')
+assert.equal(reportSelectorLabel({ type: 1 }, 'professional'), '教练报告')
+assert.equal(reportSelectorLabel({ type: 1 }, 'public'), '个人版报告')
+assert.equal(reportSelectorLabel({ type: 2 }, 'public'), '团队版报告')
 
 console.log('report button labels: PASS')

+ 29 - 1
utils/reportArtifactLinks.js

@@ -19,4 +19,32 @@ function reportArtifactDownloadUrl(url) {
 	return `${url}${separator}_download=${Date.now()}`
 }
 
-module.exports = { reportArtifactUrl, reportArtifactLinks, reportArtifactDownloadUrl }
+function safeTeamName(item = {}) {
+	const teamName = String(item.teamName || '').trim()
+	const safeName = teamName.replace(/[\u0000-\u001f\\/:*?"<>|]/g, '_')
+	return Array.from(safeName || '未命名团队').slice(0, 60).join('')
+}
+
+function reportPreviewName(item = {}, kind) {
+	const teamName = safeTeamName(item)
+	if (kind === 'background') return `${teamName}_团队背景信息报告`
+	if (kind === 'professional') return `${teamName}_PERILL评估报告_教练版`
+	if (kind === 'public' && String(item.type) === '1') return `${teamName}_PERILL评估报告_个人版`
+	if (kind === 'public') return `${teamName}_PERILL评估报告_团队版`
+	return `${teamName}_PERILL评估报告`
+}
+
+function reportSelectorLabel(item = {}, kind) {
+	if (kind === 'background') return '背景报告'
+	if (kind === 'professional') return '教练报告'
+	if (kind === 'public' && String(item.type) === '1') return '个人版报告'
+	return '团队版报告'
+}
+
+module.exports = {
+	reportArtifactUrl,
+	reportArtifactLinks,
+	reportArtifactDownloadUrl,
+	reportPreviewName,
+	reportSelectorLabel
+}