share.vue 18 KB

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