// 首先 引入sdk
import wx from 'jweixin-module'
// 然后定义一个方法
function getUrlKey(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
}
toPay() {
const token = uni.getStorageSync("token");
if (!code) {
(async () => {
var appid = '公众号的唯一标识';
let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(window.location.href)}&response_type=code&scope=snsapi_userinfo&state=${0}#wechat_redirect`;
window.location.href = url
return
})()
} else {
this.$http.get('接口url', {
params: {
appId: '公众号的唯一标识',
code,
},
})
.then((response) => {
localStorage.setItem('openid', response.data.openid)
this.$http.post(`创建订单的接口`, {
// 创建订单的一些参数
}).then(res => {
// 创建订单完成 这里唤起支付
var that = this;
wx.config({
debug: false,
appId: '公众号的唯一标识', // 必填,公众号的唯一标识
timestamp: res.data.data.timeStamp, // 必填,生成签名的时间戳
nonceStr: res.data.data.nonceStr, // 必填,生成签名的随机串
signature: res.data.data.paySign, // 必填,签名
jsApiList: ['chooseWXPay'],
});
wx.ready(function() {
wx.chooseWXPay({
timestamp: res.data.data.timeStamp,
nonceStr: res.data.data.nonceStr, // 支付签名随机串,不长于 32 位
package: res.data.data.package,
signType: res.data.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: res.data.data.paySign, // 支付签名
success(res) {
console.log('success:' + JSON.stringify(res));
},
fail(res) {
console.log('失败res',res)
}
});
});
})
});
}
}
大概就这些 剩下就是配置安全域名之类的 得去公众号的后台配置了
拜了个拜 要是有漏下的 我再补