poster.js
6.48 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
import Poster from '../../miniprogram_dist/poster/poster';
let app = getApp();
Page({
data: {
isOverShare: true,
imageUrl: "", // 海报图片
wxShareTitle: "", // 分享标题
wxCodePath: "", // 微信二维码参数地址,分享链接公用
wxCodeUrl: "", // 微信二维码图片地址
wishList: [],
},
onShareAppMessage(res) {
if (res.from === 'button') {
// 来自页面内转发按钮
}
let userInfo = app.globalData.userInfo;
let billCode = app.globalData.indexInfo.wishBillCode;
let title = `${userInfo.nickname}正在参加丸美眼霜节心愿单活动,需要你的倾情相助!`;
let path = `/pages/coop/coop?code=${billCode}&s=share`
return {
title,
path,
imageUrl: 'https://kdcdn.oss-cn-shenzhen.aliyuncs.com/weapp/marubi/wish-list/share.png'
}
},
onLoad(options) {
// this.setData({
// wishList: wishList
// })
app.queryIndex({
auth: true,
loading: false,
}).then((result) => {
console.log("result:", result);
wx.showLoading({
title: '海报在修图中···”',
});
this.initData();
})
},
initData() {
// 设置 分享链接,分享文案
let nickname = app.globalData.userInfo && app.globalData.userInfo.nickname || "";
let billCode = app.globalData.indexInfo.wishBillCode;
let wxShareTitle = nickname + `正在参加丸美眼霜节心愿单活动,需要你的倾情相助!`;
let wxCodePath = `/pages/coop/coop?code=${billCode}&s=share`
this.setData({
wxCodePath: wxCodePath,
wxShareTitle: wxShareTitle
})
// 获取小程序码
app.post({
loading: false,
url: app.api.wxacodeGet,
data: {
path: encodeURIComponent(wxCodePath)
}
}).then((result) => {
this.setData({
wxCodeUrl: result
})
// 获取海报数据
let posterData = this.getPosterConfig();
// 绘制设置海报
this.onCreatePoster(posterData);
})
// // 获取海报数据
// let posterData = this.getPosterConfig();
// // 绘制设置海报
// this.onCreatePoster(posterData);
},
onPosterSuccess(e) {
console.log("onPosterSuccess");
wx.hideLoading();
const {
detail
} = e;
console.log("detail:", detail)
this.setData({
imageUrl: detail
})
},
onPosterFail(err) {
wx.hideLoading();
console.error(err);
},
/**
* 查看图片
*/
onPreviewImage() {
let imageUrl = this.data.imageUrl
console.log("imageUrl:", imageUrl);
wx.previewImage({
current: imageUrl,
urls: [imageUrl]
})
},
/**
* 保存图片到本地
*/
saveImageToPhotosAlbum() {
let _this = this;
wx.saveImageToPhotosAlbum({
filePath: _this.data.imageUrl,
success(res) {
wx.showToast({
title: '海报保存成功',
icon: 'success'
});
},
fail(err) {
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.writePhotosAlbum']) {
// 未授权
wx.showModal({
title: '提示',
content: '小程序请求访问相册权限',
confirmText: '前往授权',
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {}
})
} else if (res.cancel) {}
}
})
}
}
})
}
})
},
/**
* 异步生成海报
*/
onCreatePoster(posterConfig) {
console.log("posterConfig:", posterConfig);
this.setData({
posterConfig: posterConfig
}, () => {
Poster.create(true); // 入参:true为抹掉重新生成
});
},
// 获取海报数据
getPosterConfig() {
// 合成图片需要的数据
let {
userInfo,
posterWishList,
} = app.globalData;
let codeUrl = this.data.wxCodeUrl;
console.log("posterWishList:", posterWishList);
let images = [
// 底图
{
width: 700,
height: 921,
x: 0,
y: 0,
url: '../../image/draw/draw-c1.png',
},
// 头像
{
width: 102,
height: 102,
x: 35,
y: 779,
borderRadius: 102,
url: userInfo.avatar,
},
// 小程序码
{
width: 118,
height: 118,
x: 560,
y: 780,
url: codeUrl,
zIndex: 14
}
];
let texts = [{
x: 165,
y: 810,
width: 360,
lineHeight: 32,
fontSize: 26,
lineNum: 3,
baseLine: 'middle',
// text: `${userInfo.nickname}正在参加丸美眼霜节心愿单活动,需要你的倾情相助!`,
text: `我正在参加丸美集弹力值领心愿礼,需要你的倾情相助!`,
color: '#666666',
zIndex: 111
}];
let originPoint = {
x: 55,
y: 160,
}
// 产品居中修正
let productLength = posterWishList.length;
let productSpace = 215;
if (productLength == 1) {
originPoint.x = (700 - 165) / 2
} else if (productLength == 2) {
productSpace = 250;
originPoint.x = (700 - (200 * 2)) / 2
}
posterWishList.forEach((element, index) => {
// 产品背景
let egb = {
width: 165,
height: 165,
x: originPoint.x + (productSpace * index),
y: originPoint.y,
url: '../../image/draw/draw-c2.png',
}
// 产品图
let product = {
width: 165,
height: 165,
x: originPoint.x + (productSpace * index),
y: originPoint.y,
url: '../../image/prize/prize-' + element.tag + '.png',
}
let productDesc = {
x: originPoint.x + (productSpace * index),
y: originPoint.y + 165 + 12,
width: 165,
fontSize: 24,
lineHeight: 28,
lineNum: 3,
baseLine: 'top',
text: `${element.prizeName}`,
color: '#bc3239',
zIndex: 111
}
images.push(egb);
images.push(product);
texts.push(productDesc);
});
let posterData = {
width: 700,
height: 921,
debug: false,
blocks: [],
images: images,
lines: [],
texts: texts,
}
return posterData;
},
// 子组件事件
evtcomp(evt) {
let {
name,
data
} = evt.detail;
switch (name) {
case "_evt_hide":
break;
default:
break;
}
},
})