share copy.vue 12 KB

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