share.vue 13 KB

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