yrjy_mini_sdk 1.0.0

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,18 @@
1
+ import { HttpRequest } from "./request";
2
+ import config from "../config/index";
3
+ let request = new HttpRequest({
4
+ baseURL: config.baseURL,
5
+ timeout: 10000,
6
+ header: {
7
+ "Content-Type": "application/json",
8
+ },
9
+ });
10
+
11
+ /**
12
+ * 埋点上报
13
+ * @param data 请求参数
14
+ * @returns 请求结果
15
+ */
16
+ export const reportLog = (url, data) => {
17
+ return request.post(url, data, { loading: false });
18
+ };
@@ -0,0 +1,137 @@
1
+ class HttpRequest {
2
+ constructor(options = {}) {
3
+ this.config = {
4
+ baseURL: options.baseURL || "",
5
+ timeout: options.timeout || 10000,
6
+ header: {
7
+ "Content-Type": "application/json",
8
+ ...options.header,
9
+ },
10
+ };
11
+ }
12
+
13
+ // 请求拦截器
14
+ requestInterceptor(options) {
15
+ // 合并配置
16
+ options.url = this.config.baseURL + options.url;
17
+ options.timeout = options.timeout || this.config.timeout;
18
+ options.header = {
19
+ ...this.config.header,
20
+ ...options.header,
21
+ };
22
+
23
+ return options;
24
+ }
25
+
26
+ // 响应拦截器
27
+ responseInterceptor(response) {
28
+ // 检查响应状态码
29
+ if (response.statusCode === 200) {
30
+ // 响应成功,返回响应数据
31
+ return Promise.resolve(response);
32
+ } else {
33
+ // 响应失败,创建错误对象并返回被拒绝的 Promise
34
+ const error = {
35
+ code: response.statusCode,
36
+ message: response.data.message || "请求失败",
37
+ };
38
+ uni.showToast({
39
+ title: error.message,
40
+ icon: "none",
41
+ duration: 2000,
42
+ });
43
+ return Promise.reject(error);
44
+ }
45
+ }
46
+
47
+ // 请求方法
48
+ request(options) {
49
+ // 请求拦截
50
+ options = this.requestInterceptor(options);
51
+ return new Promise((resolve, reject) => {
52
+ uni.request({
53
+ ...options,
54
+ success: (res) => {
55
+ try {
56
+ const result = this.responseInterceptor(res, options);
57
+ resolve(result);
58
+ } catch (error) {
59
+ reject(error);
60
+ }
61
+ },
62
+ fail: (err) => {
63
+ // 错误处理
64
+ const error = {
65
+ code: err.errMsg ? -1 : -2,
66
+ message: err.errMsg || "网络请求失败",
67
+ };
68
+ uni.showToast({
69
+ title: error.message,
70
+ icon: "none",
71
+ duration: 2000,
72
+ });
73
+ reject(error);
74
+ },
75
+ });
76
+ });
77
+ }
78
+
79
+ // GET请求
80
+ get(url, data = {}, options = {}) {
81
+ return this.request({
82
+ url,
83
+ data,
84
+ method: "GET",
85
+ ...options,
86
+ });
87
+ }
88
+
89
+ // POST请求
90
+ post(url, data = {}, options = {}) {
91
+ return this.request({
92
+ url,
93
+ data,
94
+ method: "POST",
95
+ ...options,
96
+ });
97
+ }
98
+
99
+ // PUT请求
100
+ put(url, data = {}, options = {}) {
101
+ return this.request({
102
+ url,
103
+ data,
104
+ method: "PUT",
105
+ ...options,
106
+ });
107
+ }
108
+
109
+ // DELETE请求
110
+ delete(url, data = {}, options = {}) {
111
+ return this.request({
112
+ url,
113
+ data,
114
+ method: "DELETE",
115
+ ...options,
116
+ });
117
+ }
118
+
119
+ // 设置baseURL
120
+ setBaseURL(baseURL) {
121
+ this.config.baseURL = baseURL;
122
+ }
123
+
124
+ // 设置header
125
+ setHeader(header) {
126
+ this.config.header = {
127
+ ...this.config.header,
128
+ ...header,
129
+ };
130
+ }
131
+ }
132
+
133
+ // 创建默认实例
134
+ const http = new HttpRequest();
135
+
136
+ export default http;
137
+ export { HttpRequest };
@@ -0,0 +1,5 @@
1
+ export default {
2
+ baseURL: '',
3
+ // 日志上报
4
+ logURL: "https://log.cdyrjygs.com/binlog",
5
+ };
package/src/index.js ADDED
@@ -0,0 +1,155 @@
1
+ /**
2
+ * npm 包入口文件
3
+ * 统一导出核心模块,简化使用者导入
4
+ * 适配 UniApp + Vue3 环境(小程序专用)
5
+ */
6
+ import Track from "./struct/trackCore.js";
7
+ import { trackPageMixin } from "./struct/trackPageMixin.js";
8
+
9
+ // 标记是否已安装,避免重复注册
10
+ let _installed = false;
11
+
12
+ /**
13
+ * 全局安装方法(Vue3 插件规范)
14
+ * @param {App} app - Vue3 应用实例
15
+ * @param {Object} [options={}] - 可选配置
16
+ */
17
+ function install(app, options = {}) {
18
+ if (_installed) return;
19
+ _installed = true;
20
+
21
+ // 初始化埋点核心模块
22
+ Track.init(options);
23
+
24
+ // 1. 挂载到 Vue3 全局属性
25
+ app.config.globalProperties.$Track = Track;
26
+
27
+ // 2. 全局注册页面埋点混入
28
+ app.mixin(trackPageMixin);
29
+
30
+ // 3. 挂载到 uni 全局(UniApp 规范,非组件环境也可调用)
31
+ uni.$Track = Track;
32
+
33
+ // 4. 重写 UniApp 页面跳转 API,实现页面跳转事件自动埋点
34
+ if (typeof uni === 'object' && uni !== null) {
35
+ // 页面跳转方法列表
36
+ const navigationMethods = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab', 'navigateBack'];
37
+
38
+ navigationMethods.forEach(method => {
39
+ if (typeof uni[method] === 'function') {
40
+ const originalMethod = uni[method];
41
+ uni[method] = function(options) {
42
+ // 记录页面跳转事件
43
+ try {
44
+ // 获取当前页面
45
+ let currentPagePath = '';
46
+
47
+ // 方案1:优先使用全局跟踪的当前页面路径(最可靠)
48
+ if (Track.currentPagePath) {
49
+ currentPagePath = Track.currentPagePath;
50
+ }
51
+
52
+ // 方案2:尝试通过 uni.getCurrentPages() 获取
53
+ if (!currentPagePath && typeof uni.getCurrentPages === 'function') {
54
+ const pages = uni.getCurrentPages() || [];
55
+ const currentPage = pages[pages.length - 1] || {};
56
+ // 尝试从多个属性获取当前页面路径
57
+ currentPagePath = currentPage.route || currentPage.__route__ || currentPage.path || '';
58
+ }
59
+
60
+ // 方案3:尝试从 getApp() 获取
61
+ if (!currentPagePath && typeof getApp === 'function') {
62
+ try {
63
+ const app = getApp();
64
+ if (app && app._pages && app._pages.length > 0) {
65
+ const currentPage = app._pages[app._pages.length - 1] || {};
66
+ currentPagePath = currentPage.route || currentPage.__route__ || currentPage.path || '';
67
+ } else if (app && app.$vm && app.$vm.$page) {
68
+ // 对于 Vue3 应用实例
69
+ const page = app.$vm.$page;
70
+ currentPagePath = page.route || page.__route__ || page.path || '';
71
+ }
72
+ } catch (error) {
73
+ // 静默捕获错误
74
+ }
75
+ }
76
+
77
+ // 方案4:尝试从全局对象获取
78
+ if (!currentPagePath) {
79
+ try {
80
+ // 尝试从 uni.$app 获取
81
+ if (uni.$app && uni.$app._pages && uni.$app._pages.length > 0) {
82
+ const currentPage = uni.$app._pages[uni.$app._pages.length - 1] || {};
83
+ currentPagePath = currentPage.route || currentPage.__route__ || currentPage.path || '';
84
+ }
85
+ } catch (error) {
86
+ // 静默捕获错误
87
+ }
88
+ }
89
+
90
+ // 解析目标页面路径和参数
91
+ let targetPagePath = '';
92
+ let navigationParams = {};
93
+
94
+ if (method === 'navigateBack') {
95
+ // 对于返回操作,目标页面是上一页
96
+ targetPagePath = 'back';
97
+ navigationParams = { delta: options || 1 };
98
+ } else if (typeof options === 'object' && options !== null) {
99
+ // 对于其他跳转操作
100
+ targetPagePath = options.url || '';
101
+ // 解析 URL 中的参数
102
+ if (targetPagePath.includes('?')) {
103
+ const [path, queryString] = targetPagePath.split('?');
104
+ targetPagePath = path;
105
+ // 解析查询字符串为对象
106
+ navigationParams = queryString.split('&').reduce((params, pair) => {
107
+ const [key, value] = pair.split('=');
108
+ if (key) {
109
+ params[key] = decodeURIComponent(value || '');
110
+ }
111
+ return params;
112
+ }, {});
113
+ }
114
+ // 合并其他参数
115
+ Object.keys(options).forEach(key => {
116
+ if (key !== 'url' && key !== 'success' && key !== 'fail' && key !== 'complete') {
117
+ navigationParams[key] = options[key];
118
+ }
119
+ });
120
+ }
121
+
122
+ // 构建页面跳转事件数据
123
+ const customParams = {
124
+ currentPagePath: currentPagePath || "",
125
+ targetPagePath: targetPagePath || "",
126
+ navigationParams: navigationParams || {},
127
+ }
128
+
129
+ // 自动上报页面跳转事件
130
+ Track.customReport('页面跳转', customParams, "navigationEvent", false);
131
+
132
+ } catch (error) {
133
+ // 静默捕获错误,避免影响跳转功能
134
+ }
135
+
136
+ // 调用原始方法
137
+ return originalMethod.call(uni, options);
138
+ };
139
+ }
140
+ });
141
+ }
142
+ }
143
+
144
+ // 支持按需导入
145
+ export { Track, trackPageMixin, install };
146
+
147
+ // 插件对象(支持 app.use() 注册)
148
+ const UniTrackPlugin = {
149
+ install,
150
+ Track,
151
+ trackPageMixin,
152
+ };
153
+
154
+ // 默认导出
155
+ export default UniTrackPlugin;