share.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <div class="share">
  3. <div class="share-content" ref="shareContent" v-if="!canvasImageUrl">
  4. <div class="header">
  5. <div class="left-icon">
  6. <img :src="require('@/assets/shareLeft.png')" />
  7. </div>
  8. <div class="right-icon">
  9. <img :src="require('@/assets/shareRight.png')" />
  10. </div>
  11. </div>
  12. <!-- 点评 -->
  13. <!-- <template v-for="item in reportTarget.reportRemarks"> -->
  14. <div class="comment summaryDay" v-if="reportRemarksIndex != -1">
  15. <div class="title-box">
  16. <div class="title">
  17. <div class="name">
  18. {{ reportTarget.reportRemarks[reportRemarksIndex].nickName }}点评
  19. </div>
  20. <div class="date">
  21. {{
  22. reportTarget.reportRemarks[reportRemarksIndex].createTime
  23. ? formatChineseDate(
  24. reportTarget.reportRemarks[reportRemarksIndex].createTime.split(' ')[0]
  25. )
  26. : ''
  27. }}
  28. </div>
  29. </div>
  30. </div>
  31. <div class="commentMessage">
  32. {{ reportTarget.reportRemarks[reportRemarksIndex].remarkContent }}
  33. </div>
  34. </div>
  35. <!-- </template> -->
  36. <!-- 内容 -->
  37. <div class="summaryDay">
  38. <div class="title-box">
  39. <div class="title">
  40. <div class="name">{{ reportTarget.nickName }}的日报</div>
  41. <div class="date">
  42. {{
  43. reportTarget.commitTime
  44. ? formatChineseDate(reportTarget.commitTime.split(' ')[0])
  45. : ''
  46. }}
  47. </div>
  48. </div>
  49. </div>
  50. <!-- <template
  51. v-for="(item, index) in reportTarget.reportContents"
  52. v-if="reportTarget.reportContents && reportTarget.reportContents.length > 0">
  53. <div :class="['text']">{{ filterText(index) }}</div>
  54. <div class="content">{{ item.dayContent }}</div>
  55. </template> -->
  56. <div :class="['text']">今日机会与挑战总结</div>
  57. <div class="content">
  58. {{ reportTarget.reportContents ? reportTarget.reportContents[0].dayContent : '' }}
  59. </div>
  60. <div :class="['text']">明日工作计划</div>
  61. <div class="content">
  62. {{ reportTarget.reportContents ? reportTarget.reportContents[1].dayContent : '' }}
  63. </div>
  64. <div class="text">今日拜访照片</div>
  65. <div class="content-photos">
  66. <template v-for="item in photosData">
  67. <template v-for="item1 in item.photos" v-if="item1.base64Url">
  68. <img
  69. :src="'data:image/jpg;base64,' + item1.base64Url"
  70. crossorigin="anonymous"
  71. referrerpolicy="no-referrer"
  72. alt=""
  73. style="display: block" />
  74. </template>
  75. </template>
  76. <!-- <template v-for="item in photosData">
  77. <img
  78. :src="'data:image/jpg;base64,' + item"
  79. crossorigin="anonymous"
  80. referrerpolicy="no-referrer"
  81. alt=""
  82. style="display: block" />
  83. </template> -->
  84. </div>
  85. </div>
  86. <div class="footerShare">
  87. <div class="QRcodes" ref="QRcodes">
  88. <div class="logo">
  89. <img :src="require('@/assets/logo1.png')" />
  90. </div>
  91. </div>
  92. <div class="right-text">
  93. <div>长按识别二维码</div>
  94. <div>查看详情&点评</div>
  95. </div>
  96. </div>
  97. </div>
  98. <div class="html2canvasBox">
  99. <div class="closeShare" @click="closeShare"><van-icon name="close" /></div>
  100. <div id="html2canvas" ref="html2canvas">
  101. <div class="scroll-container">
  102. <img :src="canvasImageUrl" width="100%" />
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </template>
  108. <script>
  109. import html2canvas from 'html2canvas';
  110. import QRCode from 'qrcodejs2';
  111. import { getReportImg } from '@/api/index';
  112. export default {
  113. name: 'share',
  114. props: {
  115. // 日报数据
  116. reportTarget: {
  117. type: Object,
  118. default() {
  119. return {};
  120. },
  121. },
  122. //日报id
  123. reportId: {
  124. type: [String, Number],
  125. },
  126. // 点评数据
  127. reportRemarksIndex: {
  128. type: Number,
  129. default: -1,
  130. },
  131. },
  132. data() {
  133. return {
  134. canvasImageUrl: '',
  135. retryCount: 0,
  136. photosData: [],
  137. };
  138. },
  139. created() {
  140. this.canvasImageUrl = '';
  141. },
  142. mounted() {
  143. this.toastLoading(0, '生成中...', true);
  144. // 二维码
  145. this.creatQrCode();
  146. getReportImg({ reportId: this.reportId }).then((res) => {
  147. if (res.data) {
  148. this.photosData = res.data;
  149. this.$nextTick(async () => {
  150. await this.htmlToCanvas();
  151. });
  152. }
  153. });
  154. },
  155. methods: {
  156. //异步执行
  157. imageUrlToBase64(imageUrl) {
  158. return new Promise((resolve, reject) => {
  159. //一定要设置为let,不然图片不显示
  160. let image = new Image();
  161. //解决跨域问题
  162. image.setAttribute('crossOrigin', 'anonymous');
  163. image.src = imageUrl;
  164. image.onload = () => {
  165. var canvas = document.createElement('canvas');
  166. canvas.width = image.width;
  167. canvas.height = image.height;
  168. var context = canvas.getContext('2d');
  169. context.drawImage(image, 0, 0, image.width, image.height);
  170. var quality = 1;
  171. //这里的dataurl就是base64类型
  172. let dataURL = canvas.toDataURL('image/png', quality); //使用toDataUrl将图片转换成jpeg的格式,不要把图片压缩成png,因为压缩成png后base64的字符串可能比不转换前的长!
  173. resolve(dataURL);
  174. };
  175. image.onerror = () => {
  176. reject(new Error('图像加载失败'));
  177. };
  178. });
  179. },
  180. filterText(index) {
  181. if (index == 0) {
  182. return '明日工作计划';
  183. } else {
  184. return '今日机会与挑战总结';
  185. }
  186. },
  187. htmlToCanvas() {
  188. html2canvas(this.$refs.shareContent, {
  189. scale: window.devicePixelRatio || 1,
  190. useCORS: true,
  191. allowTaint: false,
  192. backgroundColor: null,
  193. logging: false, // 关闭日志提升性能
  194. onclone: (clonedDoc) => {
  195. // 确保克隆的DOM保持原始样式
  196. clonedDoc.getElementById('html2canvas').style.overflow = 'auto';
  197. },
  198. })
  199. .then((canvas) => {
  200. this.toastLoading().clear();
  201. let imageUrl = canvas.toDataURL('image/png');
  202. this.canvasImageUrl = imageUrl;
  203. // 图片加载完成后设置滚动容器高度
  204. const img = new Image();
  205. img.onload = () => {
  206. const scrollContainer = this.$refs.html2canvas.querySelector('.scroll-container');
  207. // 根据图片实际高度设置容器高度(增加20px缓冲)
  208. // 根据设备像素比调整图片高度
  209. // 使用实际渲染高度而非原始图片高度
  210. const imgHeight = img.offsetHeight + 20;
  211. // 设置容器最小高度保证内容显示,同时允许自动扩展
  212. scrollContainer.style.minHeight = `${imgHeight}px`;
  213. // 保持父容器为可见滚动
  214. scrollContainer.parentElement.style.overflow = 'visible';
  215. // 强制浏览器重排
  216. scrollContainer.style.display = 'none';
  217. scrollContainer.offsetHeight; // 触发重排
  218. scrollContainer.style.display = 'block';
  219. // 添加移动端滚动优化
  220. requestAnimationFrame(() => {
  221. scrollContainer.style.overflow = 'auto';
  222. scrollContainer.style.overflowScrolling = 'touch';
  223. scrollContainer.style.webkitOverflowScrolling = 'touch';
  224. scrollContainer.style.overscrollBehavior = 'contain';
  225. scrollContainer.style.touchAction = 'pan-y';
  226. });
  227. };
  228. img.src = imageUrl;
  229. this.$emit('setShareImg', true);
  230. })
  231. .catch((error) => {
  232. this.toastLoading().clear();
  233. console.error('html2canvas error:', error);
  234. this.$toast('生成分享图失败,请稍后重试');
  235. });
  236. },
  237. creatQrCode() {
  238. // let proText = 'https://suishenbang.nipponpaint.com.cn';
  239. let path =
  240. process.env.VUE_APP_Target +
  241. '/mobile/dailyHistoricalDetails&source=share&reportId=' +
  242. this.reportId || '';
  243. var qrcode = new QRCode(this.$refs.QRcodes, {
  244. text: process.env.VUE_APP_SSB_LINK + '/homeIndex?path=' + path,
  245. colorDark: '#000000',
  246. colorLight: 'rgba(255,255,255,0)', // 完全透明背景
  247. correctLevel: QRCode.CorrectLevel.H,
  248. width: 150, // 固定尺寸保证安卓一致性
  249. height: 150,
  250. margin: 2,
  251. render: 'canvas',
  252. onRenderingEnd: () => {
  253. const canvas = this.$refs.QRcodes.querySelector('canvas');
  254. // 显式设置物理像素尺寸
  255. const dpr = window.devicePixelRatio || 1;
  256. canvas.width = 200 * dpr;
  257. canvas.height = 200 * dpr;
  258. // 设置CSS显示尺寸保持200x200
  259. canvas.style.width = '200px';
  260. canvas.style.height = '200px';
  261. // 优化安卓设备显示
  262. canvas.style.imageRendering = 'pixelated';
  263. canvas.style.transform = 'translateZ(0)'; // 触发硬件加速
  264. canvas.style.backgroundColor = '#fff';
  265. // 调整画布缩放比例
  266. const ctx = canvas.getContext('2d');
  267. ctx.scale(dpr, dpr);
  268. },
  269. });
  270. },
  271. closeShare() {
  272. this.canvasImageUrl = '';
  273. this.$emit('setShareImg', false);
  274. },
  275. },
  276. };
  277. </script>
  278. <style lang="scss" scoped>
  279. .share {
  280. width: 100%;
  281. height: 100%;
  282. overflow: hidden;
  283. background: #fff;
  284. .share-content {
  285. background: url('../assets/shareBack.png') no-repeat center center;
  286. background-size: cover;
  287. background-attachment: local;
  288. width: 100%;
  289. padding: vw(30);
  290. position: absolute;
  291. padding-bottom: vw(190);
  292. top: 0;
  293. z-index: -10;
  294. }
  295. .header {
  296. display: flex;
  297. justify-content: space-between;
  298. .left-icon {
  299. width: vw(114);
  300. height: vw(71);
  301. }
  302. .right-icon {
  303. width: vw(230);
  304. height: vw(78);
  305. }
  306. img {
  307. width: 100%;
  308. height: 100%;
  309. }
  310. }
  311. .title-box {
  312. position: relative;
  313. width: 100%;
  314. height: auto;
  315. display: flex;
  316. justify-content: center;
  317. align-items: center;
  318. top: -6px;
  319. .title {
  320. // width: vw(262);
  321. // height: vw(115);
  322. display: flex;
  323. flex-direction: column;
  324. justify-content: center;
  325. align-items: center;
  326. color: #600d0e;
  327. font-weight: bold;
  328. text-align: center;
  329. // background: url('../assets/titleBack.png') no-repeat;
  330. // background-size: 100% 100%;
  331. padding: vw(20) vw(30);
  332. background-color: #ecdd9a;
  333. border-radius: 6px 6px 25px 25px;
  334. .name {
  335. font-size: vw(36);
  336. }
  337. .date {
  338. font-size: vw(28);
  339. }
  340. }
  341. }
  342. .summaryDay {
  343. background: url('../assets/summaryDay.png') no-repeat;
  344. background-size: 100%;
  345. padding: vw(24);
  346. position: relative;
  347. margin-top: vw(55);
  348. border-radius: 0 0 vw(35) vw(35);
  349. padding-top: 0;
  350. .text {
  351. background: url('../assets/textBack.png') no-repeat;
  352. width: 100%;
  353. height: vw(94);
  354. background-size: cover;
  355. color: #7d0207;
  356. font-size: vw(36);
  357. text-align: center;
  358. line-height: vw(94);
  359. // margin-top: vw(152);
  360. // margin-bottom: vw(45);
  361. margin: vw(35) 0;
  362. font-weight: bold;
  363. }
  364. .content {
  365. font-size: vw(28);
  366. font-weight: bold;
  367. color: #ffff;
  368. line-height: vw(63);
  369. white-space: pre-wrap;
  370. }
  371. .content-photos {
  372. display: flex;
  373. flex-wrap: wrap;
  374. // justify-content: space-between;
  375. img {
  376. width: 30%;
  377. height: vw(188);
  378. margin-bottom: 15px;
  379. margin-right: 5%;
  380. background-color: rgba(255, 255, 255, 0.1);
  381. &:nth-child(3n) {
  382. margin-right: 0;
  383. }
  384. }
  385. }
  386. }
  387. .comment {
  388. .title {
  389. // background: url('../assets/comment.png') no-repeat;
  390. // background-size: 100% 100%;
  391. background-color: #032371;
  392. border-radius: 6px 6px 25px 25px;
  393. color: #e6cd78;
  394. }
  395. .commentMessage {
  396. padding: vw(60) 0;
  397. font-family: PingFang-SC-Medium;
  398. font-size: vw(28);
  399. font-weight: bold;
  400. color: #ffffff;
  401. }
  402. }
  403. .footerShare {
  404. display: flex;
  405. // align-items: center;
  406. justify-content: center;
  407. margin-top: vw(100);
  408. .right-text {
  409. margin-left: vw(12.5);
  410. display: flex;
  411. flex-direction: column;
  412. justify-content: space-between;
  413. padding: vw(10) 0;
  414. div {
  415. color: #ffffff;
  416. font-size: vw(26);
  417. font-weight: bold;
  418. }
  419. }
  420. }
  421. .html2canvasBox {
  422. width: 100%;
  423. height: 100%;
  424. min-height: auto;
  425. min-height: 100vh;
  426. overflow: hidden !important;
  427. display: flex;
  428. flex-direction: column;
  429. .closeShare {
  430. position: absolute;
  431. z-index: 1;
  432. right: 10px;
  433. top: 10px;
  434. color: #fff;
  435. .van-icon {
  436. font-size: 20px;
  437. }
  438. }
  439. }
  440. #html2canvas {
  441. width: 100%;
  442. height: 100%;
  443. position: static; /* 改为静态定位 */
  444. .scroll-container {
  445. width: 100%;
  446. height: 100%;
  447. flex: 1;
  448. overflow-y: auto !important;
  449. -webkit-overflow-scrolling: touch;
  450. overscroll-behavior: contain;
  451. touch-action: pan-y;
  452. // padding: 20px 0;
  453. box-sizing: border-box;
  454. /* 修复iOS弹性滚动 */
  455. overflow-scrolling: touch;
  456. max-height: 100vh; /* 添加最大高度限制 */
  457. position: relative; /* 修复定位上下文 */
  458. img {
  459. width: 100%;
  460. display: block;
  461. }
  462. }
  463. }
  464. }
  465. </style>
  466. <style lang="scss">
  467. .share {
  468. .QRcodes {
  469. width: 150px; /* 固定像素尺寸 */
  470. height: 150px;
  471. margin-right: 12px;
  472. position: relative;
  473. background: #ffffff;
  474. image-rendering: crisp-edges;
  475. image-rendering: -webkit-optimize-contrast; /* 安卓浏览器兼容 */
  476. // padding: 2px;
  477. .logo {
  478. position: absolute;
  479. top: 50%;
  480. left: 50%;
  481. transform: translate(-50%, -50%);
  482. width: 40px;
  483. height: 40px;
  484. img {
  485. width: 100% !important;
  486. height: 100% !important;
  487. object-fit: contain;
  488. }
  489. z-index: 3;
  490. }
  491. img {
  492. width: 100%;
  493. height: 100%;
  494. }
  495. }
  496. }
  497. </style>