share copy.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. 长按图片可下载、收藏、转发
  109. <!-- <img :src="require('@/assets/shareTips.png')" alt="" /> -->
  110. </div>
  111. <div class="closeShare" @click="closeShare"><van-icon name="close" /></div>
  112. </div>
  113. <div id="html2canvas" ref="html2canvas">
  114. <div class="scroll-container">
  115. <img :src="canvasImageUrl" width="100%" />
  116. </div>
  117. </div>
  118. </div>
  119. </div>
  120. </template>
  121. <script>
  122. import html2canvas from 'html2canvas';
  123. import QRCode from 'qrcodejs2';
  124. import { imgToBase64 } from '@/api/index';
  125. export default {
  126. name: 'share',
  127. props: {
  128. // 日报数据
  129. reportTarget: {
  130. type: Object,
  131. default() {
  132. return {};
  133. },
  134. },
  135. //日报id
  136. reportId: {
  137. type: [String, Number],
  138. },
  139. // 点评数据
  140. reportRemarksIndex: {
  141. type: Number,
  142. default: -1,
  143. },
  144. // 图片转换列表
  145. urlList: {
  146. type: Array,
  147. default() {
  148. return [];
  149. },
  150. },
  151. },
  152. data() {
  153. return {
  154. canvasImageUrl: '',
  155. retryCount: 0,
  156. photosData: [],
  157. zIndex: -1,
  158. };
  159. },
  160. created() {
  161. this.zIndex = -1;
  162. this.canvasImageUrl = '';
  163. },
  164. mounted() {
  165. this.toastLoading(0, '生成中...', true);
  166. // 二维码
  167. this.creatQrCode();
  168. if (this.urlList.length) {
  169. imgToBase64({ urlList: this.urlList }).then((res) => {
  170. if (res.data && res.code == 200) {
  171. this.photosData = res.data;
  172. this.$nextTick(async () => {
  173. await this.htmlToCanvas();
  174. });
  175. } else {
  176. this.$toast(res.msg);
  177. this.$nextTick(async () => {
  178. await this.htmlToCanvas();
  179. });
  180. }
  181. });
  182. } else {
  183. this.$nextTick(async () => {
  184. await this.htmlToCanvas();
  185. });
  186. }
  187. },
  188. methods: {
  189. //异步执行
  190. imageUrlToBase64(imageUrl) {
  191. return new Promise((resolve, reject) => {
  192. //一定要设置为let,不然图片不显示
  193. let image = new Image();
  194. //解决跨域问题
  195. image.setAttribute('crossOrigin', 'anonymous');
  196. image.src = imageUrl;
  197. image.onload = () => {
  198. var canvas = document.createElement('canvas');
  199. canvas.width = image.width;
  200. canvas.height = image.height;
  201. var context = canvas.getContext('2d');
  202. context.drawImage(image, 0, 0, image.width, image.height);
  203. var quality = 1;
  204. //这里的dataurl就是base64类型
  205. let dataURL = canvas.toDataURL('image/png', quality); //使用toDataUrl将图片转换成jpeg的格式,不要把图片压缩成png,因为压缩成png后base64的字符串可能比不转换前的长!
  206. resolve(dataURL);
  207. };
  208. image.onerror = () => {
  209. reject(new Error('图像加载失败'));
  210. };
  211. });
  212. },
  213. filterText(index) {
  214. if (index == 0) {
  215. return '明日工作计划';
  216. } else {
  217. return '今日机会与挑战总结';
  218. }
  219. },
  220. htmlToCanvas() {
  221. html2canvas(this.$refs.shareContent, {
  222. scale: window.devicePixelRatio || 1,
  223. useCORS: true,
  224. allowTaint: false,
  225. backgroundColor: null,
  226. logging: false, // 关闭日志提升性能
  227. onclone: (clonedDoc) => {
  228. // 确保克隆的DOM保持原始样式
  229. clonedDoc.getElementById('html2canvas').style.overflow = 'auto';
  230. },
  231. })
  232. .then((canvas) => {
  233. this.toastLoading().clear();
  234. let imageUrl = canvas.toDataURL('image/png');
  235. this.canvasImageUrl = imageUrl;
  236. // 图片加载完成后设置滚动容器高度
  237. const img = new Image();
  238. img.onload = () => {
  239. const scrollContainer = this.$refs.html2canvas.querySelector('.scroll-container');
  240. // 根据图片实际高度设置容器高度(增加20px缓冲)
  241. // 根据设备像素比调整图片高度
  242. // 使用实际渲染高度而非原始图片高度
  243. const imgHeight = img.offsetHeight + 20;
  244. // 设置容器最小高度保证内容显示,同时允许自动扩展
  245. scrollContainer.style.minHeight = `${imgHeight}px`;
  246. // 保持父容器为可见滚动
  247. scrollContainer.parentElement.style.overflow = 'visible';
  248. // 强制浏览器重排
  249. scrollContainer.style.display = 'none';
  250. scrollContainer.offsetHeight; // 触发重排
  251. scrollContainer.style.display = 'block';
  252. // 添加移动端滚动优化
  253. requestAnimationFrame(() => {
  254. scrollContainer.style.overflow = 'auto';
  255. scrollContainer.style.overflowScrolling = 'touch';
  256. scrollContainer.style.webkitOverflowScrolling = 'touch';
  257. scrollContainer.style.overscrollBehavior = 'contain';
  258. scrollContainer.style.touchAction = 'pan-y';
  259. });
  260. };
  261. img.src = imageUrl;
  262. this.zIndex = 9;
  263. this.$emit('setShareImg', true);
  264. })
  265. .catch((error) => {
  266. this.toastLoading().clear();
  267. console.error('html2canvas error:', error);
  268. this.$toast('生成分享图失败,请稍后重试');
  269. });
  270. },
  271. creatQrCode() {
  272. // let proText = 'https://suishenbang.nipponpaint.com.cn';
  273. let path =
  274. process.env.VUE_APP_Target +
  275. '/mobile/dailyHistoricalDetails&source=share&reportId=' +
  276. this.reportId || '';
  277. var qrcode = new QRCode(this.$refs.QRcodes, {
  278. text: process.env.VUE_APP_SSB_LINK + '/homeIndex?path=' + path,
  279. colorDark: '#000000',
  280. colorLight: 'rgba(255,255,255,0)', // 完全透明背景
  281. correctLevel: QRCode.CorrectLevel.H,
  282. width: 140, // 固定尺寸保证安卓一致性
  283. height: 140,
  284. margin: 2,
  285. render: 'canvas',
  286. onRenderingEnd: () => {
  287. const canvas = this.$refs.QRcodes.querySelector('canvas');
  288. // 显式设置物理像素尺寸
  289. const dpr = window.devicePixelRatio || 1;
  290. canvas.width = 200 * dpr;
  291. canvas.height = 200 * dpr;
  292. // 设置CSS显示尺寸保持200x200
  293. canvas.style.width = '200px';
  294. canvas.style.height = '200px';
  295. // 优化安卓设备显示
  296. canvas.style.imageRendering = 'pixelated';
  297. canvas.style.transform = 'translateZ(0)'; // 触发硬件加速
  298. canvas.style.backgroundColor = '#fff';
  299. // 调整画布缩放比例
  300. const ctx = canvas.getContext('2d');
  301. ctx.scale(dpr, dpr);
  302. },
  303. });
  304. },
  305. closeShare() {
  306. this.canvasImageUrl = '';
  307. this.$emit('setShareImg', false);
  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.png') 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: 140px; /* 固定像素尺寸 */
  536. height: 140px;
  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. .logo {
  544. position: absolute;
  545. top: 50%;
  546. left: 50%;
  547. transform: translate(-50%, -50%);
  548. width: 40px;
  549. height: 40px;
  550. img {
  551. width: 100% !important;
  552. height: 100% !important;
  553. object-fit: contain;
  554. }
  555. z-index: 3;
  556. }
  557. img {
  558. width: 100%;
  559. height: 100%;
  560. }
  561. }
  562. }
  563. </style>