share.vue 12 KB

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