sdk.js
5.2 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
var jweixin = require("jweixin-module");
import http from "@/shopro/request/index";
import $platform from "@/shopro/platform";
export default {
//判断是否在微信中
isWechat() {
var ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/micromessenger/i) == "micromessenger") {
return true;
} else {
return false;
}
},
// 鉴权页面
initJssdk(callback) {
http("common.wxJssdk", {
uri: encodeURIComponent($platform.entry())
}).then(res => {
jweixin.config({
debug: res.data.debug,
appId: res.data.appId,
timestamp: res.data.timestamp,
nonceStr: res.data.nonceStr,
signature: res.data.signature,
jsApiList: res.data.jsApiList,
openTagList: res.data.openTagList
});
if (callback) {
callback(res.data);
}
});
},
//在需要定位页面调用
getLocation(callback) {
this.isWechat() && this.initJssdk(function(res) {
jweixin.ready(function() {
jweixin.getLocation({
type: "gcj02", // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function(res) {
callback(res);
},
fail: function(res) {
console.log("%c微信H5sdk,getLocation失败:",
"color:green;background:yellow");
},
});
});
});
},
//获取微信收货地址
openAddress(callback) {
this.isWechat() && this.initJssdk(function(res) {
jweixin.ready(function() {
jweixin.openAddress({
success: function(res) {
callback(res);
},
fail: function(err) {
console.log("%c微信H5sdk,openAddress失败:",
"color:green;background:yellow");
},
complete: function(msg) {}
});
});
});
},
// 微信扫码
scanQRCode(callback) {
this.isWechat() && this.initJssdk(function(res) {
jweixin.ready(function() {
jweixin.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: function(res) {
callback(res);
},
fail: function(res) {
console.log("%c微信H5sdk,scanQRCode失败:",
"color:green;background:yellow");
},
});
});
});
},
// 微信分享
share(data, callback) {
this.isWechat() && this.initJssdk(function(res) {
jweixin.ready(function() {
var shareData = {
title: data.title,
desc: data.desc,
link: data.path,
imgUrl: data.image,
success: function(res) {
callback(res);
// 分享后的一些操作,比如分享统计等等
},
cancel: function(res) {}
};
jweixin.updateAppMessageShareData(shareData); //新版接口
//分享到朋友圈接口
// jweixin.updateTimelineShareData(shareData);
});
});
},
// 打开坐标位置
openLocation(data, callback) { //打开位置
this.isWechat() && this.initJssdk(function(res) {
jweixin.ready(function() {
jweixin.openLocation({ //根据传入的坐标打开地图
latitude: data.latitude,
longitude: data.longitude
});
});
});
},
// 选择图片
chooseImage(callback) { //选择图片
this.isWechat() && this.initJssdk(function(res) {
jweixin.ready(function() {
jweixin.chooseImage({
count: 1,
sizeType: ["compressed"],
sourceType: ["album"],
success: function(rs) {
callback(rs);
}
});
});
});
},
//微信支付
wxpay(data, callback) {
let that = this;
this.isWechat() && this.initJssdk(function(res) {
jweixin.ready(function() {
WeixinJSBridge.invoke('getBrandWCPayRequest', {
"appId": data.appId, //公众号ID,由商户传入
"timeStamp": data.timeStamp, //时间戳,自1970年以来的秒数
"nonceStr": data.nonceStr, //随机串
"package": data.package,
"signType": data.signType, //微信签名方式:
"paySign": data.paySign,
},
function(res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
callback(res);
} else {
console.log("%c微信H5sdk,chooseWXPay失败:",
"color:green;background:yellow");
callback(res);
}
});
// jweixin.chooseWXPay({
// timestamp: data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
// nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
// package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
// signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
// paySign: data.paySign, // 支付签名
// success: function(res) {
// callback(res);
// },
// fail: function(res) {
// console.log("%c微信H5sdk,chooseWXPay失败:",
// "color:green;background:yellow");
// callback(res);
// },
// cancel: function(res) {
// },
// });
});
});
}
};