common.vue
19.9 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
<template>
<div class="common">
<!-- iOS QQ内 & Android WeChat内无法唤起APP,提示用Safari或浏览器打开蒙层 -->
<mask-layer ref="maskLayer"
:maskLayers="maskLayers">
</mask-layer>
<!-- showModal -->
<show-modal
:options="showModals">
</show-modal>
<router-view></router-view>
</div>
</template>
<script>
// 引入组件
import Helper from "@/common/helper.class";
import maskLayer from "@/components/maskLayer/maskLayer";
import showModal from "@/components/showModal/showModal";
// 引入组件
import { Toast, Indicator } from 'mint-ui';
export default {
name: "common",
data () {
return {
top: {
value: 0
},
helper: null,
appShareParams: null,
schemeParams: null,
callApps: null,
maskLayers: null,
showModals: null
}
},
components: {
maskLayer,
showModal
},
created() {
// 设置访问设备
this.setDeviceType();
// 设置浏览器类型
this.setBrowser();
// 设置uid
this.setUid();
// 实例化helper对象
this.helper = new Helper(this);
// 填写收货地址
var specianame = this.$route.name
if(specianame==="luckyWheel"||specianame==="drawRecord") {
if (this.isAndroid.value) {
var addressId = localStorage.getItem('addressId');
var giftId = localStorage.getItem('ext').split("=")[1]
} else {
var addressId = this.$route.query.addressId;
var giftId = this.$route.query.giftId;
}
if (addressId && giftId) {
this.getaddLuckDrawAddress();
}
}
},
methods: {
/**
* 判断是否有获取头部高度的方法有这个方法证明该webview在咱们自己部门的view里面
*/
isBackIcon(){
// 判断获取状态栏高度的方法存不存在
if(typeof window.webkit === "object" && typeof window.webkit.messageHandlers === "object"
&& typeof window.webkit.messageHandlers.getStatusHeight==="function"){
return true
}
},
/**
* APP内拉起分享
* @param {int} type [分享类型]
* @param {int} id [页面id]
*/
toShare(type, id) {
// 判断APP内
if (this.isCNLive.value) {
// 调用分享(新方法)
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.toShare) {
// 分享
window.webkit.messageHandlers.toShare.postMessage({body: {type: type, id: id}});
} else if (window.obj && window.obj.toShare) {
// 分享
window.obj.toShare(type, id);
}
// 调用分享(旧方法)
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.share) {
// 分享
window.webkit.messageHandlers.share.postMessage({body: this.appShareParams});
} else if (window.obj && window.obj.shareUrl) {
// 分享
window.obj.shareUrl(JSON.stringify(this.appShareParams));
}
}
},
/**
* 返回上一页
*/
back(){
// APP内
if (this.isCNLive.value) {
// 调用App,返回按键
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.destory) {
// APP内,iOS销毁WebView
window.webkit.messageHandlers.destory.postMessage({body: {}});
} else if (window.obj && window.obj.finish) {
// APP内,Android销毁WebView
window.obj.finish();
}
} else {
// 后退
window.history.go(-1);
}
},
/**
* 设置访问设备
*/
setDeviceType () {
// 判断设备和浏览器类型
let ua = window.navigator.userAgent.toLowerCase();
// 判断设备类型
if(ua.match(/Android/i) == "android") {
this.$store.commit("setDeviceType", "Android");
this.$set(this.isAndroid, "value", true);
} else if (ua.match(/iPhone/i) == "iphone") {
this.$store.commit("setDeviceType", "iPhone");
this.$set(this.isiPhone, "value", true);
} else if (ua.match(/iPad/i) == "ipad") {
this.$store.commit("setDeviceType", "iPad");
this.$set(this.isiPad, "value", true);
} else {
this.$store.commit("setDeviceType", "Other");
}
},
/**
* 设置浏览器类型
*/
setBrowser () {
// 判断设备和浏览器类型
let ua = window.navigator.userAgent.toLowerCase();
// 判断浏览器类型
if(ua.match(/MicroMessenger/i) == "micromessenger") {
this.$store.commit("setBrowser", "weChat");
this.$set(this.isWeChat, "value", true);
} else if (ua.match(/QQ/i) == "qq") {
this.$store.commit("setBrowser", "QQ");
this.$set(this.isQQ, "value", true);
} else if (ua.match(/alipay/i) == "alipay") {
this.$store.commit("setBrowser", "AliPay");
this.$set(this.isAliPay, "value", true);
} else if (ua.match(/company\/cnlive/i) == "company/cnlive") {
this.$store.commit("setBrowser", "CNLive");
this.$set(this.isCNLive, "value", true);
} else {
this.$store.commit("setBrowser", "Other");
}
},
/**
* 设置uid
*/
setUid() {
if (this.$route.query.uid) {
let uid = this.$route.query.uid;
// 容错,截取uid
if (typeof uid === "object") {
uid = uid.pop();
}
// 设置uid
this.$store.commit("setUid", uid);
}
},
/**
* 通用跳转
* @param {object} item [服务详情对象]
* @param {object} params [地址栏传参]
*/
gotoDetail(item, params = null) {
// 判断App内
if (this.isCNLive.value) {
// 提取key,判断是广告位还是详情
let keyArr = Object.keys(item);
// 判断是Ads还是Detail
let isAds = (keyArr.includes("type") && keyArr.includes("to") && keyArr.includes("shop_type"));
// 初始化参数
let options = {};
console.log(isAds)
// 广告处理
if (isAds) {
// 设置参数
options = {
type: item.type,
to: item.to,
shop_type: item.shop_type
};
console.log(options)
// 调用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));
}
}
}
},
/**
* 唤起APP
*/
callApp() {
// 唤起APP层
this.helper.schemeLayer(this.schemeParams, this.showModal);
},
/* 获取并设置分享参数 开始 */
/**
* 获取并设置分享参数
* @param {object} params [获取分享数据参数]
*/
getShareData(params) {
// 请求接口
this.http("/Api/" + (this.getApiVersion().index) + "/getShareData", params, this.getShareDataCallback, {
method: "POST"
})
},
/**
* 获取并设置分享参数回调
* @param {object} res [服务器返回数据]
* @param {object} ext [扩展参数]
*/
getShareDataCallback(res, ext) {
// 返回处理
if (res.code == 200) {
// 解构数据
let {data: datas} = res;
// 判断APP内
if (this.isCNLive.value) {
/* 旧版APP内分享参数设置 开始 */
// APP内分享参数
this.$set(this, "appShareParams", {
shareUrl: encodeURI(datas.url),
shareIcon: datas.share_img,
shareTitle: datas.share_title,
shareContent: datas.share_desc,
isShareWXMiniProgram: false,
wxMiniProgramId: datas.mini_app_id,
wxMiniProgramPath: datas.mini_app_url
});
/* 旧版APP内分享参数设置 结束 */
} else {
/* 设置唤起APP参数 开始 */
if (datas.inside_aes && datas.inside_aes.type && datas.inside_aes.to && datas.inside_aes.shop_type) {
// 附加其它参数到唤起APP连接中
this.$set(this, "schemeParams", this.$route.query);
// 设置唤起APP参数
Object.assign(this.schemeParams, {
share_mold: "shop",
shop_type: datas.inside_aes.type,
shop_to: datas.inside_aes.to,
shop_shop_type: datas.inside_aes.shop_type
});
// 遍历对象,重新编码
for (let index in this.schemeParams) {
this.schemeParams[index] = encodeURIComponent(decodeURIComponent(this.schemeParams[index]));
}
this.$set(this, "callApps", {
funcs: {
callApp: this.helper,
callAppParams: this.schemeParams,
callAppCallback: this.showModalCallback
}
});
}
/* 设置唤起APP参数 结束 */
/* 设置微信分享参数 开始 */
if (this.isWeChat.value) {
// 微信分享参数
let shares = {
title: datas.share_title, // 分享标题
desc: datas.share_desc, // 分享描述
link: datas.url, // 分享链接
imgUrl: datas.share_img, // 分享图标
// type: "", // 分享类型,music、video或link,不填默认为link
// dataUrl: "", // 如果type是music或video,则要提供数据链接,默认为空
}
// 设置分享
this.getWxParams(shares);
}
/* 设置微信分享参数 结束 */
}
}
},
/**
* 获取微信公众号JS-SDK签名的等参数
* @param {object} shares [分享参数]
*/
getWxParams(shares) {
// 如果在微信内
if (this.isWeChat.value) {
// 请求接口
this.http("/Api/" + (this.getApiVersion().miniapp) + "/getWxParams", {
url: encodeURIComponent(window.location.href.split('#')[0])
}, this.getWxParamsCallback, {
method: "POST",
shares
})
}
},
/**
* 获取微信公众号JS-SDK签名的等参数回调
* @param {object} res [服务器返回数据]
* @param {object} ext [扩展参数]
*/
getWxParamsCallback(res, ext) {
// 返回处理
if (res.code == 200) {
// 解构数据
let {data: datas} = res;
/* 微信JS-SDK 开始 */
// 上下文
let context = this;
// 引入微信JS-SDK
this.$set(this.jweixin, "value", require("jweixin-module"));
//获取微信公众号的配置
this.jweixin.value.config({
debug: context.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: datas.appId, // 必填,公众号的唯一标识
timestamp: datas.timestamp, // 必填,生成签名的时间戳
nonceStr: datas.nonceStr, // 必填,生成签名的随机串
signature: datas.signature.toLowerCase(), // 必填,签名,见附录1
jsApiList: [
"onMenuShareTimeline",
"onMenuShareAppMessage",
"onMenuShareQQ",
"onMenuShareWeibo",
"onMenuShareQZone"
]
});
// 上下文
let contextWX = this.jweixin.value;
// 设置分享参数
let shares = ext.shares;
// JS-SDK准备好之后
this.jweixin.value.ready(function() {
// 分享参数
let sharesParams = {
title: shares.title, // 分享标题
desc: shares.desc, // 分享描述
link: shares.link, // 分享链接
imgUrl: shares.imgUrl, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
this.setStorageParams("message", "分享成功", true);
},
cancel: function () {
// 用户取消分享后执行的回调函数
this.setStorageParams("message", "取消分享", true);
}
}
//获取“分享到QQ”按钮点击状态及自定义分享内容接口(即将废弃)
contextWX.onMenuShareQQ(sharesParams);
//获取“分享给朋友”按钮点击状态及自定义分享内容接口(即将废弃)
contextWX.onMenuShareAppMessage(sharesParams);
//获取“分享到朋友圈”按钮点击状态及自定义分享内容接口(即将废弃)
contextWX.onMenuShareTimeline(sharesParams);
//获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口
contextWX.onMenuShareWeibo(sharesParams);
//获取“分享到QQ空间”按钮点击状态及自定义分享内容接口(即将废弃)
contextWX.onMenuShareQZone(sharesParams);
});
/* 微信JS-SDK 结束 */
}
},
/**
* 唤起APP弹窗
* @param {object} options [成功回调选项]
*/
showModal(options) {
// 调用showModal层参数
this.$set(this, "showModals", {
title: "",
content: "即将离开页面<br />打开网家家APP",
success: options.success,
success_params: options.success_params,
cb: this.showModalCallback,
isShow: true
});
},
/**
* 唤起APP操作后回调
*/
showModalCallback(options) {
// 判断是否在Android WeChat内或在iOS QQ内
if ((this.isAndroid.value && this.isWeChat.value) || (this.isiPhone.value && this.isQQ.value)) {
// 判断微信或QQ,给出不同的图片
if (this.isiPhone.value && this.isQQ.value) {
// 设置蒙层图片
this.$set(this, "maskLayers", {
img: "static/img/mask_layer_browser_iOS.png"
});
} else {
// 设置蒙层图片
this.$set(this, "maskLayers", {
img: "static/img/mask_layer_browser.png"
});
}
// 显示蒙层
this.$refs.maskLayer.showLayer();
} else {
// H5唤起APP
options.success(options.success_params.callApp, options.success_params.path, options.success_params.param);
}
},
/* 获取并设置分享参数 结束 */
/**
* 获取状态栏高度
*/
getStatusHeight() {
if (this.isCNLive.value) {
// 获取状态栏高度
if(window.webkit && window.webkit.messageHandlers) {
// 请求状态栏高度,等待App调用
window.webkit.messageHandlers.getStatusHeight.postMessage({body: {}});
} else if(window.obj) {
// 请求状态栏高度,等待App调用
window.obj.getStatusHeight();
}
}
},
/**
* 获取最近使用的服务的接口
* @param {string} uid [用户]
* @param {string} id [服务id]
*/
gethistoryServiceApp(item) {
// 请求接口
this.http("/Api/" + (this.getApiVersion().serviceApp) + "/historyServiceApp", {
uid: this.getStore("uid"),
id: item.id,
}, this.gethistoryServiceAppCallback, {
method: "POST"
});
},
/**
* 获取最近使用的服务的接口
* @param {object} res [服务器返回数据]
* @param {object} ext [扩展参数]
*/
gethistoryServiceAppCallback(res, ext) {
// 加载完成
Indicator.close();
// 返回处理
if (res.code == 200) {
} else if (res.code == 400) {
// 调取最近使用服务号失败
Toast(res.message);
} else {
// 调取最近使用服务号失败
Toast("调取最近使用服务号失败");
}
},
// 填写收货地址
/**
* 填写收货地址
* @param {string} id [礼物订单id]
* * @param {string} addressId [地址]
*/
getaddLuckDrawAddress() {
// 加载中提示
Indicator.open('加载中...');
// 填写收货地址
var addressId,giftId
if(this.isAndroid.value){
addressId = localStorage.getItem('addressId');
giftId = localStorage.getItem('ext').split("=")[1]
}else {
addressId = this.$route.query.addressId;
giftId = this.$route.query.giftId;
}
// 请求接口
this.http("/Api/" + (this.getApiVersion().LuckDraw) + "/addLuckDrawAddress", {
addressId: addressId,
id: giftId
}, this.getaddLuckDrawAddressCallback, {
method: "POST"
});
},
/**
* 填写收货地址回调
* @param {object} res [服务器返回数据]
* @param {object} ext [扩展参数]
*/
getaddLuckDrawAddressCallback(res, ext) {
// 加载完成
Indicator.close();
// 返回处理
if (res.code == 200) {
// 在安卓环境下清除
if(this.isAndroid.value){
localStorage.removeItem('addressId');
localStorage.removeItem('ext');
}
// 解构数据
let {data: datas} = res;
/* 填写收货地址 */
this.$set(this, "prize_list", {
list: datas.gift
});
/* 获取活动详情页 结束 */
} else if (res.code == 400) {
// 填写收货地址失败
Toast(res.message);
} else {
// 填写收货地址失败
Toast("填写收货地址失败");
}
},
},
mounted () {
// 判断APP内
if (this.isCNLive.value) {
// 上下文
let context = this;
// 暴露方法,由App调用,接收返回
window.getStatusHeight = function(statusString) {
// 获取状态栏高度
let statusJSON = JSON.parse(statusString);
// 设置状态栏参数
context.$set(context.top, "value", statusJSON.statusHeight || context.top.value);
};
this.$nextTick(function(){
// 获取状态栏高度,等待App调用window["getStatusHeight"]
this.getStatusHeight();
})
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style rel="stylesheet/less" lang="less">
@import 'common';
</style>