@tmsfe/tms-core 0.0.26 → 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/index-proxy.js +10 -0
- package/src/index.js +11 -4
- package/src/distanceBetweenPoints.js +0 -73
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/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';
|
|
@@ -11,7 +12,6 @@ import { cache as report, startListenApp } from './report';
|
|
|
11
12
|
import EventDispatcher from './eventDispatcher';
|
|
12
13
|
import { serialize } from './objUtils';
|
|
13
14
|
import { rpxToPx } from './rpx';
|
|
14
|
-
import { distanceBetweenPoints } from './distanceBetweenPoints';
|
|
15
15
|
import {
|
|
16
16
|
formatPlate,
|
|
17
17
|
subStr,
|
|
@@ -74,6 +74,11 @@ const getReporter = () => ({ report });
|
|
|
74
74
|
*/
|
|
75
75
|
const getFastReporter = () => FastReport;
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* @description 云函数埋点上报
|
|
79
|
+
*/
|
|
80
|
+
const getCloudReport = () => CloudReport;
|
|
81
|
+
|
|
77
82
|
/**
|
|
78
83
|
* @description 自定义事件机制
|
|
79
84
|
* @returns {Object} [EventDispatcher实例](#class-eventdispatcher)
|
|
@@ -100,14 +105,16 @@ const getLocationBaseClass = () => LocationBase;
|
|
|
100
105
|
const init = (options = {}) => {
|
|
101
106
|
startListenApp();
|
|
102
107
|
const { appVersion, wxAppId, client, defaultHost, cloudEnvId, appEnv, appPagePaths, homePage } = options;
|
|
103
|
-
|
|
108
|
+
const envInfo = {
|
|
104
109
|
wxAppId,
|
|
105
110
|
appVersion,
|
|
106
111
|
appEnv,
|
|
107
112
|
client,
|
|
108
113
|
cloudEnvId,
|
|
109
|
-
}
|
|
114
|
+
};
|
|
115
|
+
setEnvInfo(envInfo);
|
|
110
116
|
setAppPagePaths(appPagePaths, homePage);
|
|
117
|
+
CloudReport.init(envInfo);
|
|
111
118
|
Request.defaultHost = defaultHost;
|
|
112
119
|
// 初始化云环境
|
|
113
120
|
wx.cloud.init({ env: cloudEnvId });
|
|
@@ -133,6 +140,7 @@ const api = {
|
|
|
133
140
|
getRealtimeLogManager,
|
|
134
141
|
md5,
|
|
135
142
|
getReporter,
|
|
143
|
+
getCloudReport,
|
|
136
144
|
getFastReporter,
|
|
137
145
|
getLocationManager,
|
|
138
146
|
getLocationBaseClass,
|
|
@@ -181,7 +189,6 @@ const api = {
|
|
|
181
189
|
|
|
182
190
|
/** rpx转px */
|
|
183
191
|
rpxToPx,
|
|
184
|
-
distanceBetweenPoints,
|
|
185
192
|
|
|
186
193
|
storage,
|
|
187
194
|
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
// 墨卡托坐标转经纬度
|
|
3
|
-
function mercator2LonLat(p) {
|
|
4
|
-
const r = p.y / 111319.49077777778;
|
|
5
|
-
const n = Math.atan(Math.exp(0.017453292519943295 * r)) / 0.008726646259971648 - 90;
|
|
6
|
-
return {
|
|
7
|
-
x: p.x / 111319.49077777778,
|
|
8
|
-
y: n,
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// 角度转弧度
|
|
14
|
-
function angle2Rad(t) {
|
|
15
|
-
return (t * Math.PI) / 180;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
function client2ServerX(t) {
|
|
20
|
-
return t - 20037508;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function client2ServerY(t) {
|
|
24
|
-
return t - 30240971;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function longitude2ClientX(t) {
|
|
28
|
-
return Number(111319.49077777778 * Number(t) + 20037508).toFixed(0);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function latitude2ClientY(t) {
|
|
32
|
-
const n = Math.log(Math.tan(0.008726646259971648 * (90 + Number(t)))) / 0.017453292519943295;
|
|
33
|
-
return Number(111319.49077777778 * n + 30240971).toFixed(0);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function geoPointToServerPoint(t) {
|
|
37
|
-
let n = {};
|
|
38
|
-
if (t) {
|
|
39
|
-
n = {
|
|
40
|
-
x: client2ServerX(longitude2ClientX(t.longitude)),
|
|
41
|
-
y: client2ServerY(latitude2ClientY(t.latitude)),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
return n;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 两个经纬度间地理距离(单位:米)
|
|
50
|
-
* @param {*} start {longitude, latitude}
|
|
51
|
-
* @param {*} end {longitude, latitude}
|
|
52
|
-
* @returns
|
|
53
|
-
*/
|
|
54
|
-
function distanceBetweenPoints(start, end) {
|
|
55
|
-
let s = geoPointToServerPoint(start);
|
|
56
|
-
let t = geoPointToServerPoint(end);
|
|
57
|
-
s = mercator2LonLat(s); // eslint-disable-line
|
|
58
|
-
t = mercator2LonLat(t); // eslint-disable-line
|
|
59
|
-
const deltaY = angle2Rad(s.y) - angle2Rad(t.y);
|
|
60
|
-
const deltaX = angle2Rad(s.x) - angle2Rad(t.x);
|
|
61
|
-
let res = 2
|
|
62
|
-
* Math.asin(Math.sqrt(Math.pow(Math.sin(deltaY / 2), 2) // eslint-disable-line
|
|
63
|
-
+ Math.cos(angle2Rad(s.y))
|
|
64
|
-
* Math.cos(angle2Rad(t.y))
|
|
65
|
-
* Math.pow(Math.sin(deltaX / 2), 2))); // eslint-disable-line
|
|
66
|
-
res *= 6378137;
|
|
67
|
-
res = Math.floor(res * 10000 + 0.5) / 10000;
|
|
68
|
-
return res;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export {
|
|
72
|
-
distanceBetweenPoints,
|
|
73
|
-
};
|