common.vue
27.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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
<template>
<div class="common">
<!-- iOS QQ内 & Android WeChat内无法唤起APP,提示用Safari或浏览器打开蒙层 -->
<mask-layer ref="maskLayer"
:maskLayers="maskLayers">
</mask-layer>
<!-- 通用弹窗 -->
<show-modal ref="showModal"
:options="showOptions">
</show-modal>
<!-- 唤起APP弹窗 -->
<show-modal ref="callAppModal"
:options="callAppModals">
</show-modal>
<router-view ref="childCommon"></router-view>
</div>
</template>
<script>
// 引入组件
import Helper from "@/common/helper.class";
import maskLayer from "@/components/maskLayer/maskLayer";
import showModal from "@/components/showModal/showModal";
export default {
name: "common",
data () {
return {
top: {
value: 0
},
helper: null,
appShareParams: null,
schemeParams: null,
callApps: null,
maskLayers: null,
callAppModals: null,
modals: null,
showOptions: null,
/* 上传图片参数开始 开始 */
files: {
name: "", // 图片名字
type: "" // 图片类型
},
Orientation: null, // 旋转角度
headerImage: null
/* 上传图片参数开始 结束 */
}
},
components: {
maskLayer,
showModal
},
created() {
// 设置访问设备
this.setDeviceType();
// 设置浏览器类型
this.setBrowser();
// 设置uid
this.setUid();
// 实例化helper对象
this.helper = new Helper(this);
},
methods: {
/**
* 设置访问设备
*/
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 {function} callback [回调函数]
*/
doVote(item, callback) {
// 请求接口
this.http("/Api/" + (this.getApiVersion().index) + "/moviesVote", {
id: item.id,
uid: item.uid,
type: 1
}, callback || function() {}, {
method: "POST",
item
});
},
/**
* App内拉起分享
* @param {int} type [分享类型]
* @param {int} id [页面id]
* @param {object} ext [分享扩展参数,可选参数]
*/
toShare(type, id, ext = null) {
// 判断APP内
if (this.isCNLive.value) {
// 调用分享(新方法)
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.toShare) {
// 分享
if (ext) {
window.webkit.messageHandlers.toShare.postMessage({body: {type: type, id: id, ext: JSON.stringify(ext)}});
} else {
window.webkit.messageHandlers.toShare.postMessage({body: {type: type, id: id}});
}
} else if (window.obj && window.obj.toShare) {
// 分享
if (ext) {
window.obj.toShare(type, id, JSON.stringify(ext));
} else {
window.obj.toShare(type, id);
}
}
}
},
/**
* 唤起APP
*/
callApp() {
// 唤起APP层
this.helper.schemeLayer(this.schemeParams, this.callAppModal);
},
/* 获取并设置分享参数 开始 */
/**
* 获取并设置分享参数
* @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.callAppModalCallback
}
});
}
/* 设置唤起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 [成功回调选项]
*/
callAppModal(options) {
// 调用showModal层参数
this.$set(this, "callAppModals", {
title: "",
message: "即将离开页面<br />打开家园App",
confirmButtonText: "允许",
confirmButtonCallback: this.callAppModalCallback,
showCancelButton: true,
cancelButtonText: "取消",
success: options.success,
success_params: options.success_params
});
// 显示唤起APP浮层
this.$refs.callAppModal.showLayer();
},
/**
* 唤起APP操作后回调
*/
callAppModalCallback(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);
}
},
/* 获取并设置分享参数 结束 */
/**
* 唤起APP弹窗
* @param {object} options [成功回调选项]
*/
showModal(options) {
// 调用showModal层参数
this.$set(this, "showOptions", options);
// 显示通用浮层
this.$refs.showModal.showLayer();
},
/**
* 唤起APP操作后回调
*/
showModalCallback(options) {
// 暂未使用
},
/**
* 获取状态栏高度
*/
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 {object} goods [商品详情对象]
* @param {object} query [地址栏参数]
*/
gotoDetail(goods, query) {
// let params = null;
let params = this.getParams(this.mode);
// 提取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
};
// 判断是否是请求商品详情页面
if (options.type == '2') {
// 按如果有query参数,并且有query.activityCode参数,设置activityCode参数
if (query !== null && typeof query === 'object' && query.activityCode) {
// 设置渠道页面参数
this.$set(options, 'activity_code', query.activityCode);
}
}
// 商品详情处理
} else {
// 设置参数
options = {
type: '2',
to: goods.id,
shop_type: goods.shop_type
};
// 按如果有query参数,并且有query.activityCode参数,设置activityCode参数
if (query !== null && typeof query === 'object' && query.activityCode) {
// 设置渠道页面参数
this.$set(options, 'activity_code', query.activityCode);
}
}
// 调用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{
if (isAds) {
switch(goods.type){
case "1","2":
window.location.href = params.shop.apiAddress + "/html/mall/1/#/pages/main/detail/detail?id=" + goods.to + (query.channelName ? '&channelName=' + query.channelName : '');
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 "1":
window.location.href = params.shop.apiAddress + "/html/mall/1/#/pages/main/detail/detail?id=" + goods.id + (query.channelName ? '&channelName=' + query.channelName : '');
break;
case "2":
// 跳转文创商品详情页
window.location.href = params.shop.apiAddress + "/html/mall/1/#/pages/main/cultureDetail/cultureDetail?id=" + goods.id + (query.channelName ? '&channelName=' + query.channelName : '');
break;
default:
// 跳转详情页
// uni.navigateTo({
// url: "/pages/main/detail/detail?id=" + goods.id + this.getGotoDetailParams(params, keysArr)
// });
}
}
}
}
},
/* 通用跳转封装 结束 */
/* 图片上传压缩/旋转优化 开始 */
/**
* 待上传图片处理
* @param {object} file [文件对象]
* @param {object} ext [扩展参数]
*/
imgPreview(file, ext) {
// 定义变量
let context = this;
/* 定义文件名/文件类型 开始 */
this.$set(this.files, {
name: "",
type: ""
});
this.$set(this.files, "name", file.name);
this.$set(this.files, "type", file.type);
/* 定义文件名/文件类型 结束 */
// 初始化文件编码/图片方向对象
this.$set(this, "headerImage", null);
this.$set(this, "Orientation", null);
// 去获取拍照时的信息,解决拍出来的照片旋转问题
Exif.getData(file, function () {
context.Orientation = Exif.getTag(this, "Orientation");
});
// 检测是否支持FileReader
if (!file || !window.FileReader) {
return;
}
if (/^image/.test(file.type)) {
// 创建一个reader
let reader = new FileReader();
// 将图片转成 base64 格式
reader.readAsDataURL(file);
// 读取成功后的回调
reader.onloadend = function () {
// 实例化图片对象
let result = this.result;
let img = new Image();
img.src = result;
// 判断图片是否大于500K,是就直接上传,反之压缩图片
if (this.result.length <= 500 * 1024) {
context.headerImage = this.result;
context.postImg(ext);
} else {
img.onload = function () {
let data = context.compress(img, context.Orientation);
context.headerImage = data;
context.postImg(ext);
};
}
};
}
},
/**
* 压缩图片
* @param {object} img [待处理图片对象]
* @param {int} Orientation [图片上传旋转值]
*/
compress(img, Orientation) {
// 初始化canvas对象
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
// 瓦片canvas
let tCanvas = document.createElement("canvas");
let tctx = tCanvas.getContext("2d");
// let initSize = img.src.length;
let width = img.width;
let height = img.height;
// 如果图片大于四百万像素,计算压缩比并将大小压至400万以下
// 缩放比例
let ratio;
if ((ratio = (width * height) / 4000000) > 1) {
// 大于400万像素
ratio = Math.sqrt(ratio);
width /= ratio;
height /= ratio;
} else {
// 等比不缩放
ratio = 1;
}
// 设置canvas宽高
canvas.width = width;
canvas.height = height;
// 背景色
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 如果图片像素大于100万则使用瓦片绘制
let count;
if ((count = (width * height) / 1000000) > 1) {
// 超过100万像素;
count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
// 计算每块瓦片的宽和高
let nw = ~~(width / count);
let nh = ~~(height / count);
tCanvas.width = nw;
tCanvas.height = nh;
for (let i = 0; i < count; i++) {
for (let j = 0; j < count; j++) {
tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
}
}
} else {
ctx.drawImage(img, 0, 0, width, height);
}
// 修复iOS上传图片的时候,被旋转的问题
if (Orientation != "" && Orientation != 1) {
switch (Orientation) {
case 6: // 需要顺时针(向左)90度旋转
this.rotateImg(img, "left", canvas);
break;
case 8: // 需要逆时针(向右)90度旋转
this.rotateImg(img, "right", canvas);
break;
case 3: // 需要180度旋转
this.rotateImg(img, "right", canvas); // 转两次
this.rotateImg(img, "right", canvas);
break;
}
}
// 进行最小压缩
let ndata = canvas.toDataURL("image/jpeg", 0.1);
tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
return ndata;
},
/**
* 旋转图片
* @param {object} img [图片对象]
* @param {string} direction [旋转方向]
* @param {object} canvas对象
*/
rotateImg(img, direction, canvas) {
// 最小与最大旋转方向,图片旋转4次后回到原方向
const min_step = 0;
const max_step = 3;
if (img == null) {
return;
}
// img的高度和宽度不能在img元素隐藏后获取,否则会出错
let height = img.height;
let width = img.width;
let step = 2;
if (step == null) {
step = min_step;
}
if (direction == "right") {
step++;
//旋转到原位置,即超过最大值
step > max_step && (step = min_step);
} else {
step--;
step < min_step && (step = max_step);
}
// 旋转角度以弧度值为参数
let degree = (step * 90 * Math.PI) / 180;
let ctx = canvas.getContext("2d");
switch (step) {
case 0:
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, 0, -height);
break;
case 2:
canvas.width = width;
canvas.height = height;
ctx.rotate(degree);
ctx.drawImage(img, -width, -height);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, -width, 0);
break;
}
},
/**
* 将base64字符串转成file对象
* @param {string} dataurl [图片URL/base64编码]
*/
dataURLtoFile(dataurl) {
var arr = dataurl.split(","),
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], this.files.name, {
type: this.files.type
});
},
/**
* 七牛云图片上传
* @param {object} ext [扩展参数]
*/
async postImg(ext) {
// 设置返回参数,加上base64编码数据
let files = {
content: this.headerImage,
file: this.dataURLtoFile(this.headerImage)
};
// 返回压缩后的文件数据
if (typeof ext.postImgCallback === "function") {
ext.postImgCallback(files, ext);
}
}
/* 图片上传压缩/旋转优化 结束 */
},
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>