| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- <template>
- <div class="share">
- <div class="share-content" ref="shareContent" v-if="!canvasImageUrl">
- <div class="header">
- <div class="left-icon">
- <img :src="require('@/assets/shareLeft.png')" />
- </div>
- <div class="right-icon">
- <img :src="require('@/assets/shareRight.png')" />
- </div>
- </div>
- <div class="summaryDay">
- <div class="title-box">
- <div class="title">
- <div class="name">{{ reportTarget.nickName }}的日报</div>
- <div class="date">
- {{
- reportTarget.commitTime
- ? formatChineseDate(reportTarget.commitTime.split(' ')[0])
- : ''
- }}
- </div>
- </div>
- </div>
- <template
- v-for="(item, index) in reportTarget.reportContents"
- v-if="reportTarget.reportContents && reportTarget.reportContents.length > 0">
- <div :class="['text', 'text' + index]">{{ filterText(index) }}</div>
- <div class="content">{{ item.dayContent }}</div>
- </template>
- <div class="text">今日拜访照片</div>
- <div class="content-photos">
- <template v-for="item in photosData">
- <template v-for="item1 in item.photos" v-if="item1.base64Url">
- <img
- :src="'data:image/jpg;base64,' + item1.base64Url"
- crossorigin="anonymous"
- referrerpolicy="no-referrer"
- alt=""
- style="display: block" />
- </template>
- </template>
- <!-- <template v-for="item in photosData">
- <img
- :src="'data:image/jpg;base64,' + item"
- crossorigin="anonymous"
- referrerpolicy="no-referrer"
- alt=""
- style="display: block" />
- </template> -->
- </div>
- </div>
- <div class="footerShare">
- <div class="QRcodes" ref="QRcodes">
- <div class="logo">
- <img :src="require('@/assets/logo1.png')" />
- </div>
- </div>
- <div class="right-text">
- <div>长按识别二维码</div>
- <div>查看详情&点评</div>
- </div>
- </div>
- </div>
- <div class="html2canvasBox">
- <div class="closeShare" @click="closeShare"><van-icon name="close" /></div>
- <div id="html2canvas" ref="html2canvas">
- <div class="scroll-container">
- <img :src="canvasImageUrl" width="100%" />
- </div>
- </div>
- </div>
- </div>
- </template>
- <script>
- import html2canvas from 'html2canvas';
- import QRCode from 'qrcodejs2';
- import { getReportImg } from '@/api/index';
- export default {
- name: 'share',
- props: {
- reportTarget: {
- type: Object,
- default() {
- return {};
- },
- },
- reportId: {
- type: [String, Number],
- },
- },
- data() {
- return {
- canvasImageUrl: '',
- retryCount: 0,
- photosData: [],
- };
- },
- created() {
- this.canvasImageUrl = '';
- },
- mounted() {
- this.toastLoading(0, '生成中...', true);
- // 二维码
- this.creatQrCode();
- getReportImg({ reportId: this.reportId }).then((res) => {
- if (res.data) {
- this.photosData = res.data;
- this.$nextTick(async () => {
- await this.htmlToCanvas();
- });
- }
- });
- // 使用Promise.all确保所有图片加载完成
- // const photos = this.reportTarget.photos;
- // const photoPromises = [];
- // // 收集所有图片转换的Promise
- // photos.forEach((item) => {
- // item.photos.forEach((photo) => {
- // photoPromises.push(this.imageUrlToBase64(photo.fileUrl));
- // });
- // });
- // // 等待所有图片加载完成
- // Promise.all(photoPromises)
- // .then((photosArr) => {
- // this.photosData = photosArr;
- // this.$nextTick(async () => {
- // // await this.htmlToCanvas();
- // });
- // })
- // .catch((error) => {
- // console.error('图片加载失败:', error);
- // this.$toast('图片加载失败,请检查网络');
- // });
- // console.log(photos);
- // 优化touch事件处理
- this.setupTouchHandlers();
- },
- methods: {
- //异步执行
- imageUrlToBase64(imageUrl) {
- return new Promise((resolve, reject) => {
- //一定要设置为let,不然图片不显示
- let image = new Image();
- //解决跨域问题
- image.setAttribute('crossOrigin', 'anonymous');
- image.src = imageUrl;
- //image.onload为异步加载
- image.onload = () => {
- var canvas = document.createElement('canvas');
- canvas.width = image.width;
- canvas.height = image.height;
- var context = canvas.getContext('2d');
- context.drawImage(image, 0, 0, image.width, image.height);
- var quality = 1;
- //这里的dataurl就是base64类型
- let dataURL = canvas.toDataURL('image/png', quality); //使用toDataUrl将图片转换成jpeg的格式,不要把图片压缩成png,因为压缩成png后base64的字符串可能比不转换前的长!
- resolve(dataURL);
- };
- image.onerror = () => {
- reject(new Error('图像加载失败'));
- };
- });
- },
- filterText(index) {
- if (index == 0) {
- return '明日工作计划';
- } else {
- return '今日机会与挑战总结';
- }
- },
- htmlToCanvas() {
- html2canvas(this.$refs.shareContent, {
- scale: window.devicePixelRatio || 1,
- useCORS: true,
- allowTaint: false,
- backgroundColor: null,
- logging: false, // 关闭日志提升性能
- onclone: (clonedDoc) => {
- // 确保克隆的DOM保持原始样式
- clonedDoc.getElementById('html2canvas').style.overflow = 'auto';
- },
- })
- .then((canvas) => {
- this.toastLoading().clear();
- let imageUrl = canvas.toDataURL('image/png');
- this.canvasImageUrl = imageUrl;
- // 图片加载完成后设置滚动容器高度
- const img = new Image();
- img.onload = () => {
- const scrollContainer = this.$refs.html2canvas.querySelector('.scroll-container');
- // 根据图片实际高度设置容器高度(增加20px缓冲)
- // 根据设备像素比调整图片高度
- // 使用实际渲染高度而非原始图片高度
- const imgHeight = img.offsetHeight + 20;
- // 设置容器最小高度保证内容显示,同时允许自动扩展
- scrollContainer.style.minHeight = `${imgHeight}px`;
- // 保持父容器为可见滚动
- scrollContainer.parentElement.style.overflow = 'visible';
- // 强制浏览器重排
- scrollContainer.style.display = 'none';
- scrollContainer.offsetHeight; // 触发重排
- scrollContainer.style.display = 'block';
- // 添加移动端滚动优化
- requestAnimationFrame(() => {
- scrollContainer.style.overflow = 'auto';
- scrollContainer.style.overflowScrolling = 'touch';
- scrollContainer.style.webkitOverflowScrolling = 'touch';
- scrollContainer.style.overscrollBehavior = 'contain';
- scrollContainer.style.touchAction = 'pan-y';
- });
- // 重新绑定触摸事件
- this.setupTouchHandlers();
- };
- img.src = imageUrl;
- this.$emit('setShareImg', true);
- })
- .catch((error) => {
- this.toastLoading().clear();
- console.error('html2canvas error:', error);
- this.$toast('生成分享图失败,请稍后重试');
- });
- },
- creatQrCode() {
- let devText = 'https://suishenbangtest.nipponpaint.com.cn/homeIndex';
- let proText = 'https://suishenbang.nipponpaint.com.cn/homeIndex';
- let text = process.env.NODE_ENV == 'development' ? devText : proText;
- var qrcode = new QRCode(this.$refs.QRcodes, {
- text: text + '?reportId=' + this.reportId || '', // 需要转换为二维码的内容
- colorDark: '#000000',
- colorLight: '#ffffff',
- correctLevel: QRCode.CorrectLevel.H,
- width: 50,
- height: 50,
- });
- },
- closeShare() {
- this.canvasImageUrl = '';
- this.$emit('setShareImg', false);
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .share {
- width: 100%;
- height: 100%;
- overflow: hidden;
- // position: fixed;
- // top: 0;
- // z-index: -1;
- // overflow-y: auto;
- .share-content {
- background: url('../assets/shareBack.png') no-repeat center center;
- background-size: cover;
- background-attachment: local;
- width: 100%;
- // height: 100%;
- padding: vw(30);
- position: absolute;
- // z-index: -1;
- padding-bottom: vw(190);
- top: 0;
- z-index: -1;
- }
- .header {
- display: flex;
- justify-content: space-between;
- .left-icon {
- width: vw(114);
- height: vw(71);
- }
- .right-icon {
- width: vw(230);
- height: vw(78);
- }
- img {
- width: 100%;
- height: 100%;
- }
- }
- .title-box {
- display: flex;
- justify-content: center;
- align-items: center;
- position: absolute;
- top: -6px;
- left: 50%;
- margin-left: vw(-169);
- .title {
- color: #600d0e;
- font-size: vw(36);
- font-weight: bold;
- // margin-top: vw(81);
- // width: vw(338);
- // height: vw(115);
- text-align: center;
- background: url('../assets/titleBack.png') no-repeat;
- background-size: 100% 100%;
- padding: vw(20) vw(30);
- }
- }
- .summaryDay {
- background: url('../assets/summaryDay.png') no-repeat;
- background-size: 100%;
- padding: vw(24);
- position: relative;
- margin-top: vw(55);
- border-radius: 0 0 vw(35) vw(35);
- .text {
- background: url('../assets/textBack.png') no-repeat;
- width: 100%;
- height: vw(94);
- background-size: cover;
- color: #7d0207;
- font-size: vw(36);
- text-align: center;
- line-height: vw(94);
- // margin-top: vw(152);
- // margin-bottom: vw(45);
- margin: vw(35) 0;
- font-weight: bold;
- }
- .text0 {
- margin-top: vw(152);
- }
- .content {
- font-size: vw(28);
- font-weight: bold;
- color: #ffff;
- line-height: vw(63);
- white-space: pre-wrap;
- }
- .content-photos {
- display: flex;
- flex-wrap: wrap;
- // justify-content: space-between;
- img {
- width: vw(188);
- height: vw(188);
- margin-bottom: 15px;
- margin-right: 15px;
- background-color: rgba(255, 255, 255, 0.1);
- }
- }
- }
- .footerShare {
- display: flex;
- // align-items: center;
- justify-content: center;
- margin-top: vw(100);
- .right-text {
- margin-left: vw(12.5);
- display: flex;
- flex-direction: column;
- justify-content: space-between;
- div {
- color: #ffffff;
- font-size: vw(24);
- font-weight: bold;
- }
- }
- }
- .html2canvasBox {
- width: 100%;
- height: 100%;
- min-height: 100vh;
- overflow: hidden !important;
- display: flex;
- flex-direction: column;
- .closeShare {
- position: absolute;
- z-index: 1;
- right: 10px;
- top: 10px;
- color: #fff;
- .van-icon {
- font-size: 20px;
- }
- }
- }
- #html2canvas {
- width: 100%;
- height: 100%;
- position: static; /* 改为静态定位 */
- .scroll-container {
- width: 100%;
- height: 100%;
- flex: 1;
- overflow-y: auto !important;
- -webkit-overflow-scrolling: touch;
- overscroll-behavior: contain;
- touch-action: pan-y;
- // padding: 20px 0;
- box-sizing: border-box;
- /* 修复iOS弹性滚动 */
- overflow-scrolling: touch;
- max-height: 100vh; /* 添加最大高度限制 */
- position: relative; /* 修复定位上下文 */
- img {
- width: 100%;
- display: block;
- }
- }
- }
- }
- </style>
- <style lang="scss">
- .share {
- .QRcodes {
- width: vw(87);
- height: vw(87);
- margin-right: vw(12.5);
- position: relative;
- .logo {
- position: absolute;
- top: vw(43);
- left: vw(43);
- margin: vw(-15) 0 0 vw(-15);
- z-index: 3;
- img {
- width: vw(30);
- height: vw(30);
- }
- }
- img {
- width: 100%;
- height: 100%;
- }
- }
- }
- </style>
|