@tmsfe/tms-core 0.0.23 → 0.0.27
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/package.json +1 -1
- package/src/cloudReport.ts +192 -0
- package/src/env.js +1 -1
- package/src/index-proxy.js +10 -0
- package/src/index.js +13 -4
- package/src/location/index.ts +1 -0
- package/src/report.js +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
initOptions = options;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function getSystemInfoString(): string {
|
|
29
|
+
const system = syncApi.getSystemInfoSync();
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
// eslint-disable-next-line
|
|
32
|
+
const { model = '', version: wxVersion = '', platform = '', SDKVersion = '', host = '' } = system;
|
|
33
|
+
const info = { model, wxVersion, platform, SDKVersion, host };
|
|
34
|
+
return encodeURIComponent(JSON.stringify(info));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function getSeqInfo(): { timestamp: number, seqId: string } {
|
|
38
|
+
const timestamp = Date.now();
|
|
39
|
+
const random = Math.random().toString();
|
|
40
|
+
const randomStr = random.slice(2, 7);
|
|
41
|
+
const sourceId = 7; // 6 未知 7 云函数 8 出行 9 我的车
|
|
42
|
+
const seqId = `${timestamp}${sourceId}${randomStr}`;
|
|
43
|
+
return { timestamp, seqId };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function getPageInfo(): IPage | null {
|
|
47
|
+
const pages = getCurrentPages();
|
|
48
|
+
// 首页未渲染
|
|
49
|
+
if (pages.length === 0) {
|
|
50
|
+
const launch = syncApi.getLaunchOptionsSync();
|
|
51
|
+
return { route: launch.path, options: launch.query };
|
|
52
|
+
}
|
|
53
|
+
const page = pages.pop();
|
|
54
|
+
// 插件页
|
|
55
|
+
if (!page) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return page;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 获取埋点的基础字段
|
|
63
|
+
* @param page
|
|
64
|
+
* @param seqId 请求ID
|
|
65
|
+
*/
|
|
66
|
+
function getBaseData(page: IPage, seqId: string): { arr: DataType[], nextIndex: number } {
|
|
67
|
+
const pageQuery = encodeURIComponent(JSON.stringify(page.options));
|
|
68
|
+
const pageUrl = page.route;
|
|
69
|
+
const { appVersion, client } = initOptions;
|
|
70
|
+
const system = getSystemInfoString();
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
const { scene = -1 } = syncApi.getLaunchOptionsSync();
|
|
73
|
+
|
|
74
|
+
const arr = new Array(41);
|
|
75
|
+
// 0: log_time
|
|
76
|
+
// 1: access_time,用户点击时间,服务端统一处理
|
|
77
|
+
// 2: user_ip,前端无需赋值
|
|
78
|
+
// 3: qimei,灯塔中的用户ID
|
|
79
|
+
// 4: imei,Android手机的imei IOS系统中的idfv 车联网中的wecarid
|
|
80
|
+
// 5: user_id,用户ID
|
|
81
|
+
// 6: qq_no,登录QQ的openid,
|
|
82
|
+
// 7: wechat_id,登录微信的openid,由云函数填充
|
|
83
|
+
// 8: phone_no,手机号
|
|
84
|
+
// 9: platform,客户端请求来源,小程序是2
|
|
85
|
+
arr[9] = '2';
|
|
86
|
+
// 10: app_version,客户端|小程序版本号
|
|
87
|
+
arr[10] = appVersion;
|
|
88
|
+
// 11: channel,外部推广渠道或车辆所属渠道
|
|
89
|
+
// 12: net_type,wifi、3G、4G等
|
|
90
|
+
// 13: product_id,手图APP、车联网等
|
|
91
|
+
// 14: busi_type,1: 定位、2: 检索、3: 导航、4: 车主服务、5: 小程序
|
|
92
|
+
arr[14] = '5';
|
|
93
|
+
// 15: request_id,请求ID
|
|
94
|
+
arr[15] = seqId;
|
|
95
|
+
// 16: session_id,标识用户一次访问过程的ID
|
|
96
|
+
// 17: f17 - province,省份
|
|
97
|
+
// 18: f18 - city,城市
|
|
98
|
+
// 19: f19 - 系统信息
|
|
99
|
+
arr[19] = system;
|
|
100
|
+
// 20: f20 - sinan、mycar等
|
|
101
|
+
arr[20] = client;
|
|
102
|
+
// 21: f21 - 小程序场景值
|
|
103
|
+
arr[21] = scene.toString();
|
|
104
|
+
// 22: f22 - 标记是云函数上报
|
|
105
|
+
arr[22] = 'cloud';
|
|
106
|
+
// 23: f23 - 当前页面的url
|
|
107
|
+
arr[23] = pageUrl;
|
|
108
|
+
// 24: f24 - 当前页面的query
|
|
109
|
+
arr[24] = pageQuery;
|
|
110
|
+
// 25 ~ 30: 预留字典给后续扩展使用
|
|
111
|
+
// 31 ~ 40: 提供给开发自定义
|
|
112
|
+
return { arr, nextIndex: 31 };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function formatData(page: IPage, seqId: string, data: any[]): DataType[] {
|
|
116
|
+
const { arr, nextIndex } = getBaseData(page, seqId);
|
|
117
|
+
let index = nextIndex;
|
|
118
|
+
for (const item of data) {
|
|
119
|
+
if (index >= arr.length) {
|
|
120
|
+
console.error(data);
|
|
121
|
+
throw new Error('埋点参数个数超出上限');
|
|
122
|
+
}
|
|
123
|
+
const type = typeof item;
|
|
124
|
+
let value: string;
|
|
125
|
+
if (type === 'string') {
|
|
126
|
+
value = item;
|
|
127
|
+
} else if (type === 'object') {
|
|
128
|
+
value = JSON.stringify(item);
|
|
129
|
+
} else {
|
|
130
|
+
value = String(item);
|
|
131
|
+
}
|
|
132
|
+
arr[index] = value;
|
|
133
|
+
index += 1;
|
|
134
|
+
}
|
|
135
|
+
return arr;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function report(...data: any[]): void {
|
|
139
|
+
console.log('云埋点', data);
|
|
140
|
+
const page = getPageInfo();
|
|
141
|
+
if (page === null) {
|
|
142
|
+
console.log('插件页不上报埋点');
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
// @ts-ignore
|
|
146
|
+
const { scene = -1 } = syncApi.getLaunchOptionsSync();
|
|
147
|
+
// 小程序爬虫产生的日志
|
|
148
|
+
if (scene === 1129 || scene === 1030) {
|
|
149
|
+
console.warn('小程序爬虫,不上报');
|
|
150
|
+
} else {
|
|
151
|
+
const { timestamp, seqId } = getSeqInfo();
|
|
152
|
+
const arr = formatData(page, seqId, data);
|
|
153
|
+
doCallCloudFunc(arr, timestamp, seqId, openidIndex);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function doCallCloudFunc(arr: DataType[], timestamp: number, seqId: string, openidIndex: number): void {
|
|
158
|
+
const { cloudEnvId, appVersion } = initOptions;
|
|
159
|
+
const onFail = (err: any) => {
|
|
160
|
+
console.error('云埋点失败:', arr, err);
|
|
161
|
+
};
|
|
162
|
+
wx.cloud.callFunction({
|
|
163
|
+
name: 'report',
|
|
164
|
+
data: {
|
|
165
|
+
openidIndex,
|
|
166
|
+
params: { seqId, timestamp, appVersion, batch: [arr] },
|
|
167
|
+
},
|
|
168
|
+
config: { env: cloudEnvId },
|
|
169
|
+
success: (res: any) => {
|
|
170
|
+
const body = JSON.parse(res.result.body);
|
|
171
|
+
if (body.errCode !== 0) {
|
|
172
|
+
onFail(res);
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
fail: onFail,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 调用云函数将已经组装完整的埋点数据arr发出去
|
|
181
|
+
* @param arr
|
|
182
|
+
*/
|
|
183
|
+
function callCloudFunc(arr: string[]): void {
|
|
184
|
+
const { timestamp, seqId } = getSeqInfo();
|
|
185
|
+
doCallCloudFunc(arr, timestamp, seqId, -1);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export default {
|
|
189
|
+
init,
|
|
190
|
+
report,
|
|
191
|
+
callCloudFunc,
|
|
192
|
+
};
|
package/src/env.js
CHANGED
|
@@ -96,7 +96,7 @@ export const isAppPageExist = (page) => {
|
|
|
96
96
|
// 以下变量标识各小程序appId
|
|
97
97
|
const MOBILITY_APPID = 'wx65cc950f42e8fff1'; // 出行服务小程序AppId
|
|
98
98
|
const MOBILITY_DEMO_APPID = 'wxa7ce727b525f80b0'; // 出行服务demo小程序AppId
|
|
99
|
-
const SINAN_HOME = '/modules/
|
|
99
|
+
const SINAN_HOME = '/modules/home/pages/index/index'; // 出行首页地址
|
|
100
100
|
const MYCAR_HOME = '/modules/car/index/index'; // 我的车首页地址
|
|
101
101
|
|
|
102
102
|
/**
|
package/src/index-proxy.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import syncApi from './syncfnmanager';
|
|
9
9
|
import md5 from './md5';
|
|
10
|
+
import CloudReport from './cloudReport';
|
|
10
11
|
import { rpxToPx } from './rpx';
|
|
11
12
|
import { serialize } from './objUtils';
|
|
12
13
|
import * as stringUtils from './stringUtils';
|
|
@@ -39,6 +40,7 @@ function invoke(obj, funcName, args) {
|
|
|
39
40
|
function initProxy(appObj, options) {
|
|
40
41
|
app = appObj;
|
|
41
42
|
initOptions = options;
|
|
43
|
+
CloudReport.init(options);
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
function awaitTMS() {
|
|
@@ -201,10 +203,18 @@ function getHomePage() {
|
|
|
201
203
|
return initOptions.homePage;
|
|
202
204
|
}
|
|
203
205
|
|
|
206
|
+
function getCloudReport() {
|
|
207
|
+
if (tms) {
|
|
208
|
+
return tms.getCloudReport();
|
|
209
|
+
}
|
|
210
|
+
return CloudReport;
|
|
211
|
+
}
|
|
212
|
+
|
|
204
213
|
const api = {
|
|
205
214
|
isProxy: true, // 方便定位问题时判断是否proxy
|
|
206
215
|
initProxy,
|
|
207
216
|
md5,
|
|
217
|
+
getCloudReport,
|
|
208
218
|
getCarManager,
|
|
209
219
|
getLocationManager,
|
|
210
220
|
rpxToPx,
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Request from './request';
|
|
2
2
|
import FastReport from './fastreport';
|
|
3
|
+
import CloudReport from './cloudReport';
|
|
3
4
|
import { getConfig } from './config';
|
|
4
5
|
import syncApi from './syncfnmanager';
|
|
5
6
|
import nav from './navigator';
|
|
@@ -35,8 +36,8 @@ import {
|
|
|
35
36
|
getIpxClass,
|
|
36
37
|
getIpxConfig,
|
|
37
38
|
} from './ipxHelper';
|
|
38
|
-
import getLocInstance from './location/index
|
|
39
|
-
import LocationBase from './location/base
|
|
39
|
+
import getLocInstance from './location/index';
|
|
40
|
+
import LocationBase from './location/base';
|
|
40
41
|
import { getMpOpenId, getOuterOpenId } from './mpInfo';
|
|
41
42
|
import * as storage from './storage';
|
|
42
43
|
|
|
@@ -73,6 +74,11 @@ const getReporter = () => ({ report });
|
|
|
73
74
|
*/
|
|
74
75
|
const getFastReporter = () => FastReport;
|
|
75
76
|
|
|
77
|
+
/**
|
|
78
|
+
* @description 云函数埋点上报
|
|
79
|
+
*/
|
|
80
|
+
const getCloudReport = () => CloudReport;
|
|
81
|
+
|
|
76
82
|
/**
|
|
77
83
|
* @description 自定义事件机制
|
|
78
84
|
* @returns {Object} [EventDispatcher实例](#class-eventdispatcher)
|
|
@@ -99,14 +105,16 @@ const getLocationBaseClass = () => LocationBase;
|
|
|
99
105
|
const init = (options = {}) => {
|
|
100
106
|
startListenApp();
|
|
101
107
|
const { appVersion, wxAppId, client, defaultHost, cloudEnvId, appEnv, appPagePaths, homePage } = options;
|
|
102
|
-
|
|
108
|
+
const envInfo = {
|
|
103
109
|
wxAppId,
|
|
104
110
|
appVersion,
|
|
105
111
|
appEnv,
|
|
106
112
|
client,
|
|
107
113
|
cloudEnvId,
|
|
108
|
-
}
|
|
114
|
+
};
|
|
115
|
+
setEnvInfo(envInfo);
|
|
109
116
|
setAppPagePaths(appPagePaths, homePage);
|
|
117
|
+
CloudReport.init(envInfo);
|
|
110
118
|
Request.defaultHost = defaultHost;
|
|
111
119
|
// 初始化云环境
|
|
112
120
|
wx.cloud.init({ env: cloudEnvId });
|
|
@@ -132,6 +140,7 @@ const api = {
|
|
|
132
140
|
getRealtimeLogManager,
|
|
133
141
|
md5,
|
|
134
142
|
getReporter,
|
|
143
|
+
getCloudReport,
|
|
135
144
|
getFastReporter,
|
|
136
145
|
getLocationManager,
|
|
137
146
|
getLocationBaseClass,
|
package/src/location/index.ts
CHANGED
package/src/report.js
CHANGED