share copy.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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" v-if="false">
  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: '#ffffff',
  239. correctLevel: QRCode.CorrectLevel.H,
  240. width: 70,
  241. height: 70,
  242. });
  243. },
  244. closeShare() {
  245. this.canvasImageUrl = '';
  246. this.$emit('setShareImg', false);
  247. },
  248. },
  249. };
  250. </script>
  251. <style lang="scss" scoped>
  252. .share {
  253. width: 100%;
  254. height: 100%;
  255. overflow: hidden;
  256. background: #fff;
  257. .share-content {
  258. background: url('../assets/shareBack.png') no-repeat center center;
  259. background-size: cover;
  260. background-attachment: local;
  261. width: 100%;
  262. padding: vw(30);
  263. position: absolute;
  264. padding-bottom: vw(190);
  265. top: 0;
  266. // z-index: -10;
  267. }
  268. .header {
  269. display: flex;
  270. justify-content: space-between;
  271. .left-icon {
  272. width: vw(114);
  273. height: vw(71);
  274. }
  275. .right-icon {
  276. width: vw(230);
  277. height: vw(78);
  278. }
  279. img {
  280. width: 100%;
  281. height: 100%;
  282. }
  283. }
  284. .title-box {
  285. position: relative;
  286. width: 100%;
  287. height: auto;
  288. display: flex;
  289. justify-content: center;
  290. align-items: center;
  291. top: -6px;
  292. .title {
  293. // width: vw(262);
  294. // height: vw(115);
  295. display: flex;
  296. flex-direction: column;
  297. justify-content: center;
  298. align-items: center;
  299. color: #600d0e;
  300. font-weight: bold;
  301. text-align: center;
  302. // background: url('../assets/titleBack.png') no-repeat;
  303. // background-size: 100% 100%;
  304. padding: vw(20) vw(30);
  305. background-color: #ecdd9a;
  306. border-radius: 6px 6px 25px 25px;
  307. .name {
  308. font-size: vw(36);
  309. }
  310. .date {
  311. font-size: vw(28);
  312. }
  313. }
  314. }
  315. .summaryDay {
  316. background: url('../assets/summaryDay.png') no-repeat;
  317. background-size: 100%;
  318. padding: vw(24);
  319. position: relative;
  320. margin-top: vw(55);
  321. border-radius: 0 0 vw(35) vw(35);
  322. padding-top: 0;
  323. .text {
  324. background: url('../assets/textBack.png') no-repeat;
  325. width: 100%;
  326. height: vw(94);
  327. background-size: cover;
  328. color: #7d0207;
  329. font-size: vw(36);
  330. text-align: center;
  331. line-height: vw(94);
  332. // margin-top: vw(152);
  333. // margin-bottom: vw(45);
  334. margin: vw(35) 0;
  335. font-weight: bold;
  336. }
  337. .content {
  338. font-size: vw(28);
  339. font-weight: bold;
  340. color: #ffff;
  341. line-height: vw(63);
  342. white-space: pre-wrap;
  343. }
  344. .content-photos {
  345. display: flex;
  346. flex-wrap: wrap;
  347. // justify-content: space-between;
  348. img {
  349. width: 30%;
  350. height: vw(188);
  351. margin-bottom: 15px;
  352. margin-right: 5%;
  353. background-color: rgba(255, 255, 255, 0.1);
  354. &:nth-child(3n) {
  355. margin-right: 0;
  356. }
  357. }
  358. }
  359. }
  360. .comment {
  361. .title {
  362. // background: url('../assets/comment.png') no-repeat;
  363. // background-size: 100% 100%;
  364. background-color: #032371;
  365. border-radius: 6px 6px 25px 25px;
  366. color: #e6cd78;
  367. }
  368. .commentMessage {
  369. padding: vw(60) 0;
  370. font-family: PingFang-SC-Medium;
  371. font-size: vw(28);
  372. font-weight: bold;
  373. color: #ffffff;
  374. }
  375. }
  376. .footerShare {
  377. display: flex;
  378. // align-items: center;
  379. justify-content: center;
  380. margin-top: vw(100);
  381. .right-text {
  382. margin-left: vw(12.5);
  383. display: flex;
  384. flex-direction: column;
  385. justify-content: space-between;
  386. padding: vw(10) 0;
  387. div {
  388. color: #ffffff;
  389. font-size: vw(26);
  390. font-weight: bold;
  391. }
  392. }
  393. }
  394. .html2canvasBox {
  395. width: 100%;
  396. height: 100%;
  397. min-height: auto;
  398. min-height: 100vh;
  399. overflow: hidden !important;
  400. display: flex;
  401. flex-direction: column;
  402. .closeShare {
  403. position: absolute;
  404. z-index: 1;
  405. right: 10px;
  406. top: 10px;
  407. color: #fff;
  408. .van-icon {
  409. font-size: 20px;
  410. }
  411. }
  412. }
  413. #html2canvas {
  414. width: 100%;
  415. height: 100%;
  416. position: static; /* 改为静态定位 */
  417. .scroll-container {
  418. width: 100%;
  419. height: 100%;
  420. flex: 1;
  421. overflow-y: auto !important;
  422. -webkit-overflow-scrolling: touch;
  423. overscroll-behavior: contain;
  424. touch-action: pan-y;
  425. // padding: 20px 0;
  426. box-sizing: border-box;
  427. /* 修复iOS弹性滚动 */
  428. overflow-scrolling: touch;
  429. max-height: 100vh; /* 添加最大高度限制 */
  430. position: relative; /* 修复定位上下文 */
  431. img {
  432. width: 100%;
  433. display: block;
  434. }
  435. }
  436. }
  437. }
  438. </style>
  439. <style lang="scss">
  440. .share {
  441. .QRcodes {
  442. width: vw(140);
  443. height: vw(140);
  444. margin-right: vw(12.5);
  445. position: relative;
  446. // padding: 2px;
  447. // background: #fff;
  448. .logo {
  449. position: absolute;
  450. top: vw(70);
  451. left: vw(70);
  452. margin: vw(-17.5) 0 0 vw(-17.5);
  453. z-index: 3;
  454. img {
  455. width: vw(35);
  456. height: vw(35);
  457. }
  458. }
  459. img {
  460. width: 100%;
  461. height: 100%;
  462. }
  463. }
  464. }
  465. </style>