main.js
16.8 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import Vue from "vue";
import Vuex from "vuex";
import router from "./router/";
import VueResource from "vue-resource";
import VueHead from "vue-head";
import sha1 from "sha1";
import 'mint-ui/lib/style.css';
import { Toast, Indicator,InfiniteScroll } from "mint-ui";
import { Rate, Popup, Lazyload, Search, PullRefresh, Picker, Tab, Tabs} from "vant";
import App from "./App";
import IdCard from './common/IdCard';
import VueBus from 'vue-bus';
Vue.use(Vuex);
Vue.use(VueResource);
Vue.use(VueHead, { separator: " " });
Vue.use(Search);
Vue.use(Rate);
Vue.use(Popup);
Vue.use(PullRefresh);
Vue.use(Picker);
Vue.use(Tab);
Vue.use(Tabs);
Vue.use(Lazyload, {
preload: 90,
loading: "static/img/x.jpg",
error: "static/img/x.jpg",
attempt: 3
});
// 身份证校验
Vue.prototype.validid = IdCard;
// 新增Bug通讯
Vue.use(VueBus);
// mint-ui
Vue.use(InfiniteScroll);
// 解决低版手机浏览器不支持es6写法,将es6转成es5
import 'babel-polyfill';
import promise from 'es6-promise';
require('es6-promise').polyfill();
promise.polyfill();
// 安卓5.1兼容
import 'url-search-params-polyfill';
Vue.config.productionTip = false;
// 部署模式设置(开发模式:"dev",测试模式:"test",生产模式:"prod")
Vue.prototype.mode = "test";
// 微信JS-SDK对象
Vue.prototype.jweixin = {
value: null
}
// 是否是iPhone平台,默认不是,common.vue进行判断设置
Vue.prototype.isiPhone = {
value: false
}
// 是否是iPad平台,默认不是,common.vue进行判断设置
Vue.prototype.isiPad = {
value: false
}
// 是否是Android平台,默认不是,common.vue进行判断设置
Vue.prototype.isAndroid = {
value: false
}
// 是否在App内,默认不在,common.vue进行判断设置
Vue.prototype.isCNLive = {
value: false
}
// 是否在App内(一组WebView),默认不在,common.vue进行判断设置
Vue.prototype.isPlat = {
value: false
}
// 是否在支付宝内,默认不在,common.vue进行判断设置
Vue.prototype.isAliPay = {
value: false
}
// 是否在微信内,默认不在,common.vue进行判断设置
Vue.prototype.isWeChat = {
value: false,
version: []
}
// 是否在QQ内,默认不在,common.vue进行判断设置
Vue.prototype.isQQ = {
value: false
}
// 是否在小米浏览器内,默认不在,common.vue进行判断设置
Vue.prototype.isXiaoMi = {
value: false
}
/* 后端API版本控制参数 开始 */
// 参数设置
Vue.prototype.apiVersion = {
LuckDraw: "LuckDraw", // 活动接口
index:"Index3",// 主接口
miniapp:"Miniapp2",// 调用微信相关接口
dynamic: "Dynamic", // 达人动态相关接口
LeadGoose: "LeadGoose", // 领头雁项目
// addAddresss:"https://managemall.cnlive.com/",// 生产环境url
addAddresss:"https://wjjshop.cnlive.com/"// 测试环境url
}
/**
* 获取后端API版本
*/
Vue.prototype.getApiVersion = function() {
return this.apiVersion;
}
/* 后端API版本控制参数 结束 */
/* 设置图片尺寸参数 开始 */
// 参数设置
Vue.prototype.SIGN = {
SMALL: "?imageMogr2/thumbnail/200",
MIDDLE: "?imageMogr2/thumbnail/400",
BIG: "?imageMogr2/thumbnail/750",
SOURCE: ""
}
/* 设置图片尺寸参数 结束 */
/* 根据模式,获取相关请求参数 开始 */
Vue.prototype.getParams = function(mode) {
// 返回参数
let params = {};
switch (mode.toLowerCase()) {
case "test":
params.shop = {
apiAddress: "https://wjjshop.cnlive.com",
headers: {
"content-type": "application/x-www-form-urlencoded",
"Phone-Model": "Wap",
"Version":"1.0.1"
},
sellerCategory: {
url: "http://wjjh5test.cnlive.com/hqyw.html"
}
}
params.cnlive = {
apiAddress: "https://api.cnlive.com",
params: {
appId: "802_jhbqccxw08",
appKey: "9c5619e9be747bb3b925dd215ca5923d86f12c09b9394f",
secret: "1e6380cb347255e777f72b5aa60f41daa68443a132aaea",
ver: "1.0",
page_url: "https://wjjshop.cnlive.com/html/votes/1/#/pages/redirect/redirect",
timestamp: new Date().getTime().toString().substr(0, 10)
},
headers: {
"content-type": "application/x-www-form-urlencoded"
}
}
break;
case "prod":
// 请求头
let app_key = "769_jnjxdh3w53";
let timestamp = new Date().getTime().toString().substr(0, 10);
let nonce = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpeLgK0vk5POU5PQ";
params.shop = {
apiAddress: "https://managemall.cnlive.com",
headers: {
"App-Key": app_key,
"Timestamp": timestamp,
"Signature": sha1(app_key + nonce + timestamp),
"content-type": "application/x-www-form-urlencoded",
"Phone-Model": "Wap",
"Version":"1.0.1"
},
sellerCategory: {
url: "http://wjjh5.cnlive.com/hqyw.html"
}
}
params.cnlive = {
apiAddress: "https://api.cnlive.com",
params: {
appId: "769_jetj7bq525",
appKey: "0d57b045ec0c3988a682d94c644e66b9b8e0d2b8ef937d",
secret: "275e8dc29274a1186d82f65bc607c231d02bcb3bfd8976",
ver: "1.0",
page_url: "https://managemall.cnlive.com/html/votes/1/#/pages/redirect/redirect",
timestamp: new Date().getTime().toString().substr(0, 10)
},
headers: {
"content-type": "application/x-www-form-urlencoded"
}
}
break;
case "dev":
default:
params.shop = {
apiAddress: "https://wjjshop.cnlive.com",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
sellerCategory: {
url: "http://wjjh5test.cnlive.com/hqyw.html"
}
}
params.cnlive = {
apiAddress: "https://api.cnlive.com",
params: {
appId: "802_jhbqccxw08",
appKey: "9c5619e9be747bb3b925dd215ca5923d86f12c09b9394f",
secret: "1e6380cb347255e777f72b5aa60f41daa68443a132aaea",
ver: "1.0",
page_url: "http://172.16.2.171:8083/html/votes/1/#/pages/redirect/redirect",
timestamp: new Date().getTime().toString().substr(0, 10)
},
headers: {
"content-type": "application/x-www-form-urlencoded"
}
}
}
return params;
}
/* 根据模式,获取相关请求参数 结束 */
/* cnlive接口,sign处理 开始 */
Vue.prototype.getCnliveParams = function(datas, params, method = "GET") {
// 返回结果,根据method返回不同值,Object or Array
let result = {};
// 构造参数对象
let p = {};
// 单独取出appKey
let appKey = params.appKey;
// 删除appKey
delete params.appKey;
// 删除secret
delete params.secret;
// 合并对象/排序key
let keySortArr = Object.keys(Object.assign(p, params, datas)).sort();
// 整理签名字符串数组
let stringArr = new Array();
// OPEN方式参数
let paramsArr = new Array();
for (let i in keySortArr) {
if (p[keySortArr[i]]) {
// 签名数组
stringArr.push(keySortArr[i] + "=" + p[keySortArr[i]]);
// OPEN方式参数
if (method.toUpperCase() == "OPEN") {
p[keySortArr[i]] = encodeURIComponent(p[keySortArr[i]]);
paramsArr.push(keySortArr[i] + "=" + p[keySortArr[i]]);
}
}
}
// 签名字符串
let strings = stringArr.join("&") + "&key=" + appKey;
// 签名
let sign = sha1(strings).toUpperCase();
// OPEN方式参数
if (method.toUpperCase() == "OPEN") {
// 加入签名
paramsArr.push("sign=" + sign);
// 设置返回
result = paramsArr;
} else {
// 加入签名
p.sign = sign;
// 设置返回
result = p;
}
return result;
}
/* cnlive接口,sign处理 结束 */
/* AJAX请求 开始 */
/**
* Vue.$http封装
* @param {string} path [请求URL]
* @param {object} data [请求数据]
* @param {function} callback [回调函数]
* @param {object} ext [扩展参数]
*/
Vue.prototype.http = function(path, data, callback, ext) {
/**
* get请求处理
* @param {object} context [Vue对象]
* @param {string} path [请求URL]
* @param {object} data [请求数据]
* @param {function} callback [回调函数]
* @param {object} ext [扩展参数]
* @param {object} headers [请求头]
*/
function get(context, path, data, callback, ext, headers) {
// 时间戳
data._timestamp = new Date().getTime();
let params = {
emulateJSON: true, // 按照普通表单形式传送
// withCredentials: true,
params: data,
_timeout: 30000,
headers
}
// HTTP请求
context.$http.get(path, params).then(resp => {
// 获取返回数据
let respBody = resp.body;
// 处理回调
if (typeof callback === "function") {
if (ext) {
// 回调,同时返回扩展参数
callback(respBody, ext);
} else {
// 回调
callback(respBody);
}
}
}, err => {
/* 加载错误处理 开始 */
Indicator.close();
Toast("请求出错");
/* 加载错误处理 结束 */
});
}
/**
* post请求处理
* @param {object} context [Vue对象]
* @param {string} path [请求URL]
* @param {object} data [请求数据]
* @param {function} callback [回调函数]
* @param {object} ext [扩展参数]
* @param {object} headers [请求头]
*/
function post(context, path, data, callback, ext, headers) {
// 时间戳
data._timestamp = new Date().getTime();
// 请求配置
let requestConfig = {
emulateJSON: true, // 按照普通表单形式传送
// withCredentials: true,
headers,
_timeout: 30000
}
// HTTP请求
context.$http.post(path, data, requestConfig).then(resp => {
// 获取返回数据
let respBody = resp.body;
// 处理回调
if (typeof(callback) === "function") {
if (ext) {
// 回调,同时返回扩展参数
callback(respBody, ext);
} else {
// 回调
callback(respBody);
}
}
}, err => {
/* 加载错误处理 开始 */
Indicator.close();
Toast("请求出错");
/* 加载错误处理 结束 */
});
}
if(path) {
// 上下文
let context = this;
// 获取相关请求参数
let params = this.getParams(this.mode);
// 允许method数组
let methodsArr = ["GET", "POST", "OPEN"];
// 默认请求方式(GET)
let method = "GET";
// 允许的请求地址
let apiArr = ["shop", "cnlive"];
// 默认请求接口(shop)
let api = "shop";
// 请求方式
if (typeof(ext) === "object" && ext.method) {
if(methodsArr.includes(ext.method.toUpperCase())) {
method = ext.method.toUpperCase();
}
}
// 请求地址
if (typeof(ext) === "object" && ext.api) {
if(apiArr.includes(ext.api.toLowerCase())) {
api = ext.api.toLowerCase();
}
}
// cnlive接口,sign处理
if (api == "cnlive") {
// 获取cnlive接口所需参数(包括sign获取等封装)
data = this.getCnliveParams(data, params[api].params, method);
}
// 判断请求方式
if (method.toUpperCase() === "OPEN") {
// 打开页面
window.location.href = params[api].apiAddress + path + "?" + data.join("&");
} else {
// 判断请求类型
switch (method) {
case "POST":
// 调用post方法
post(context, params[api].apiAddress + path, data, callback, ext, params.shop.headers);
break;
case "GET":
default:
// 调用get方法
get(context, params[api].apiAddress + path, data, callback, ext, params.shop.headers);
break;
}
}
}
};
/*vue-resource请求超时timeout设置*/
Vue.http.interceptors.push((request, next) => {
let timeout;
// 如果某个请求设置了_timeout,那么超过该时间,会中断该(abort)请求,并执行请求设置的钩子函数onTimeout方法,不会执行then方法。
if (request._timeout) {
timeout = setTimeout(() => {
request.abort();
}, request._timeout);
}
next(response => {
clearTimeout(timeout);
return response;
});
});
/* ajax请求 结束 */
/* 过滤器 开始 */
Vue.filter("datetime", function(value, fmt) {
// 时间戳转换日期对象
let datetime = new Date(value);
// datetime.setTime(value);
let o = {
"M+": datetime.getMonth() + 1,
"d+": datetime.getDate(),
"h+": datetime.getHours(),
"H+": datetime.getHours(),
"m+": datetime.getMinutes(),
"s+": datetime.getSeconds(),
"q+": Math.floor((datetime.getMonth() + 3) / 3),
S: datetime.getMilliseconds()
};
let week = {
"0": "/u65e5",
"1": "/u4e00",
"2": "/u4e8c",
"3": "/u4e09",
"4": "/u56db",
"5": "/u4e94",
"6": "/u516d"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (datetime.getFullYear() + "").substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length > 1 ? RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468" : "") + week[datetime.getDay() + ""]);
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return fmt;
});
/* 过滤器 结束 */
/* 修改页面标题指令 开始 */
Vue.directive("title", {
inserted: function(el, binding) {
document.getElementsByTagName("title").innerHtml(binding.value);
el.remove();
}
});
/* 修改页面标题指令 结束 */
/* 插入iframe,用于刷新标题 开始 */
Vue.prototype.$iFrame = {
insertIFrame(title) {
let i = document.createElement("iframe");
i.src = "static/img/favicon.png";
i.style.display = "none";
i.onload = function() {
setTimeout(function() {
i.remove();
}, 10);
};
document.title = title;
document.body.appendChild(i);
}
};
/* 插入iframe,用于刷新标题 结束 */
/* 获取Vuex参数 开始 */
/**
* 获取Vuex参数
* @param {string} key [参数名]
* @returns {any}
*/
Vue.prototype.getStore = function(key) {
return store.state[key];
}
/* 获取Vuex参数 结束 */
/* 状态管理 开始 */
const store = new Vuex.Store({
state: {
// debug模式
debug: true,
// 设备类型
deviceType: "",
// 浏览器类型
browser: "",
// uid
uid: 0
},
mutations: {
// 设置设备类型
setDeviceType(state, deviceType) {
state.deviceType = deviceType;
},
// 设置浏览器类型
setBrowser(state, browser) {
state.browser = browser;
},
// 设置浏览器类型
setUid(state, uid) {
state.uid = uid;
}
}
});
/* 状态管理 结束 */
/* eslint-disable no-new */
new Vue({
el: "#app",
router,
store,
components: { App },
template: "<App/>"
});