visual-buried-point-platform-h5 1.3.4

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.
@@ -0,0 +1,574 @@
1
+ import { Config } from "../config";
2
+ import { GlobalVal } from "../config/global";
3
+
4
+ export const noop = function () {};
5
+
6
+ export function queryString(obj: object): string {
7
+ return encodeURIComponent(JSON.stringify(obj));
8
+ }
9
+
10
+ export function isObject(obj: any): boolean {
11
+ if (
12
+ Object.prototype.toString.call(obj) == "[object Object]" ||
13
+ Object.prototype.toString.call(obj) == "[object Array]"
14
+ ) {
15
+ return true;
16
+ }
17
+ return false;
18
+ }
19
+
20
+ export function randomString() {
21
+ for (
22
+ var e, t, n = 20, r = new Array(n), a = Date.now().toString(36).split("");
23
+ n-- > 0;
24
+
25
+ )
26
+ (t = (e = (36 * Math.random()) | 0).toString(36)),
27
+ (r[n] = e % 3 ? t : t.toUpperCase());
28
+ for (var i = 0; i < 8; i++) r.splice(3 * i + 2, 0, a[i]);
29
+ return r.join("");
30
+ }
31
+
32
+ // 将{ method: 'get', state: '200' }转为?method=get&state=200
33
+ export function serialize(obj) {
34
+ var str = [];
35
+ for (var p in obj)
36
+ if (obj.hasOwnProperty(p)) {
37
+ str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
38
+ }
39
+ return str.join("&");
40
+ }
41
+
42
+ export function each(data, fn) {
43
+ var n = 0,
44
+ r = data.length;
45
+ if (isTypeOf(data, "Array"))
46
+ for (; n < r && !1 !== fn.call(data[n], data[n], n); n++);
47
+ else for (var m in data) if (!1 === fn.call(data[m], data[m], m)) break;
48
+ return data;
49
+ }
50
+
51
+ /**
52
+ * 是否是某类型
53
+ *
54
+ * @export
55
+ * @param {*} data
56
+ * @param {*} type
57
+ * @returns 有type就返回true/false,没有就返回对于类型
58
+ */
59
+ export function isTypeOf(data: any, type?: string) {
60
+ var n = Object.prototype.toString.call(data).substring(8).replace("]", "");
61
+ return type ? n === type : n;
62
+ }
63
+
64
+ export const on = function (event, fn, remove?) {
65
+ window.addEventListener
66
+ ? window.addEventListener(
67
+ event,
68
+ function a(i) {
69
+ remove && window.removeEventListener(event, a, true),
70
+ fn.call(this, i);
71
+ },
72
+ true
73
+ )
74
+ : window.attachEvent &&
75
+ window.attachEvent("on" + event, function i(a) {
76
+ remove && window.detachEvent("on" + event, i), fn.call(this, a);
77
+ });
78
+ };
79
+
80
+ export const off = function (event, fn) {
81
+ return fn
82
+ ? (window.removeEventListener
83
+ ? window.removeEventListener(event, fn)
84
+ : window.detachEvent && window.detachEvent(event, fn),
85
+ this)
86
+ : this;
87
+ };
88
+
89
+ export const parseHash = function (e: string) {
90
+ let defaultPath =
91
+ location.pathname.toLowerCase() === "/"
92
+ ? "[index]"
93
+ : location.pathname.toLowerCase();
94
+ return (e ? parseUrl(e.replace(/^#\/?/, "")) : "") || defaultPath;
95
+ };
96
+
97
+ export const parseUrl = function (e: string) {
98
+ return e && "string" == typeof e
99
+ ? e.replace(/^(https?:)?\/\//, "").replace(/\?.*$/, "")
100
+ : "";
101
+ };
102
+
103
+ // 函数toString方法
104
+ export const fnToString = function (e: string) {
105
+ return function () {
106
+ return e + "() { [native code] }";
107
+ };
108
+ };
109
+
110
+ export const warn: any = (function () {
111
+ var e = "object" == typeof console ? console.warn : noop;
112
+ try {
113
+ var t = {
114
+ warn: e,
115
+ };
116
+ t.warn.call(t);
117
+ } catch (n) {
118
+ return noop;
119
+ }
120
+ return e;
121
+ })();
122
+
123
+ // 自定义事件,并dispatch
124
+ export const dispatchCustomEvent = function (e, t) {
125
+ var r;
126
+ window.CustomEvent
127
+ ? (r = new CustomEvent(e, {
128
+ detail: t,
129
+ }))
130
+ : ((r = window.document.createEvent("HTMLEvents")).initEvent(e, !1, !0),
131
+ (r.detail = t));
132
+
133
+ window.dispatchEvent(r);
134
+ };
135
+
136
+ // group::key
137
+ export const splitGroup = function (e: string) {
138
+ var n = e.split("::");
139
+ return n.length > 1
140
+ ? {
141
+ group: n[0],
142
+ key: n[1],
143
+ }
144
+ : {
145
+ group: "default_group",
146
+ key: n[0],
147
+ };
148
+ };
149
+
150
+ // HACK: 在IE浏览器及猎豹浏览器中,对象不支持findIndex的问题
151
+ export const findIndex = function (arr, fn) {
152
+ return arr.reduce(function (carry, item, idx) {
153
+ if (fn(item, idx)) {
154
+ return idx;
155
+ }
156
+ return carry;
157
+ }, -1);
158
+ };
159
+
160
+ // 检查是否是Edge浏览器
161
+ export const checkEdge = function () {
162
+ var isEdge = navigator.userAgent.indexOf("Edge") > -1;
163
+ return isEdge;
164
+ };
165
+
166
+ export const isInIframe = self != top;
167
+
168
+ // 判断IOS或者Android 环境
169
+ export const isPlatform = () => {
170
+ const ua = navigator.userAgent;
171
+ const isAndroid = ua.indexOf("Android") > -1 || ua.indexOf("Linux") > -1;
172
+ const isIOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
173
+ if (isAndroid) {
174
+ return "android";
175
+ }
176
+ if (isIOS) {
177
+ return "ios";
178
+ }
179
+ return null;
180
+ };
181
+
182
+ // 获取元素的绝对位置坐标(像对于浏览器视区左上角)
183
+ function getElementViewPosition(element) {
184
+ //计算x坐标
185
+ let currentActualLeft = element?.offsetLeft || 0;
186
+ let parentActualLeft = element?.offsetParent || null;
187
+ while (parentActualLeft !== null) {
188
+ currentActualLeft +=
189
+ (parentActualLeft?.offsetLeft || 0) + (parentActualLeft?.clientLeft || 0);
190
+ parentActualLeft = parentActualLeft?.offsetParent;
191
+ }
192
+ let elementScrollLeft;
193
+ if (document.compatMode == "BackCompat") {
194
+ elementScrollLeft = document.body.scrollLeft;
195
+ } else {
196
+ elementScrollLeft = document.documentElement.scrollLeft;
197
+ }
198
+ const left = currentActualLeft - elementScrollLeft;
199
+
200
+ //计算y坐标
201
+ let currentActualTop = element?.offsetTop || 0;
202
+ let parentActual = element?.offsetParent || null;
203
+ while (parentActual !== null) {
204
+ currentActualTop +=
205
+ (parentActual?.offsetTop || 0) + (parentActual?.clientTop || 0);
206
+ parentActual = parentActual?.offsetParent;
207
+ }
208
+ let elementScrollTop;
209
+ if (document.compatMode == "BackCompat") {
210
+ elementScrollTop = document.body.scrollTop;
211
+ } else {
212
+ elementScrollTop = document.documentElement.scrollTop;
213
+ }
214
+ const right = currentActualTop - elementScrollTop;
215
+ //返回结果
216
+ return { x: left, y: right };
217
+ }
218
+
219
+ /**
220
+ * webview加载本地资源 对URL做特殊处理
221
+ */
222
+ export function getLocationHref(): string {
223
+ const platform = isPlatform();
224
+ const oldLocationUrl: string = location.href.split("?")[0];
225
+ let newLocationUrl: string = "";
226
+ if (platform && platform === "ios") {
227
+ if (
228
+ oldLocationUrl.startsWith("file") &&
229
+ oldLocationUrl.indexOf("dist") > -1
230
+ ) {
231
+ newLocationUrl = oldLocationUrl.split("dist")[1];
232
+ } else {
233
+ newLocationUrl = oldLocationUrl;
234
+ }
235
+ } else {
236
+ newLocationUrl = oldLocationUrl;
237
+ }
238
+ return newLocationUrl;
239
+ }
240
+
241
+ /**
242
+ * 重写history
243
+ */
244
+ export function rewriteHistory(type) {
245
+ // 先将原函数存放起来
246
+ let origin = window.history[type];
247
+ // 当window.history[type]函数被执行时,这个函数就会被执行
248
+ return function () {
249
+ // 执行原函数
250
+ let rs = origin.apply(this, arguments);
251
+ // 定义一个自定义事假
252
+ let e: any = new Event(type.toLocaleLowerCase());
253
+ // 把默认参数,绑定到自定义事件上,new Event返回的结果,自身上是没有arguments的
254
+ e.arguments = arguments;
255
+ // 触发自定义事件,把载荷传给自定义事件
256
+ window.dispatchEvent(e);
257
+ return rs;
258
+ };
259
+ }
260
+
261
+ /**
262
+ * 分割访问 url & 参数
263
+ */
264
+ export function getParams(url: string, item?: any) {
265
+ let dataMap = JSON.parse(JSON.stringify(item.dataMap));
266
+ console.log("url ", url);
267
+ console.log("item ", item);
268
+ var params = {};
269
+ const queryString = decodeURIComponent(url).split("?")[1];
270
+ if (queryString) {
271
+ const pairs = queryString.split("&");
272
+ for (let pair of pairs) {
273
+ const [key, value] = pair.split("=");
274
+ if (key in dataMap) {
275
+ params[dataMap[key]] = value;
276
+ } else {
277
+ params[key] = value;
278
+ }
279
+ }
280
+ }
281
+ return Object.keys(params).length === 0 ? "" : JSON.stringify(params);
282
+ }
283
+
284
+ /**
285
+ * UUID
286
+ * 16位:
287
+ * xxxx-4xxx-yxxx-xxxx
288
+ * 32位:
289
+ * xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
290
+ */
291
+ export const randomUUID = () => {
292
+ //方法一
293
+ // let uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
294
+ // let r = (Math.random() * 16) | 0;
295
+ // let v = c === "x" ? r : (r & 0x3) | 0x8;
296
+ // return v.toString(16);
297
+ // });
298
+ // return uuid;
299
+ //方法二
300
+ var d = new Date().getTime();
301
+ if (window.performance && typeof window.performance.now === "function") {
302
+ d += performance.now();
303
+ }
304
+ var uuid = "xxxx-4xxx-yxxx-xxxx".replace(/[xy]/g, function (c) {
305
+ var r = (d + Math.random() * 16) % 16 | 0; // d是随机种子
306
+ d = Math.floor(d / 16);
307
+ return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
308
+ });
309
+ return uuid;
310
+ };
311
+
312
+ /**
313
+ * 计算时间戳间隔时间
314
+ * @param time 两个日期的时间戳差值
315
+ * @returns 相差的秒数
316
+ */
317
+ export const dateToSeconds = (time: number) => {
318
+ if (time === 0 || !time) return 0;
319
+ let day = Math.floor(time / 86400000); //天
320
+ let hours = Math.floor((time % 86400000) / 3600000); //时
321
+ let minutes = Math.floor((time % 3600000) / 60000); //分
322
+ let seconds = Math.floor((time % 60000) / 1000); //秒
323
+ return minutes + ":" + seconds;
324
+ };
325
+
326
+ // 获取访问来源环境-> 浏览器嗅探 Win、Mac、Android、IOS、微信小程序
327
+ export function detect() {
328
+ let ua = navigator.userAgent;
329
+ let wxUserAgent: any = navigator;
330
+ // 判断设备类型
331
+ let isMobile =
332
+ /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua);
333
+ // 判断浏览器名字和版本
334
+ getBrowserAndVersion();
335
+ // 获取lsi值
336
+ GlobalVal.lsi = "{{LSI}}";
337
+ // 获取域名
338
+ GlobalVal.packageName = window.location.hostname || "";
339
+ // 获取设备型号
340
+ getDeviceBrandModel();
341
+ // 判断操作系统
342
+ GlobalVal.deviceType = getCurDeviceType();
343
+ // 获取操作系统
344
+ GlobalVal.os = getOperatingSystem(ua);
345
+ let platform = navigator.platform;
346
+ let isWindowsPC = platform.includes("Win");
347
+ let isMacPC = platform.includes("Mac");
348
+ let isAndroid = ua.indexOf("Android") > -1 || ua.indexOf("Linux") > -1;
349
+ let isIOS = /iPad|iPhone|iPod/.test(ua);
350
+ let isHarmonyOS = ua.indexOf("HarmonyOS") !== -1;
351
+ // 判断浏览器类型
352
+ let isWeixinBrowser = /micromessenger/i.test(ua.toLowerCase());
353
+ let isMiniProgram = /miniprogram/i.test(ua.toLowerCase());
354
+ // 判断当前页面环境
355
+ let pageEnv = "other";
356
+ let ecology = "unknown";
357
+ if (isMobile) {
358
+ ecology = "app";
359
+ if (isAndroid) {
360
+ pageEnv = "Android";
361
+ } else if (isIOS) {
362
+ pageEnv = "IOS";
363
+ } else if (isHarmonyOS) {
364
+ pageEnv = "HarmonyOS";
365
+ }
366
+ } else {
367
+ ecology = "brower";
368
+ if (isWindowsPC) {
369
+ pageEnv = "Windowns";
370
+ } else if (isMacPC) {
371
+ pageEnv = "Mac";
372
+ }
373
+ }
374
+ let uatlc = navigator.userAgent.toLowerCase();
375
+ if (uatlc.includes("alipay")) {
376
+ ecology = "alipay";
377
+ } else if (uatlc.includes("unipay")) {
378
+ ecology = "unipay";
379
+ } else if (uatlc.includes("onbuygz")) {
380
+ ecology = "onbuygz";
381
+ } else if (uatlc.includes("onetravel")) {
382
+ ecology = "onetravel";
383
+ }
384
+ if (uatlc.includes("micromessenger")) {
385
+ if (uatlc.includes("miniprogram")) {
386
+ ecology = "wechat";
387
+ } else {
388
+ ecology = "brower";
389
+ }
390
+ }
391
+ GlobalVal.ecology = ecology;
392
+ // 设置数据结构公共信息
393
+ setBaseCommonInfo();
394
+ if (
395
+ ua.toLowerCase().indexOf("micromessenger") > -1 ||
396
+ typeof wxUserAgent.wxuserAgent !== "undefined"
397
+ ) {
398
+ if (ua.toLowerCase().indexOf("miniprogram") > -1) {
399
+ pageEnv = "微信小程序";
400
+ } else {
401
+ pageEnv = "微信公众号";
402
+ }
403
+ }
404
+ return pageEnv;
405
+ }
406
+
407
+ export function getOperatingSystem(uA) {
408
+ let operatingSystem = "other";
409
+ if (/Windows/.test(uA)) {
410
+ operatingSystem = "Windowns";
411
+ } else if (/Macintosh/.test(uA)) {
412
+ operatingSystem = "MAC";
413
+ } else if (/iPhone|iPad|iPod/.test(uA)) {
414
+ operatingSystem = "IOS";
415
+ } else if (/Android/.test(uA)) {
416
+ operatingSystem = "Android";
417
+ } else if (/Linux/.test(uA)) {
418
+ operatingSystem = "Linux";
419
+ }
420
+ return operatingSystem;
421
+ }
422
+
423
+ //------------------------------新的数据结构----------------------------
424
+
425
+ // 新数据结构获取的时间戳转YYYY-MM-dd HH:mm:ss
426
+ export function timeToStr(timestamp) {
427
+ const date = new Date(timestamp);
428
+ const year = date.getFullYear();
429
+ const month =
430
+ date.getMonth() + 1 < 10
431
+ ? "0" + (date.getMonth() + 1)
432
+ : date.getMonth() + 1;
433
+ const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
434
+ const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
435
+ const minute =
436
+ date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
437
+ const second =
438
+ date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
439
+ const formattedDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
440
+ return formattedDate;
441
+ }
442
+
443
+ // 新数据结构获取的设备类型
444
+ export function getCurDeviceType() {
445
+ const userAgent = navigator.userAgent.toLowerCase();
446
+ if (/(windows|win32|win64|wow64)/.test(userAgent)) {
447
+ return 1;
448
+ } else if (/(linux|android)/.test(userAgent)) {
449
+ return 2;
450
+ } else if (/(macintosh|mac os x|iphone|ipad|ipod)/.test(userAgent)) {
451
+ return 2;
452
+ } else {
453
+ return 2;
454
+ }
455
+ }
456
+
457
+ // 新数据结构获取的浏览器和版本
458
+ export function getBrowserAndVersion() {
459
+ const userAgent = navigator.userAgent.toLowerCase();
460
+ let browserName = "";
461
+ let browserVersion = "";
462
+ if (userAgent.includes("firefox")) {
463
+ browserName = "Firefox";
464
+ browserVersion = userAgent.match(/firefox\/([\d.]+)/)[1];
465
+ } else if (userAgent.includes("edg")) {
466
+ browserName = "Microsoft Edge";
467
+ browserVersion = userAgent.match(/edg\/([\d.]+)/)[1];
468
+ } else if (userAgent.includes("chrome")) {
469
+ browserName = "Google Chrome";
470
+ browserVersion = userAgent.match(/chrome\/([\d.]+)/)[1];
471
+ } else if (userAgent.includes("safari")) {
472
+ browserName = "Safari";
473
+ browserVersion = userAgent.match(/version\/([\d.]+)/)[1];
474
+ } else if (userAgent.includes("opera") || userAgent.includes("opr")) {
475
+ browserName = "Opera";
476
+ browserVersion = userAgent.match(/(?:opera|opr)\/([\d.]+)/)[1];
477
+ } else if (userAgent.includes("msie") || userAgent.includes("trident")) {
478
+ browserName = "Internet Explorer";
479
+ browserVersion = userAgent.match(/(?:msie |rv:)(\d+(\.\d+)?)/)[1];
480
+ }
481
+ GlobalVal.browserName = browserName;
482
+ GlobalVal.browserVersion = browserVersion;
483
+ }
484
+
485
+ // 获取新的设备类型和型号
486
+ export function getDeviceBrandModel() {
487
+ const userAgent = navigator.userAgent;
488
+ var pContent = userAgent.match(/\((.*?)\)/)[1];
489
+ var parts = pContent.split(";");
490
+ let brand = parts[1];
491
+ if (brand.includes("iPhone")) {
492
+ var model = brand.replace("CPU ", "").trim();
493
+ GlobalVal.brand = model;
494
+ GlobalVal.osVersion = model;
495
+ } else {
496
+ GlobalVal.brand = brand.trim() + (parts[2] ? parts[2] : "");
497
+ GlobalVal.osVersion = brand.trim();
498
+ }
499
+ // 获取新的操作系统类型 osNew
500
+ if (brand.includes("Win")) {
501
+ GlobalVal.osNew = "Windows";
502
+ } else if (brand.includes("Linux")) {
503
+ GlobalVal.osNew = "Linux";
504
+ } else if (brand.includes("Macintosh")) {
505
+ GlobalVal.osNew = "macOS";
506
+ } else if (brand.includes("Android")) {
507
+ GlobalVal.osNew = "Android";
508
+ } else if (
509
+ brand.includes("iPhone") ||
510
+ brand.includes("iPad") ||
511
+ brand.includes("iPod")
512
+ ) {
513
+ GlobalVal.osNew = "iOS";
514
+ } else if (brand.includes("Harmony")) {
515
+ GlobalVal.osNew = "Harmony OS";
516
+ } else {
517
+ GlobalVal.osNew = "unknown";
518
+ }
519
+ }
520
+
521
+ // 常规浏览器语言和IE浏览器
522
+ const getLang = () => {
523
+ return navigator.language || (navigator as any).userLanguage;
524
+ };
525
+
526
+ // 获取设备的宽高
527
+ const getScreen = () => {
528
+ let w = document.documentElement.clientWidth || document.body.clientWidth;
529
+ let h = document.documentElement.clientHeight || document.body.clientHeight;
530
+ return w + "x" + h;
531
+ };
532
+
533
+ const setBaseCommonInfo = () => {
534
+ let ng = navigator as any;
535
+ let connect = ng.connection;
536
+ GlobalVal.app = {
537
+ name: Config.t_name ? Config.t_name : "",
538
+ packageName: GlobalVal.packageName,
539
+ version: Config.t_version ? Config.t_version : "",
540
+ carrier: "h5",
541
+ ecology: GlobalVal.ecology,
542
+ };
543
+ GlobalVal.device = {
544
+ lang: getLang(),
545
+ brand: GlobalVal.brand,
546
+ os: GlobalVal.osNew,
547
+ osVersion: GlobalVal.osVersion,
548
+ resolution: getScreen(),
549
+ browser: GlobalVal.browserName,
550
+ browserVersion: GlobalVal.browserVersion,
551
+ color: "",
552
+ deviceId: "",
553
+ deviceType: GlobalVal.deviceType,
554
+ network: connect.effectiveType,
555
+ };
556
+ };
557
+
558
+ export const parsePVUVUrl = () => {
559
+ let sourceUrl = location.href;
560
+ let url = "";
561
+ if (sourceUrl && sourceUrl.includes("/#")) {
562
+ url = sourceUrl
563
+ .replace(/^(https?:)?\/\//, "")
564
+ .replace(/\?.*$/, "")
565
+ .split("/#")[1];
566
+ } else {
567
+ url = location.pathname;
568
+ }
569
+ let index = GlobalVal.routerArray.indexOf(url);
570
+ if (index !== -1) {
571
+ GlobalVal.routerArray.splice(index, 1);
572
+ }
573
+ GlobalVal.routerArray.push(url);
574
+ };
@@ -0,0 +1,4 @@
1
+ export const originalProto = XMLHttpRequest.prototype
2
+ export const originalOpen = originalProto.open
3
+ export const originalSet = originalProto.setRequestHeader
4
+ export const originalSend = originalProto.send
package/tsconfig.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "compilerOptions": {
3
+ "removeComments": false,
4
+ "preserveConstEnums": true,
5
+ "sourceMap": true,
6
+ "noUnusedParameters": false,
7
+ "noUnusedLocals": false, //未引用报错开关
8
+ "noImplicitThis": false, //this 必须有引用地址
9
+ "diagnostics": true, //诊断
10
+ "listFiles": true,
11
+ "pretty": false,
12
+ "module": "es2015",
13
+ "moduleResolution": "node",
14
+ "stripInternal": false,
15
+ "target": "ES5",
16
+ "outDir": "./dist",
17
+ "lib": [
18
+ "es5",
19
+ "es2015.iterable",
20
+ "es2015.collection",
21
+ "es2015.promise",
22
+ "es2015.symbol",
23
+ "es2015.symbol.wellknown",
24
+ "dom"
25
+ ]
26
+ },
27
+ "formatCodeOptions": {
28
+ "indentSize": 2,
29
+ "tabSize": 2
30
+ },
31
+ "include": ["src/**/*", "index.d.ts", "config/webpack.dev.js"],
32
+ "bazelOptions": {
33
+ "suppressTsconfigOverrideWarnings": true
34
+ }
35
+ }