main.js
20.5 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
import Vue from "vue";
import Vuex from "vuex";
import 'babel-polyfill';
import Es6Promise from 'es6-promise';
require('es6-promise').polyfill();
import 'url-search-params-polyfill';
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 { Indicator, InfiniteScroll } from 'mint-ui';
import { Toast, Lazyload} from 'vant';
//VideoPlayer视频播放
import VideoPlayer from 'vue-video-player';
import hls from 'videojs-contrib-hls';
import App from "./App";
Vue.use(Vuex);
Vue.use(VueResource);
Vue.use(VueHead, { separator: " " });
Vue.use(Indicator).use(InfiniteScroll);
Vue.use(VideoPlayer);
// HLSL视频流
Vue.use(hls);
Vue.use(Toast);
Vue.use(Lazyload,{
preload: 90,
loading: "static/img/x.jpg",
error: "static/img/x.jpg",
attempt: 3
});
Es6Promise.polyfill();
Vue.config.productionTip = false;
// 部署模式设置(开发模式:"dev",测试模式:"test",生产模式:"prod")
Vue.prototype.mode = "prod";
// 微信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内,默认不在,common.vue进行判断设置
Vue.prototype.isCNLive1 = {
value: false
}
// 是否在支付宝内,默认不在,common.vue进行判断设置
Vue.prototype.isAliPay = {
value: false
}
// 是否在微信内,默认不在,common.vue进行判断设置
Vue.prototype.isWeChat = {
value: false
}
// 是否在QQ内,默认不在,common.vue进行判断设置
Vue.prototype.isQQ = {
value: false
}
/* 后端API版本控制参数 开始 */
// 参数设置
Vue.prototype.apiVersion = {
index: "Index3",
miniapp: "Miniapp2"
}
/**
* 获取后端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: ""
}
/**
* 处理加水印图片链接
* @param {string} imageUrl [图片链接]
* @param {string} SIGN [缩略图参数字符串]
*/
Vue.prototype.formatPicLink = function(imageUrl, SIGN = "") {
// 返回结果
let result = imageUrl;
// 如果没有SIGN,直接返回
if (!SIGN) {
return result;
}
// 处理加水印图片链接
if (imageUrl.indexOf("?") !== -1) {
result = imageUrl + SIGN.replace("?", "/");
}
// 返回结果
return result;
}
/* 设置图片尺寸参数 结束 */
/* 通用跳转封装 开始 */
/**
* 获取跳转参数
* @param {object} params [参数对象]
* @param {object} keysArr [所需key数组]
* @param {string} symbol [链接字符串,默认 &]
*/
Vue.prototype.getGotoDetailParams = function(params, keysArr = [], symbol = "&") {
let paramsString = "";
// 判断是否有参数
if (typeof params === "object") {
// 参数字符串数组
let paramsStringArr = new Array();
// 如果未设置需要数组,取全部key
if (!keysArr.length) {
keysArr = Object.keys(params);
}
// 遍历参数对象,取出需要的keys和value
for (let i = 0; i < keysArr.length; i++) {
// 判断key如果存在,推入数组
if (params[keysArr[i]]) {
paramsStringArr.push(keysArr[i] + "=" + params[keysArr[i]]);
}
}
// 判断数组,设置返回参数
if (paramsStringArr.length) {
paramsString = symbol + paramsStringArr.join("&");
}
}
return paramsString;
}
/**
* 通用跳转
* @param {object} goods [商品详情对象]
* @param {object} params [地址栏传参]
*/
Vue.prototype.gotoDetail = function(goods, params) {
// 提取key,判断是广告位还是详情
let keyArr = Object.keys(goods);
// 判断是Ads还是Detail
let isAds = (keyArr.includes("type") && keyArr.includes("to") && keyArr.includes("shop_type"));
// 通用所需附加参数key
let keysArr = [];
// 好家园App内跳转处理
if (this.isCNLive.value) {
// 初始化参数
let options = {};
// 广告处理
if (isAds) {
// 设置参数
options = {
type: goods.type,
to: goods.to,
shop_type: goods.shop_type
};
// 商品详情处理
} else {
// 设置参数
options = {
type: "2",
to: goods.id,
shop_type: goods.shop_type
};
}
// 调用App,提交参数
if(window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.wjjToAppOnlineRetailers) {
window.webkit.messageHandlers.wjjToAppOnlineRetailers.postMessage({body: options});
} else if(window.obj && window.obj.wjjToAppOnlineRetailers) {
window.obj.wjjToAppOnlineRetailers(JSON.stringify(options));
}
} else { // H5页面跳转处理
if (isAds) {
// 判断广告类型,进行处理
switch (goods.type) {
case "2": // 跳转商品详情,shop_type 1跳转普通商品 2跳转文创商品 to为商品id 为0不跳转
// 判断是否需要跳转
if (goods.to) {
// 判断跳转详情对应页面类型
switch (goods.shop_type) {
case "2":
// 跳转文创商品详情页
// uni.navigateTo({
// url: "/pages/main/cultureDetail/cultureDetail?id=" + goods.to + this.getGotoDetailParams(params, keysArr)
// });
window.location.href = this.params.shop.apiAddress + "/#/pages/main/cultureDetail/cultureDetail?id=" + goods.to + this.getGotoDetailParams(params, keysArr);
break;
case "1":
default:
// 跳转详情页
// uni.navigateTo({
// url: "/pages/main/detail/detail?id=" + goods.to + this.getGotoDetailParams(params, keysArr)
// });
window.location.href = this.params.shop.apiAddress + "/#/pages/main/cultureDetail/cultureDetail?id=" + goods.to + this.getGotoDetailParams(params, keysArr);
}
}
break;
case "5": // webView页 to Url地址
// 跳转URL
if (goods.to) {
window.location.href = goods.to;
}
break;
case "12": // 商城总分类 to 分类id shop_type 1,2,3 级分类
// 跳转商品列表页面,带着参数,平台分类(groupid)
if (goods.to) {
// TODO
}
break;
case "14": // 商城首页
// 跳转商城首页
uni.redirectTo({
url: "/" + this.HomePage + this.getGotoDetailParams(params, keysArr, "?")
});
break;
case "20": // 商城店铺分类 to 分类id shop_type 1,2 级分类
// 跳转商品列表页面,带着参数,商家分类(categoryId)
if (goods.to) {
// 跳转商城分类列表页
uni.navigateTo({
url: "/pages/goods/goodsList/goodsList?categoryId=" + goods.to + this.getGotoDetailParams(params, keysArr)
});
}
break;
case "21": // 跳转店铺首页 to 店铺id shop_type 页面地址
// 跳转店铺首页
uni.navigateTo({
url: "/pages/main/shopIndex/shop?shop_id=" + goods.to + this.getGotoDetailParams(params, keysArr)
});
break;
}
} else {
// 判断是否需要跳转
if (goods.id) {
// 判断跳转详情对应页面类型
switch (goods.shop_type) {
case "2":
// 跳转文创商品详情页
// uni.navigateTo({
// url: "/pages/main/cultureDetail/cultureDetail?id=" + goods.id + this.getGotoDetailParams(params, keysArr)
// });
window.location.href = this.params.shop.apiAddress + "/#/pages/main/cultureDetail/cultureDetail?id=" + goods.id + this.getGotoDetailParams(params, keysArr);
break;
case "1":
default:
// 跳转详情页
uni.navigateTo({
url: "/pages/main/detail/detail?id=" + goods.id + this.getGotoDetailParams(params, keysArr)
});
}
}
}
}
}
/* 通用跳转封装 结束 */
/* 根据模式,获取相关请求参数 开始 */
Vue.prototype.getParams = function(mode) {
// 返回参数
let params = {};
let app_key = "769_jnjxdh3w53";
let timestamp = new Date().getTime().toString().substr(0, 10);
switch (mode.toLowerCase()) {
case "test":
// 请求头
app_key = "769_jnjxdh3w53";
timestamp = new Date().getTime().toString().substr(0, 10);
nonce = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpeLgK0vk5POU5PQ";
params.shop = {
apiAddress: "https://wjjshop.cnlive.com",
headers: {
"App-Key": app_key,
"Timestamp": timestamp,
"Signature": sha1(app_key + nonce + timestamp),
"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: "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":
// 请求头
app_key = "769_jnjxdh3w53";
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"
},
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/>"
});