yas.js
6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import udid from './udid';
import Taro from '@tarojs/taro';
import config from '../config';
import rules from '../router/rules';
import { MD5 } from './crypto';
import { parse, stringify } from 'querystringify';
import { getGlobalData } from '../libs/login/login.js'
import pathReport from '../router/path-report';
export default class yas {
constructor(app) {
let self = this;
this.app = app;
this.pvid = MD5(`${new Date().getTime()}${udid.get()}`).toString();
this.deviceInfo = {
os: '', // 系统类型
dm: '', // 设备型号
res: '', // 屏幕大小
osv: '', // 系统版本
ak: 'yohoufo_mp',
ch: '',
udid: udid.get()
};
// 获取设备信息
Taro.getSystemInfo({
success(res) {
self.language = res.language;
if (res.platform === 'devtools') {
// self.devEnv = true; //判断开发环境
}
Object.assign(self.deviceInfo, {
os: res.platform,
dm: res.model,
res: `${res.screenWidth}*${res.screenHeight}`,
osv: res.system,
});
}
});
}
uploadData(params) {
let self = this;
let sid = '';
let globalData = getGlobalData() || {};
sid = self.app.globalData && self.app.globalData.sid || globalData.sid || '';
return Taro.request({
url: config.domains.yasHost,
data: { _mlogs: JSON.stringify(params) },
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded',
'x-yoho-sid': MD5(sid).toString(),
},
});
}
report(event, info = {}) {
let self = this;
for (let key in info) {
if (info[key]) {
info[key] = decodeURIComponent(info[key]);
}
}
const pageNameMap = {};
for (let i in rules) {
if (rules.hasOwnProperty(i) && rules[i].path) {
pageNameMap[rules[i].path] = i;
}
}
if (event === 'YB_SHARE_RESULT_L') {
info.PAGE_NAME = pageNameMap['/' + info.PATH];
}
let userInfo = info || {};
let statusInfo = { ln: this.language };
let user = Taro.getStorageSync('userInfo') || {};
userInfo.UNION_ID = user.unionid || user.union_id || Taro.getStorageSync('unionid') || '';
userInfo.APP_ID = config.appid || '';
if (!userInfo.PV_ID) {
userInfo.PV_ID = info.PV_ID;
}
return new Promise(resolve => {
Taro.getNetworkType({
success(res) {
switch (res.networkType) {
case 'wifi':
statusInfo.net = '1';
break;
case '2g':
statusInfo.net = '2';
break;
case '3g':
statusInfo.net = '3';
break;
case '4g':
statusInfo.net = '4';
break;
default:
statusInfo.net = '0';
break;
}
},
complete() {
let globalData = getGlobalData() || {};
let userData = globalData.userInfo || {};
if (!Object.keys(userData).length) {
userData = Taro.getStorageSync('userInfo') || {};
}
let ch = self.app.globalData && self.app.globalData.ch || globalData.ch || '';
let uid = userData.uid || '';
self.deviceInfo.ch = ch;
return resolve(self.uploadData({
status: statusInfo,
device: self.deviceInfo,
events: [{
param: userInfo,
ts: new Date().getTime(),
op: event,
uid: uid,
sid: self.app.globalData && self.app.globalData.sid ||globalData.sid || ''
}]
}));
}
});
});
}
_pageName(options, path) {
let hasConf = pathReport[path];
// 多入口页面依据参数设定上报页面名称
if (hasConf && hasConf.multiEntry) {
let pathName = '';
Object.keys((hasConf.multiName || {})).forEach(key => {
if (options[key]) {
pathName = hasConf.multiName[key];
return false;
}
});
return pathName;
}
return hasConf ? hasConf.pathName : '';
}
_pageParam(options = {}, fromPath) {
if (!options) return '';
let hasConf = pathReport[fromPath];
if (hasConf && hasConf.multiEntry) {
let param = '';
Object.keys((hasConf.multiName || {})).forEach(key => {
if (options[key]) {
param = options[key];
return false;
}
});
return param;
}
if (pathReport[fromPath] && pathReport[fromPath].paramKey) {
return `${decodeURIComponent(options[pathReport[fromPath].paramKey])}` || '';
}
return stringify(options, '');
}
pageOpenReport(pvid, extra) {
let self = this;
let pages = getCurrentPages();
let currentPage = pages[pages.length - 1];
let path = `${currentPage.route}`,
options = currentPage.options || {}; // 拷贝options对象,用于获取当前页面参数
let info = { PV_ID: pvid || this.pvid };
// from page relevant
let fromPage, fromPath, fromOptions;
if (pages.length > 1) {
fromPage = pages[pages.length - 2];
fromPath = `${fromPage.route}`;
fromOptions = fromPage.options || {};
}
Object.assign(info, {
PAGE_PATH: path,
PAGE_PARAM: self._pageParam(options, path),
PAGE_NAME: self._pageName(options, path),
FROM_PAGE_NAME: self._pageName(options, fromPath),
FROM_PAGE_PARAM: self._pageParam(fromOptions, fromPath)
});
this.report('YB_PAGE_OPEN_L', Object.assign(info, extra || {}));
}
}