helper.class.js
7.28 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* 定义常用方法类
*/
// 引入组件
import CallApp from 'callapp-lib';
export default class Helper {
// 构造函数
constructor(context) {
// 设置上下文
this.context = context;
}
/**
* 设置唤起App弹窗
* @param {object} param [唤起App定向页面参数]
* @param {function} callback [回调函数]
*/
schemeLayer = function(param, callback) {
// 唤起App操作
function _doApp(callApp, path, param) {
callApp.open({
path: path,
param: param
});
}
// 设置平台固定参数
let path = this.context.isiPhone.value ? "ios-path" : "shop/schemaFilter";
let intentPackage = this.context.mode == "prod" ? "com.cnlive.strike" : "com.cnlive.strike.debug";
let universalHost = this.context.mode == "prod" ? "applelinkstest.cnlive.com" : "applelinkstest.cnlive.com";
// H5唤起App参数
let options = {
scheme: {
protocol: "cnlive"
},
intent: {
package: intentPackage,
scheme: "cnlive"
},
universal: {
host: universalHost + "/app_store.html",
pathKey: "page"
},
appstore: "https://sj.qq.com/myapp/detail.htm?apkName=com.cnlive.strike",
yingyongbao: "https://sj.qq.com/myapp/detail.htm?apkName=com.cnlive.strike",
fallback: "http://wjjh5.cnlive.com/apkdownload.php"
}
// 实例化对象
let callApp = new CallApp(options);
// 回调
if (typeof callback === "function") {
// 点击确定执行数据
let options = {
success: _doApp,
success_params: {
callApp, path, param
}
}
// 回调函数
callback(options);
}
}
/**
* 正则校验URL
* @param {string} strURL [URL地址]
*/
checkURL = function(strURL) {
// 校验参数
if (strURL) {
// 正则
let reg = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
// 校验匹配
if (reg.test(strURL)) {
return true;
}
}
return false;
}
/* 加密/解密算法 开始 */
/**
* 加密算法
* @param {string} word [原文]
*/
encrypt = function(word) {
let CryptoJS = require("crypto-js/crypto-js");
// KEY和IV
const KEY = CryptoJS.enc.Utf8.parse("cnlive!$@cnlive_");
const IV = CryptoJS.enc.Utf8.parse("yrEpH8Nx6umBLc1f");
// 明文编码
let srcs = CryptoJS.enc.Utf8.parse(word);
// crypt加密
var encrypted = CryptoJS.AES.encrypt(srcs, KEY, {
iv: IV,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
/**
* 解密算法
* @param {string} word [密文]
*/
decrypt = function(word) {
let CryptoJS = require("crypto-js/crypto-js");
// KEY和IV
const KEY = CryptoJS.enc.Utf8.parse("cnlive!$@cnlive_");
const IV = CryptoJS.enc.Utf8.parse("yrEpH8Nx6umBLc1f");
let srcs = word;
// let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
// let srcs = CryptoJS.enc.Base64.stringify(word);
let decrypt = CryptoJS.AES.decrypt(srcs, KEY, { iv: IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
/* 加密/解密算法 结束 */
/* 数字格式化 开始 */
/**
* 获取输入数字的整数部分
*/
getInteger() {
return (number) => {
return parseInt(number);
}
}
/**
* 获取输入数字的小数部分
*/
getDecimal() {
return (number) => {
// 拆分数字
let numberArr = number.toString().split(".");
// 判断是否有效数部分
if (numberArr.length === 2) {
return numberArr[1];
} else {
return "00";
}
}
}
/* 数字格式化 结束 */
/* 数据格式处理 开始 */
/**
* 对象内指定字符串内容替换
* @param {Object} o [目标对象]
* @param {String} key [目标字段名]
* @param {String} oldString [原始字符串]
* @param {String} newString [新字符串]
*/
objectStringReplaceAll(o, key, oldString, newString) {
// 判断对象是否为数组
if (typeof o === "object" && o !== null && o.length) {
// 遍历数组,替换内容
for (let i = 0; i < o.length; i++) {
o[i][key] = o[i][key].replace(new RegExp(oldString, "gm"), newString);
}
}
}
/* 数据格式处理 结束 */
/* 定位相关处理 开始 */
/**
* 获取用户地理位置坐标
* @param {Function} success [成功回调函数,获取定位坐标]
* @param {Function} error [错误回调函数,错误代码和信息]
* @param {Function} notSupport [浏览器不支持定位方法]
*/
getLocation(success, error, notSupport) {
if (navigator.geolocation) { // 支持定位
navigator.geolocation.getCurrentPosition(success, error);
} else { // 不支持定位
if (typeof notSupport === "function") {
notSupport();
}
}
}
/**
* GPS坐标转换为高德火星坐标,调用gcj_encrypt函数,传入wgsLat(纬度)和wgsLon(经度)参数
*/
GPS = {
PI : 3.14159265358979324,
x_pi : 3.14159265358979324 * 3000.0 / 180.0,
delta : function (lat, lon) {
// Krasovsky 1940
//
// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
var a = 6378245.0; // a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
var ee = 0.00669342162296594323; // ee: 椭球的偏心率。
var dLat = this.transformLat(lon - 105.0, lat - 35.0);
var dLon = this.transformLon(lon - 105.0, lat - 35.0);
var radLat = lat / 180.0 * this.PI;
var magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
var sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
return {'lat': dLat, 'lon': dLon};
},
//GPS---高德
wgs84ToGcj02 : function ( wgsLat , wgsLon ) {
// 将坐标转成浮点数
wgsLat = parseFloat(wgsLat);
wgsLon = parseFloat(wgsLon);
if (this.outOfChina(wgsLat, wgsLon)) {
return {'lat': wgsLat, 'lon': wgsLon};
}
var d = this.delta(wgsLat, wgsLon);
return {'lat' : wgsLat + d.lat,'lon' : wgsLon + d.lon};
},
outOfChina : function (lat, lon) {
if (lon < 72.004 || lon > 137.8347) {
return true;
}
if (lat < 0.8293 || lat > 55.8271) {
return true;
}
return false;
},
transformLat : function (x, y) {
var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
return ret;
},
transformLon : function (x, y) {
var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
return ret;
}
};
/* 定位相关处理 结束 */
}