share.vue 16 KB

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