@10yun/cv-mobile-ui 0.5.35 → 0.5.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/js-sdk/QuShe-SharerPoster_3.1.8/QS-SharePoster/QRCodeAlg.js +1046 -0
- package/js-sdk/QuShe-SharerPoster_3.1.8/QS-SharePoster/QS-SharePoster.js +1309 -0
- package/js-sdk/QuShe-SharerPoster_3.1.8/QS-SharePoster/QS-SharePoster2.js +1575 -0
- package/js-sdk/QuShe-SharerPoster_3.1.8/QS-SharePoster/app.js +572 -0
- package/js-sdk/QuShe-SharerPoster_3.1.8/QS-SharePoster/app2.js +570 -0
- package/js-sdk/QuShe-SharerPoster_3.1.8/QS-SharePoster/image-tools.js +168 -0
- package/js-sdk/QuShe-SharerPoster_3.1.8/package.json +12 -0
- package/js-sdk/Sansnn-uQRCode/uqrcode.js +1295 -0
- package/js-sdk/ican-H5Api/ican-H5Api.js +669 -0
- package/package.json +1 -1
- package/ui-cv/components/cv-editor-quill/index.js +154 -133
- package/uni-ui/lib/uni-badge/uni-badge.vue +3 -1
- package/uni-ui/lib/uni-list/uni-refresh.wxs +1 -1
- package/uni-ui/lib/uni-load-more/uni-load-more.vue +3 -0
- package/uni-ui/lib/uni-swipe-action-item/uni-swipe-action-item.vue +355 -213
- package/uview-plus/components/u-qrcode/qrcode.js +4 -0
- package/uview-plus/libs/ctocode/date.js +17 -0
- package/uview-plus/libs/ctocode/index.js +19 -0
|
@@ -0,0 +1,1575 @@
|
|
|
1
|
+
import _app from './app.js';
|
|
2
|
+
import QRCodeAlg from './QRCodeAlg.js';
|
|
3
|
+
import { base64ToPath } from './image-tools.js';
|
|
4
|
+
const ShreUserPosterBackgroundKey = 'ShrePosterBackground_'; // 背景图片缓存名称前缀
|
|
5
|
+
const idKey = 'QSSHAREPOSTER_IDKEY'; //drawArray自动生成的idkey
|
|
6
|
+
var nbgScale = 1;
|
|
7
|
+
// export default
|
|
8
|
+
function getSharePoster(obj) {
|
|
9
|
+
let {
|
|
10
|
+
type,
|
|
11
|
+
formData,
|
|
12
|
+
background,
|
|
13
|
+
posterCanvasId,
|
|
14
|
+
backgroundImage,
|
|
15
|
+
reserve,
|
|
16
|
+
textArray,
|
|
17
|
+
drawArray,
|
|
18
|
+
qrCodeArray,
|
|
19
|
+
imagesArray,
|
|
20
|
+
setCanvasWH,
|
|
21
|
+
setCanvasToTempFilePath,
|
|
22
|
+
canvas2image,
|
|
23
|
+
setDraw,
|
|
24
|
+
bgScale,
|
|
25
|
+
Context,
|
|
26
|
+
_this,
|
|
27
|
+
delayTimeScale,
|
|
28
|
+
drawDelayTime,
|
|
29
|
+
draw
|
|
30
|
+
} = obj;
|
|
31
|
+
return new Promise(async (rs, rj) => {
|
|
32
|
+
try {
|
|
33
|
+
_app.showLoading('正在准备海报数据');
|
|
34
|
+
if (!Context) {
|
|
35
|
+
_app.log('没有画布对象,创建画布对象');
|
|
36
|
+
Context = uni.createCanvasContext(posterCanvasId, _this || null);
|
|
37
|
+
}
|
|
38
|
+
let bgObj;
|
|
39
|
+
if (background && background.width && background.height) {
|
|
40
|
+
bgObj = background;
|
|
41
|
+
} else {
|
|
42
|
+
bgObj = await getShreUserPosterBackground({
|
|
43
|
+
backgroundImage,
|
|
44
|
+
type,
|
|
45
|
+
formData
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
bgScale = bgScale || nbgScale;
|
|
49
|
+
bgObj.width = bgObj.width * bgScale;
|
|
50
|
+
bgObj.height = bgObj.height * bgScale;
|
|
51
|
+
|
|
52
|
+
_app.log('获取背景图信息对象成功:' + JSON.stringify(bgObj));
|
|
53
|
+
const params = {
|
|
54
|
+
bgObj,
|
|
55
|
+
type,
|
|
56
|
+
bgScale,
|
|
57
|
+
getBgObj: function () {
|
|
58
|
+
return params.bgObj;
|
|
59
|
+
},
|
|
60
|
+
setBgObj: function (newBgObj) {
|
|
61
|
+
const n = {
|
|
62
|
+
...params.bgObj,
|
|
63
|
+
...newBgObj
|
|
64
|
+
};
|
|
65
|
+
params.bgObj = n;
|
|
66
|
+
bgObj = n;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
if (imagesArray) {
|
|
70
|
+
if (typeof imagesArray == 'function') imagesArray = imagesArray(params);
|
|
71
|
+
_app.showLoading('正在生成需绘制图片的临时路径');
|
|
72
|
+
_app.log('准备设置图片');
|
|
73
|
+
imagesArray = await setImage(imagesArray);
|
|
74
|
+
_app.hideLoading();
|
|
75
|
+
}
|
|
76
|
+
if (textArray) {
|
|
77
|
+
if (typeof textArray == 'function') textArray = textArray(params);
|
|
78
|
+
textArray = setText(Context, textArray);
|
|
79
|
+
}
|
|
80
|
+
if (qrCodeArray) {
|
|
81
|
+
if (typeof qrCodeArray == 'function') qrCodeArray = qrCodeArray(params);
|
|
82
|
+
_app.showLoading('正在生成需绘制图片的临时路径');
|
|
83
|
+
for (let i = 0; i < qrCodeArray.length; i++) {
|
|
84
|
+
_app.log(i);
|
|
85
|
+
if (qrCodeArray[i].image) qrCodeArray[i].image = await _app.downloadFile_PromiseFc(qrCodeArray[i].image);
|
|
86
|
+
}
|
|
87
|
+
_app.hideLoading();
|
|
88
|
+
}
|
|
89
|
+
if (drawArray) {
|
|
90
|
+
if (typeof drawArray == 'function') {
|
|
91
|
+
drawArray = drawArray(params);
|
|
92
|
+
}
|
|
93
|
+
if (_app.isPromise(drawArray)) {
|
|
94
|
+
drawArray = await drawArray;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (_app.isArray(drawArray) && drawArray.length > 0) {
|
|
98
|
+
// let hasAllInfoCallback = false;
|
|
99
|
+
const addDrawArray = [];
|
|
100
|
+
for (let i = 0; i < drawArray.length; i++) {
|
|
101
|
+
const drawArrayItem = drawArray[i];
|
|
102
|
+
// if (_app.isFn(drawArrayItem.allInfoCallback) && !hasAllInfoCallback)
|
|
103
|
+
// hasAllInfoCallback = true;
|
|
104
|
+
drawArrayItem[idKey] = i;
|
|
105
|
+
let newData;
|
|
106
|
+
let addDraw = false;
|
|
107
|
+
switch (drawArrayItem.type) {
|
|
108
|
+
case 'image':
|
|
109
|
+
newData = await setImage(drawArrayItem);
|
|
110
|
+
break;
|
|
111
|
+
case 'text':
|
|
112
|
+
newData = setText(Context, drawArrayItem, params.bgObj);
|
|
113
|
+
// if (_app.isArray(setTextResult)) {
|
|
114
|
+
// addDraw = true;
|
|
115
|
+
// addDrawArray.push({
|
|
116
|
+
// index: i,
|
|
117
|
+
// items: setTextResult
|
|
118
|
+
// });
|
|
119
|
+
// } else {
|
|
120
|
+
// newData = setTextResult;
|
|
121
|
+
// }
|
|
122
|
+
break;
|
|
123
|
+
case 'qrcode':
|
|
124
|
+
if (drawArrayItem.image)
|
|
125
|
+
newData = {
|
|
126
|
+
image: await _app.downloadFile_PromiseFc(drawArrayItem.image)
|
|
127
|
+
};
|
|
128
|
+
break;
|
|
129
|
+
case 'custom':
|
|
130
|
+
break;
|
|
131
|
+
case 'fillrect':
|
|
132
|
+
break;
|
|
133
|
+
case 'strokeRect':
|
|
134
|
+
break;
|
|
135
|
+
case 'roundStrokeRect':
|
|
136
|
+
break;
|
|
137
|
+
case 'roundFillRect':
|
|
138
|
+
break;
|
|
139
|
+
default:
|
|
140
|
+
_app.log('未识别的类型');
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
if (!addDraw && newData && _app.isObject(newData)) {
|
|
144
|
+
drawArray[i] = {
|
|
145
|
+
...drawArrayItem,
|
|
146
|
+
...newData
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// if (addDrawArray.length) {
|
|
152
|
+
// for (let i = 0; i < addDrawArray.length; i++) {
|
|
153
|
+
// const item = addDrawArray[i];
|
|
154
|
+
// const index = drawArray.findIndex(ite => ite[idKey] == item.index);
|
|
155
|
+
// if (-1 != index) {
|
|
156
|
+
// item.items.forEach((ite, index) => {
|
|
157
|
+
// ite[idKey] = drawArray.length + index;
|
|
158
|
+
// ite.allInfoCallback = null;
|
|
159
|
+
// });
|
|
160
|
+
// drawArray.splice(index, 1, ...item.items)
|
|
161
|
+
// }
|
|
162
|
+
// }
|
|
163
|
+
// }
|
|
164
|
+
_app.log('AllInfoCallback之前', JSON.stringify(drawArray));
|
|
165
|
+
|
|
166
|
+
// if (hasAllInfoCallback) {
|
|
167
|
+
_app.log('----------------hasAllInfoCallback----------------');
|
|
168
|
+
const drawArray_copy = [...drawArray];
|
|
169
|
+
drawArray_copy.sort((a, b) => {
|
|
170
|
+
const a_serialNum =
|
|
171
|
+
!_app.isUndef(a.serialNum) && !_app.isNull(a.serialNum) ? Number(a.serialNum) : Number.NEGATIVE_INFINITY;
|
|
172
|
+
const b_serialNum =
|
|
173
|
+
!_app.isUndef(b.serialNum) && !_app.isNull(b.serialNum) ? Number(b.serialNum) : Number.NEGATIVE_INFINITY;
|
|
174
|
+
return a_serialNum - b_serialNum;
|
|
175
|
+
});
|
|
176
|
+
_app.log('开始for循环');
|
|
177
|
+
|
|
178
|
+
for (let i = 0; i < drawArray_copy.length; i++) {
|
|
179
|
+
const item = {
|
|
180
|
+
...drawArray_copy[i]
|
|
181
|
+
};
|
|
182
|
+
const item_idKey = item[idKey];
|
|
183
|
+
_app.log('item_idKey', item_idKey);
|
|
184
|
+
const ind = drawArray.findIndex((it) => it[idKey] == item_idKey);
|
|
185
|
+
_app.log('ind', ind);
|
|
186
|
+
if (-1 == ind) break;
|
|
187
|
+
if (_app.isFn(item.allInfoCallback)) {
|
|
188
|
+
let newData = item.allInfoCallback({
|
|
189
|
+
drawArray
|
|
190
|
+
});
|
|
191
|
+
_app.log('newData', JSON.stringify(newData));
|
|
192
|
+
if (_app.isPromise(newData)) newData = await newData;
|
|
193
|
+
|
|
194
|
+
if (drawArray[ind].type === 'text' && newData.size) {
|
|
195
|
+
const textLength = countTextLength(Context, {
|
|
196
|
+
text: newData.text || drawArray[ind].text,
|
|
197
|
+
size: newData.size
|
|
198
|
+
});
|
|
199
|
+
newData.textLength = textLength;
|
|
200
|
+
}
|
|
201
|
+
drawArray[ind] = {
|
|
202
|
+
...item,
|
|
203
|
+
...newData
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
_app.log('drawArray[ind]', JSON.stringify(drawArray[ind]));
|
|
207
|
+
if (drawArray[ind].type === 'text') {
|
|
208
|
+
const setLineFeedResult = setLineFeed(Context, drawArray[ind], params.bgObj);
|
|
209
|
+
if (_app.isArray(setLineFeedResult)) {
|
|
210
|
+
setLineFeedResult.forEach((ite, index) => {
|
|
211
|
+
ite[idKey] = drawArray.length + index;
|
|
212
|
+
ite.allInfoCallback = null;
|
|
213
|
+
});
|
|
214
|
+
drawArray.splice(ind, 1, ...setLineFeedResult);
|
|
215
|
+
} else {
|
|
216
|
+
drawArray.splice(ind, 1, setLineFeedResult);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
_app.log('for循环结束');
|
|
221
|
+
_app.log('allInfocallback结束', JSON.stringify(drawArray));
|
|
222
|
+
// }
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
drawArray.sort((a, b) => {
|
|
226
|
+
const a_zIndex = !_app.isUndef(a.zIndex) && !_app.isNull(a.zIndex) ? Number(a.zIndex) : Number.NEGATIVE_INFINITY;
|
|
227
|
+
const b_zIndex = !_app.isUndef(b.zIndex) && !_app.isNull(b.zIndex) ? Number(b.zIndex) : Number.NEGATIVE_INFINITY;
|
|
228
|
+
return a_zIndex - b_zIndex;
|
|
229
|
+
});
|
|
230
|
+
_app.log('params:' + JSON.stringify(params));
|
|
231
|
+
if (setCanvasWH && typeof setCanvasWH == 'function') {
|
|
232
|
+
await new Promise((resolve, reject) => {
|
|
233
|
+
setCanvasWH(params);
|
|
234
|
+
setTimeout(() => {
|
|
235
|
+
resolve();
|
|
236
|
+
}, 50);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
const poster = await drawShareImage({
|
|
240
|
+
Context,
|
|
241
|
+
type,
|
|
242
|
+
posterCanvasId,
|
|
243
|
+
reserve,
|
|
244
|
+
drawArray,
|
|
245
|
+
textArray,
|
|
246
|
+
imagesArray,
|
|
247
|
+
bgObj,
|
|
248
|
+
qrCodeArray,
|
|
249
|
+
setCanvasToTempFilePath,
|
|
250
|
+
setDraw,
|
|
251
|
+
bgScale,
|
|
252
|
+
_this,
|
|
253
|
+
delayTimeScale,
|
|
254
|
+
drawDelayTime,
|
|
255
|
+
canvas2image,
|
|
256
|
+
draw
|
|
257
|
+
});
|
|
258
|
+
_app.hideLoading();
|
|
259
|
+
rs({
|
|
260
|
+
bgObj,
|
|
261
|
+
poster,
|
|
262
|
+
type
|
|
263
|
+
});
|
|
264
|
+
} catch (e) {
|
|
265
|
+
//TODO handle the exception
|
|
266
|
+
rj(e);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function drawShareImage(obj) {
|
|
272
|
+
//绘制海报方法
|
|
273
|
+
let {
|
|
274
|
+
Context,
|
|
275
|
+
type,
|
|
276
|
+
posterCanvasId,
|
|
277
|
+
reserve,
|
|
278
|
+
bgObj,
|
|
279
|
+
drawArray,
|
|
280
|
+
textArray,
|
|
281
|
+
qrCodeArray,
|
|
282
|
+
imagesArray,
|
|
283
|
+
setCanvasToTempFilePath,
|
|
284
|
+
setDraw,
|
|
285
|
+
bgScale,
|
|
286
|
+
_this,
|
|
287
|
+
delayTimeScale,
|
|
288
|
+
drawDelayTime,
|
|
289
|
+
canvas2image,
|
|
290
|
+
draw
|
|
291
|
+
} = obj;
|
|
292
|
+
const params = {
|
|
293
|
+
Context,
|
|
294
|
+
bgObj,
|
|
295
|
+
type,
|
|
296
|
+
bgScale
|
|
297
|
+
};
|
|
298
|
+
delayTimeScale = delayTimeScale !== undefined ? delayTimeScale : 15;
|
|
299
|
+
drawDelayTime = drawDelayTime !== undefined ? drawDelayTime : 100;
|
|
300
|
+
return new Promise((rs, rj) => {
|
|
301
|
+
try {
|
|
302
|
+
_app.showLoading('正在绘制海报');
|
|
303
|
+
_app.log('背景对象:' + JSON.stringify(bgObj));
|
|
304
|
+
if (bgObj && bgObj.path) {
|
|
305
|
+
_app.log('背景有图片路径');
|
|
306
|
+
Context.drawImage(bgObj.path, 0, 0, bgObj.width, bgObj.height);
|
|
307
|
+
} else {
|
|
308
|
+
_app.log('背景没有图片路径');
|
|
309
|
+
if (bgObj.backgroundColor) {
|
|
310
|
+
_app.log('背景有背景颜色:' + bgObj.backgroundColor);
|
|
311
|
+
Context.setFillStyle(bgObj.backgroundColor);
|
|
312
|
+
Context.fillRect(0, 0, bgObj.width, bgObj.height);
|
|
313
|
+
} else {
|
|
314
|
+
_app.log('背景没有背景颜色');
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
_app.showLoading('绘制图片');
|
|
319
|
+
if (imagesArray && imagesArray.length > 0) drawImage(Context, imagesArray);
|
|
320
|
+
|
|
321
|
+
_app.showLoading('绘制自定义内容');
|
|
322
|
+
if (setDraw && typeof setDraw == 'function') setDraw(params);
|
|
323
|
+
|
|
324
|
+
_app.showLoading('绘制文本');
|
|
325
|
+
if (textArray && textArray.length > 0) drawText(Context, textArray, bgObj);
|
|
326
|
+
|
|
327
|
+
_app.showLoading('绘制二维码');
|
|
328
|
+
if (qrCodeArray && qrCodeArray.length > 0) {
|
|
329
|
+
for (let i = 0; i < qrCodeArray.length; i++) {
|
|
330
|
+
drawQrCode(Context, qrCodeArray[i]);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
_app.showLoading('绘制可控层级序列');
|
|
335
|
+
if (drawArray && drawArray.length > 0) {
|
|
336
|
+
for (let i = 0; i < drawArray.length; i++) {
|
|
337
|
+
const drawArrayItem = drawArray[i];
|
|
338
|
+
_app.log('绘制可控层级序列, drawArrayItem:' + JSON.stringify(drawArrayItem));
|
|
339
|
+
switch (drawArrayItem.type) {
|
|
340
|
+
case 'image':
|
|
341
|
+
_app.log('绘制可控层级序列, 绘制图片');
|
|
342
|
+
drawImage(Context, drawArrayItem);
|
|
343
|
+
break;
|
|
344
|
+
case 'text':
|
|
345
|
+
_app.log('绘制可控层级序列, 绘制文本');
|
|
346
|
+
drawText(Context, drawArrayItem, bgObj);
|
|
347
|
+
break;
|
|
348
|
+
case 'qrcode':
|
|
349
|
+
_app.log('绘制可控层级序列, 绘制二维码');
|
|
350
|
+
drawQrCode(Context, drawArrayItem);
|
|
351
|
+
break;
|
|
352
|
+
case 'custom':
|
|
353
|
+
_app.log('绘制可控层级序列, 绘制自定义内容');
|
|
354
|
+
if (drawArrayItem.setDraw && typeof drawArrayItem.setDraw === 'function') drawArrayItem.setDraw(Context);
|
|
355
|
+
break;
|
|
356
|
+
drawRoundStrokeRect, drawStrokeRect;
|
|
357
|
+
case 'fillRect':
|
|
358
|
+
_app.log('绘制可控层级序列, 绘制填充直角矩形');
|
|
359
|
+
drawFillRect(Context, drawArrayItem);
|
|
360
|
+
break;
|
|
361
|
+
case 'strokeRect':
|
|
362
|
+
_app.log('绘制可控层级序列, 绘制线条直角矩形');
|
|
363
|
+
drawStrokeRect(Context, drawArrayItem);
|
|
364
|
+
break;
|
|
365
|
+
case 'roundStrokeRect':
|
|
366
|
+
_app.log('绘制可控层级序列, 绘制线条圆角矩形');
|
|
367
|
+
drawRoundStrokeRect(Context, drawArrayItem);
|
|
368
|
+
break;
|
|
369
|
+
case 'roundFillRect':
|
|
370
|
+
_app.log('绘制可控层级序列, 绘制填充圆角矩形');
|
|
371
|
+
drawRoundFillRect(Context, drawArrayItem);
|
|
372
|
+
break;
|
|
373
|
+
default:
|
|
374
|
+
_app.log('未识别的类型');
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
_app.showLoading('绘制中');
|
|
380
|
+
_app.log('准备执行draw方法');
|
|
381
|
+
_app.log('Context:', Context);
|
|
382
|
+
if (draw === false) {
|
|
383
|
+
_app.log('draw属性为false,请自行调用canvas实例的draw方法');
|
|
384
|
+
rs();
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const fn = function () {
|
|
388
|
+
let setObj = setCanvasToTempFilePath || {};
|
|
389
|
+
if (setObj && typeof setObj == 'function') setObj = setCanvasToTempFilePath(bgObj, type);
|
|
390
|
+
let canvasToTempFilePathFn;
|
|
391
|
+
const dpr = uni.getSystemInfoSync().pixelRatio;
|
|
392
|
+
const data = {
|
|
393
|
+
// 注释的设置使用uni自己的默认值更为稳定
|
|
394
|
+
// x: 0,
|
|
395
|
+
// y: 0,
|
|
396
|
+
// width: Number(bgObj.width),
|
|
397
|
+
// height: Number(bgObj.height),
|
|
398
|
+
// destWidth: Number(bgObj.width) * dpr,
|
|
399
|
+
// destHeight: Number(bgObj.height) * dpr,
|
|
400
|
+
quality: 0.8,
|
|
401
|
+
fileType: 'jpg',
|
|
402
|
+
...setObj,
|
|
403
|
+
canvasId: posterCanvasId
|
|
404
|
+
};
|
|
405
|
+
if (canvas2image === false) {
|
|
406
|
+
_app.hideLoading();
|
|
407
|
+
return rs({
|
|
408
|
+
setCanvasToTempFilePath: data
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
_app.showLoading('正在输出图片');
|
|
412
|
+
_app.log('canvasToTempFilePath的data对象:' + JSON.stringify(data));
|
|
413
|
+
canvasToTempFilePathFn = function () {
|
|
414
|
+
const toTempFilePathObj = {
|
|
415
|
+
//输出为图片
|
|
416
|
+
...data,
|
|
417
|
+
success(res) {
|
|
418
|
+
_app.hideLoading();
|
|
419
|
+
rs({
|
|
420
|
+
...res,
|
|
421
|
+
setCanvasToTempFilePath: data
|
|
422
|
+
});
|
|
423
|
+
},
|
|
424
|
+
fail(err) {
|
|
425
|
+
_app.hideLoading();
|
|
426
|
+
_app.log('输出图片失败');
|
|
427
|
+
_app.log('输出图片失败:' + JSON.stringify(err));
|
|
428
|
+
rj('输出图片失败:' + JSON.stringify(err));
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
uni.canvasToTempFilePath(toTempFilePathObj, _this || null);
|
|
432
|
+
};
|
|
433
|
+
let delayTime = 0;
|
|
434
|
+
if (qrCodeArray) {
|
|
435
|
+
qrCodeArray.forEach((item) => {
|
|
436
|
+
if (item.text) {
|
|
437
|
+
delayTime += Number(item.text.length);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
if (imagesArray) {
|
|
442
|
+
imagesArray.forEach(() => {
|
|
443
|
+
delayTime += delayTimeScale;
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
if (textArray) {
|
|
447
|
+
textArray.forEach(() => {
|
|
448
|
+
delayTime += delayTimeScale;
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
if (drawArray) {
|
|
452
|
+
drawArray.forEach((item) => {
|
|
453
|
+
switch (item.type) {
|
|
454
|
+
case 'text':
|
|
455
|
+
if (item.text) {
|
|
456
|
+
delayTime += item.text.length;
|
|
457
|
+
}
|
|
458
|
+
break;
|
|
459
|
+
case 'qrcode':
|
|
460
|
+
if (item.text) {
|
|
461
|
+
delayTime += item.text.length * 2;
|
|
462
|
+
}
|
|
463
|
+
break;
|
|
464
|
+
default:
|
|
465
|
+
delayTime += delayTimeScale;
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
_app.log('延时系数:' + delayTimeScale);
|
|
471
|
+
_app.log('总计延时:' + delayTime);
|
|
472
|
+
setTimeout(canvasToTempFilePathFn, delayTime);
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
Context.draw(typeof reserve == 'boolean' ? reserve : false, setTimeout(fn, drawDelayTime));
|
|
476
|
+
} catch (e) {
|
|
477
|
+
//TODO handle the exception
|
|
478
|
+
_app.hideLoading();
|
|
479
|
+
rj(e);
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// export
|
|
485
|
+
function drawFillRect(Context, drawArrayItem = {}) {
|
|
486
|
+
//填充矩形
|
|
487
|
+
_app.log('进入绘制填充直角矩形方法, drawArrayItem:' + JSON.stringify(drawArrayItem));
|
|
488
|
+
Context.setFillStyle(drawArrayItem.backgroundColor || 'black');
|
|
489
|
+
Context.setGlobalAlpha(drawArrayItem.alpha || 1);
|
|
490
|
+
Context.fillRect(drawArrayItem.dx || 0, drawArrayItem.dy || 0, drawArrayItem.width || 0, drawArrayItem.height || 0);
|
|
491
|
+
Context.setGlobalAlpha(1);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// export
|
|
495
|
+
function drawStrokeRect(Context, drawArrayItem = {}) {
|
|
496
|
+
//线条矩形
|
|
497
|
+
Context.setStrokeStyle(drawArrayItem.color || 'black');
|
|
498
|
+
Context.setLineWidth(drawArrayItem.lineWidth || 1);
|
|
499
|
+
Context.strokeRect(drawArrayItem.dx, drawArrayItem.dy, drawArrayItem.width, drawArrayItem.height);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// export
|
|
503
|
+
function drawRoundStrokeRect(Context, drawArrayItem = {}) {
|
|
504
|
+
let { dx, dy, width, height, r, lineWidth, color } = drawArrayItem;
|
|
505
|
+
r = r || width * 0.1;
|
|
506
|
+
|
|
507
|
+
if (width < 2 * r) {
|
|
508
|
+
r = width / 2;
|
|
509
|
+
}
|
|
510
|
+
if (width < 2 * r) {
|
|
511
|
+
r = width / 2;
|
|
512
|
+
}
|
|
513
|
+
Context.beginPath();
|
|
514
|
+
Context.arc(dx + r, dy + r, r, 1 * Math.PI, 1.5 * Math.PI);
|
|
515
|
+
Context.lineTo(dx + width - r, dy);
|
|
516
|
+
Context.arc(dx + width - r, dy + r, r, 1.5 * Math.PI, 0);
|
|
517
|
+
Context.lineTo(dx + width, dy + height - r);
|
|
518
|
+
Context.arc(dx + width - r, dy + height - r, r, 0, 0.5 * Math.PI);
|
|
519
|
+
Context.lineTo(dx + r, dy + height);
|
|
520
|
+
Context.arc(dx + r, dy + height - r, r, 0.5 * Math.PI, 1 * Math.PI);
|
|
521
|
+
Context.lineTo(dx, dy + r);
|
|
522
|
+
Context.closePath();
|
|
523
|
+
Context.setLineWidth(lineWidth || 1);
|
|
524
|
+
Context.setStrokeStyle(color || 'black');
|
|
525
|
+
Context.stroke();
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// export
|
|
529
|
+
function drawRoundFillRect(Context, drawArrayItem = {}) {
|
|
530
|
+
let { dx, dy, width, height, r, backgroundColor } = drawArrayItem;
|
|
531
|
+
r = r || width * 0.1;
|
|
532
|
+
|
|
533
|
+
if (width < 2 * r) {
|
|
534
|
+
r = width / 2;
|
|
535
|
+
}
|
|
536
|
+
if (width < 2 * r) {
|
|
537
|
+
r = width / 2;
|
|
538
|
+
}
|
|
539
|
+
Context.beginPath();
|
|
540
|
+
Context.arc(dx + r, dy + r, r, 1 * Math.PI, 1.5 * Math.PI);
|
|
541
|
+
Context.lineTo(dx + width - r, dy);
|
|
542
|
+
Context.arc(dx + width - r, dy + r, r, 1.5 * Math.PI, 0);
|
|
543
|
+
Context.lineTo(dx + width, dy + height - r);
|
|
544
|
+
Context.arc(dx + width - r, dy + height - r, r, 0, 0.5 * Math.PI);
|
|
545
|
+
Context.lineTo(dx + r, dy + height);
|
|
546
|
+
Context.arc(dx + r, dy + height - r, r, 0.5 * Math.PI, 1 * Math.PI);
|
|
547
|
+
Context.lineTo(dx, dy + r);
|
|
548
|
+
Context.closePath();
|
|
549
|
+
Context.setFillStyle(backgroundColor);
|
|
550
|
+
Context.fill();
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// export
|
|
554
|
+
function setText(Context, texts, bgObj) {
|
|
555
|
+
// 设置文本数据
|
|
556
|
+
_app.log('进入设置文字方法, texts:' + JSON.stringify(texts));
|
|
557
|
+
if (texts && _app.isArray(texts)) {
|
|
558
|
+
_app.log('texts是数组');
|
|
559
|
+
if (texts.length > 0) {
|
|
560
|
+
for (let i = 0; i < texts.length; i++) {
|
|
561
|
+
_app.log('字符串信息-初始化之前:' + JSON.stringify(texts[i]));
|
|
562
|
+
texts[i] = setTextFn(Context, texts[i], bgObj);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
} else {
|
|
566
|
+
_app.log('texts是对象');
|
|
567
|
+
texts = setTextFn(Context, texts, bgObj);
|
|
568
|
+
_app.log('返回texts:' + JSON.stringify(texts));
|
|
569
|
+
return texts;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function setTextFn(Context, textItem, bgObj) {
|
|
574
|
+
_app.log('进入设置文字方法, textItem:' + JSON.stringify(textItem));
|
|
575
|
+
if (_app.isNotNull_string(textItem.text)) {
|
|
576
|
+
textItem.text = String(textItem.text);
|
|
577
|
+
textItem.alpha = textItem.alpha !== undefined ? Number(textItem.alpha) : 1;
|
|
578
|
+
textItem.color = textItem.color || 'black';
|
|
579
|
+
textItem.size = textItem.size !== undefined ? Number(textItem.size) : 10;
|
|
580
|
+
textItem.textAlign = textItem.textAlign || 'left';
|
|
581
|
+
textItem.textBaseline = textItem.textBaseline || 'middle';
|
|
582
|
+
textItem.dx = Number(textItem.dx) || 0;
|
|
583
|
+
textItem.dy = Number(textItem.dy) || 0;
|
|
584
|
+
textItem.size = Math.ceil(Number(textItem.size));
|
|
585
|
+
_app.log('字符串信息-初始化默认值后:' + JSON.stringify(textItem));
|
|
586
|
+
let textLength = countTextLength(Context, {
|
|
587
|
+
text: textItem.text,
|
|
588
|
+
size: textItem.size
|
|
589
|
+
});
|
|
590
|
+
_app.log('字符串信息-初始化时的文本长度:' + textLength);
|
|
591
|
+
let infoCallBackObj = {};
|
|
592
|
+
if (textItem.infoCallBack && typeof textItem.infoCallBack === 'function') {
|
|
593
|
+
infoCallBackObj = textItem.infoCallBack(textLength);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (infoCallBackObj.size)
|
|
597
|
+
textLength = countTextLength(Context, {
|
|
598
|
+
text: textItem.text,
|
|
599
|
+
size: textItem.size
|
|
600
|
+
});
|
|
601
|
+
textItem = {
|
|
602
|
+
...textItem,
|
|
603
|
+
...infoCallBackObj,
|
|
604
|
+
textLength
|
|
605
|
+
};
|
|
606
|
+
_app.log('字符串信息-infoCallBack后:' + JSON.stringify(textItem));
|
|
607
|
+
}
|
|
608
|
+
return textItem;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function setLineFeed(Context, textItem, bgObj) {
|
|
612
|
+
if (textItem.text && textItem.lineFeed) {
|
|
613
|
+
_app.log('设置换行');
|
|
614
|
+
let lineNum = -1,
|
|
615
|
+
maxWidth = bgObj.width,
|
|
616
|
+
lastLineMaxWidth,
|
|
617
|
+
lineHeight = textItem.size,
|
|
618
|
+
dx = textItem.dx;
|
|
619
|
+
if (_app.isObject(textItem.lineFeed)) {
|
|
620
|
+
const lineFeed = textItem.lineFeed;
|
|
621
|
+
lineNum =
|
|
622
|
+
lineFeed.lineNum !== undefined && typeof lineFeed.lineNum === 'number' && lineFeed.lineNum >= 0
|
|
623
|
+
? lineFeed.lineNum
|
|
624
|
+
: lineNum;
|
|
625
|
+
maxWidth = lineFeed.maxWidth !== undefined && typeof lineFeed.maxWidth === 'number' ? lineFeed.maxWidth : maxWidth;
|
|
626
|
+
|
|
627
|
+
lastLineMaxWidth =
|
|
628
|
+
lineFeed.lastLineMaxWidth !== undefined && typeof lineFeed.lastLineMaxWidth === 'number'
|
|
629
|
+
? lineFeed.lastLineMaxWidth
|
|
630
|
+
: maxWidth;
|
|
631
|
+
|
|
632
|
+
lineHeight =
|
|
633
|
+
lineFeed.lineHeight !== undefined && typeof lineFeed.lineHeight === 'number' ? lineFeed.lineHeight : lineHeight;
|
|
634
|
+
dx = lineFeed.dx !== undefined && typeof lineFeed.dx === 'number' ? lineFeed.dx : dx;
|
|
635
|
+
}
|
|
636
|
+
_app.lineFeedTags.forEach((i) => {
|
|
637
|
+
textItem.text = textItem.text.split(i).join(_app.tagetLineFeedTag);
|
|
638
|
+
});
|
|
639
|
+
const chr = textItem.text.split('');
|
|
640
|
+
let temp = '';
|
|
641
|
+
const row = [];
|
|
642
|
+
//循环出几行文字组成数组
|
|
643
|
+
for (let a = 0, len = chr.length; a < len; a++) {
|
|
644
|
+
if (chr[a] === _app.tagetLineFeedTag) {
|
|
645
|
+
row.push(temp);
|
|
646
|
+
temp = chr[++a];
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
if (
|
|
650
|
+
countTextLength(Context, {
|
|
651
|
+
text: temp,
|
|
652
|
+
size: textItem.size
|
|
653
|
+
}) <= maxWidth &&
|
|
654
|
+
countTextLength(Context, {
|
|
655
|
+
text: temp + chr[a],
|
|
656
|
+
size: textItem.size
|
|
657
|
+
}) <= maxWidth
|
|
658
|
+
) {
|
|
659
|
+
temp += chr[a];
|
|
660
|
+
if (a == chr.length - 1) {
|
|
661
|
+
row.push(temp);
|
|
662
|
+
}
|
|
663
|
+
} else {
|
|
664
|
+
row.push(temp);
|
|
665
|
+
temp = chr[a];
|
|
666
|
+
if (a == chr.length - 1) row.push(chr[a]);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
_app.log('循环出的文本数组:' + JSON.stringify(row));
|
|
670
|
+
//只显示几行 变量间距lineHeight 变量行数lineNum
|
|
671
|
+
let allNum = lineNum >= 0 && lineNum < row.length ? lineNum : row.length;
|
|
672
|
+
const newArr = [];
|
|
673
|
+
for (let i = 0; i < allNum; i++) {
|
|
674
|
+
let str = row[i];
|
|
675
|
+
if (i == allNum - 1 && allNum < row.length && row.length > 1) {
|
|
676
|
+
const chr2 = str.split('');
|
|
677
|
+
let temp2 = '';
|
|
678
|
+
//循环出几行文字组成数组
|
|
679
|
+
for (let a = 0, len = chr2.length; a < len; a++) {
|
|
680
|
+
if (
|
|
681
|
+
countTextLength(Context, {
|
|
682
|
+
text: temp2,
|
|
683
|
+
size: textItem.size
|
|
684
|
+
}) <= lastLineMaxWidth &&
|
|
685
|
+
countTextLength(Context, {
|
|
686
|
+
text: temp2 + chr2[a],
|
|
687
|
+
size: textItem.size
|
|
688
|
+
}) <= lastLineMaxWidth
|
|
689
|
+
) {
|
|
690
|
+
temp2 += chr2[a];
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
str = temp2;
|
|
694
|
+
if (
|
|
695
|
+
countTextLength(Context, {
|
|
696
|
+
text: str,
|
|
697
|
+
size: textItem.size
|
|
698
|
+
}) >
|
|
699
|
+
(lastLineMaxWidth - textItem.size) * 0.9
|
|
700
|
+
) {
|
|
701
|
+
str = str.substring(0, str.length - 1) + '...';
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
const obj = {
|
|
705
|
+
...textItem,
|
|
706
|
+
text: str,
|
|
707
|
+
dx: i === 0 ? textItem.dx : dx >= 0 ? dx : textItem.dx,
|
|
708
|
+
dy: textItem.dy + i * lineHeight,
|
|
709
|
+
textLength: countTextLength(Context, {
|
|
710
|
+
text: str,
|
|
711
|
+
size: textItem.size
|
|
712
|
+
})
|
|
713
|
+
};
|
|
714
|
+
_app.log('重新组成的文本对象:' + JSON.stringify(obj));
|
|
715
|
+
newArr.push(obj);
|
|
716
|
+
}
|
|
717
|
+
_app.log('newArr: -----', JSON.stringify(newArr));
|
|
718
|
+
const result = newArr.length > 1 ? newArr : newArr[0];
|
|
719
|
+
return result;
|
|
720
|
+
}
|
|
721
|
+
return textItem;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
function countTextLength(Context, obj) {
|
|
725
|
+
_app.log('计算文字长度, obj:' + JSON.stringify(obj));
|
|
726
|
+
const { text, size } = obj;
|
|
727
|
+
Context.setFontSize(size);
|
|
728
|
+
let textLength;
|
|
729
|
+
try {
|
|
730
|
+
textLength = Context.measureText(text); // 官方文档说 App端自定义组件编译模式暂时不可用measureText方法
|
|
731
|
+
} catch (e) {
|
|
732
|
+
//TODO handle the exception
|
|
733
|
+
textLength = {};
|
|
734
|
+
}
|
|
735
|
+
_app.log('measureText计算文字长度, textLength:' + JSON.stringify(textLength));
|
|
736
|
+
textLength = textLength && textLength.width ? textLength.width : 0;
|
|
737
|
+
if (!textLength) {
|
|
738
|
+
let l = 0;
|
|
739
|
+
for (let j = 0; j < text.length; j++) {
|
|
740
|
+
let t = text.substr(j, 1);
|
|
741
|
+
const countL = countStrLength(t);
|
|
742
|
+
_app.log('计算文字宽度系数:' + countL);
|
|
743
|
+
l += countL;
|
|
744
|
+
}
|
|
745
|
+
_app.log('文字宽度总系数:' + l);
|
|
746
|
+
textLength = l * size;
|
|
747
|
+
}
|
|
748
|
+
return textLength;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
//计算字符长度系数
|
|
752
|
+
function countStrLength(t) {
|
|
753
|
+
let l;
|
|
754
|
+
if (/a/.test(t)) {
|
|
755
|
+
l = 0.552734375;
|
|
756
|
+
} else if (/b/.test(t)) {
|
|
757
|
+
l = 0.638671875;
|
|
758
|
+
} else if (/c/.test(t)) {
|
|
759
|
+
l = 0.50146484375;
|
|
760
|
+
} else if (/d/.test(t)) {
|
|
761
|
+
l = 0.6396484375;
|
|
762
|
+
} else if (/e/.test(t)) {
|
|
763
|
+
l = 0.5673828125;
|
|
764
|
+
} else if (/f/.test(t)) {
|
|
765
|
+
l = 0.3466796875;
|
|
766
|
+
} else if (/g/.test(t)) {
|
|
767
|
+
l = 0.6396484375;
|
|
768
|
+
} else if (/h/.test(t)) {
|
|
769
|
+
l = 0.61572265625;
|
|
770
|
+
} else if (/i/.test(t)) {
|
|
771
|
+
l = 0.26611328125;
|
|
772
|
+
} else if (/j/.test(t)) {
|
|
773
|
+
l = 0.26708984375;
|
|
774
|
+
} else if (/k/.test(t)) {
|
|
775
|
+
l = 0.54443359375;
|
|
776
|
+
} else if (/l/.test(t)) {
|
|
777
|
+
l = 0.26611328125;
|
|
778
|
+
} else if (/m/.test(t)) {
|
|
779
|
+
l = 0.93701171875;
|
|
780
|
+
} else if (/n/.test(t)) {
|
|
781
|
+
l = 0.6162109375;
|
|
782
|
+
} else if (/o/.test(t)) {
|
|
783
|
+
l = 0.6357421875;
|
|
784
|
+
} else if (/p/.test(t)) {
|
|
785
|
+
l = 0.638671875;
|
|
786
|
+
} else if (/q/.test(t)) {
|
|
787
|
+
l = 0.6396484375;
|
|
788
|
+
} else if (/r/.test(t)) {
|
|
789
|
+
l = 0.3818359375;
|
|
790
|
+
} else if (/s/.test(t)) {
|
|
791
|
+
l = 0.462890625;
|
|
792
|
+
} else if (/t/.test(t)) {
|
|
793
|
+
l = 0.37255859375;
|
|
794
|
+
} else if (/u/.test(t)) {
|
|
795
|
+
l = 0.6162109375;
|
|
796
|
+
} else if (/v/.test(t)) {
|
|
797
|
+
l = 0.52490234375;
|
|
798
|
+
} else if (/w/.test(t)) {
|
|
799
|
+
l = 0.78955078125;
|
|
800
|
+
} else if (/x/.test(t)) {
|
|
801
|
+
l = 0.5068359375;
|
|
802
|
+
} else if (/y/.test(t)) {
|
|
803
|
+
l = 0.529296875;
|
|
804
|
+
} else if (/z/.test(t)) {
|
|
805
|
+
l = 0.49169921875;
|
|
806
|
+
} else if (/A/.test(t)) {
|
|
807
|
+
l = 0.70361328125;
|
|
808
|
+
} else if (/B/.test(t)) {
|
|
809
|
+
l = 0.62744140625;
|
|
810
|
+
} else if (/C/.test(t)) {
|
|
811
|
+
l = 0.6689453125;
|
|
812
|
+
} else if (/D/.test(t)) {
|
|
813
|
+
l = 0.76171875;
|
|
814
|
+
} else if (/E/.test(t)) {
|
|
815
|
+
l = 0.5498046875;
|
|
816
|
+
} else if (/F/.test(t)) {
|
|
817
|
+
l = 0.53125;
|
|
818
|
+
} else if (/G/.test(t)) {
|
|
819
|
+
l = 0.74365234375;
|
|
820
|
+
} else if (/H/.test(t)) {
|
|
821
|
+
l = 0.7734375;
|
|
822
|
+
} else if (/I/.test(t)) {
|
|
823
|
+
l = 0.2939453125;
|
|
824
|
+
} else if (/J/.test(t)) {
|
|
825
|
+
l = 0.39599609375;
|
|
826
|
+
} else if (/K/.test(t)) {
|
|
827
|
+
l = 0.634765625;
|
|
828
|
+
} else if (/L/.test(t)) {
|
|
829
|
+
l = 0.51318359375;
|
|
830
|
+
} else if (/M/.test(t)) {
|
|
831
|
+
l = 0.97705078125;
|
|
832
|
+
} else if (/N/.test(t)) {
|
|
833
|
+
l = 0.81298828125;
|
|
834
|
+
} else if (/O/.test(t)) {
|
|
835
|
+
l = 0.81494140625;
|
|
836
|
+
} else if (/P/.test(t)) {
|
|
837
|
+
l = 0.61181640625;
|
|
838
|
+
} else if (/Q/.test(t)) {
|
|
839
|
+
l = 0.81494140625;
|
|
840
|
+
} else if (/R/.test(t)) {
|
|
841
|
+
l = 0.65283203125;
|
|
842
|
+
} else if (/S/.test(t)) {
|
|
843
|
+
l = 0.5771484375;
|
|
844
|
+
} else if (/T/.test(t)) {
|
|
845
|
+
l = 0.5732421875;
|
|
846
|
+
} else if (/U/.test(t)) {
|
|
847
|
+
l = 0.74658203125;
|
|
848
|
+
} else if (/V/.test(t)) {
|
|
849
|
+
l = 0.67626953125;
|
|
850
|
+
} else if (/W/.test(t)) {
|
|
851
|
+
l = 1.017578125;
|
|
852
|
+
} else if (/X/.test(t)) {
|
|
853
|
+
l = 0.64501953125;
|
|
854
|
+
} else if (/Y/.test(t)) {
|
|
855
|
+
l = 0.603515625;
|
|
856
|
+
} else if (/Z/.test(t)) {
|
|
857
|
+
l = 0.6201171875;
|
|
858
|
+
} else if (/[0-9]/.test(t)) {
|
|
859
|
+
l = 0.58642578125;
|
|
860
|
+
} else if (/[\u4e00-\u9fa5]/.test(t)) {
|
|
861
|
+
l = 1;
|
|
862
|
+
} else if (/ /.test(t)) {
|
|
863
|
+
l = 0.2958984375;
|
|
864
|
+
} else if (/\`/.test(t)) {
|
|
865
|
+
l = 0.294921875;
|
|
866
|
+
} else if (/\~/.test(t)) {
|
|
867
|
+
l = 0.74169921875;
|
|
868
|
+
} else if (/\!/.test(t)) {
|
|
869
|
+
l = 0.3125;
|
|
870
|
+
} else if (/\@/.test(t)) {
|
|
871
|
+
l = 1.03125;
|
|
872
|
+
} else if (/\#/.test(t)) {
|
|
873
|
+
l = 0.63818359375;
|
|
874
|
+
} else if (/\$/.test(t)) {
|
|
875
|
+
l = 0.58642578125;
|
|
876
|
+
} else if (/\%/.test(t)) {
|
|
877
|
+
l = 0.8896484375;
|
|
878
|
+
} else if (/\^/.test(t)) {
|
|
879
|
+
l = 0.74169921875;
|
|
880
|
+
} else if (/\&/.test(t)) {
|
|
881
|
+
l = 0.8701171875;
|
|
882
|
+
} else if (/\*/.test(t)) {
|
|
883
|
+
l = 0.455078125;
|
|
884
|
+
} else if (/\(/.test(t)) {
|
|
885
|
+
l = 0.333984375;
|
|
886
|
+
} else if (/\)/.test(t)) {
|
|
887
|
+
l = 0.333984375;
|
|
888
|
+
} else if (/\_/.test(t)) {
|
|
889
|
+
l = 0.4482421875;
|
|
890
|
+
} else if (/\-/.test(t)) {
|
|
891
|
+
l = 0.4326171875;
|
|
892
|
+
} else if (/\+/.test(t)) {
|
|
893
|
+
l = 0.74169921875;
|
|
894
|
+
} else if (/\=/.test(t)) {
|
|
895
|
+
l = 0.74169921875;
|
|
896
|
+
} else if (/\|/.test(t)) {
|
|
897
|
+
l = 0.26904296875;
|
|
898
|
+
} else if (/\\/.test(t)) {
|
|
899
|
+
l = 0.416015625;
|
|
900
|
+
} else if (/\[/.test(t)) {
|
|
901
|
+
l = 0.333984375;
|
|
902
|
+
} else if (/\]/.test(t)) {
|
|
903
|
+
l = 0.333984375;
|
|
904
|
+
} else if (/\;/.test(t)) {
|
|
905
|
+
l = 0.24072265625;
|
|
906
|
+
} else if (/\'/.test(t)) {
|
|
907
|
+
l = 0.25634765625;
|
|
908
|
+
} else if (/\,/.test(t)) {
|
|
909
|
+
l = 0.24072265625;
|
|
910
|
+
} else if (/\./.test(t)) {
|
|
911
|
+
l = 0.24072265625;
|
|
912
|
+
} else if (/\//.test(t)) {
|
|
913
|
+
l = 0.42724609375;
|
|
914
|
+
} else if (/\{/.test(t)) {
|
|
915
|
+
l = 0.333984375;
|
|
916
|
+
} else if (/\}/.test(t)) {
|
|
917
|
+
l = 0.333984375;
|
|
918
|
+
} else if (/\:/.test(t)) {
|
|
919
|
+
l = 0.24072265625;
|
|
920
|
+
} else if (/\"/.test(t)) {
|
|
921
|
+
l = 0.435546875;
|
|
922
|
+
} else if (/\</.test(t)) {
|
|
923
|
+
l = 0.74169921875;
|
|
924
|
+
} else if (/\>/.test(t)) {
|
|
925
|
+
l = 0.74169921875;
|
|
926
|
+
} else if (/\?/.test(t)) {
|
|
927
|
+
l = 0.48291015625;
|
|
928
|
+
} else {
|
|
929
|
+
l = 1;
|
|
930
|
+
}
|
|
931
|
+
return l;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
// export
|
|
935
|
+
function setImage(images) {
|
|
936
|
+
// 设置图片数据
|
|
937
|
+
_app.log('进入设置图片数据方法');
|
|
938
|
+
return new Promise(async (resolve, rejcet) => {
|
|
939
|
+
try {
|
|
940
|
+
if (images && _app.isArray(images)) {
|
|
941
|
+
_app.log('images是一个数组');
|
|
942
|
+
for (let i = 0; i < images.length; i++) {
|
|
943
|
+
_app.log('设置图片数据循环中:' + i);
|
|
944
|
+
images[i] = await setImageFn(images[i]);
|
|
945
|
+
}
|
|
946
|
+
} else {
|
|
947
|
+
_app.log('images是一个对象');
|
|
948
|
+
images = await setImageFn(images);
|
|
949
|
+
}
|
|
950
|
+
resolve(images);
|
|
951
|
+
} catch (e) {
|
|
952
|
+
//TODO handle the exception
|
|
953
|
+
rejcet(e);
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
function base64ToPathFn(path) {
|
|
959
|
+
var reg = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i;
|
|
960
|
+
if (!reg.test(path)) {
|
|
961
|
+
return Promise.resolve(path);
|
|
962
|
+
}
|
|
963
|
+
return base64ToPath(path);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function setImageFn(image) {
|
|
967
|
+
return new Promise(async (resolve, reject) => {
|
|
968
|
+
if (image.url) {
|
|
969
|
+
image.url = await base64ToPathFn(image.url);
|
|
970
|
+
let imgUrl = image.url;
|
|
971
|
+
const oldImgUrl = imgUrl;
|
|
972
|
+
imgUrl = await _app.downloadFile_PromiseFc(imgUrl);
|
|
973
|
+
image.url = imgUrl;
|
|
974
|
+
const hasinfoCallBack = image.infoCallBack && typeof image.infoCallBack === 'function';
|
|
975
|
+
let imageInfo = {};
|
|
976
|
+
imageInfo = await _app.getImageInfo_PromiseFc(oldImgUrl);
|
|
977
|
+
if (hasinfoCallBack) {
|
|
978
|
+
image = {
|
|
979
|
+
...image,
|
|
980
|
+
...image.infoCallBack(imageInfo)
|
|
981
|
+
};
|
|
982
|
+
}
|
|
983
|
+
image.dx = Number(image.dx) || 0;
|
|
984
|
+
image.dy = Number(image.dy) || 0;
|
|
985
|
+
image.dWidth = Number(image.dWidth || imageInfo.width);
|
|
986
|
+
image.dHeight = Number(image.dHeight || imageInfo.height);
|
|
987
|
+
image = {
|
|
988
|
+
...image,
|
|
989
|
+
imageInfo
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
resolve(image);
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// export
|
|
997
|
+
function drawText(Context, textArray, bgObj) {
|
|
998
|
+
// 先遍历换行再绘制
|
|
999
|
+
if (!_app.isArray(textArray)) {
|
|
1000
|
+
_app.log('遍历文本方法, 不是数组');
|
|
1001
|
+
textArray = [textArray];
|
|
1002
|
+
} else {
|
|
1003
|
+
_app.log('遍历文本方法, 是数组');
|
|
1004
|
+
}
|
|
1005
|
+
_app.log('遍历文本方法, textArray:' + JSON.stringify(textArray));
|
|
1006
|
+
const newArr = textArray;
|
|
1007
|
+
// if (textArray && textArray.length > 0) {
|
|
1008
|
+
// for (let j = 0; j < textArray.length; j++) {
|
|
1009
|
+
// const textItem = textArray[j];
|
|
1010
|
+
// if (textItem.text && textItem.lineFeed) {
|
|
1011
|
+
// let lineNum = -1,
|
|
1012
|
+
// maxWidth = bgObj.width,
|
|
1013
|
+
// lineHeight = textItem.size,
|
|
1014
|
+
// dx = textItem.dx;
|
|
1015
|
+
// if (_app.isObject(textItem.lineFeed)) {
|
|
1016
|
+
// const lineFeed = textItem.lineFeed;
|
|
1017
|
+
// lineNum = (lineFeed.lineNum !== undefined && typeof(lineFeed.lineNum) === 'number') && lineFeed
|
|
1018
|
+
// .lineNum >= 0 ?
|
|
1019
|
+
// lineFeed.lineNum : lineNum;
|
|
1020
|
+
// maxWidth = (lineFeed.maxWidth !== undefined && typeof(lineFeed.maxWidth) === 'number') ? lineFeed
|
|
1021
|
+
// .maxWidth :
|
|
1022
|
+
// maxWidth;
|
|
1023
|
+
// lineHeight = (lineFeed.lineHeight !== undefined && typeof(lineFeed.lineHeight) === 'number') ?
|
|
1024
|
+
// lineFeed.lineHeight :
|
|
1025
|
+
// lineHeight;
|
|
1026
|
+
// dx = (lineFeed.dx !== undefined && typeof(lineFeed.dx) === 'number') ? lineFeed.dx : dx;
|
|
1027
|
+
// }
|
|
1028
|
+
// const chr = (textItem.text).split("");
|
|
1029
|
+
// let temp = "";
|
|
1030
|
+
// const row = [];
|
|
1031
|
+
// //循环出几行文字组成数组
|
|
1032
|
+
// for (let a = 0, len = chr.length; a < len; a++) {
|
|
1033
|
+
// if (countTextLength(Context, {
|
|
1034
|
+
// text: temp,
|
|
1035
|
+
// size: textItem.size
|
|
1036
|
+
// }) <= maxWidth && countTextLength(Context, {
|
|
1037
|
+
// text: (temp + chr[a]),
|
|
1038
|
+
// size: textItem.size
|
|
1039
|
+
// }) <= maxWidth) {
|
|
1040
|
+
// temp += chr[a];
|
|
1041
|
+
// if (a == (chr.length - 1)) {
|
|
1042
|
+
// row.push(temp);
|
|
1043
|
+
// }
|
|
1044
|
+
// } else {
|
|
1045
|
+
// row.push(temp);
|
|
1046
|
+
// temp = chr[a];
|
|
1047
|
+
// if (a == chr.length - 1) row.push(chr[a]);
|
|
1048
|
+
// }
|
|
1049
|
+
// }
|
|
1050
|
+
// _app.log('循环出的文本数组:' + JSON.stringify(row));
|
|
1051
|
+
// //只显示几行 变量间距lineHeight 变量行数lineNum
|
|
1052
|
+
// let allNum = (lineNum >= 0 && lineNum < row.length) ? lineNum : row.length;
|
|
1053
|
+
|
|
1054
|
+
// for (let i = 0; i < allNum; i++) {
|
|
1055
|
+
// let str = row[i];
|
|
1056
|
+
// if (i == (allNum - 1) && allNum < row.length) {
|
|
1057
|
+
// str = str.substring(0, str.length - 1) + '...';
|
|
1058
|
+
// }
|
|
1059
|
+
// const obj = {
|
|
1060
|
+
// ...textItem,
|
|
1061
|
+
// text: str,
|
|
1062
|
+
// dx: i === 0 ? textItem.dx : (dx >= 0 ? dx : textItem.dx),
|
|
1063
|
+
// dy: textItem.dy + (i * lineHeight),
|
|
1064
|
+
// textLength: countTextLength(Context, {
|
|
1065
|
+
// text: str,
|
|
1066
|
+
// size: textItem.size
|
|
1067
|
+
// })
|
|
1068
|
+
// };
|
|
1069
|
+
// _app.log('重新组成的文本对象:' + JSON.stringify(obj));
|
|
1070
|
+
// newArr.push(obj);
|
|
1071
|
+
// }
|
|
1072
|
+
// } else {
|
|
1073
|
+
// newArr.push(textItem);
|
|
1074
|
+
// }
|
|
1075
|
+
// }
|
|
1076
|
+
// }
|
|
1077
|
+
_app.log('绘制文本新数组:' + JSON.stringify(newArr));
|
|
1078
|
+
drawTexts(Context, newArr);
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
function setFont(textItem = {}) {
|
|
1082
|
+
if (textItem.font && typeof textItem.font === 'string') {
|
|
1083
|
+
_app.log(textItem.font);
|
|
1084
|
+
return textItem.font;
|
|
1085
|
+
} else {
|
|
1086
|
+
let fontStyle = 'normal';
|
|
1087
|
+
let fontVariant = 'normal';
|
|
1088
|
+
let fontWeight = 'normal';
|
|
1089
|
+
let fontSize = textItem.size || 10;
|
|
1090
|
+
let fontFamily = 'sans-serif';
|
|
1091
|
+
fontSize = Math.ceil(Number(fontSize));
|
|
1092
|
+
if (textItem.fontStyle && typeof textItem.fontStyle === 'string') fontStyle = textItem.fontStyle.trim();
|
|
1093
|
+
if (textItem.fontVariant && typeof textItem.fontVariant === 'string') fontVariant = textItem.fontVariant.trim();
|
|
1094
|
+
if (textItem.fontWeight && (typeof textItem.fontWeight === 'string' || typeof textItem.fontWeight === 'number'))
|
|
1095
|
+
fontWeight = textItem.fontWeight.trim();
|
|
1096
|
+
if (textItem.fontFamily && typeof textItem.fontFamily === 'string') fontFamily = textItem.fontFamily.trim();
|
|
1097
|
+
return fontStyle + ' ' + fontVariant + ' ' + fontWeight + ' ' + fontSize + 'px' + ' ' + fontFamily;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
function drawTexts(Context, texts) {
|
|
1102
|
+
// 绘制文本
|
|
1103
|
+
_app.log('准备绘制文本方法, texts:' + JSON.stringify(texts));
|
|
1104
|
+
if (texts && _app.isArray(texts)) {
|
|
1105
|
+
_app.log('准备绘制文本方法, 是数组');
|
|
1106
|
+
if (texts.length > 0) {
|
|
1107
|
+
for (let i = 0; i < texts.length; i++) {
|
|
1108
|
+
drawTextFn(Context, texts[i]);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
} else {
|
|
1112
|
+
_app.log('准备绘制文本方法, 不是数组');
|
|
1113
|
+
drawTextFn(Context, texts);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
function drawTextFn(Context, textItem) {
|
|
1118
|
+
_app.log('进入绘制文本方法, textItem:' + JSON.stringify(textItem));
|
|
1119
|
+
if (textItem && _app.isObject(textItem) && textItem.text) {
|
|
1120
|
+
Context.font = setFont(textItem);
|
|
1121
|
+
Context.setFillStyle(textItem.color);
|
|
1122
|
+
Context.setGlobalAlpha(textItem.alpha);
|
|
1123
|
+
Context.setTextAlign(textItem.textAlign);
|
|
1124
|
+
Context.setTextBaseline(textItem.textBaseline);
|
|
1125
|
+
Context.fillText(textItem.text, textItem.dx, textItem.dy);
|
|
1126
|
+
if (textItem.lineThrough && _app.isObject(textItem.lineThrough)) {
|
|
1127
|
+
_app.log('有删除线');
|
|
1128
|
+
let lineThrough = textItem.lineThrough;
|
|
1129
|
+
lineThrough.alpha = lineThrough.alpha !== undefined ? lineThrough.alpha : textItem.alpha;
|
|
1130
|
+
lineThrough.style = lineThrough.style || textItem.color;
|
|
1131
|
+
lineThrough.width = lineThrough.width !== undefined ? lineThrough.width : textItem.size / 10;
|
|
1132
|
+
lineThrough.cap = lineThrough.cap !== undefined ? lineThrough.cap : 'butt';
|
|
1133
|
+
_app.log('删除线对象:' + JSON.stringify(lineThrough));
|
|
1134
|
+
Context.setGlobalAlpha(lineThrough.alpha);
|
|
1135
|
+
Context.setStrokeStyle(lineThrough.style);
|
|
1136
|
+
Context.setLineWidth(lineThrough.width);
|
|
1137
|
+
Context.setLineCap(lineThrough.cap);
|
|
1138
|
+
let mx, my;
|
|
1139
|
+
switch (textItem.textAlign) {
|
|
1140
|
+
case 'left':
|
|
1141
|
+
mx = textItem.dx;
|
|
1142
|
+
break;
|
|
1143
|
+
case 'center':
|
|
1144
|
+
mx = textItem.dx - textItem.textLength / 2;
|
|
1145
|
+
break;
|
|
1146
|
+
default:
|
|
1147
|
+
mx = textItem.dx - textItem.textLength;
|
|
1148
|
+
break;
|
|
1149
|
+
}
|
|
1150
|
+
switch (textItem.textBaseline) {
|
|
1151
|
+
case 'top':
|
|
1152
|
+
my = textItem.dy + textItem.size * 0.5;
|
|
1153
|
+
break;
|
|
1154
|
+
case 'middle':
|
|
1155
|
+
my = textItem.dy;
|
|
1156
|
+
break;
|
|
1157
|
+
default:
|
|
1158
|
+
my = textItem.dy - textItem.size * 0.5;
|
|
1159
|
+
break;
|
|
1160
|
+
}
|
|
1161
|
+
Context.beginPath();
|
|
1162
|
+
Context.moveTo(mx, my);
|
|
1163
|
+
Context.lineTo(mx + textItem.textLength, my);
|
|
1164
|
+
Context.stroke();
|
|
1165
|
+
Context.closePath();
|
|
1166
|
+
_app.log('删除线完毕');
|
|
1167
|
+
}
|
|
1168
|
+
Context.setGlobalAlpha(1);
|
|
1169
|
+
Context.font = '10px sans-serif';
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
// export
|
|
1173
|
+
function drawImage(Context, images) {
|
|
1174
|
+
// 绘制图片
|
|
1175
|
+
_app.log('判断图片数据类型:' + JSON.stringify(images));
|
|
1176
|
+
if (images && _app.isArray(images)) {
|
|
1177
|
+
if (images.length > 0) {
|
|
1178
|
+
for (let i = 0; i < images.length; i++) {
|
|
1179
|
+
readyDrawImageFn(Context, images[i]);
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
} else {
|
|
1183
|
+
readyDrawImageFn(Context, images);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
function readyDrawImageFn(Context, img) {
|
|
1188
|
+
_app.log('判断绘制图片形状, img:' + JSON.stringify(img));
|
|
1189
|
+
if (img.url) {
|
|
1190
|
+
if (img.circleSet) {
|
|
1191
|
+
drawCircleImage(Context, img);
|
|
1192
|
+
} else if (img.roundRectSet) {
|
|
1193
|
+
drawRoundRectImage(Context, img);
|
|
1194
|
+
} else {
|
|
1195
|
+
drawImageFn(Context, img);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
const drawImageModes = {
|
|
1201
|
+
scaleToFill(Context, img) {
|
|
1202
|
+
_app.log('准备绘制mode为scaleToFill的图片');
|
|
1203
|
+
Context.drawImage(
|
|
1204
|
+
img.url,
|
|
1205
|
+
Number(img.dx || 0),
|
|
1206
|
+
Number(img.dy || 0),
|
|
1207
|
+
Number(img.dWidth) || false,
|
|
1208
|
+
Number(img.dHeight) || false
|
|
1209
|
+
);
|
|
1210
|
+
_app.log('mode为scaleToFill的图片绘制完毕');
|
|
1211
|
+
},
|
|
1212
|
+
aspectFit(Context, img) {
|
|
1213
|
+
_app.log('准备绘制mode为aspectFit的图片');
|
|
1214
|
+
const { imageInfo, dWidth, dHeight } = img;
|
|
1215
|
+
const { height, width } = imageInfo;
|
|
1216
|
+
let drawWidth = dWidth;
|
|
1217
|
+
let drawHeight = (height / width) * drawWidth;
|
|
1218
|
+
if (drawHeight < dHeight) {
|
|
1219
|
+
const diffHeight = ((Number(dHeight) - Number(drawHeight)) / Number(dHeight)) * height;
|
|
1220
|
+
img.dy = Number(img.dy) + diffHeight / 2;
|
|
1221
|
+
} else {
|
|
1222
|
+
drawHeight = dHeight;
|
|
1223
|
+
drawWidth = (width / height) * drawHeight;
|
|
1224
|
+
const diffWidth = ((Number(dWidth) - Number(drawWidth)) / Number(dWidth)) * width;
|
|
1225
|
+
img.dx = Number(img.dx) + diffWidth / 2;
|
|
1226
|
+
}
|
|
1227
|
+
Context.drawImage(img.url, 0, 0, width, height, img.dx, img.dy, drawWidth, drawHeight);
|
|
1228
|
+
_app.log('mode为aspectFit的图片绘制完毕');
|
|
1229
|
+
},
|
|
1230
|
+
aspectFill(Context, img) {
|
|
1231
|
+
const dpr = uni.getSystemInfoSync().pixelRatio;
|
|
1232
|
+
_app.log('准备绘制mode为aspectFill的图片');
|
|
1233
|
+
const { imageInfo, dWidth, dHeight } = img;
|
|
1234
|
+
const { height, width } = imageInfo;
|
|
1235
|
+
let sx = 0,
|
|
1236
|
+
sy = 0,
|
|
1237
|
+
sWidth = width,
|
|
1238
|
+
sHeight = height;
|
|
1239
|
+
let drawWidth = dWidth;
|
|
1240
|
+
let drawHeight = (height / width) * drawWidth;
|
|
1241
|
+
if (drawHeight < dHeight) {
|
|
1242
|
+
_app.log('绘制高度 小于 预定高度');
|
|
1243
|
+
drawHeight = dHeight;
|
|
1244
|
+
drawWidth = (width / height) * drawHeight;
|
|
1245
|
+
const diffWidth = ((Number(drawWidth) - Number(dWidth)) / Number(drawWidth)) * width;
|
|
1246
|
+
sx = diffWidth / 2;
|
|
1247
|
+
sWidth = width - diffWidth;
|
|
1248
|
+
} else {
|
|
1249
|
+
const diffHeight = ((Number(drawHeight) - Number(dHeight)) / Number(drawHeight)) * height;
|
|
1250
|
+
sy = diffHeight / 2;
|
|
1251
|
+
sHeight = height - diffHeight;
|
|
1252
|
+
}
|
|
1253
|
+
_app.log(
|
|
1254
|
+
`aspectFill 最终绘制: sx: ${sx}, sy: ${sy}, sWidth: ${sWidth}, sHeight: ${sHeight}, dx: ${img.dx}, dy: ${img.dy}, dWidth: ${dWidth}, dHeight: ${dHeight}`
|
|
1255
|
+
);
|
|
1256
|
+
Context.drawImage(img.url, sx, sy, sWidth, sHeight, img.dx, img.dy, dWidth, dHeight);
|
|
1257
|
+
_app.log('mode为aspectFill的图片绘制完毕');
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
1260
|
+
|
|
1261
|
+
function drawImageFn(Context, img) {
|
|
1262
|
+
_app.log('进入绘制默认图片方法, img:' + JSON.stringify(img));
|
|
1263
|
+
if (img.url) {
|
|
1264
|
+
const hasAlpha = !_app.isUndef(img.alpha);
|
|
1265
|
+
img.alpha = Number(!_app.isUndef(img.alpha) ? img.alpha : 1);
|
|
1266
|
+
Context.setGlobalAlpha(img.alpha);
|
|
1267
|
+
_app.log('绘制默认图片方法, 有url');
|
|
1268
|
+
if (img.dHeight === undefined) img.dHeight = img.imageInfo.height;
|
|
1269
|
+
if (img.dWidth === undefined) img.dWidth = img.imageInfo.width;
|
|
1270
|
+
const fn = drawImageModes[img.mode];
|
|
1271
|
+
if (fn) {
|
|
1272
|
+
fn(Context, img);
|
|
1273
|
+
} else {
|
|
1274
|
+
if (img.dWidth && img.dHeight && img.sx && img.sy && img.sWidth && img.sHeight) {
|
|
1275
|
+
_app.log('绘制默认图片方法, 绘制第一种方案');
|
|
1276
|
+
Context.drawImage(
|
|
1277
|
+
img.url,
|
|
1278
|
+
Number(img.sx) || false,
|
|
1279
|
+
Number(img.sy) || false,
|
|
1280
|
+
Number(img.sWidth) || false,
|
|
1281
|
+
Number(img.sHeight) || false,
|
|
1282
|
+
Number(img.dx || 0),
|
|
1283
|
+
Number(img.dy || 0),
|
|
1284
|
+
Number(img.dWidth) || false,
|
|
1285
|
+
Number(img.dHeight) || false
|
|
1286
|
+
);
|
|
1287
|
+
} else if (img.dWidth && img.dHeight) {
|
|
1288
|
+
_app.log('绘制默认图片方法, 绘制第二种方案');
|
|
1289
|
+
Context.drawImage(
|
|
1290
|
+
img.url,
|
|
1291
|
+
Number(img.dx || 0),
|
|
1292
|
+
Number(img.dy || 0),
|
|
1293
|
+
Number(img.dWidth) || false,
|
|
1294
|
+
Number(img.dHeight) || false
|
|
1295
|
+
);
|
|
1296
|
+
} else {
|
|
1297
|
+
_app.log('绘制默认图片方法, 绘制第三种方案');
|
|
1298
|
+
Context.drawImage(img.url, Number(img.dx || 0), Number(img.dy || 0));
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
if (hasAlpha) {
|
|
1302
|
+
Context.setGlobalAlpha(1);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
_app.log('绘制默认图片方法, 绘制完毕');
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
function drawCircleImage(Context, obj) {
|
|
1309
|
+
_app.log('进入绘制圆形图片方法, obj:' + JSON.stringify(obj));
|
|
1310
|
+
let { dx, dy, dWidth, dHeight, circleSet, imageInfo } = obj;
|
|
1311
|
+
let x, y, r;
|
|
1312
|
+
if (typeof circleSet === 'object') {
|
|
1313
|
+
x = circleSet.x;
|
|
1314
|
+
y = circleSet.y;
|
|
1315
|
+
r = circleSet.r;
|
|
1316
|
+
}
|
|
1317
|
+
if (!r) {
|
|
1318
|
+
let d;
|
|
1319
|
+
d = dWidth > dHeight ? dHeight : dWidth;
|
|
1320
|
+
r = d / 2;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
x = x ? dx + x : (dx || 0) + r;
|
|
1324
|
+
y = y ? dy + y : (dy || 0) + r;
|
|
1325
|
+
Context.save();
|
|
1326
|
+
Context.beginPath();
|
|
1327
|
+
Context.arc(x, y, r, 0, 2 * Math.PI, false);
|
|
1328
|
+
Context.closePath();
|
|
1329
|
+
Context.setGlobalAlpha(0);
|
|
1330
|
+
Context.fillStyle = '#FFFFFF';
|
|
1331
|
+
Context.fill();
|
|
1332
|
+
Context.setGlobalAlpha(1);
|
|
1333
|
+
Context.clip();
|
|
1334
|
+
drawImageFn(Context, obj);
|
|
1335
|
+
_app.log('默认图片绘制完毕');
|
|
1336
|
+
Context.restore();
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
function drawRoundRectImage(Context, obj) {
|
|
1340
|
+
// 绘制矩形
|
|
1341
|
+
_app.log('进入绘制矩形图片方法, obj:' + JSON.stringify(obj));
|
|
1342
|
+
Context.save();
|
|
1343
|
+
let { dx, dy, dWidth, dHeight, roundRectSet, imageInfo } = obj;
|
|
1344
|
+
let r;
|
|
1345
|
+
if (typeof roundRectSet === 'object') {
|
|
1346
|
+
r = roundRectSet.r;
|
|
1347
|
+
}
|
|
1348
|
+
r = r || dWidth * 0.1;
|
|
1349
|
+
|
|
1350
|
+
if (dWidth < 2 * r) {
|
|
1351
|
+
r = dWidth / 2;
|
|
1352
|
+
}
|
|
1353
|
+
if (dHeight < 2 * r) {
|
|
1354
|
+
r = dHeight / 2;
|
|
1355
|
+
}
|
|
1356
|
+
Context.beginPath();
|
|
1357
|
+
|
|
1358
|
+
// Context.moveTo(dx + r, dy);
|
|
1359
|
+
Context.arc(dx + r, dy + r, r, 1 * Math.PI, 1.5 * Math.PI);
|
|
1360
|
+
Context.lineTo(dx + dWidth - r, dy);
|
|
1361
|
+
Context.arc(dx + dWidth - r, dy + r, r, 1.5 * Math.PI, 0);
|
|
1362
|
+
Context.lineTo(dx + dWidth, dy + dHeight - r);
|
|
1363
|
+
Context.arc(dx + dWidth - r, dy + dHeight - r, r, 0, 0.5 * Math.PI);
|
|
1364
|
+
Context.lineTo(dx + r, dy + dHeight);
|
|
1365
|
+
Context.arc(dx + r, dy + dHeight - r, r, 0.5 * Math.PI, 1 * Math.PI);
|
|
1366
|
+
Context.lineTo(dx, dy + r);
|
|
1367
|
+
|
|
1368
|
+
// Context.arcTo(dx + dWidth, dy, dx + dWidth, dy + dHeight, r);
|
|
1369
|
+
// Context.arcTo(dx + dWidth, dy + dHeight, dx, dy + dHeight, r);
|
|
1370
|
+
// Context.arcTo(dx, dy + dHeight, dx, dy, r);
|
|
1371
|
+
// Context.arcTo(dx, dy, dx + dWidth, dy, r);
|
|
1372
|
+
Context.closePath();
|
|
1373
|
+
Context.setGlobalAlpha(0);
|
|
1374
|
+
Context.fillStyle = '#FFFFFF';
|
|
1375
|
+
Context.fill();
|
|
1376
|
+
Context.setGlobalAlpha(1);
|
|
1377
|
+
Context.clip();
|
|
1378
|
+
drawImageFn(Context, obj);
|
|
1379
|
+
Context.restore();
|
|
1380
|
+
_app.log('进入绘制矩形图片方法, 绘制完毕');
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
// export
|
|
1384
|
+
function drawQrCode(Context, qrCodeObj) {
|
|
1385
|
+
//生成二维码方法, 参考了 诗小柒 的二维码生成器代码
|
|
1386
|
+
_app.log('进入绘制二维码方法');
|
|
1387
|
+
_app.showLoading('正在生成二维码');
|
|
1388
|
+
let qrcodeAlgObjCache = [];
|
|
1389
|
+
let options = {
|
|
1390
|
+
text: String(qrCodeObj.text || '') || '', // 生成内容
|
|
1391
|
+
size: Number(qrCodeObj.size || 0) || 200, // 二维码大小
|
|
1392
|
+
background: String(qrCodeObj.background || '') || '#ffffff', // 背景色
|
|
1393
|
+
foreground: String(qrCodeObj.foreground || '') || '#000000', // 前景色
|
|
1394
|
+
pdground: String(qrCodeObj.pdground || '') || '#000000', // 定位角点颜色
|
|
1395
|
+
correctLevel: Number(qrCodeObj.correctLevel || 0) || 3, // 容错级别
|
|
1396
|
+
image: String(qrCodeObj.image || '') || '', // 二维码图标
|
|
1397
|
+
imageSize: Number(qrCodeObj.imageSize || 0) || 40, // 二维码图标大小
|
|
1398
|
+
dx: Number(qrCodeObj.dx || 0) || 0, // x轴距离
|
|
1399
|
+
dy: Number(qrCodeObj.dy || 0) || 0 // y轴距离
|
|
1400
|
+
};
|
|
1401
|
+
let qrCodeAlg = null;
|
|
1402
|
+
let d = 0;
|
|
1403
|
+
for (var i = 0, l = qrcodeAlgObjCache.length; i < l; i++) {
|
|
1404
|
+
d = i;
|
|
1405
|
+
if (qrcodeAlgObjCache[i].text == options.text && qrcodeAlgObjCache[i].text.correctLevel == options.correctLevel) {
|
|
1406
|
+
qrCodeAlg = qrcodeAlgObjCache[i].obj;
|
|
1407
|
+
break;
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
if (d == l) {
|
|
1411
|
+
qrCodeAlg = new QRCodeAlg(options.text, options.correctLevel);
|
|
1412
|
+
qrcodeAlgObjCache.push({
|
|
1413
|
+
text: options.text,
|
|
1414
|
+
correctLevel: options.correctLevel,
|
|
1415
|
+
obj: qrCodeAlg
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
let getForeGround = function (config) {
|
|
1419
|
+
let options = config.options;
|
|
1420
|
+
if (
|
|
1421
|
+
options.pdground &&
|
|
1422
|
+
((config.row > 1 && config.row < 5 && config.col > 1 && config.col < 5) ||
|
|
1423
|
+
(config.row > config.count - 6 && config.row < config.count - 2 && config.col > 1 && config.col < 5) ||
|
|
1424
|
+
(config.row > 1 && config.row < 5 && config.col > config.count - 6 && config.col < config.count - 2))
|
|
1425
|
+
) {
|
|
1426
|
+
return options.pdground;
|
|
1427
|
+
}
|
|
1428
|
+
return options.foreground;
|
|
1429
|
+
};
|
|
1430
|
+
let count = qrCodeAlg.getModuleCount();
|
|
1431
|
+
let ratioSize = options.size;
|
|
1432
|
+
let ratioImgSize = options.imageSize;
|
|
1433
|
+
//计算每个点的长宽
|
|
1434
|
+
let tileW = (ratioSize / count).toPrecision(4);
|
|
1435
|
+
let tileH = (ratioSize / count).toPrecision(4);
|
|
1436
|
+
//绘制
|
|
1437
|
+
for (let row = 0; row < count; row++) {
|
|
1438
|
+
for (let col = 0; col < count; col++) {
|
|
1439
|
+
let w = Math.ceil((col + 1) * tileW) - Math.floor(col * tileW);
|
|
1440
|
+
let h = Math.ceil((row + 1) * tileW) - Math.floor(row * tileW);
|
|
1441
|
+
let foreground = getForeGround({
|
|
1442
|
+
row: row,
|
|
1443
|
+
col: col,
|
|
1444
|
+
count: count,
|
|
1445
|
+
options: options
|
|
1446
|
+
});
|
|
1447
|
+
Context.setFillStyle(qrCodeAlg.modules[row][col] ? foreground : options.background);
|
|
1448
|
+
Context.fillRect(options.dx + Math.round(col * tileW), options.dy + Math.round(row * tileH), w, h);
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
if (options.image) {
|
|
1452
|
+
let x = options.dx + Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
|
|
1453
|
+
let y = options.dy + Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
|
|
1454
|
+
drawRoundedRect(Context, x, y, ratioImgSize, ratioImgSize, 2, 6, true, true);
|
|
1455
|
+
Context.drawImage(options.image, x, y, ratioImgSize, ratioImgSize);
|
|
1456
|
+
// 画圆角矩形
|
|
1457
|
+
function drawRoundedRect(ctxi, x, y, width, height, r, lineWidth, fill, stroke) {
|
|
1458
|
+
ctxi.setLineWidth(lineWidth);
|
|
1459
|
+
ctxi.setFillStyle(options.background);
|
|
1460
|
+
ctxi.setStrokeStyle(options.background);
|
|
1461
|
+
ctxi.beginPath(); // draw top and top right corner
|
|
1462
|
+
ctxi.moveTo(x + r, y);
|
|
1463
|
+
ctxi.arcTo(x + width, y, x + width, y + r, r); // draw right side and bottom right corner
|
|
1464
|
+
ctxi.arcTo(x + width, y + height, x + width - r, y + height, r); // draw bottom and bottom left corner
|
|
1465
|
+
ctxi.arcTo(x, y + height, x, y + height - r, r); // draw left and top left corner
|
|
1466
|
+
ctxi.arcTo(x, y, x + r, y, r);
|
|
1467
|
+
ctxi.closePath();
|
|
1468
|
+
if (fill) {
|
|
1469
|
+
ctxi.fill();
|
|
1470
|
+
}
|
|
1471
|
+
if (stroke) {
|
|
1472
|
+
ctxi.stroke();
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
_app.log('进入绘制二维码方法完毕');
|
|
1477
|
+
_app.hideLoading();
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
function getShreUserPosterBackground(objs) {
|
|
1481
|
+
//检查背景图是否存在于本地, 若存在直接返回, 否则调用getShreUserPosterBackgroundFc方法
|
|
1482
|
+
let { backgroundImage, type } = objs;
|
|
1483
|
+
return new Promise(async (resolve, reject) => {
|
|
1484
|
+
try {
|
|
1485
|
+
_app.showLoading('正在获取海报背景图');
|
|
1486
|
+
const savedFilePath = await getShreUserPosterBackgroundFc(objs);
|
|
1487
|
+
_app.hideLoading();
|
|
1488
|
+
resolve(savedFilePath);
|
|
1489
|
+
} catch (e) {
|
|
1490
|
+
_app.hideLoading();
|
|
1491
|
+
_app.showToast('获取分享用户背景图失败:' + JSON.stringify(e));
|
|
1492
|
+
_app.log(JSON.stringify(e));
|
|
1493
|
+
reject(e);
|
|
1494
|
+
}
|
|
1495
|
+
});
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
function getPosterStorage(type) {
|
|
1499
|
+
return _app.getStorageSync(getStorageKey(type));
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
function removePosterStorage(type) {
|
|
1503
|
+
const ShreUserPosterBackgroundKey = getStorageKey(type);
|
|
1504
|
+
const pbg = _app.getStorageSync(ShreUserPosterBackgroundKey);
|
|
1505
|
+
if (pbg && pbg.path) {
|
|
1506
|
+
_app.removeStorageSync(ShreUserPosterBackgroundKey);
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
function setPosterStorage(type, data) {
|
|
1511
|
+
_app.setStorage(getStorageKey(type), data);
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
function getStorageKey(type) {
|
|
1515
|
+
return ShreUserPosterBackgroundKey + (type || 'default');
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
function getShreUserPosterBackgroundFc(objs, upimage) {
|
|
1519
|
+
//下载并保存背景图方法
|
|
1520
|
+
let { backgroundImage, type } = objs;
|
|
1521
|
+
_app.log('获取分享背景图, 尝试清空本地数据');
|
|
1522
|
+
return new Promise(async (resolve, reject) => {
|
|
1523
|
+
try {
|
|
1524
|
+
_app.showLoading('正在下载海报背景图');
|
|
1525
|
+
_app.log('没有从后端获取的背景图片路径, 尝试从后端获取背景图片路径');
|
|
1526
|
+
let image = backgroundImage ? backgroundImage : await _app.getPosterUrl(objs);
|
|
1527
|
+
image = await base64ToPathFn(image);
|
|
1528
|
+
_app.log('尝试下载并保存背景图:' + image);
|
|
1529
|
+
const savedFilePath = await _app.downLoadAndSaveFile_PromiseFc(image);
|
|
1530
|
+
if (savedFilePath) {
|
|
1531
|
+
_app.log('下载并保存背景图成功:' + savedFilePath);
|
|
1532
|
+
const imageObj = await _app.getImageInfo_PromiseFc(image);
|
|
1533
|
+
_app.log('获取图片信息成功');
|
|
1534
|
+
const returnObj = {
|
|
1535
|
+
path: savedFilePath,
|
|
1536
|
+
width: imageObj.width,
|
|
1537
|
+
height: imageObj.height,
|
|
1538
|
+
name: _app.fileNameInPath(image)
|
|
1539
|
+
};
|
|
1540
|
+
_app.log('拼接背景图信息对象成功:' + JSON.stringify(returnObj));
|
|
1541
|
+
|
|
1542
|
+
// #ifndef H5
|
|
1543
|
+
setPosterStorage(type, {
|
|
1544
|
+
...returnObj
|
|
1545
|
+
});
|
|
1546
|
+
// #endif
|
|
1547
|
+
|
|
1548
|
+
_app.hideLoading();
|
|
1549
|
+
_app.log('返回背景图信息对象');
|
|
1550
|
+
resolve({
|
|
1551
|
+
...returnObj
|
|
1552
|
+
});
|
|
1553
|
+
} else {
|
|
1554
|
+
_app.hideLoading();
|
|
1555
|
+
reject('not find savedFilePath');
|
|
1556
|
+
}
|
|
1557
|
+
} catch (e) {
|
|
1558
|
+
//TODO handle the exception
|
|
1559
|
+
reject(e);
|
|
1560
|
+
}
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
export default {
|
|
1565
|
+
getSharePoster,
|
|
1566
|
+
setText,
|
|
1567
|
+
setImage,
|
|
1568
|
+
drawText,
|
|
1569
|
+
drawImage,
|
|
1570
|
+
drawQrCode,
|
|
1571
|
+
drawFillRect,
|
|
1572
|
+
drawStrokeRect,
|
|
1573
|
+
drawRoundStrokeRect,
|
|
1574
|
+
drawRoundFillRect
|
|
1575
|
+
};
|