@gctrack/uni 0.1.0 → 0.1.2
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/dist/index.cjs +17 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +17 -18
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -6,19 +6,19 @@ const uniAdapter = {
|
|
|
6
6
|
getStorage: (k) => {
|
|
7
7
|
try {
|
|
8
8
|
return uni.getStorageSync(k);
|
|
9
|
-
} catch {
|
|
9
|
+
} catch (e) {
|
|
10
10
|
return null;
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
setStorage: (k, v) => {
|
|
14
14
|
try {
|
|
15
15
|
uni.setStorageSync(k, v);
|
|
16
|
-
} catch {}
|
|
16
|
+
} catch (e) {}
|
|
17
17
|
},
|
|
18
18
|
removeStorage: (k) => {
|
|
19
19
|
try {
|
|
20
20
|
uni.removeStorageSync(k);
|
|
21
|
-
} catch {}
|
|
21
|
+
} catch (e) {}
|
|
22
22
|
},
|
|
23
23
|
request: (opts) => new Promise((resolve, reject) => {
|
|
24
24
|
uni.request({
|
|
@@ -36,7 +36,7 @@ const uniAdapter = {
|
|
|
36
36
|
getSystemInfo: () => {
|
|
37
37
|
try {
|
|
38
38
|
return uni.getSystemInfoSync();
|
|
39
|
-
} catch {
|
|
39
|
+
} catch (e) {
|
|
40
40
|
return {};
|
|
41
41
|
}
|
|
42
42
|
},
|
|
@@ -44,15 +44,15 @@ const uniAdapter = {
|
|
|
44
44
|
try {
|
|
45
45
|
const pages = getCurrentPages();
|
|
46
46
|
const top = pages[pages.length - 1];
|
|
47
|
-
const route = top
|
|
48
|
-
const options = top
|
|
47
|
+
const route = top && top.route || "";
|
|
48
|
+
const options = top && top.options || {};
|
|
49
49
|
const query = Object.keys(options).map((k) => `${k}=${options[k]}`).join("&");
|
|
50
50
|
return {
|
|
51
51
|
pathname: "/" + route,
|
|
52
52
|
url: "/" + route + (query ? "?" + query : ""),
|
|
53
53
|
title: ""
|
|
54
54
|
};
|
|
55
|
-
} catch {
|
|
55
|
+
} catch (e) {
|
|
56
56
|
return {
|
|
57
57
|
pathname: "",
|
|
58
58
|
url: "",
|
|
@@ -63,7 +63,7 @@ const uniAdapter = {
|
|
|
63
63
|
onUnload: (cb) => {
|
|
64
64
|
try {
|
|
65
65
|
uni.onAppHide && uni.onAppHide(cb);
|
|
66
|
-
} catch {}
|
|
66
|
+
} catch (e) {}
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
69
|
/**
|
|
@@ -94,7 +94,7 @@ function detectPlatform() {
|
|
|
94
94
|
if (p.includes("mp-baidu") || p.includes("baidu")) return _gctrack_protocol.Platform.BaiduMp;
|
|
95
95
|
if (p.includes("h5")) return _gctrack_protocol.Platform.H5;
|
|
96
96
|
}
|
|
97
|
-
} catch {}
|
|
97
|
+
} catch (e) {}
|
|
98
98
|
return _gctrack_protocol.Platform.WeixinMp;
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
@@ -104,23 +104,23 @@ function detectPlatform() {
|
|
|
104
104
|
* App.onError 需在 App.vue 里手动调用 tracker.track(见 design.md 13.2)。
|
|
105
105
|
*/
|
|
106
106
|
function installErrorCapture(Vue, tracker) {
|
|
107
|
-
if (Vue
|
|
107
|
+
if (Vue && Vue.config) Vue.config.errorHandler = (err, vm, info) => {
|
|
108
108
|
tracker.track(_gctrack_protocol.EventType.VueError, {
|
|
109
|
-
message: err
|
|
110
|
-
stack: err
|
|
109
|
+
message: err && err.message || String(err),
|
|
110
|
+
stack: err ? err.stack : void 0,
|
|
111
111
|
info,
|
|
112
|
-
componentName: vm
|
|
112
|
+
componentName: vm && vm.$options ? vm.$options.name || vm.$options._componentTag : void 0
|
|
113
113
|
});
|
|
114
114
|
};
|
|
115
115
|
try {
|
|
116
116
|
uni.onUnhandledRejection && uni.onUnhandledRejection((res) => {
|
|
117
|
-
const reason = res
|
|
117
|
+
const reason = res ? res.reason : void 0;
|
|
118
118
|
tracker.track(_gctrack_protocol.EventType.PromiseError, {
|
|
119
|
-
message: reason
|
|
120
|
-
stack: reason
|
|
119
|
+
message: reason && reason.message || String(reason),
|
|
120
|
+
stack: reason ? reason.stack : void 0
|
|
121
121
|
});
|
|
122
122
|
});
|
|
123
|
-
} catch {}
|
|
123
|
+
} catch (e) {}
|
|
124
124
|
}
|
|
125
125
|
//#endregion
|
|
126
126
|
exports.createUniSendFn = createUniSendFn;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["Platform","EventType"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @gctrack/uni —— 小程序多端适配\n *\n * 小程序无 window/document/navigator/sendBeacon/fetch/PerformanceObserver,\n * 本包提供 PlatformAdapter(存储/网络/生命周期/设备/页面)的 uni 实现,\n * 以及错误捕获(App.onError / unhandledRejection / Vue.errorHandler)一键挂载。\n *\n * 详见 design.md 5.5。完整实现(PV、点击、白屏耗时等)在「@gctrack/uni 完善」阶段补充。\n */\nimport type { Tracker } from '@gctrack/core'\nimport { EventType, Platform } from '@gctrack/protocol'\n\n// 小程序运行时注入的全局(各端一致提供 uni / getCurrentPages)\ndeclare const uni: any\ndeclare function getCurrentPages(): any[]\n\n/** 平台适配器接口 —— core 通过它解耦浏览器 API,详见 design.md 5.5 */\nexport interface PlatformAdapter {\n getStorage(key: string): any\n setStorage(key: string, value: any): void\n removeStorage(key: string): void\n request(opts: {\n url: string\n method?: string\n data?: any\n header?: Record<string, string>\n }): Promise<{ statusCode: number; data: any }>\n getSystemInfo(): Record<string, any>\n getPageInfo(): { pathname: string; url: string; title: string }\n onUnload(cb: () => void): void\n}\n\n/** uni 适配实现 —— 全端用 uni.* 标准 API,不写 #ifdef */\nexport const uniAdapter: PlatformAdapter = {\n getStorage: (k) => {\n try {\n return uni.getStorageSync(k)\n } catch {\n return null\n }\n },\n setStorage: (k, v) => {\n try {\n uni.setStorageSync(k, v)\n } catch {\n /* ignore */\n }\n },\n removeStorage: (k) => {\n try {\n uni.removeStorageSync(k)\n } catch {\n /* ignore */\n }\n },\n request: (opts) =>\n new Promise((resolve, reject) => {\n uni.request({\n url: opts.url,\n method: (opts.method || 'POST') as any,\n data: opts.data,\n header: opts.header || {},\n success: (res: any) => resolve({ statusCode: res.statusCode, data: res.data }),\n fail: (err: any) => reject(err),\n })\n }),\n getSystemInfo: () => {\n try {\n return uni.getSystemInfoSync()\n } catch {\n return {}\n }\n },\n getPageInfo: () => {\n try {\n const pages = getCurrentPages()\n const top = pages[pages.length - 1]\n const route: string = top
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Platform","EventType"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @gctrack/uni —— 小程序多端适配\n *\n * 小程序无 window/document/navigator/sendBeacon/fetch/PerformanceObserver,\n * 本包提供 PlatformAdapter(存储/网络/生命周期/设备/页面)的 uni 实现,\n * 以及错误捕获(App.onError / unhandledRejection / Vue.errorHandler)一键挂载。\n *\n * 详见 design.md 5.5。完整实现(PV、点击、白屏耗时等)在「@gctrack/uni 完善」阶段补充。\n */\nimport type { Tracker } from '@gctrack/core'\nimport { EventType, Platform } from '@gctrack/protocol'\n\n// 小程序运行时注入的全局(各端一致提供 uni / getCurrentPages)\ndeclare const uni: any\ndeclare function getCurrentPages(): any[]\n\n/** 平台适配器接口 —— core 通过它解耦浏览器 API,详见 design.md 5.5 */\nexport interface PlatformAdapter {\n getStorage(key: string): any\n setStorage(key: string, value: any): void\n removeStorage(key: string): void\n request(opts: {\n url: string\n method?: string\n data?: any\n header?: Record<string, string>\n }): Promise<{ statusCode: number; data: any }>\n getSystemInfo(): Record<string, any>\n getPageInfo(): { pathname: string; url: string; title: string }\n onUnload(cb: () => void): void\n}\n\n/** uni 适配实现 —— 全端用 uni.* 标准 API,不写 #ifdef */\nexport const uniAdapter: PlatformAdapter = {\n getStorage: (k) => {\n try {\n return uni.getStorageSync(k)\n } catch (e) {\n return null\n }\n },\n setStorage: (k, v) => {\n try {\n uni.setStorageSync(k, v)\n } catch (e) {\n /* ignore */\n }\n },\n removeStorage: (k) => {\n try {\n uni.removeStorageSync(k)\n } catch (e) {\n /* ignore */\n }\n },\n request: (opts) =>\n new Promise((resolve, reject) => {\n uni.request({\n url: opts.url,\n method: (opts.method || 'POST') as any,\n data: opts.data,\n header: opts.header || {},\n success: (res: any) => resolve({ statusCode: res.statusCode, data: res.data }),\n fail: (err: any) => reject(err),\n })\n }),\n getSystemInfo: () => {\n try {\n return uni.getSystemInfoSync()\n } catch (e) {\n return {}\n }\n },\n getPageInfo: () => {\n try {\n const pages = getCurrentPages()\n const top = pages[pages.length - 1]\n const route: string = (top && top.route) || ''\n const options = (top && top.options) || {}\n const query = Object.keys(options)\n .map((k) => `${k}=${options[k]}`)\n .join('&')\n return {\n pathname: '/' + route,\n url: '/' + route + (query ? '?' + query : ''),\n title: '',\n }\n } catch (e) {\n return { pathname: '', url: '', title: '' }\n }\n },\n onUnload: (cb) => {\n // 兜底:App.onHide / Page.onHide 由 installAppLifecycle 接管,这里注册 uni 级监听\n try {\n uni.onAppHide && uni.onAppHide(cb)\n } catch (e) {\n /* ignore */\n }\n },\n}\n\n/**\n * 构造基于 uni.request 的 sendFn,供 createTracker({ sendFn }) 使用。\n * 小程序无 sendBeacon/fetch,统一走 uni.request;header 值强制 String()(XHS 要求)。\n */\nexport function createUniSendFn(reportUrl: string, appKey?: string) {\n return (events: any[]): Promise<void> =>\n uniAdapter\n .request({\n url: reportUrl,\n method: 'POST',\n data: events,\n header: {\n 'content-type': 'application/json',\n 'X-App-Key': appKey ? String(appKey) : '',\n },\n })\n .then(() => undefined)\n}\n\n/** 根据 uni 运行环境推断 Platform 标识(写入 context.platform) */\nexport function detectPlatform(): string {\n try {\n const info = uni.getSystemInfoSync()\n // uni-app 注入 UNI_PLATFORM / uniPlatform\n const p = info.uniPlatform || info.platform\n if (typeof p === 'string') {\n if (p.includes('mp-weixin') || p.includes('weixin')) return Platform.WeixinMp\n if (p.includes('mp-alipay') || p.includes('alipay')) return Platform.AlipayMp\n if (p.includes('mp-jd') || p.includes('jd')) return Platform.JdMp\n if (p.includes('mp-xhs') || p.includes('xhs')) return Platform.XhsMp\n if (p.includes('mp-baidu') || p.includes('baidu')) return Platform.BaiduMp\n if (p.includes('h5')) return Platform.H5\n }\n } catch (e) {\n /* ignore */\n }\n return Platform.WeixinMp\n}\n\n/**\n * 错误捕获一键挂载:\n * - Vue.config.errorHandler → vue_error\n * - uni.onUnhandledRejection → promise_error\n * App.onError 需在 App.vue 里手动调用 tracker.track(见 design.md 13.2)。\n */\nexport function installErrorCapture(Vue: any, tracker: Tracker): void {\n if (Vue && Vue.config) {\n Vue.config.errorHandler = (err: any, vm: any, info: string) => {\n tracker.track(EventType.VueError, {\n message: (err && err.message) || String(err),\n stack: err ? err.stack : undefined,\n info,\n componentName: vm && vm.$options ? (vm.$options.name || vm.$options._componentTag) : undefined,\n })\n }\n }\n try {\n uni.onUnhandledRejection &&\n uni.onUnhandledRejection((res: any) => {\n const reason = res ? res.reason : undefined\n tracker.track(EventType.PromiseError, {\n message: (reason && reason.message) || String(reason),\n stack: reason ? reason.stack : undefined,\n })\n })\n } catch (e) {\n /* ignore */\n }\n}\n"],"mappings":";;;;AAiCA,MAAa,aAA8B;CACzC,aAAa,MAAM;EACjB,IAAI;GACF,OAAO,IAAI,eAAe,EAAE;WACrB,GAAG;GACV,OAAO;;;CAGX,aAAa,GAAG,MAAM;EACpB,IAAI;GACF,IAAI,eAAe,GAAG,EAAE;WACjB,GAAG;;CAId,gBAAgB,MAAM;EACpB,IAAI;GACF,IAAI,kBAAkB,EAAE;WACjB,GAAG;;CAId,UAAU,SACR,IAAI,SAAS,SAAS,WAAW;EAC/B,IAAI,QAAQ;GACV,KAAK,KAAK;GACV,QAAS,KAAK,UAAU;GACxB,MAAM,KAAK;GACX,QAAQ,KAAK,UAAU,EAAE;GACzB,UAAU,QAAa,QAAQ;IAAE,YAAY,IAAI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AE,SAAS,KAAK,EAAE,OAAOA,kBAAAA,SAAS;GAC7D,IAAI,EAAE,SAAS,SAAS,IAAI,EAAE,SAAS,MAAM,EAAE,OAAOA,kBAAAA,SAAS;GAC/D,IAAI,EAAE,SAAS,WAAW,IAAI,EAAE,SAAS,QAAQ,EAAE,OAAOA,kBAAAA,SAAS;GACnE,IAAI,EAAE,SAAS,KAAK,EAAE,OAAOA,kBAAAA,SAAS;;UAEjC,GAAG;CAGZ,OAAOA,kBAAAA,SAAS;;;;;;;;AASlB,SAAgB,oBAAoB,KAAU,SAAwB;CACpE,IAAI,OAAO,IAAI,QACb,IAAI,OAAO,gBAAgB,KAAU,IAAS,SAAiB;EAC7D,QAAQ,MAAMC,kBAAAA,UAAU,UAAU;GAChC,SAAU,OAAO,IAAI,WAAY,OAAO,IAAI;GAC5C,OAAO,MAAM,IAAI,QAAQ,KAAA;GACzB;GACA,eAAe,MAAM,GAAG,WAAY,GAAG,SAAS,QAAQ,GAAG,SAAS,gBAAiB,KAAA;GACtF,CAAC;;CAGN,IAAI;EACF,IAAI,wBACF,IAAI,sBAAsB,QAAa;GACrC,MAAM,SAAS,MAAM,IAAI,SAAS,KAAA;GAClC,QAAQ,MAAMA,kBAAAA,UAAU,cAAc;IACpC,SAAU,UAAU,OAAO,WAAY,OAAO,OAAO;IACrD,OAAO,SAAS,OAAO,QAAQ,KAAA;IAChC,CAAC;IACF;UACG,GAAG"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "node:module";
|
|
2
1
|
import { EventType, Platform } from "@gctrack/protocol";
|
|
3
2
|
//#region src/index.ts
|
|
4
3
|
/** uni 适配实现 —— 全端用 uni.* 标准 API,不写 #ifdef */
|
|
@@ -6,19 +5,19 @@ const uniAdapter = {
|
|
|
6
5
|
getStorage: (k) => {
|
|
7
6
|
try {
|
|
8
7
|
return uni.getStorageSync(k);
|
|
9
|
-
} catch {
|
|
8
|
+
} catch (e) {
|
|
10
9
|
return null;
|
|
11
10
|
}
|
|
12
11
|
},
|
|
13
12
|
setStorage: (k, v) => {
|
|
14
13
|
try {
|
|
15
14
|
uni.setStorageSync(k, v);
|
|
16
|
-
} catch {}
|
|
15
|
+
} catch (e) {}
|
|
17
16
|
},
|
|
18
17
|
removeStorage: (k) => {
|
|
19
18
|
try {
|
|
20
19
|
uni.removeStorageSync(k);
|
|
21
|
-
} catch {}
|
|
20
|
+
} catch (e) {}
|
|
22
21
|
},
|
|
23
22
|
request: (opts) => new Promise((resolve, reject) => {
|
|
24
23
|
uni.request({
|
|
@@ -36,7 +35,7 @@ const uniAdapter = {
|
|
|
36
35
|
getSystemInfo: () => {
|
|
37
36
|
try {
|
|
38
37
|
return uni.getSystemInfoSync();
|
|
39
|
-
} catch {
|
|
38
|
+
} catch (e) {
|
|
40
39
|
return {};
|
|
41
40
|
}
|
|
42
41
|
},
|
|
@@ -44,15 +43,15 @@ const uniAdapter = {
|
|
|
44
43
|
try {
|
|
45
44
|
const pages = getCurrentPages();
|
|
46
45
|
const top = pages[pages.length - 1];
|
|
47
|
-
const route = top
|
|
48
|
-
const options = top
|
|
46
|
+
const route = top && top.route || "";
|
|
47
|
+
const options = top && top.options || {};
|
|
49
48
|
const query = Object.keys(options).map((k) => `${k}=${options[k]}`).join("&");
|
|
50
49
|
return {
|
|
51
50
|
pathname: "/" + route,
|
|
52
51
|
url: "/" + route + (query ? "?" + query : ""),
|
|
53
52
|
title: ""
|
|
54
53
|
};
|
|
55
|
-
} catch {
|
|
54
|
+
} catch (e) {
|
|
56
55
|
return {
|
|
57
56
|
pathname: "",
|
|
58
57
|
url: "",
|
|
@@ -63,7 +62,7 @@ const uniAdapter = {
|
|
|
63
62
|
onUnload: (cb) => {
|
|
64
63
|
try {
|
|
65
64
|
uni.onAppHide && uni.onAppHide(cb);
|
|
66
|
-
} catch {}
|
|
65
|
+
} catch (e) {}
|
|
67
66
|
}
|
|
68
67
|
};
|
|
69
68
|
/**
|
|
@@ -94,7 +93,7 @@ function detectPlatform() {
|
|
|
94
93
|
if (p.includes("mp-baidu") || p.includes("baidu")) return Platform.BaiduMp;
|
|
95
94
|
if (p.includes("h5")) return Platform.H5;
|
|
96
95
|
}
|
|
97
|
-
} catch {}
|
|
96
|
+
} catch (e) {}
|
|
98
97
|
return Platform.WeixinMp;
|
|
99
98
|
}
|
|
100
99
|
/**
|
|
@@ -104,23 +103,23 @@ function detectPlatform() {
|
|
|
104
103
|
* App.onError 需在 App.vue 里手动调用 tracker.track(见 design.md 13.2)。
|
|
105
104
|
*/
|
|
106
105
|
function installErrorCapture(Vue, tracker) {
|
|
107
|
-
if (Vue
|
|
106
|
+
if (Vue && Vue.config) Vue.config.errorHandler = (err, vm, info) => {
|
|
108
107
|
tracker.track(EventType.VueError, {
|
|
109
|
-
message: err
|
|
110
|
-
stack: err
|
|
108
|
+
message: err && err.message || String(err),
|
|
109
|
+
stack: err ? err.stack : void 0,
|
|
111
110
|
info,
|
|
112
|
-
componentName: vm
|
|
111
|
+
componentName: vm && vm.$options ? vm.$options.name || vm.$options._componentTag : void 0
|
|
113
112
|
});
|
|
114
113
|
};
|
|
115
114
|
try {
|
|
116
115
|
uni.onUnhandledRejection && uni.onUnhandledRejection((res) => {
|
|
117
|
-
const reason = res
|
|
116
|
+
const reason = res ? res.reason : void 0;
|
|
118
117
|
tracker.track(EventType.PromiseError, {
|
|
119
|
-
message: reason
|
|
120
|
-
stack: reason
|
|
118
|
+
message: reason && reason.message || String(reason),
|
|
119
|
+
stack: reason ? reason.stack : void 0
|
|
121
120
|
});
|
|
122
121
|
});
|
|
123
|
-
} catch {}
|
|
122
|
+
} catch (e) {}
|
|
124
123
|
}
|
|
125
124
|
//#endregion
|
|
126
125
|
export { createUniSendFn, detectPlatform, installErrorCapture, uniAdapter };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @gctrack/uni —— 小程序多端适配\n *\n * 小程序无 window/document/navigator/sendBeacon/fetch/PerformanceObserver,\n * 本包提供 PlatformAdapter(存储/网络/生命周期/设备/页面)的 uni 实现,\n * 以及错误捕获(App.onError / unhandledRejection / Vue.errorHandler)一键挂载。\n *\n * 详见 design.md 5.5。完整实现(PV、点击、白屏耗时等)在「@gctrack/uni 完善」阶段补充。\n */\nimport type { Tracker } from '@gctrack/core'\nimport { EventType, Platform } from '@gctrack/protocol'\n\n// 小程序运行时注入的全局(各端一致提供 uni / getCurrentPages)\ndeclare const uni: any\ndeclare function getCurrentPages(): any[]\n\n/** 平台适配器接口 —— core 通过它解耦浏览器 API,详见 design.md 5.5 */\nexport interface PlatformAdapter {\n getStorage(key: string): any\n setStorage(key: string, value: any): void\n removeStorage(key: string): void\n request(opts: {\n url: string\n method?: string\n data?: any\n header?: Record<string, string>\n }): Promise<{ statusCode: number; data: any }>\n getSystemInfo(): Record<string, any>\n getPageInfo(): { pathname: string; url: string; title: string }\n onUnload(cb: () => void): void\n}\n\n/** uni 适配实现 —— 全端用 uni.* 标准 API,不写 #ifdef */\nexport const uniAdapter: PlatformAdapter = {\n getStorage: (k) => {\n try {\n return uni.getStorageSync(k)\n } catch {\n return null\n }\n },\n setStorage: (k, v) => {\n try {\n uni.setStorageSync(k, v)\n } catch {\n /* ignore */\n }\n },\n removeStorage: (k) => {\n try {\n uni.removeStorageSync(k)\n } catch {\n /* ignore */\n }\n },\n request: (opts) =>\n new Promise((resolve, reject) => {\n uni.request({\n url: opts.url,\n method: (opts.method || 'POST') as any,\n data: opts.data,\n header: opts.header || {},\n success: (res: any) => resolve({ statusCode: res.statusCode, data: res.data }),\n fail: (err: any) => reject(err),\n })\n }),\n getSystemInfo: () => {\n try {\n return uni.getSystemInfoSync()\n } catch {\n return {}\n }\n },\n getPageInfo: () => {\n try {\n const pages = getCurrentPages()\n const top = pages[pages.length - 1]\n const route: string = top
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @gctrack/uni —— 小程序多端适配\n *\n * 小程序无 window/document/navigator/sendBeacon/fetch/PerformanceObserver,\n * 本包提供 PlatformAdapter(存储/网络/生命周期/设备/页面)的 uni 实现,\n * 以及错误捕获(App.onError / unhandledRejection / Vue.errorHandler)一键挂载。\n *\n * 详见 design.md 5.5。完整实现(PV、点击、白屏耗时等)在「@gctrack/uni 完善」阶段补充。\n */\nimport type { Tracker } from '@gctrack/core'\nimport { EventType, Platform } from '@gctrack/protocol'\n\n// 小程序运行时注入的全局(各端一致提供 uni / getCurrentPages)\ndeclare const uni: any\ndeclare function getCurrentPages(): any[]\n\n/** 平台适配器接口 —— core 通过它解耦浏览器 API,详见 design.md 5.5 */\nexport interface PlatformAdapter {\n getStorage(key: string): any\n setStorage(key: string, value: any): void\n removeStorage(key: string): void\n request(opts: {\n url: string\n method?: string\n data?: any\n header?: Record<string, string>\n }): Promise<{ statusCode: number; data: any }>\n getSystemInfo(): Record<string, any>\n getPageInfo(): { pathname: string; url: string; title: string }\n onUnload(cb: () => void): void\n}\n\n/** uni 适配实现 —— 全端用 uni.* 标准 API,不写 #ifdef */\nexport const uniAdapter: PlatformAdapter = {\n getStorage: (k) => {\n try {\n return uni.getStorageSync(k)\n } catch (e) {\n return null\n }\n },\n setStorage: (k, v) => {\n try {\n uni.setStorageSync(k, v)\n } catch (e) {\n /* ignore */\n }\n },\n removeStorage: (k) => {\n try {\n uni.removeStorageSync(k)\n } catch (e) {\n /* ignore */\n }\n },\n request: (opts) =>\n new Promise((resolve, reject) => {\n uni.request({\n url: opts.url,\n method: (opts.method || 'POST') as any,\n data: opts.data,\n header: opts.header || {},\n success: (res: any) => resolve({ statusCode: res.statusCode, data: res.data }),\n fail: (err: any) => reject(err),\n })\n }),\n getSystemInfo: () => {\n try {\n return uni.getSystemInfoSync()\n } catch (e) {\n return {}\n }\n },\n getPageInfo: () => {\n try {\n const pages = getCurrentPages()\n const top = pages[pages.length - 1]\n const route: string = (top && top.route) || ''\n const options = (top && top.options) || {}\n const query = Object.keys(options)\n .map((k) => `${k}=${options[k]}`)\n .join('&')\n return {\n pathname: '/' + route,\n url: '/' + route + (query ? '?' + query : ''),\n title: '',\n }\n } catch (e) {\n return { pathname: '', url: '', title: '' }\n }\n },\n onUnload: (cb) => {\n // 兜底:App.onHide / Page.onHide 由 installAppLifecycle 接管,这里注册 uni 级监听\n try {\n uni.onAppHide && uni.onAppHide(cb)\n } catch (e) {\n /* ignore */\n }\n },\n}\n\n/**\n * 构造基于 uni.request 的 sendFn,供 createTracker({ sendFn }) 使用。\n * 小程序无 sendBeacon/fetch,统一走 uni.request;header 值强制 String()(XHS 要求)。\n */\nexport function createUniSendFn(reportUrl: string, appKey?: string) {\n return (events: any[]): Promise<void> =>\n uniAdapter\n .request({\n url: reportUrl,\n method: 'POST',\n data: events,\n header: {\n 'content-type': 'application/json',\n 'X-App-Key': appKey ? String(appKey) : '',\n },\n })\n .then(() => undefined)\n}\n\n/** 根据 uni 运行环境推断 Platform 标识(写入 context.platform) */\nexport function detectPlatform(): string {\n try {\n const info = uni.getSystemInfoSync()\n // uni-app 注入 UNI_PLATFORM / uniPlatform\n const p = info.uniPlatform || info.platform\n if (typeof p === 'string') {\n if (p.includes('mp-weixin') || p.includes('weixin')) return Platform.WeixinMp\n if (p.includes('mp-alipay') || p.includes('alipay')) return Platform.AlipayMp\n if (p.includes('mp-jd') || p.includes('jd')) return Platform.JdMp\n if (p.includes('mp-xhs') || p.includes('xhs')) return Platform.XhsMp\n if (p.includes('mp-baidu') || p.includes('baidu')) return Platform.BaiduMp\n if (p.includes('h5')) return Platform.H5\n }\n } catch (e) {\n /* ignore */\n }\n return Platform.WeixinMp\n}\n\n/**\n * 错误捕获一键挂载:\n * - Vue.config.errorHandler → vue_error\n * - uni.onUnhandledRejection → promise_error\n * App.onError 需在 App.vue 里手动调用 tracker.track(见 design.md 13.2)。\n */\nexport function installErrorCapture(Vue: any, tracker: Tracker): void {\n if (Vue && Vue.config) {\n Vue.config.errorHandler = (err: any, vm: any, info: string) => {\n tracker.track(EventType.VueError, {\n message: (err && err.message) || String(err),\n stack: err ? err.stack : undefined,\n info,\n componentName: vm && vm.$options ? (vm.$options.name || vm.$options._componentTag) : undefined,\n })\n }\n }\n try {\n uni.onUnhandledRejection &&\n uni.onUnhandledRejection((res: any) => {\n const reason = res ? res.reason : undefined\n tracker.track(EventType.PromiseError, {\n message: (reason && reason.message) || String(reason),\n stack: reason ? reason.stack : undefined,\n })\n })\n } catch (e) {\n /* ignore */\n }\n}\n"],"mappings":";;;AAiCA,MAAa,aAA8B;CACzC,aAAa,MAAM;EACjB,IAAI;GACF,OAAO,IAAI,eAAe,EAAE;WACrB,GAAG;GACV,OAAO;;;CAGX,aAAa,GAAG,MAAM;EACpB,IAAI;GACF,IAAI,eAAe,GAAG,EAAE;WACjB,GAAG;;CAId,gBAAgB,MAAM;EACpB,IAAI;GACF,IAAI,kBAAkB,EAAE;WACjB,GAAG;;CAId,UAAU,SACR,IAAI,SAAS,SAAS,WAAW;EAC/B,IAAI,QAAQ;GACV,KAAK,KAAK;GACV,QAAS,KAAK,UAAU;GACxB,MAAM,KAAK;GACX,QAAQ,KAAK,UAAU,EAAE;GACzB,UAAU,QAAa,QAAQ;IAAE,YAAY,IAAI;IAAY,MAAM,IAAI;IAAM,CAAC;GAC9E,OAAO,QAAa,OAAO,IAAI;GAChC,CAAC;GACF;CACJ,qBAAqB;EACnB,IAAI;GACF,OAAO,IAAI,mBAAmB;WACvB,GAAG;GACV,OAAO,EAAE;;;CAGb,mBAAmB;EACjB,IAAI;GACF,MAAM,QAAQ,iBAAiB;GAC/B,MAAM,MAAM,MAAM,MAAM,SAAS;GACjC,MAAM,QAAiB,OAAO,IAAI,SAAU;GAC5C,MAAM,UAAW,OAAO,IAAI,WAAY,EAAE;GAC1C,MAAM,QAAQ,OAAO,KAAK,QAAQ,CAC/B,KAAK,MAAM,GAAG,EAAE,GAAG,QAAQ,KAAK,CAChC,KAAK,IAAI;GACZ,OAAO;IACL,UAA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,KAAA;GACzB;GACA,eAAe,MAAM,GAAG,WAAY,GAAG,SAAS,QAAQ,GAAG,SAAS,gBAAiB,KAAA;GACtF,CAAC;;CAGN,IAAI;EACF,IAAI,wBACF,IAAI,sBAAsB,QAAa;GACrC,MAAM,SAAS,MAAM,IAAI,SAAS,KAAA;GAClC,QAAQ,MAAM,UAAU,cAAc;IACpC,SAAU,UAAU,OAAO,WAAY,OAAO,OAAO;IACrD,OAAO,SAAS,OAAO,QAAQ,KAAA;IAChC,CAAC;IACF;UACG,GAAG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gctrack/uni",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "GCTrack 小程序多端适配 —— uni-app 微信/支付宝/京东/小红书/H5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@gctrack/protocol": "0.1.
|
|
35
|
+
"@gctrack/protocol": "0.1.2"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@gctrack/core": "0.1.
|
|
38
|
+
"@gctrack/core": "0.1.2"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "tsdown",
|