pdf.vue 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <template>
  2. <view class="page-wrappe">
  3. <cus-header title=' ' bgColor="transparent"></cus-header>
  4. <view id="pdfContainer" class="pdf-container" :style="{'transform':'scale('+scale+')', 'height': containerScaledHeight + 'px'}" v-if="reportData">
  5. <!-- 封面 -->
  6. <view class="cd_box fm2 adffc" style="margin-top: 20px;height: 868px;">
  7. <img class="fm2-logo" src="https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/fm_logo.png">
  8. <img class="fm2-logo2" src="https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/fm_logo2.png">
  9. <img class="fm2-perill" src="https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/fm_perill.png">
  10. <view class="fm2-line"></view>
  11. <view class="fm2-p">团队发展动态评估报告(基础版)</view>
  12. <view class="fm2-texts adf">
  13. <view class="fm2-texts-pre"><span style="letter-spacing: 6px;">客户公司:</span>{{ reportData.teamInfo.enterpriseName||'' }}</view>
  14. <view class="fm2-texts-pre"><span style="letter-spacing: 6px;">团队名称:</span>{{ reportData.teamInfo.teamName||'' }}</view>
  15. <view class="fm2-texts-pre"><span style="letter-spacing: 2.5px;">评估发起人:</span>{{ reportData.teamInfo.initiator||'' }}</view>
  16. <view class="fm2-texts-pre"><span>报告生成时间:</span>{{ reportData.teamInfo.reportDate||'' }}</view>
  17. </view>
  18. </view>
  19. <!-- 介绍 -->
  20. <view class="cd_box">
  21. <view class="v2-top adfacjb" :style="{'background-image':'url(https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/intro_title_bg.png)'}">
  22. <view class="vt-left" style="color: #FFFFFF;">介绍<span>PERILL团队发展动态评估简介</span></view>
  23. <view class="vt-right">PERILL团队发展动态评估报告(基础版)</view>
  24. </view>
  25. <view class="v2-box">
  26. <img class="vb-img1" :src="'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+'intro'+'_box_img1.png'">
  27. <img class="vb-img2" :src="'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+'intro'+'_box_img2.png'">
  28. <view class="v2-p">PERILL团队发展动态评估源于团队教练辅导领域的先驱、管理思维与团队绩效领域的权威、全球顶尖团队教练David Clutterbuck教授及其团队通过深入研究,提炼出影响团队绩效的140多个基于实证的因素,整合而成的团队评估和提升工具-PERILL团队发展动态评估。</view>
  29. <view class="v2-p" style="margin-top: 8px;">创衡国际基于10多年来在全球与200多家具有前瞻性的国际公司以及国内具有行业代表性公司的合作经验,结合CCMI的PERILL团队发展动态评估工具,在中国推出的团队发展动态评估系统,旨在帮助团队更全面、更有效地从六个维度评估团队的发展现状,为支持团队成为高价值团队提供全景式的客观评估。</view>
  30. <view class="v2-p" style="margin-top: 8px;">PERILL团队发展动态评估的主体内容由<span>{{ reportData.teamInfo.questionCount||0 }}</span>个关于团队的描述组成。</view>
  31. </view>
  32. <view class="v2-six">
  33. <view class="vsix-title">PERILL六大纬度</view>
  34. <view class="vsix-p">PERILL团队发展动态评估提供了一个复杂的团队系统概览,它并非针对孤立的问题,也不是简单的优缺点,而是着眼于团队系统的复杂性。它 通过6个影响因素(如下所述)提出问题,以揭示团队系统各要素之间的联系,以及这些联系如何影响团队的高效运作能力。</view>
  35. <view class="vsix-boxs">
  36. <view class="vsb adfac" v-for="(item,index) in sixWd" :key="index">
  37. <img class="vsb-img" :src="item.image"/>
  38. <view class="vsb-right">
  39. <view class="vsbr-top adfac">
  40. <view class="vsbrt-type" :style="{'background':item.color}">{{ item.type }}</view>
  41. <view class="vsbrt-title" :style="{'color':item.color}">{{ item.title }}</view>
  42. </view>
  43. <view class="vsbr-desc">{{ item.desc }}</view>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. <!-- 总体评估分析 -->
  50. <view class="cd_box adffc">
  51. <view class="v2-top adfacjb" :style="{'background-image':'url(https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+'intro'+'_top_title_bg2.png)'}">
  52. <view class="vt-left">总体评估分析</view>
  53. <view class="vt-right">PERILL团队发展动态评估报告(基础版)</view>
  54. </view>
  55. <view class="v2-box" @click="downloadZtzdfxImg">
  56. <img class="vb-img1" :src="'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+'intro'+'_box_img1.png'">
  57. <img class="vb-img2" :src="'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+'intro'+'_box_img2.png'">
  58. <view class="v2-p2">报告的核心是PERILL团队发展动态评估分析下的整体团队表现。这包括团队在PERILL团队发展动态评估每个关键要素上的综合得分,通过结合得分和置信指数,我们能够展示出高功能领域和低功能域。</view>
  59. <view class="v2-p2" style="margin-top: 16px;">下面的条形图上的位置标记显示了团队在各个支柱上的总分。</view>
  60. <view style="width:100%;height:200px;" class="pdfEchart">
  61. <l-echart ref="ztzdfxRef" :canvas2d="true" @finished="initZtzdfxChart"></l-echart>
  62. </view>
  63. </view>
  64. <view class="v2-data">
  65. <view class="vd-title" :style="{'background-image':'url(https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+'intro'+'_title_bg1.png)'}">评估结果</view>
  66. <view class="v2-p" v-html="(reportData.totalDiagnosticOutput||'').replaceAll('\n\n','<br>')"></view>
  67. </view>
  68. <view class="v2-data" style="flex: 1;margin-top: 20px;">
  69. <view class="vd-title vt2" :style="{'background-image':'url(https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+'intro'+'_title_bg2.png)'}">团队提升&教练建议</view>
  70. <view class="v2-p" v-html="(reportData.totalDiagnosisSuggest||'').replaceAll('\n\n','<br>')"></view>
  71. </view>
  72. </view>
  73. <!-- 多维度 -->
  74. <canvas type="2d" canvas-id="score-canvas" id="score-canvas" class="offscreen-canvas"></canvas>
  75. <template v-if="reportData&&reportData.dimensionAnalysis&&reportData.dimensionAnalysis.length">
  76. <view class="cd_box adffc" style="border: none;" :style="{'background-image':'url('+'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+typeDict[item.title]+'_bg.png)'}" v-for="(item,index) in reportData.dimensionAnalysis" :key="index">
  77. <view class="v2-top adfacjb" :style="{'background-image':'url('+'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+typeDict[item.title]+'_title_bg.png)'}">
  78. <view class="vt-left" :class="{'black':(item.title=='人际关系'||item.title=='学习')}">{{ item.title }}</view>
  79. <view class="vt-right">PERILL团队发展动态评估报告(基础版)</view>
  80. </view>
  81. <view class="v2-box" :style="{'border':'1px solid '+item.bcolor}">
  82. <img class="vb-img1" :src="'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+typeDict[item.title]+'_box_img1.png'">
  83. <img class="vb-img2" :src="'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+typeDict[item.title]+'_box_img2.png'">
  84. <view class="v2-p2">{{ item.desc }}</view>
  85. <!-- <view class="v2-p2" style="margin-top: 16px;">评分总体分布</view> -->
  86. <view class="vb-table" :style="{'border':'1px solid '+item.bcolor}">
  87. <view class="vbt-pre adfac" v-for="(ss,si) in item.scoreSpreads" :key="si">
  88. <view class="vbtp-left adfacjc" :class="{'black':(item.title=='人际关系'||item.title=='学习'||item.title=='内部流程、系统与结构')}" :style="{'background':item.titlecolor,'padding':'0 16px'}">{{ ss.theme||'' }}</view>
  89. <view class="vbtp-right" :style="{'border':'1px solid '+item.bcolor}">
  90. <view class="vbtpr-title">{{ ss.question||'' }}</view>
  91. <view class="vbtpr-jd">
  92. <view class="vj_num" :style="{'width':((ss.avgScore>25?25:ss.avgScore)/25*100)+'%','background':item.pfztfb}"></view>
  93. <view class="vj-val adfacjc" :style="{'border':'1px solid '+item.bcolor,'left':'calc('+((ss.avgScore>25?25:ss.avgScore)/25*100)+'% - 12px)','box-shadow':'0px 2px 6px 0px '+item.bcolor}">{{ (ss.avgScore>25?25:ss.avgScore) }}</view>
  94. </view>
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. <view class="v2-data" :style="{'border':'1px solid '+item.bcolor}">
  100. <view class="vd-title vt3" :class="{'black':(item.title=='人际关系'||item.title=='学习')}" :style="{'background-image':'url(https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+typeDict[item.title]+'_title_bg1.png)'}">评估结果</view>
  101. <view class="adfac">
  102. <view class="v2-p3" style="width: 76px;">纬度得分</view>
  103. <view class="vd-wd adfac" :style="{'background':item.wddf}">
  104. <view class="vdwd-pre">维度加权总分:<span>{{ item.weightedTotal||0 }}</span><span>/{{ item.weightedTotalFull||0 }}</span></view>
  105. <view class="vdwd-pre vp">维度同意度总分(未加权):<span>{{ item.consentTotal||0 }}</span><span>/{{ item.consentTotalFull||0 }}</span></view>
  106. <view class="vdwd-pre">维度权重:<span>{{ item.weight||0 }}</span><span>/{{ item.weightFull||0 }}</span></view>
  107. </view>
  108. </view>
  109. <view class="v2-p" style="margin-top: 8px;" v-html="(item.diagnosisOutput||'').replaceAll('\n\n','<br>')"></view>
  110. </view>
  111. <view class="v2-data" :style="{'border':'1px solid '+item.bcolor}" style="flex: 1;margin-top: 15px;">
  112. <view class="vd-title vt3" :class="{'black':(item.title=='人际关系'||item.title=='学习')}" :style="{'background-image':'url(https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/'+typeDict[item.title]+'_title_bg1.png)'}">评估建议</view>
  113. <view class="v2-p" v-html="(item.diagnosisSuggest||'').replaceAll('\n\n','<br>')"></view>
  114. </view>
  115. </view>
  116. </template>
  117. </view>
  118. <view class="pdf_btn" @click="createPdf">生成PDF</view>
  119. </view>
  120. </template>
  121. <script name="">
  122. import { BaseApi } from '@/http/baseApi.js';
  123. import * as echarts from '@/pagesHome/components/lime-echart/static/echarts.min.js'
  124. import lEchart from '@/pagesHome/components/lime-echart/components/l-echart/l-echart.vue'
  125. export default {
  126. name: 'ZtzdfxChart',
  127. components:{ lEchart },
  128. data() {
  129. return {
  130. reportId:'',
  131. reportData: null,
  132. isChartReady: false,
  133. scale:1,
  134. originalContainerHeight: 0,
  135. containerScaledHeight: 'auto',
  136. typeDict: {
  137. '宗旨与动机': 'zzdj',
  138. '外部流程、系统与结构': 'wbjg',
  139. '人际关系': 'rjgx',
  140. '内部流程、系统与结构': 'nbjg',
  141. '学习': 'xx',
  142. '领导力': 'ldl'
  143. },
  144. sixWd: [
  145. {
  146. image: 'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/intro_img_p.png',
  147. type: 'P',
  148. title: '宗旨与动机',
  149. desc: '指团队共享的目的和存在的意义, 包含对共同的愿景,目标和优先级的清晰度。',
  150. color: '#761E6A'
  151. },
  152. {
  153. image: 'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/intro_img_e.png',
  154. type: 'E',
  155. title: '外部流程、系统与结构',
  156. desc: '指团队与其外部利益相关者 - 客户,供应商,股东,组织内的其他团队等的互动关联。',
  157. color: '#009191'
  158. },
  159. {
  160. image: 'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/intro_img_r.png',
  161. type: 'R',
  162. title: '人际关系',
  163. desc: '指团队成员如何共同工作–他们是否相互尊重对方的能力,足够心理安全以能够坦诚相对,真正关心彼此的幸福感。',
  164. color: '#FFD750'
  165. },
  166. {
  167. image: 'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/intro_img_i.png',
  168. type: 'I',
  169. title: '内部流程、系统与结构',
  170. desc: '指团队如何管理工作流程,互相支持和高质量的沟通和决策(包括工作任务和团队感情)。',
  171. color: '#4EB2B2'
  172. },
  173. {
  174. image: 'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/intro_img_l.png',
  175. type: 'L',
  176. title: '学习',
  177. desc: '指团队应对多变的环境和保持持续的进步和成长的能力。团队如何提高绩效(如何完成今天的任务)、能力(如何提高技能和资源以处理明天的任务)和容量(长期的愿景, 如何用更少的资源做更多的事情)',
  178. color: '#AFCDF5'
  179. },
  180. {
  181. image: 'https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/versionTwo/intro_img_l2.png',
  182. type: 'L',
  183. title: '领导力',
  184. desc: '指团队认为需要怎样的领导行为能够让他们,作为个人或者团队做到最好。团队可以和他们的领导者讨论他们的责任,以帮助领导者成为他们需要的领导者。',
  185. color: '#002846'
  186. }
  187. ],
  188. pdfImages:[],
  189. };
  190. },
  191. onLoad(options) {
  192. this.reportId = options.reportId;
  193. this.getReportData();
  194. },
  195. mounted() {
  196. this.calculateScaleAndPosition();
  197. uni.onWindowResize(() => {
  198. this.calculateScaleAndPosition();
  199. });
  200. },
  201. methods: {
  202. getReportData(){
  203. this.$api.get(`/core/report/previewReport/${this.reportId}`).then(({data:res})=>{
  204. if(res.code!==0) return this.$showToast(res.msg)
  205. this.reportData = res.data;
  206. const tempDimensionAnalysis = [
  207. {title:'宗旨与动机',bcolor:'#E4D2E1',titlecolor:'#BA8EB4',wddf:'linear-gradient( 90deg, #F5EFF5 0%, #FAF2F9 100%)',
  208. pfztfb:'linear-gradient( 90deg, #761E6A 0%, #BA8EB4 100%)',
  209. desc:`「宗旨与动机」维度,我们旨在探究是否存在一个清晰的存在理由和明确的方向,能够激发团队成员的动力并吸引他们的想象力,以及个人与集体的身份认同是否围绕共同的目标,并达成一致。`},
  210. {title:'外部流程、系统与结构',bcolor:'#B3DEDE',titlecolor:'#80C8C8',wddf:'linear-gradient( 90deg, #E8F5F5 0%, #F0F8F8 100%)',
  211. pfztfb:'linear-gradient( 90deg, #009191 0%, #80C8C8 100%)',
  212. desc:`「外部流程、系统与结构」维度,我们旨在探究团队如何与各种利益相关者互动,他们与团队的利益相关方各自如何寻求了解对方,以及现有系统和流程的有效性,以帮助管理不同的期望和需求。`},
  213. {title:'人际关系',bcolor:'#FFDF73',titlecolor:'#FFEBA8',wddf:'linear-gradient( 90deg, rgba(255,215,80,0.34) 0%, rgba(251,225,130,0.09) 100%)',
  214. pfztfb:'linear-gradient( 90deg, #FFD750 0%, #FFEBA8 100%)',
  215. desc:`「人际关系」维度,我们旨在探究团队成员如何相互交流、信任程度、尊重和关心的程度,以及团队成员之间的关系如何促进(或破坏)协作。`},
  216. {title:'内部流程、系统与结构',bcolor:'#B3DEDE',titlecolor:'#CDE9EA',wddf:'linear-gradient( 90deg, #E8F5F5 0%, #F0F8F8 100%)',
  217. pfztfb:'linear-gradient( 90deg, #4EB2B2 0%, #CDE9EA 100%)',
  218. desc:`「内部流程、系统与结构」维度,我们旨在探究团队如何在平衡责任与自主权方面进行协作。我们关注团队的敏捷程度、沟通方式以及决策过程的有效性。`},
  219. {title:'学习',bcolor:'#E6EAED',titlecolor:'#D7E5FA',wddf:'linear-gradient( 270deg, #F2F5F9 0%, #E3ECF8 100%)',
  220. pfztfb:'linear-gradient( 90deg, #AFCDF5 0%, #D7E5FA 100%)',
  221. desc:`「学习」维度,我们旨在探究团队如何提高其绩效、技能和资源以应对当前和未来的任务。我们还希望了解团队如何管理能力和提高效率。`},
  222. {title:'领导力',bcolor:'#E6EAED',titlecolor:'#8093A3',wddf:'linear-gradient( 270deg, #F2F4F6 0%, #EDF0F2 100%)',
  223. pfztfb:'linear-gradient( 90deg, #002846 0%, #8093A3 100%)',
  224. desc:`「领导力」维度,我们旨在探究领导素质和行为如何对团队功能和其他因素产生调节影响,以及这是积极的还是消极的。`}
  225. ]
  226. if(this.reportData&&this.reportData.dimensionAnalysis){
  227. this.reportData.dimensionAnalysis.forEach((d,i)=>{
  228. d.scoreSpreads.forEach(s=>{
  229. s.theme = s.theme.replaceAll(',','').replaceAll(',','');
  230. })
  231. this.reportData.dimensionAnalysis[i] = {...d,...tempDimensionAnalysis[i]}
  232. })
  233. }
  234. })
  235. },
  236. // async createPdf(){
  237. // uni.showLoading({
  238. // title:'正在生成PDF所需的图片...'
  239. // })
  240. // const imgUrl = await this.downloadZtzdfxImg();
  241. // this.pdfImages.push(imgUrl);
  242. // for(const d of this.reportData.dimensionAnalysis){
  243. // const fileurl = await this.generateScoreImage(d);
  244. // this.pdfImages.push(fileurl)
  245. // }
  246. // this.$showToast(`生成成功,共计${this.pdfImages.length}张`);
  247. // uni.hideLoading();
  248. // console.log(this.pdfImages);
  249. // },
  250. // Promise.all方法,性能更高
  251. async createPdf(){
  252. uni.showLoading({
  253. title:'正在生成PDF所需的图片...'
  254. })
  255. try {
  256. const ztzdfxImgPromise = this.downloadZtzdfxImg();
  257. const dimensionImagePromises = this.reportData.dimensionAnalysis.map(d => {
  258. return this.generateScoreImage(d,d.scoreSpreads||[]);
  259. });
  260. const allImageUrls = await Promise.all([
  261. ztzdfxImgPromise,
  262. ...dimensionImagePromises
  263. ]);
  264. this.pdfImages = allImageUrls;
  265. this.$api.post('/core/report/reportToPdf',{
  266. images:this.pdfImages,
  267. reportId:this.reportId
  268. }).then(({data:res})=>{
  269. if(res.code!==0) return this.$showToast(res.msg)
  270. uni.hideLoading();
  271. this.$showToast('生成成功');
  272. setTimeout(()=>{
  273. uni.redirectTo({
  274. url:'/pagesHome/report'
  275. })
  276. },1500)
  277. })
  278. } catch (error) {
  279. uni.hideLoading();
  280. console.error('生成图片过程中发生错误:', error);
  281. uni.showToast({ title: '生成图片失败,请重试', icon: 'none' });
  282. }
  283. },
  284. // 绘制主函数
  285. async generateScoreImage(dimensionData,scoreData) {
  286. return new Promise(resolve=>{
  287. console.log('开始生成图片...');
  288. // --- 1. 定义尺寸和样式 ---
  289. const canvasWidth = 588; // .v2-box 的宽度大约是 630 - 20*2(padding) - 1*2(border) = 588
  290. const itemHeight = 49; // 每个评估项的高度
  291. const totalHeight = itemHeight * scoreData.length;
  292. // 调整为整数,避免边框模糊
  293. const canvasHeight = totalHeight;
  294. // --- 2. 获取 Canvas 节点 ---
  295. // 使用 ID 选择器更精确
  296. const query = uni.createSelectorQuery().in(this);
  297. query.select('#score-canvas')
  298. .fields({ node: true, size: true })
  299. .exec(async (res) => {
  300. // 【重要】增加健壮性检查
  301. if (!res || !res[0] || !res[0].node) {
  302. console.error('获取 Canvas 节点失败,请检查 canvas-id 和 type="2d" 是否正确设置。');
  303. uni.showToast({ title: '组件初始化失败', icon: 'none' });
  304. return;
  305. }
  306. const canvasNode = res[0].node;
  307. const context = canvasNode.getContext('2d');
  308. const dpr = uni.getSystemInfoSync().pixelRatio;
  309. // --- 3. 设置画布尺寸和缩放以适应高分屏 ---
  310. canvasNode.width = canvasWidth * dpr;
  311. canvasNode.height = canvasHeight * dpr;
  312. context.scale(dpr, dpr);
  313. // --- 4. 开始绘制 ---
  314. // 绘制大背景
  315. context.fillStyle = '#FFFFFF';
  316. context.fillRect(0, 0, canvasWidth, canvasHeight);
  317. // --- 5. 循环绘制每一项 ---
  318. for (let i = 0; i < scoreData.length; i++) {
  319. const item = scoreData[i];
  320. const yPos = i * itemHeight;
  321. // 注意:这里不再需要 await,因为 canvas 2d 绘图是同步的
  322. this.drawScoreItem(context, item, yPos, canvasWidth, itemHeight, dimensionData);
  323. }
  324. // 【补充】绘制最外层的上下边框,避免被循环内的矩形覆盖
  325. context.strokeStyle = dimensionData.bcolor;
  326. context.lineWidth = 1;
  327. context.strokeRect(0, 0, canvasWidth, canvasHeight);
  328. // --- 6. 生成图片 ---
  329. uni.hideLoading();
  330. uni.canvasToTempFilePath({
  331. canvas: canvasNode,
  332. x: 0,
  333. y: 0,
  334. width: canvasWidth,
  335. height: canvasHeight,
  336. destWidth: canvasWidth * dpr,
  337. destHeight: canvasHeight * dpr,
  338. success: async (result) => {
  339. console.log('图片生成成功!', result.tempFilePath);
  340. const fileurl = await this.uploadFilePromise(result.tempFilePath);
  341. console.log(fileurl, 'fileurl');
  342. resolve(fileurl)
  343. },
  344. fail: (err) => {
  345. console.error('图片生成失败', err);
  346. uni.showToast({ title: '图片生成失败', icon: 'none' });
  347. }
  348. }, this);
  349. });
  350. })
  351. },
  352. // 辅助函数:计算自动换行文字的总高度
  353. calculateWrappedTextHeight(ctx, text, lineHeight, maxWidth) {
  354. let words = text.split('');
  355. let line = '';
  356. let height = lineHeight; // 至少有一行
  357. for (let n = 0; n < words.length; n++) {
  358. let testLine = line + words[n];
  359. let metrics = ctx.measureText(testLine);
  360. let testWidth = metrics.width;
  361. if (testWidth > maxWidth && n > 0) {
  362. line = words[n];
  363. height += lineHeight;
  364. } else {
  365. line = testLine;
  366. }
  367. }
  368. return height;
  369. },
  370. // 辅助函数:绘制单个评估项
  371. drawScoreItem(ctx, scoreItem, y, width, height, dimensionData) {
  372. const leftBoxWidth = 72;
  373. const rightBoxX = leftBoxWidth;
  374. const rightBoxWidth = width - leftBoxWidth;
  375. const rightPadding = 10; // 右侧内容的通用内边距
  376. // 1. --- 绘制左侧部分 ---
  377. ctx.fillStyle = dimensionData.titlecolor;
  378. ctx.fillRect(0, y, leftBoxWidth, height);
  379. // 绘制白色下边框
  380. if (y + height < ctx.canvas.height / (uni.getSystemInfoSync().pixelRatio)) {
  381. ctx.strokeStyle = '#FFFFFF';
  382. ctx.lineWidth = 1;
  383. ctx.beginPath();
  384. ctx.moveTo(0, y + height - 1);
  385. ctx.lineTo(leftBoxWidth, y + height - 1);
  386. ctx.stroke();
  387. }
  388. // 绘制左侧标题文字 (要求 1)
  389. let theme = (scoreItem.theme||'').replaceAll(',','').replaceAll(',','');
  390. const isBlackLeftTitle = (dimensionData.title == '人际关系' || dimensionData.title == '学习' || dimensionData.title == '内部流程、系统与结构');
  391. ctx.fillStyle = isBlackLeftTitle ? '#002846' : '#FFFFFF';
  392. ctx.font = '10px sans-serif';
  393. ctx.textAlign = 'center';
  394. ctx.textBaseline = 'middle';
  395. this.drawWrappedText(ctx, theme, leftBoxWidth / 2, y + height / 2, 12, leftBoxWidth - 32); // 左右留16px边距
  396. // 2. --- 绘制右侧部分 ---
  397. // 绘制右侧外边框
  398. ctx.strokeStyle = dimensionData.bcolor;
  399. ctx.lineWidth = 1;
  400. ctx.strokeRect(rightBoxX, y, rightBoxWidth, height);
  401. // --- 计算右侧内容垂直居中需要的参数 (要求 2) ---
  402. const descLineHeight = 16;
  403. const descMaxWidth = rightBoxWidth - rightPadding * 2;
  404. ctx.font = '9px sans-serif'; // 设置好字体用于计算
  405. const descHeight = this.calculateWrappedTextHeight(ctx, scoreItem.question, descLineHeight, descMaxWidth);
  406. const spacing = 7; // 文字与进度条间距 (要求 4)
  407. const progressBarHeight = 6; // 进度条高度 (要求 3)
  408. // 计算右侧所有内容的总高度
  409. const totalContentHeight = descHeight + spacing + progressBarHeight;
  410. // 计算内容块的起始 Y 坐标,使其在右侧框内垂直居中
  411. const contentStartY = y + (height - totalContentHeight) / 2;
  412. // --- 开始绘制右侧内容 ---
  413. // 绘制右侧描述文字 (要求 2)
  414. ctx.fillStyle = '#193D59';
  415. ctx.font = '9px sans-serif';
  416. ctx.textAlign = 'left';
  417. ctx.textBaseline = 'top'; // 基线设为 top 方便计算
  418. this.drawWrappedText(ctx, scoreItem.question, rightBoxX + rightPadding, contentStartY, descLineHeight, descMaxWidth);
  419. // 绘制进度条 (要求 3)
  420. const progressBarY = contentStartY + descHeight + spacing;
  421. const progressBarWidth = rightBoxWidth - rightPadding * 2;
  422. const scoreWidth = ((scoreItem.avgScore>25?25:scoreItem.avgScore)/ 25) * progressBarWidth;
  423. const barRadius = 3;
  424. // 绘制灰色背景
  425. ctx.fillStyle = '#F0F2F8';
  426. ctx.beginPath();
  427. ctx.moveTo(rightBoxX + rightPadding + barRadius, progressBarY);
  428. ctx.arcTo(rightBoxX + rightPadding + progressBarWidth, progressBarY, rightBoxX + rightPadding + progressBarWidth, progressBarY + progressBarHeight, barRadius);
  429. ctx.arcTo(rightBoxX + rightPadding + progressBarWidth, progressBarY + progressBarHeight, rightBoxX + rightPadding, progressBarY + progressBarHeight, barRadius);
  430. ctx.arcTo(rightBoxX + rightPadding, progressBarY + progressBarHeight, rightBoxX + rightPadding, progressBarY, barRadius);
  431. ctx.arcTo(rightBoxX + rightPadding, progressBarY, rightBoxX + rightPadding + progressBarWidth, progressBarY, barRadius);
  432. ctx.closePath();
  433. ctx.fill();
  434. // 绘制实际得分的渐变色进度条
  435. const gradient = ctx.createLinearGradient(rightBoxX + rightPadding, 0, rightBoxX + rightPadding + progressBarWidth, 0);
  436. const gradientColors = this.parseGradient(dimensionData.pfztfb);
  437. gradientColors.forEach(c => gradient.addColorStop(c.stop, c.color));
  438. ctx.fillStyle = gradient;
  439. ctx.save();
  440. ctx.clip(); // 使用上面的圆角矩形路径进行裁剪
  441. ctx.fillRect(rightBoxX + rightPadding, progressBarY, scoreWidth, progressBarHeight);
  442. ctx.restore();
  443. // 绘制分数框 (要求 4)
  444. const scoreFontSize = 12;
  445. const scorePaddingY = 4; // 上下内边距
  446. const scorePaddingX = 7; // 左右内边距
  447. const scoreBoxRadius = 4;
  448. ctx.font = `bold ${scoreFontSize}px sans-serif`;
  449. const scoreTextMetrics = ctx.measureText(scoreItem.avgScore>25?25:scoreItem.avgScore);
  450. const scoreBoxWidth = scoreTextMetrics.width + scorePaddingX * 2;
  451. const scoreBoxHeight = scoreFontSize + scorePaddingY * 2;
  452. // 计算分数框的位置,使其右端对齐在进度条的末端
  453. const scoreBoxX = rightBoxX + rightPadding + scoreWidth - scoreBoxWidth;
  454. // 垂直居中于进度条
  455. const scoreBoxY = progressBarY + (progressBarHeight / 2) - (scoreBoxHeight / 2);
  456. // 绘制阴影
  457. ctx.save();
  458. ctx.shadowColor = dimensionData.bcolor;
  459. ctx.shadowBlur = 6;
  460. ctx.shadowOffsetX = 0;
  461. ctx.shadowOffsetY = 2;
  462. // 绘制分数框背景和边框
  463. ctx.fillStyle = '#FFFFFF';
  464. ctx.strokeStyle = dimensionData.bcolor;
  465. ctx.lineWidth = 1;
  466. ctx.beginPath();
  467. ctx.moveTo(scoreBoxX + scoreBoxRadius, scoreBoxY);
  468. ctx.arcTo(scoreBoxX + scoreBoxWidth, scoreBoxY, scoreBoxX + scoreBoxWidth, scoreBoxY + scoreBoxHeight, scoreBoxRadius);
  469. ctx.arcTo(scoreBoxX + scoreBoxWidth, scoreBoxY + scoreBoxHeight, scoreBoxX, scoreBoxY + scoreBoxHeight, scoreBoxRadius);
  470. ctx.arcTo(scoreBoxX, scoreBoxY + scoreBoxHeight, scoreBoxX, scoreBoxY, scoreBoxRadius);
  471. ctx.arcTo(scoreBoxX, scoreBoxY, scoreBoxX + scoreBoxWidth, scoreBoxY, scoreBoxRadius);
  472. ctx.closePath();
  473. ctx.fill();
  474. ctx.stroke();
  475. ctx.restore();
  476. // 绘制分数文字
  477. ctx.fillStyle = '#002846';
  478. ctx.font = `bold ${scoreFontSize}px sans-serif`;
  479. ctx.textAlign = 'center';
  480. ctx.textBaseline = 'middle';
  481. ctx.fillText((scoreItem.avgScore>25?25:scoreItem.avgScore), scoreBoxX + scoreBoxWidth / 2, scoreBoxY + scoreBoxHeight / 2);
  482. },
  483. // 辅助函数:绘制自动换行的文字
  484. // 辅助函数:绘制自动换行且垂直居中的文字
  485. drawWrappedText(ctx, text, x, y_center, lineHeight, maxWidth) {
  486. // 1. 将文本分割成多行
  487. let words = text.split('');
  488. let lines = [];
  489. let currentLine = '';
  490. for (let n = 0; n < words.length; n++) {
  491. let testLine = currentLine + words[n];
  492. let metrics = ctx.measureText(testLine);
  493. let testWidth = metrics.width;
  494. if (testWidth > maxWidth && n > 0) {
  495. lines.push(currentLine);
  496. currentLine = words[n];
  497. } else {
  498. currentLine = testLine;
  499. }
  500. }
  501. lines.push(currentLine); // 加入最后一行
  502. // 2. 计算文本块的总高度
  503. const totalTextHeight = lines.length * lineHeight;
  504. // 3. 计算绘制第一行文本的起始 Y 坐标
  505. // y_center 是外部传入的容器中心点
  506. // 我们从容器中心点上移一半文本总高度,得到文本块的顶部位置
  507. let startY = y_center - totalTextHeight / 2;
  508. // 4. 逐行绘制
  509. // ctx.textBaseline = 'middle' 是在外部设置的,所以我们绘制每一行时,
  510. // Y坐标需要是该行所在矩形区域的垂直中心。
  511. for (let i = 0; i < lines.length; i++) {
  512. // 计算当前行文本的中心Y坐标
  513. const lineY = startY + (i * lineHeight) + (lineHeight / 2);
  514. ctx.fillText(lines[i], x, lineY);
  515. }
  516. },
  517. // 辅助函数:解析 CSS linear-gradient 字符串
  518. parseGradient(gradientString) {
  519. const colorStops = [];
  520. // 简化解析,仅适用于 "linear-gradient(90deg, #RRGGBB 0%, #RRGGBB 100%)" 格式
  521. const matches = gradientString.match(/#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})\s+(\d+)%/g);
  522. if (matches) {
  523. matches.forEach(match => {
  524. const parts = match.split(' ');
  525. colorStops.push({ color: parts[0], stop: parseInt(parts[1]) / 100 });
  526. });
  527. }
  528. return colorStops;
  529. },
  530. calculateScaleAndPosition() {
  531. uni.getSystemInfo({
  532. success: (res) => {
  533. const screenWidth = res.windowWidth; // 手机屏幕的宽度
  534. const pcContentWidth = 630; // PC端内容的原始宽度
  535. this.scale = screenWidth / pcContentWidth;
  536. this.$nextTick(() => {
  537. if (this.$refs.ztzdfxRef) {
  538. this.initZtzdfxChart();
  539. }
  540. });
  541. }
  542. });
  543. },
  544. calculatePdfContainerHeight() {
  545. uni.createSelectorQuery().in(this).select('#pdfContainer').boundingClientRect(rect => {
  546. if (rect) {
  547. this.originalContainerHeight = rect.height;
  548. this.containerScaledHeight = this.originalContainerHeight * this.scale;
  549. // console.log('原始高度:', this.originalContainerHeight, '缩放比例:', this.scale, '缩放后高度:', this.containerScaledHeight);
  550. }
  551. }).exec();
  552. },
  553. downloadZtzdfxImg(){
  554. return new Promise(resolve=>{
  555. if (!this.isChartReady) return console.log('图表尚未准备好');
  556. const chartRef = this.$refs.ztzdfxRef;
  557. if (!chartRef) return console.log('无法找到图表组件');
  558. chartRef.canvasToTempFilePath({
  559. success: async (res) => {
  560. const imgUrl = await this.uploadFilePromise(res.tempFilePath);
  561. console.log(imgUrl,'imgUrl');
  562. resolve(imgUrl)
  563. },
  564. fail: (err) => {
  565. console.log('生成图片失败:', err);
  566. }
  567. });
  568. })
  569. },
  570. uploadFilePromise(url) {
  571. return new Promise((resolve, reject) => {
  572. let a = uni.uploadFile({
  573. url: BaseApi+'/uploadFile',
  574. filePath: url,
  575. name: 'file',
  576. success: (res) => {
  577. setTimeout(() => {
  578. let data = JSON.parse(res.data)
  579. if(data&&data.code===0){
  580. resolve(data.data);
  581. }else this.$showToast(data?.msg)
  582. }, 1000);
  583. },
  584. fail: err =>{
  585. resolve('');
  586. }
  587. });
  588. });
  589. },
  590. async initZtzdfxChart() {
  591. let xdata = ['宗旨与动机', '外部流程、系统与结构', '关系', '内部流程、系统与结构', '学习', '领导力'].reverse();
  592. let yvalue = [40, 25, 30, 35, 33, 47].reverse();
  593. let ycolor = [['#771E6A','#771E6A'],['#009191','#009191'],['#FFD750','#FFD750'],['#4EB2B2','#4EB2B2'],['#AFCDF5','#AFCDF5'],['#002846','#002846']].reverse();
  594. let yData = yvalue.map((v, i) => {
  595. return {
  596. value: v,
  597. itemStyle: {
  598. color: new echarts.graphic.LinearGradient(
  599. 0, 0, 1, 0,
  600. [
  601. { offset: 0, color: ycolor[i][0] },
  602. { offset: 1, color: ycolor[i][1] }
  603. ]
  604. ),
  605. borderRadius: [0, 4, 4, 0]
  606. }
  607. };
  608. });
  609. const chart = await this.$refs.ztzdfxRef.init(echarts);
  610. let option = {
  611. grid: {
  612. left: '2%',
  613. right: '10%',
  614. bottom: '0%',
  615. top: '10%',
  616. containLabel: true
  617. },
  618. xAxis: {
  619. type: 'value',
  620. boundaryGap: [0, 0.01],
  621. splitLine: {
  622. show: false
  623. },
  624. // 隐藏X轴轴线和标签,使图表更干净
  625. axisLine: {
  626. show: false
  627. },
  628. axisLabel: {
  629. show: true,
  630. color: '#193D59',
  631. fontSize: 10*this.scale // X轴刻度值也不显示
  632. }
  633. },
  634. yAxis: {
  635. type: 'category',
  636. data: xdata,
  637. axisLine: {
  638. show: true, // 设置为 true 来显示Y轴的轴线
  639. lineStyle: {
  640. color: '#ECEEF5',
  641. width: 1
  642. }
  643. },
  644. // 隐藏Y轴上的小刻度线(如果你想保留可以设为true)
  645. axisTick: {
  646. show: false
  647. },
  648. axisLabel: {
  649. color: '#193D59',
  650. fontSize: 10*this.scale
  651. }
  652. },
  653. series: [
  654. {
  655. type: 'bar',
  656. barWidth: `${8*this.scale}px`,
  657. data: yData
  658. }
  659. ]
  660. };
  661. chart.setOption(option);
  662. this.isChartReady = true;
  663. this.$nextTick(() => {
  664. this.calculatePdfContainerHeight();
  665. });
  666. },
  667. }
  668. };
  669. </script>
  670. <style scoped lang="scss">
  671. .offscreen-canvas {
  672. position: fixed;
  673. top: -9999px;
  674. left: -9999px;
  675. }
  676. .pdf_btn{
  677. padding: 15rpx 20rpx;
  678. border-radius: 20rpx;
  679. font-size: 28rpx;
  680. color: #FFFFFF;
  681. background: #189B9B;
  682. position: fixed;
  683. right: 30rpx;
  684. bottom: 100rpx;
  685. z-index: 1000;
  686. }
  687. .page-wrappe{
  688. width: 100%;
  689. background: #FFFFFF;
  690. overflow-x: hidden;
  691. overflow-y: auto;
  692. .pdf-container{
  693. width: 630px;
  694. padding: 0 20rpx;
  695. box-sizing: border-box;
  696. transform-origin: top left;
  697. }
  698. }
  699. @import '../static/pdf.scss';
  700. </style>