poster.vue
9.3 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<template>
<view>
<view @click="showFen = true">分享海报</view>
<u-popup :show="showFen" :round="10" mode="bottom" @close="close" @open="open">
<view class="topText flexC">立即分享给好友</view>
<view class="centerBox flexJ">
<view class="item flexA">
<button open-type="share"></button>
<image src="/static/WeChat.png" mode="scaleToFill" />
<view>微信好友</view>
</view>
<view class="item flexA" @click="showHandler">
<image src="/static/posterFen.png" mode="scaleToFill" />
<view>分享海报</view>
</view>
</view>
<view class="lineBox"></view>
<view class="bottom flexC" @click="showFen = false">取消</view>
</u-popup>
<view style="margin-top: -9999rpx">
<r-canvas ref="rCanvas"></r-canvas>
</view>
<view v-if="showPoster" class="maskBox flexA" @click="showPoster = false">
<image @click.stop="() => {return}" class="posterImg flexC" :src="tempFilePath" mode="scaleToFill" />
<view class="whiteBtnBox flexC" @click.stop="saveQrCodeHandler">
<image
src="/static/downLoad.png"
mode="scaleToFill"
/>
</view>
<view class="text flexC" @click.stop="saveQrCodeHandler">保存图片</view>
</view>
</view>
</template>
<script>
import rCanvas from '@/components/r-canvas/r-canvas.vue'
export default {
components: { rCanvas },
data() {
return {
showFen: false,
tempFilePath: '',
showPoster: false,
}
},
onReady() {
this.draw()
},
onLoad(options) {
wx.showShareMenu({
withShareTicket: true,
//设置下方的Menus菜单,才能够让发送给朋友与分享到朋友圈两个按钮可以点击
menus: ['shareAppMessage', 'shareTimeline'],
})
},
methods: {
open() {
// console.log('open');
},
close() {
this.showFen = false
// console.log('close');
},
draw() {
this.$nextTick(async () => {
// 初始化
await this.$refs.rCanvas.init({
canvas_id: 'rCanvas',
canvas_height: 456,
canvas_width: 343,
background_color: '#ffffff',
})
// 顶部大图
await this.$refs.rCanvas
.drawImage({
url: '/static/posterTop.png',
x: 0,
y: 0,
w: 343,
h: 214,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 标题
await this.$refs.rCanvas
.drawText({
text: '少儿美术培训班',
x: 116,
y: 238,
font_color: '#000000',
font_size: 16,
font_weight: 700,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 价格
await this.$refs.rCanvas
.drawText({
text: '¥234',
x: 149,
y: 264,
font_color: '#000000',
font_size: 18,
font_weight: 700,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 时间图标
await this.$refs.rCanvas
.drawImage({
url: '/static/time.png',
x: 91,
y: 300,
w: 16,
h: 16,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 时间
await this.$refs.rCanvas
.drawText({
text: '2021-08-09 10:00-12:00',
x: 116,
y: 312,
font_color: '#646566',
font_size: 12,
font_weight: 400,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 地点
await this.$refs.rCanvas
.drawText({
text: '塘沽学校-生态城校区',
x: 116,
y: 344,
font_color: '#646566',
font_size: 12,
font_weight: 400,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 地点图标
await this.$refs.rCanvas
.drawImage({
url: '/static/aabb.png',
x: 91,
y: 330,
w: 16,
h: 16,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 头像
await this.$refs.rCanvas
.drawImage({
url: '/static/headImg.jpg',
x: 18,
y: 384,
w: 44,
h: 44,
border_radius: 22,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 名字
await this.$refs.rCanvas
.drawText({
text: '张美丽',
x: 74,
y: 400,
font_color: '#000000',
font_size: 14,
font_weight: 400,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 推销文字
await this.$refs.rCanvas
.drawText({
text: '长按识别二维码去看好课程',
x: 74,
y: 421,
font_color: '#969799',
font_size: 12,
font_weight: 400,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 二维码
await this.$refs.rCanvas
.drawImage({
url: '/static/qrCode.png',
x: 245,
y: 358,
w: 80,
h: 80,
})
.catch(err_msg => {
uni.showToast({
title: err_msg,
icon: 'none',
})
})
// 生成海报
await this.$refs.rCanvas.draw(res => {
//res.tempFilePath:生成成功,返回base64图片
// 保存图片
this.tempFilePath = res.tempFilePath
// this.$refs.rCanvas.saveImage(res.tempFilePath)
})
})
},
saveQrCodeHandler() {
this.$nextTick(async () => {
uni.showLoading({
title: '生成中',
mask: true,
})
// 生成海报
uni.hideLoading()
uni
.showModal({
title: '提示',
content: '是否下载当前生成的海报?',
success: res => {
if (res.confirm) {
// 保存图片
this.$refs.rCanvas
.saveImage(this.tempFilePath)
.then(() => {
uni.showToast({
title: '保存成功',
icon: 'success',
})
})
.catch(err => {
// this.error(err)
console.log(err)
})
}
},
})
.catch(err => {
this.error(err)
})
})
},
// 分享微信好友
onShareAppMessage() {
// 此处的distSource为分享者的部分信息,需要传递给其他人
let distSource = 'jiejiejie'
if (distSource) {
return {
title: '欢迎使用xxx商城',
type: 0,
path: '/pages/index/poster?id=' + distSource,
summary: '',
imageUrl: this.tempFilePath,
}
}
},
showHandler() {
this.showFen = false
this.showPoster = true
},
},
}
</script>
<style lang="scss">
page {
background: #fff;
}
.topText {
margin: 40rpx 0;
color: rgba(49.98, 50.35, 51, 1);
font-size: 28rpx;
}
.centerBox {
padding: 0 190rpx;
box-sizing: border-box;
> .item {
flex-direction: column;
color: rgba(100, 101, 102, 1);
font-size: 24rpx;
}
image {
width: 96rpx;
height: 96rpx;
border-radius: 50%;
margin-bottom: 16rpx;
}
}
.lineBox {
width: 100%;
height: 16rpx;
background: rgba(247, 248, 250, 1);
margin-top: 32rpx;
}
.bottom {
padding: 26rpx 0 0 0;
box-sizing: border-box;
color: rgba(99.96, 100.71, 102, 1);
font-size: 32rpx;
}
.posterImg {
margin: 60rpx auto 40rpx;
width: 686rpx;
height: 912rpx;
border-radius: 32rpx;
}
.maskBox {
width: 100%;
height: 100vh;
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.5);
flex-direction: column;
}
button {
background: transparent;
width: 96rpx;
height: 96rpx;
border-radius: 50%;
position: absolute;
/* top: 0; */
}
.whiteBtnBox{
width: 112rpx;
height: 112rpx;
border-radius: 50%;
background: #fff;
image{
width: 74rpx;
height: 82rpx;
}
}
.text{
color: rgba(255,255,255,1);
font-size: 28rpx;
font-weight: 700;
margin-top: 16rpx;
}
</style>