share.vue 16 KB

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