This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.js
281 lines (258 loc) · 6.79 KB
/
utils.js
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
/**
* @description 工具集
* @author: minfive
* @createDate: 2018-06-24
* @lastModify minfive
* @lastDate: 2018-06-24
*/
/**
* 异常信息提示
*
* @param {String} info 异常提示信息
*/
function errorInfo(info) {
const message = `[wxapp-canvas] Error: ${info}`;
if (typeof console !== 'undefined') {
console.error(message);
}
/* eslint-disable */
try {
throw new Error(message);
} catch (x) { }
/* eslint-enable */
}
/**
* 检查是否为微信文件路径
*
* @param {String} url 资源路径
* @param {Boolean}
*/
function checkIsWxFliePath(url) {
// 增加对开发者工具路径的适配
return /^wxfile:\/\/(tmp|store)/.test(url) || /^http:\/\/(tmp|store)\//.test(url);
}
/**
* 检查是否为网络文件路径
*
* @param {String} url 资源路径
* @return {Boolean}
*/
function checkIsNetworkFile(url) {
return /^(http|https):\/\/(?!(tmp|store)\/)/.test(url);
}
/**
* wx api promise 装饰器
*
* @param {String} method 需要处理的wx接口
* @return {Promise}
*/
function promisify(method) {
return function(option = {}, ...args) {
return new Promise((resolve, reject) => {
let md = wx[method];
if (md && typeof md === 'function') {
md({
...option,
success(...args) {
option.success && typeof option.success === 'function' && option.success();
resolve(...args);
},
fail(...args) {
option.fail && typeof option.fail === 'function' && option.fail();
reject(...args);
}
}, ...args);
} else {
errorInfo('wx method must be a function');
}
});
};
}
/**
* promise wx apis
*
* @param {Array} methods 需要处理的所有wx接口
* @return {Object} 处理过后的接口对象集合
*/
function promisifyList(methods = []) {
let result = {};
if (Array.isArray(methods)) {
methods.forEach(method => {
result[method] = promisify(method);
});
} else {
errorInfo('wx method list must be a array');
}
};
/**
* 下载图片
*
* @param {String} src 图片路径
* @return {Promise}
*/
async function downloadImage(src) {
let tempPath = await downloadFile(src);
return promisify('getImageInfo')({
src: tempPath
});
}
/**
* 下载文件
*
* @param {String} url 资源路径
* @return {Promise}
*/
async function downloadFile(url) {
// console.log(url)
if (checkIsWxFliePath(url)) {
return url;
} else if (checkIsNetworkFile(url)) {
let {
tempFilePath,
statusCode
} = await promisify('downloadFile')({
url
});
if (statusCode !== 200 && statusCode !== 304) {
errorInfo('download file error, status code is ' + statusCode);
}
return tempFilePath;
} else {
errorInfo('The file url must be a network file or a wechat file');
}
}
/**
* 保存图片到相册
*
* @param {String} filePath 图片文件路径
* @return {Promise}
*/
async function saveImageToPhotosAlbum(filePath) {
const info = await downloadImage(filePath);
return await promisify('saveImageToPhotosAlbum')({
filePath: info.path
});
}
/**
* 异步迭代数组
*
* @param {Array} arr 需要迭代的数组
* @param {AsyncFunction} callback 操作函数
* @param {Any} callback.value 当前迭代值
* @param {Number} callback.index 当前迭代索引
*/
async function asyncEach(arr, callback) {
let idx = 0;
while (idx < arr.length) {
await callback(arr[idx], idx);
idx++;
}
}
/**
* 设置对象深层属性
*
* @param {Object} obj 需要设置的对象
* @param {String} key 需要设置的 key 值,用 . 代表嵌套层级
* @return {Object} 设置好的对象
*/
function setObjectKey(obj, key, val) {
let keys = key.split('.');
return keys.reduce((res, key, idx) => {
if (idx === keys.length - 1) {
res[key] = val;
return obj;
} else {
res[key] = res[key] || {};
return res[key];
}
}, obj);
}
/**
* 绘制一个圆角四边形
*
* @param {Object} ctx canvas 上下文
* @param {Number} lineWidth 边宽
* @param {Number} startX 左上角X轴坐标
* @param {Number} startY 左上角Y轴坐标
* @param {Number} width 宽度
* @param {Number} height 高度
* @param {Object} borderRadius 边角
* @param {Array} borderRadius['top-left']
* @param {Array} borderRadius['top-right']
* @param {Array} borderRadius['bottom-right']
* @param {Array} borderRadius['bottom-left']
*/
function drawRing(ctx, lineWidth, startX, startY, width, height, borderRadius) {
const KAPPA = 0.5522848;
ctx.lineWidth = lineWidth;
ctx.beginPath();
ctx.moveTo(
startX + borderRadius['top-left'][0],
startY
);
ctx.lineTo(
startX + width - borderRadius['top-right'][0],
startY
);
ctx.bezierCurveTo(
startX + width - borderRadius['top-right'][0] * (1 - KAPPA),
startY,
startX + width,
startY + borderRadius['top-right'][1] * (1 - KAPPA),
startX + width,
startY + borderRadius['top-right'][1]
);
ctx.lineTo(
startX + width,
startY + height - borderRadius['bottom-right'][1]
);
ctx.bezierCurveTo(
startX + width,
startY + height - borderRadius['bottom-right'][1] * (1 - KAPPA),
startX + width - borderRadius['bottom-right'][0] * (1 - KAPPA),
startY + height,
startX + width - borderRadius['bottom-right'][0],
startY + height
);
ctx.lineTo(
startX + borderRadius['bottom-left'][0],
startY + height
);
ctx.bezierCurveTo(
startX + borderRadius['bottom-left'][0] * (1 - KAPPA),
startY + height,
startX,
startY + height - borderRadius['bottom-left'][1] * (1 - KAPPA),
startX,
startY + height - borderRadius['bottom-left'][1]
);
ctx.lineTo(
startX,
startY + borderRadius['top-left'][1]
);
ctx.bezierCurveTo(
startX,
startY + borderRadius['top-left'][1] * (1 - KAPPA),
startX + borderRadius['top-left'][0] * (1 - KAPPA),
startY,
startX + borderRadius['top-left'][0],
startY
);
ctx.closePath();
// TODO:这里加上透明描边,以解决路径错乱问题(不是很理解)
ctx.strokeStyle = 'transparent';
ctx.stroke();
}
export {
errorInfo,
checkIsWxFliePath,
checkIsNetworkFile,
promisify,
promisifyList,
downloadFile,
downloadImage,
saveImageToPhotosAlbum,
asyncEach,
setObjectKey,
drawRing
};