limitSale.js
6.58 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
import wx from '../../utils/wx';
import helper from '../../utils/helper';
import util from '../../utils/util';
import Yas from '../../common/yas';
import config from '../../common/config';
import saleModel from '../../models/activity/sale/index';
let systemInfo = wx.getSystemInfoSync();
let scale = systemInfo.windowWidth / 750;
let yas;
let app = getApp();
import {
getAuthorization
} from '../../common/login';
Page({
data: {
id: 0,
qrType: 0,
snapWidth: 526 * scale,
snapHeight: 936 * scale,
canvasSrc: ''
},
onLoad({id, qrType}) {
yas = new Yas(app);
this.setData({
id,
qrType
});
},
onShow() {
this.detail(this.data.id);
yas.pageOpenReport(); // 进入页面后 上报
},
detail(id) {
saleModel.queryLimitActivityDetail({id}).then(res => {
this.drawCard(res.data);
});
},
drawCard: function(data = {}) {
return Promise.all([
wx.getImageInfo({
src: helper.imgView(data.pic, 324, 202, 1).replace('http://', 'https://')
}),
wx.getImageInfo({
src: `${config.domains.api}/wechat/miniapp/img-check.jpg?param={"id":${this.data.id}}&miniQrType=${this.data.qrType}&miniapp_type=${config.miniapp_type}` // eslint-disable-line
}).catch(() => {
return Promise.resolve({});
})
]).then(res => {
let ctx = wx.createCanvasContext('cardCanvas', this);
ctx.setFillStyle('white');
ctx.fillRect(0, 0, this.data.snapWidth, this.data.snapHeight);
// 线框
ctx.setStrokeStyle('#e0e0e0');
let innerRectWidth = 460 * scale;
let logoWidth = 176 * scale;
let logoHeight = 60 * scale;
let rectLeft = (this.data.snapWidth - innerRectWidth) / 2;
let logoTop = 42 * scale;
ctx.drawImage(
'/static/images/logo@3x.png',
(this.data.snapWidth - logoWidth) / 2,
logoTop,
logoWidth,
logoHeight
);
logoTop += logoHeight + 42 * scale;
ctx.strokeRect(rectLeft, logoTop, innerRectWidth, 605 * scale);
ctx.setFillStyle('#222222');
ctx.setFontSize(28 * scale);
let lines = this.calWrapLines(
ctx,
`限定发售 | ${data.limitActivityName}`,
400 * scale
);
this.drawWrapLines(ctx, lines, 66 * scale, 470 * scale, 40 * scale);
ctx.drawImage(
'/static/images/snapshoot/clock.png',
66 * scale,
604 * scale,
20 * scale,
20 * scale
);
ctx.setFillStyle('#444444');
ctx.setFontSize(20 * scale);
ctx.fillText(`${util.dateFormat(data.onsellTime, 'YYYY年MM月DD日 HH:mm')}发售`, 93 * scale, (603 + 20) * scale);
ctx.drawImage(
'/static/images/snapshoot/address.png',
66 * scale,
(650 + 1) * scale,
20 * scale,
20 * scale
);
ctx.setFillStyle('#444444');
ctx.setFontSize(20 * scale);
ctx.fillText(data.storeAddress || '', 93 * scale, (650 + 20) * scale);
ctx.drawImage(
res[0].path,
rectLeft, logoTop, innerRectWidth, 282 * scale
);
if (res[1].path) {
ctx.drawImage(
res[1].path,
rectLeft, 770 * scale, 112 * scale, 112 * scale
);
}
ctx.setFillStyle('#444444');
ctx.setFontSize(20 * scale);
ctx.fillText('长按识别图中小程序二维码', 170 * scale, 800 * scale);
ctx.setFillStyle('#b0b0b0');
ctx.setFontSize(16 * scale);
ctx.fillText('进入MKT限定发售频道!全球超罕', 170 * scale, 832 * scale);
ctx.setFillStyle('#b0b0b0');
ctx.setFontSize(16 * scale);
ctx.fillText('潮流尖货一手掌握!!', 170 * scale, 852 * scale);
ctx.draw(true, () => {
return wx.canvasToTempFilePath({
x: 0,
y: 0,
width: this.data.snapWidth,
height: this.data.snapHeight,
destWidth: 750,
destHeight: 1332,
quality: 1.0,
canvasId: 'cardCanvas'
}).then(dres => {
this.setData({
canvasSrc: dres.tempFilePath
});
});
});
});
},
calWrapLines(ctx, text = '', lineMaxW = 400) {
let lines = [''];
let charsArr = text.split('');
let len, lineStr, aftAdd;
charsArr.forEach(char => {
len = lines.length;
lineStr = lines[len - 1];
aftAdd = (ctx.measureText(lineStr + char)).width;
if (len < 3) {
if (aftAdd <= lineMaxW) {
lines[len - 1] = lineStr + char;
} else {
lines.push(char);
}
} else if (len === 3) {
aftAdd = (ctx.measureText(lineStr + char + '...')).width;
if (aftAdd <= lineMaxW) {
lines[2] = lineStr + char;
} else {
lines[2] = lineStr + '...';
lines.push('');
}
}
});
return lines.splice(0, 3);
},
drawWrapLines(ctx, lines, startX, startY, lineHeight) {
lines.forEach((line, idx) => {
ctx.setFillStyle('#444444');
ctx.font = `${28 * scale}px sans-serif`;
ctx.fillText(line, startX, startY + lineHeight * idx);
});
},
save: function() {
return getAuthorization('scope.writePhotosAlbum')
.then(() => {
return wx.saveImageToPhotosAlbum({
filePath: this.data.canvasSrc
}).then(() => {
wx.showToast({
title: '图片已经保存',
icon: 'success',
duration: 3000
});
setTimeout(() => {
wx.navigateBack();
}, 1000);
});
});
},
cancel() {
wx.navigateBack();
}
});