@tmsfe/tms-core 0.0.32 → 0.0.35

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,44 @@
1
+ /* eslint-disable */
2
+
3
+ type DataItem = string | null | undefined;
4
+
5
+ /**
6
+ * 初始化参数对象
7
+ */
8
+ interface IInitOptions {
9
+ client: string, // sinan、mycar等
10
+ appVersion: string,
11
+ }
12
+
13
+ /**
14
+ * 页面对象
15
+ */
16
+ interface IPage {
17
+ route: string,
18
+ options: object,
19
+ }
20
+
21
+ /**
22
+ * 旧的埋点参数类型
23
+ */
24
+ interface IOldParams {
25
+ [key: number]: any,
26
+ }
27
+
28
+ interface ISystemInfo {
29
+ model: string,
30
+ wxVersion: string,
31
+ platform: '',
32
+ SDKVersion: '',
33
+ host: '',
34
+ }
35
+
36
+ interface ILocation {
37
+ province: string,
38
+ cityName: string,
39
+ }
40
+
41
+ interface IDeviceData {
42
+ networkType: string,
43
+ location: ILocation,
44
+ }
@@ -1,199 +0,0 @@
1
- /**
2
- * 调用云函数上报埋点
3
- */
4
-
5
- import syncApi from './syncfnmanager';
6
-
7
- type DataType = string | null;
8
-
9
- interface IInitOptions {
10
- client: string, // sinan、mycar等
11
- appVersion: string,
12
- cloudEnvId: string,
13
- }
14
-
15
- interface IPage {
16
- route: string,
17
- options: object,
18
- }
19
-
20
- const openidIndex = 7; // openId的下标,由云函数填充
21
-
22
- let initOptions: IInitOptions;
23
-
24
- function init(options: IInitOptions): void {
25
- if (!initOptions) {
26
- initOptions = options;
27
- wx.cloud.init({ env: options.cloudEnvId });
28
- }
29
- }
30
-
31
- function getSystemInfoString(): string {
32
- const system = syncApi.getSystemInfoSync();
33
- // @ts-ignore
34
- // eslint-disable-next-line
35
- const { model = '', version: wxVersion = '', platform = '', SDKVersion = '', host = '' } = system;
36
- const info = { model, wxVersion, platform, SDKVersion, host };
37
- return encodeURIComponent(JSON.stringify(info));
38
- }
39
-
40
- function convert2String(value: any): string {
41
- const type = typeof value;
42
- if (type === 'string') {
43
- return value;
44
- }
45
- if (type === 'object') {
46
- return JSON.stringify(value);
47
- }
48
- return String(value);
49
- }
50
-
51
- function getSeqInfo(): { timestamp: number, seqId: string } {
52
- const timestamp = Date.now();
53
- const random = Math.random().toString();
54
- const randomStr = random.slice(2, 7);
55
- const sourceId = 7; // 6 未知 7 云函数 8 出行 9 我的车
56
- const seqId = `${timestamp}${sourceId}${randomStr}`;
57
- return { timestamp, seqId };
58
- }
59
-
60
- function getPageInfo(): IPage | null {
61
- const pages = getCurrentPages();
62
- // 首页未渲染
63
- if (pages.length === 0) {
64
- const launch = syncApi.getLaunchOptionsSync();
65
- return { route: launch.path, options: launch.query };
66
- }
67
- const page = pages.pop();
68
- // 插件页
69
- if (!page) {
70
- return null;
71
- }
72
- return page;
73
- }
74
-
75
- /**
76
- * 获取埋点的基础字段
77
- * @param page
78
- * @param seqId 请求ID
79
- */
80
- function getBaseData(page: IPage, seqId: string): { arr: DataType[], nextIndex: number } {
81
- const pageQuery = encodeURIComponent(JSON.stringify(page.options));
82
- const pageUrl = page.route;
83
- const { appVersion, client } = initOptions;
84
- const system = getSystemInfoString();
85
- // @ts-ignore
86
- const { scene = -1 } = syncApi.getLaunchOptionsSync();
87
-
88
- const arr = new Array(41);
89
- // 0: log_time
90
- // 1: access_time,用户点击时间,服务端统一处理
91
- // 2: user_ip,前端无需赋值
92
- // 3: qimei,灯塔中的用户ID
93
- // 4: imei,Android手机的imei IOS系统中的idfv 车联网中的wecarid
94
- // 5: user_id,用户ID
95
- // 6: qq_no,登录QQ的openid,
96
- // 7: wechat_id,登录微信的openid,由云函数填充
97
- // 8: phone_no,手机号
98
- // 9: platform,客户端请求来源,小程序是2
99
- arr[9] = '2';
100
- // 10: app_version,客户端|小程序版本号
101
- arr[10] = appVersion;
102
- // 11: channel,外部推广渠道或车辆所属渠道
103
- // 12: net_type,wifi、3G、4G等
104
- // 13: product_id,手图APP、车联网等
105
- // 14: busi_type,1: 定位、2: 检索、3: 导航、4: 车主服务、5: 小程序
106
- arr[14] = '5';
107
- // 15: request_id,请求ID
108
- arr[15] = seqId;
109
- // 16: session_id,标识用户一次访问过程的ID
110
- // 17: f17 - province,省份
111
- // 18: f18 - city,城市
112
- // 19: f19 - 系统信息
113
- arr[19] = system;
114
- // 20: f20 - sinan、mycar等
115
- arr[20] = client;
116
- // 21: f21 - 小程序场景值
117
- arr[21] = scene.toString();
118
- // 22: f22 - 标记是云函数上报
119
- arr[22] = 'cloud';
120
- // 23: f23 - 当前页面的url
121
- arr[23] = pageUrl;
122
- // 24: f24 - 当前页面的query
123
- arr[24] = pageQuery;
124
- // 25 ~ 30: 预留字典给后续扩展使用
125
- // 31 ~ 40: 提供给开发自定义
126
- return { arr, nextIndex: 31 };
127
- }
128
-
129
- function formatData(page: IPage, seqId: string, data: any[]): DataType[] {
130
- const { arr, nextIndex } = getBaseData(page, seqId);
131
- let index = nextIndex;
132
- for (const item of data) {
133
- if (index >= arr.length) {
134
- console.error('埋点参数个数超出上限而被截断', data);
135
- break;
136
- }
137
- arr[index] = convert2String(item);
138
- index += 1;
139
- }
140
- return arr;
141
- }
142
-
143
- function report(...data: any[]): void {
144
- console.log('云埋点', data);
145
- const page = getPageInfo();
146
- if (page === null) {
147
- console.log('插件页不上报埋点');
148
- return;
149
- }
150
- // @ts-ignore
151
- const { scene = -1 } = syncApi.getLaunchOptionsSync();
152
- // 小程序爬虫产生的日志
153
- if (scene === 1129 || scene === 1030) {
154
- console.warn('小程序爬虫,不上报');
155
- } else {
156
- const { timestamp, seqId } = getSeqInfo();
157
- const arr = formatData(page, seqId, data);
158
- doCallCloudFunc(arr, timestamp, seqId, openidIndex);
159
- }
160
- }
161
-
162
- function doCallCloudFunc(arr: DataType[], timestamp: number, seqId: string, openidIndex: number): void {
163
- const { cloudEnvId, appVersion } = initOptions;
164
- const onFail = (err: any) => {
165
- console.error('云埋点失败:', arr, err);
166
- };
167
- wx.cloud.callFunction({
168
- name: 'report',
169
- data: {
170
- openidIndex,
171
- params: { seqId, timestamp, appVersion, batch: [arr] },
172
- },
173
- config: { env: cloudEnvId },
174
- success: (res: any) => {
175
- const body = JSON.parse(res.result.body);
176
- if (body.errCode !== 0) {
177
- onFail(res);
178
- }
179
- },
180
- fail: onFail,
181
- });
182
- }
183
-
184
- /**
185
- * 调用云函数将已经组装完整的埋点数据arr发出去
186
- * @param arr
187
- */
188
- function callCloudFunc(arr: string[]): void {
189
- const { timestamp, seqId } = getSeqInfo();
190
- doCallCloudFunc(arr, timestamp, seqId, openidIndex);
191
- }
192
-
193
- export default {
194
- init,
195
- report,
196
- callCloudFunc,
197
- convert2String,
198
- getSystemInfoString,
199
- };
package/src/fastreport.js DELETED
@@ -1,51 +0,0 @@
1
- /**
2
- * @copyright 2020-present, Tencent, Inc. All rights reserved.
3
- * @author Fenggang.Sun <fenggangsun@tencent.com>
4
- * @file 快速上报数据,不处理过多逻辑,保证快速上报
5
- */
6
- import { getEnvInfo, getAuthInfo } from './env';
7
- import CloudReport from './cloudReport';
8
-
9
- const handleParamOfDifferentType = (param) => {
10
- const data = new Array(41);
11
- Object.keys(param).forEach((key) => {
12
- data[key] = CloudReport.convert2String(param[key]);
13
- });
14
- return data;
15
- };
16
-
17
- const defAssign = (value, defaultValue) => (value || defaultValue);
18
-
19
- /**
20
- * @class FastReport
21
- * @classdesc 快速上报模块,不依赖用户标识和位置
22
- */
23
- export default class FastReport {
24
- /**
25
- * @memberof FastReport
26
- * @description 快速上报
27
- * @param {Object} param 埋点数据
28
- */
29
- static report(param = {}) {
30
- if (!param[27]) {
31
- return Promise.reject('invalid report param');
32
- }
33
- const env = getEnvInfo();
34
- const data = handleParamOfDifferentType(param);
35
- data[9] = '2';
36
- data[10] = defAssign(data[10], env.appVersion);
37
- data[26] = defAssign(data[26], data[27]?.[0]);
38
- data[28] = env.client;
39
- if (!data[29]) {
40
- const appShowScene = wx.getStorageSync('appShowScene');
41
- if (appShowScene) {
42
- data[29] = String(appShowScene);
43
- }
44
- }
45
- data[33] = CloudReport.getSystemInfoString();
46
- getAuthInfo().then((user) => {
47
- data[5] = user.uid;
48
- CloudReport.callCloudFunc(data);
49
- });
50
- }
51
- };