share.vue
6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<!--
* @Date: 2023-12-30 22:43:25
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-04-11 13:29:29
* @FilePath: /fxPark/src/views/fxPark/share.vue
* @Description: 文件描述
-->
<template>
<div class="share-page">
<div v-if="flag" ref="canvasRef" class="share-bg">
<div style="width: 90vw; height: 90vh;position: relative; overflow: hidden;">
<img src="https://cdn.ipadbiz.cn/gy/new_year_2024/-h-%E6%96%B0%E5%B9%B4%E7%AD%BE.png" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); max-width: 100%; max-height: 100%;">
</div>
<div style="text-align: center; position: absolute; top: 27.5vh; left: calc(50% - 10vw); font-size: 0.6rem;color:#000;">交往 · 交流 · 交融</div>
<div class="desc-text" v-for="(item, index) in sloganStyle" :key="index" :style="{ ...item }">{{ stringToArray(slogan)[index] }}</div>
<!-- <img :src="qr_code" :style="{ ...qrCodeStyle }"> -->
<div style="text-align: center; position: absolute; bottom: 11vh; left: calc(50% - 15vw); font-size: 0.8rem;color:#c53447;">收听来自远方的祝福</div>
</div>
<div v-if="imgUrl" class="share-img">
<img :src="imgUrl" alt="" crossOrigin="anonymous" :style="{ width: ref_width, height: ref_height }">
</div>
<div v-if="wish_list.length > 1" class="control-wrapper">
<div class="prev" @click="prevWish">上一个</div>
<div>长按保存到相册分享</div>
<div class="next" @click="nextWish">下一个</div>
</div>
<div v-else class="control-wrapper" style="justify-content: center;">
<div>长按保存到相册分享</div>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { shareWishListAPI } from '@/api/new_year_2024.js'
import { showToast } from 'vant';
import { Cookies, $, _, axios, storeToRefs, mainStore, Toast, useTitle } from '@/utils/generatePackage.js'
//import { } from '@/utils/generateModules.js'
//import { } from '@/utils/generateIcons.js'
//import { } from '@/composables'
// import qr_code from '@images/new_year_2024/http_qr_code.png'
const $route = useRoute();
const $router = useRouter();
useTitle($route.meta.title);
const wish_index = ref(0);
const slogan = ref('');
const wish_list = ref([]);
const canvasRef = ref(null);
const imgUrl = ref('');
const flag = ref(true);
const ref_width = ref('');
const ref_height = ref('');
// TAG: slogan位置配置
const sloganStyle = [{
top: '34vh',
left: '26vw',
fontSize: '14vw'
}, {
top: '34vh',
right: '26vw',
fontSize: '14vw'
}, {
top: '48vh',
left: '26vw',
fontSize: '14vw'
}, {
top: '48vh',
right: '26vw',
fontSize: '14vw'
}];
const qrCodeStyle = ref({
position: 'absolute',
bottom: '15.5vh',
left: '36vw',
width: '19vw',
height: '10vh',
})
onMounted(async () => {
nextTick(() => {
let canvasDom = canvasRef.value;
ref_width.value = canvasDom.offsetWidth + 'px';
ref_height.value = canvasDom.offsetHeight + 'px';
// const screenWidth = window.innerWidth;
// alert(screenWidth); // 输出移动设备屏幕的宽度
});
const { code, data } = await shareWishListAPI();
if (code) {
wish_list.value = data;
if (wish_list.value.length) {
slogan.value = wish_list.value[wish_index.value].slogon;
createImage();
}
}
});
const stringToArray = (str) => {
return str.split('');
}
const prevWish = () => {
if (!wish_index.value) {
showToast('已经是第一张了');
return;
}
wish_index.value -= 1;
slogan.value = wish_list.value[wish_index.value].slogon;
// 生成图片
imgUrl.value = '';
flag.value = true;
createImage();
}
const nextWish = () => {
if (wish_index.value === wish_list.value.length - 1) {
showToast('已经是最后一张了');
return;
}
wish_index.value += 1;
slogan.value = wish_list.value[wish_index.value].slogon;
// 生成图片
imgUrl.value = '';
flag.value = true;
createImage();
}
// 获取像素比
const DPR = () => {
// 获取设备dpi
if (window.devicePixelRatio && window.devicePixelRatio > 1) {
return window.devicePixelRatio * 2
}
// 直接返回高像素比
return 8
}
const createImage = () => {
nextTick(() => {
// 获取要生成图片的 DOM 元素
let canvasDom = canvasRef.value;
const options = {
backgroundColor: '#d21f2c',
// canvas: canvas,
useCORS: true,//配置允许跨域
scale: DPR(),
// windowWidth: document.body.scrollWidth,
// windowHeight: document.body.scrollHeight,
// x: 0,
// y: window.pageYOffset,
// allowTaint: true,
// background: "#d21f2c", // 一定要添加背景颜色,否则出来的图片,背景全部都是透明的
// dpi: 300 // 处理模糊问题
};
// console.log("获取指定的宽高", width, height, canvas);
html2canvas(canvasDom, options)
.then(canvas => {
try {
// 生成图片地址
imgUrl.value = canvas.toDataURL("image/png");
// console.log("canvas.toDataURL('image/png')", this.imgUrl);
flag.value = false;
} catch (e) {
alert("图片跨域,保存失败");
}
})
.catch(error => {
console.error("绘制失败");
console.error(error);
});
});
}
</script>
<script>
import mixin from 'common/mixin';
import html2canvas from "html2canvas";
import wx from 'weixin-js-sdk'
export default {
mixins: [mixin.init],
data() {
return {
}
},
mounted() {
},
methods: {
}
}
</script>
<style lang="less" scoped>
.share-page {
position: relative;
background-color: #d21f2c;
height: 100vh;
width: 100vw;
.share-bg {
position: absolute;
// top: 8vh;
top: 0;
left: 5vw;
width: 90vw;
height: 90vh;
// background-image: url('https://cdn.ipadbiz.cn/gy/new_year_2024/%E6%96%B0%E5%B9%B4%E7%AD%BE@2x.png');
// background-image: url('https://cdn.ipadbiz.cn/gy/new_year_2024/-h-%E6%96%B0%E5%B9%B4%E7%AD%BE.png');
// background-repeat: no-repeat;
// background-size: contain;
// background-position: center;
.desc-text {
color: #c53447;
// font-size: 13vw;
// font-size: 7vh;
// font-size: 3.5rem;
position: absolute;
font-weight: bold;
}
}
.share-img {
position: absolute;
// top: 8vh;
top: 0;
left: 5vw;
}
.control-wrapper {
display: flex;
position: absolute;
bottom: 5vh;
width: 100%;
justify-content: space-between;
color: #FFF;
align-items: center;
.prev {
background: #B41C1E;
border-radius: 0px 33px 33px 0px;
padding: 0.5rem 1rem 0.5rem 0.35rem;
}
.next {
background: #B41C1E;
border-radius: 33px 0px 0px 33px;
padding: 0.5rem 0.35rem 0.5rem 1rem;
}
}
}
</style>