-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxCommon.js
550 lines (501 loc) · 15.1 KB
/
wxCommon.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
function stopScroll() {
$("body").on("touchmove", function (a) {
a.preventDefault();
});
$("html").css({'overflow':'hidden'});
}
function startScroll() {
$("body").off("touchmove");
$("html").css({'overflow':'auto'});
}
/**
* 获得url中的queryString OBJ
*/
function getRequest() {
var searchString = window.location.search.substring(1),
params = searchString.split("&"),
hash = {};
if (searchString == "") return {};
for (var i = 0; i < params.length; i++) {
var pos = params[i].indexOf('=');
if (pos == -1) { continue; }
var paraName = params[i].substring(0, pos),
paraValue = params[i].substring(pos + 1);
hash[paraName] = paraValue;
}
return hash;
}
//获取客户端类型
var envi = {
hasTouch: 'ontouchstart' in window,
isAndroid: false,
isIOS: false,
isIPhone: false,
isIPad: false,
isPC: false,
isInWeixin: false,
isInQQ: false,
deviceType: null,
deviceModel: null,
osVersion: null,
//获取网络类型
getNetType: function () {
if (navigator.connection) return navigator.connection.type;
return 0;
},
//获取客户端类型 ios android等
init: function () {
var av = window.navigator.appVersion;
if (/Android /.test(av)) this.isAndroid = true;
else if (/iPhone;/.test(av)) this.isIPhone = true;
else if (/iPad;/.test(av)) this.isIPad = true;
else this.isPC = true;
if (/MicroMessenger/.test(av)) this.isInWeixin = true;
else if (/MQQBrowser/.test(av)) this.isInQQ = true;
this.deviceType = this.isAndroid ? "Android" : (this.isIPhone ? "iPhone" : (this.isIPad ? "iPad" : "Other"));
if (this.isAndroid) this.osVersion = /Android (\d+[^;]+)?;/.exec(av)[1], this.deviceModel = /; ([^;\)]+)\)/.exec(av)[1];
else if (this.isIPhone || this.isIPad) this.osVersion = / OS (\d+[^ ]+)? /.exec(av)[1].replace(/_/g, '.');
this.isIOS = /Mac OS X/.test(av);
},
}
envi.init();
/**
* 获取移动终端浏览器版本信息
*/
var browser = {
versions: function () {
var u = navigator.userAgent;
return {
trident: u.indexOf('Trident') > -1, //IE内核
presto: u.indexOf('Presto') > -1, //opera内核
webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), //是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1, //是否iPad
webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
};
}(),
language: (navigator.browserLanguage || navigator.language).toLowerCase()
}
(function (S) {
S.prototype.tTrim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
S.prototype.tstartWith = function (s) {
return this.indexOf(s) == 0
}
S.prototype.tEndWiths = function (str) {
return this.lastIndexOf(str) == this.length - str.length&&(this.length - str.length)>0;
};
S.prototype.tGetByteLength = function () {
return this.replace(/[\u4e00-\u9fa5]+/g,"**").length;
};
S.prototype.treplaceAll = function (s1, s2) {
return this.replace(new RegExp(s1, "g"), s2)
}
})(String);
/**
* 计算两个日期时间的时间差
* @param {Date, Date} date1 date2
* @return {object | null}
* @example
* getDiff(new Date('2017-09-08'), new Date())
*/
function getDiff(date1, date2) {
if (!date1.getTime || !date2.getTime) return null
var ms = (date1.getTime() - date2.getTime());
var day1 = Math.round(ms / 24 / 3600 / 1000),
hh1 = Math.round((ms / 3600 / 1000) % 24),
mm1 = Math.round((ms / 1000 / 60) % 60),
ss1 = Math.round((ms / 1000) % 60);
return {
day: day1,
hour: hh1,
minute: mm1,
second: ss1
};
}
function setTitle(title) {
document.title = title;
if (envi.isIOS) {
//兼容ios微信中无法修改document.title的情况
var ifr = $('<iframe src="/favicon.ico"></iframe>');
ifr.on('load', function () {
setTimeout(function () {
ifr.off('load').remove();
}, 0);
}).appendTo($('body'));
}
}
//根据滚动更新进度条
(function() {
var $w = $(window);
var $prog2 = $('.progress-indicator-2');
var wh = $w.height();
var h = $('body').height();
var sHeight = h - wh;
$w.on('scroll', function() {
window.requestAnimationFrame(function(){
var perc = Math.max(0, Math.min(1, $w.scrollTop() / sHeight));
updateProgress(perc);
});
});
function updateProgress(perc) {
$prog2.css({width: perc * 100 + '%'});
ditto.save_progress && store.set('page-progress', perc);
}
}());
/**
* 将canvas转化为image格式
* @param {string} cId
* @return {object HTMLImageElement}
* @example
* canvasToImg('canvas') canvasToImg('#canvarsId')
*/
function canvasToImg(cId){
let canvas = document.querySelector(cId)
if (!canvas || !canvas.toDataURL) return new Image()
let imgData = canvas.toDataURL('image/png'),
imgs= new Image();
imgs.src=imgData;
return imgs
}
Date.prototype.format = function(b) {
var c = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
S: this.getMilliseconds(),
"w+": Date.getWeek(this.getDay())
};
if (/(y+)/.test(b)) {
b = b.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length))
}
for (var a in c) {
if (new RegExp("(" + a + ")").test(b)) {
b = b.replace(RegExp.$1, RegExp.$1.length == 1 ? c[a] : ("00" + c[a]).substr(("" + c[a]).length))
}
}
return b
}
Date.getWeek = function(a) {
this.aWeek = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
return this.aWeek[a]
}
/**
* 生成一个唯一的guid
* @return {string}
* @example
* // 7f603b20-17ff-4f47-aeb9-e7996de04939
* util.guid();
* @see http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
*/
function guid () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
let jsLimit
function loadScript (url, success, error) {
if (Lizard.isHybrid) {
return
}
if (!jsLimit) {
jsLimit = new Set()
}
if (jsLimit.has(url)) {
return;
}
jsLimit.add(url);
var node = document.createElement('script');
node.addEventListener('load', success, false);
node.addEventListener('error', function() {
node.parentNode.removeChild(node);
jsLimit.delete(url);
if (error) {
error();
}
}, false);
if (url.includes('lechebang')) {
node.crossOrigin = 'anonymous';
}
node.src = url;
document.querySelector('head').appendChild(node);
return node;
}
function pick (obj, keys) {
let result = {}
if (!obj) {
return result
}
keys.forEach((item) => {
if (obj.hasOwnProperty(item)) {
result[item] = obj[item]
}
})
return result
}
function getCurrentYear () {
var now = new Date(Date.now());
return now.getFullYear();
}
function isLeapYear (year) {
if (year % 100 === 0) {
if (year % 400 === 0) {
return true;
}
} else if (year % 4 === 0) {
return true;
}
return false;
}
function getDaysInMonth (year, month) {
return [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
/**
* 深度clone
* @example
* util.noopPromise()
*/
function cloneDeep (obj) {
if (!_.isObject(obj)) return obj;
let result
if (Array.isArray(obj)) {
result = []
obj.forEach((item) => {
result.push(cloneDeep(item))
})
return result
}
result = {}
for (let key in obj) {
let item = obj[key]
if (_.isObject(item)) {
result[key] = cloneDeep(item)
} else {
result[key] = item
}
}
return result
}
/**
* 获取两个高德坐标的距离, 后一个点,不传,默认为用户坐标
* @return {null|number} 距离多少米,没有定位信息,返回null
* @example
* util.getDistance(31.282055633974, 121.379623888259)
* util.getDistance(latGd, lonGd)
*/
function getDistance (endLat, endLon, startLat, startLon) {
if (!startLat) {
let address = Lizard.state.address
if (address && address.lat) {
startLat = address.lat
startLon = address.lon
}
}
// 没有定位
if (!startLat) {
return null
}
const PI = Math.PI
let lon1 = (PI / 180) * startLon
let lon2 = (PI / 180) * endLon
let lat1 = (PI / 180) * startLat
let lat2 = (PI / 180) * endLat
// 地球半径
let R = 6378.137;
// 两点间距离 km,如果想要米的话,结果*1000就可以了
let d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;
return parseInt(d * 1000, 10)
}
/**
* 追加url参数
* @param {string} url url参数
* @param {string|object} key 名字或者对象
* @param {string} value 值
* @return {string} 返回新的url
* @example
* util.appendQuery('lechebang.com', 'id', 3);
* util.appendQuery('lechebang.com?key=value', { cityId:2,cityName: '北京'});
*/
function appendQuery (url, key, value) {
var options = key;
if (typeof options == 'string') {
options = {};
options[key] = value;
}
options = $.param(options);
if (url.includes('?')) {
url += '&' + options
} else {
url += '?' + options
}
return url;
}
function deleteKey (obj, arr) {
arr.forEach(function(item) {
delete obj[item];
});
return obj
}
function loadImg (url) {
return new Promise((resolve, reject) => {
let img = new Image()
img.addEventListener('load', function() {
resolve([img.width, img.height])
}, false)
img.addEventListener('error', reject, false)
img.src = url
})
}
const repeat = (str, n) => {
let res = ''
while (n) {
if (n % 2 === 1) res += str
if (n > 1) str += str
n >>= 1
}
return res
}
/**
* Parse simple path.
*/
const bailRE = /[^\w.$]/
export function parsePath (path: string): any {
if (bailRE.test(path)) {
return
}
const segments = path.split('.')
return function (obj) {
for (let i = 0; i < segments.length; i++) {
if (!obj) return
obj = obj[segments[i]]
}
return obj
}
}
/**
* Check if a string starts with $ or _
*/
export function isReserved (str) {
const c = (str + '').charCodeAt(0)
return c === 0x24 || c === 0x5F
}
/**
* Helper that recursively merges two data objects together.
*/
function mergeData (to, from) {
if (!from) return to
let key, toVal, fromVal
const keys = Object.keys(from)
for (let i = 0; i < keys.length; i++) {
key = keys[i]
toVal = to[key]
fromVal = from[key]
if (!hasOwn(to, key)) {
set(to, key, fromVal)
} else if (isPlainObject(toVal) && isPlainObject(fromVal)) {
mergeData(toVal, fromVal)
}
}
return to
}
function isNative (Ctor) {
return typeof Ctor === 'function' && /native code/.test(Ctor.toString())
}
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
_.throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = _.now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
function looseEqual (a, b) {
var isObjectA = isObject(a);
var isObjectB = isObject(b);
if (isObjectA && isObjectB) {
return JSON.stringify(a) === JSON.stringify(b)
} else if (!isObjectA && !isObjectB) {
return String(a) === String(b)
} else {
return false
}
}
function isReserved (str) {
var c = (str + '').charCodeAt(0);
return c === 0x24 || c === 0x5F
}
Promise.prototype.finally = function (callback) {
let P = this.constructor;
return this.then(
value => P.resolve(callback()).then(() => value),
reason => P.resolve(callback()).then(() => { throw reason })
);
};