引用用微信 js-sdk微信官方文档 //NPM安装方式(不会用NPM就不要用这种方式)
npm install jweixin-module --save
下载地址:https://unpkg.com/jweixin-module@1.6.0/lib/index.js
import jweixin from '@/components/jweixin-module/index.js'
Vue.prototype.jweixin = jweixin
//判断是否在h5页面是的话进行第三方微信授权登录
<!-- #ifdef H5 -->
<!-- <view class="third-login">
<view class="title"><text>第三方登录</text></view>
<view class="login-icon">
<view class="" @click="weixinAuth()">
<uni-icons type="weixin" size="16" color="#2CBE78"></uni-icons>
</view>
</view>
</view> -->
<!-- #endif -->
weixinAuth(){
var that = this
//调起微信授权
that.weachtAuthorization()
},
//进行微信授权
const weachtAuthorization = (path) => {
let href= encodeURIComponent(window.location.href)
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx782dd7ff4e4002c3&redirect_uri="+href +"&response_type=code&scope=snsapi_userinfo#wechat_redirect"
}
//微信授权回调以后获取code
//获取url后面拼接的参数
const getQueryString = (name) => {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
let r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
that.weixinH5Login(that.getQueryString('code'))
//获取code传递给后端进行判断是否进行过手机号微信绑定,未绑定去进行手机号绑定已经绑定后正常货token 存储跳转 代码中有从新拼接跳转原因是获取code是?拼接的,所以从新拼接路径删去?跳转到相应的页面
Vue.prototype.weixinH5Login = function(code){
var that = this
that.$http.get('/api/Account/WebWechatLogin', {
params: {
'code': code,
}
}).then(res => {
// 登陆成功
if(res.code==1){
var href=window.location.host
var pathname = window.location.pathname
if(res.data.token){
// 登陆成功的
uni.setStorageSync("goble_token",res.data.token.accessToken);
uni.setStorageSync("goble_accessTokenExpires",res.data.token.accessTokenExpires)
if(!res.data.passwordStatus){
window.location.href='http://'+href+pathname+'#/pages/registerLogin/setPwd';
return false
}
if(!res.data.requirementStatus){
window.location.href='http://'+href+pathname+'#/pages/registerLogin/userInfo';
return false
}
window.location.href='http://'+href+pathname+'#/pages/index/index';
}else{
if(res.data.openId){
uni.setStorageSync("unionId",res.data.unionId);
uni.setStorageSync("openId",res.data.openId);
window.location.href='http://'+href+pathname+'#/pages/registerLogin/weachtBindMobile';
}else{
window.location.href='http://'+href+pathname+'#/pages/registerLogin/login';
}
}
}
})
}
//授权登录后可以进行微信支付
//微信支付首先后端生成订单 然后输入输入order 获取校验数据和支付数据
paymentH5Money(order){
var that = this
if(that.userInfo.wechatOpenId){
that.$http.post('/api/Payment/JsApiUnifiedorder', {
"openId":that.userInfo.wechatOpenId,
"orderCode": order,
"totalfee": that.projectInfo.ultimatelyPrice
}).then(res => {
//获取支付数据
that.jsdkZfData = res.data
//权限数据获取
that.getJsApiSignatureg()
})
}
},
/* 权限获取 */
getJsApiSignatureg:function(){
var that = this
that.$http.post('/api/Payment/GetJsApiSignature',{}).then(res => {
console.log(res)
if(res.code==1){
that.jsdkData = res.data
//开始调取微信支付接口
that.payH5Request(that.jsdkData,that.jsdkZfData)
}
});
},
//微信公众号支付开始
Vue.prototype.payH5Request=function(verifyData,payData){
var that = this
console.log('权限校验开始1')
that.jweixin.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId:verifyData.appId, // 必填,公众号的唯一标识
timestamp:verifyData.timestamp, // 必填,生成签名的时间戳
nonceStr:verifyData.nonceStr, // 必填,生成签名的随机串
signature:verifyData.signature, // 必填,签名,见附录1
jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
console.log('权限校验结束')
that.jweixin.ready(function() {
console.log('支付开始1-校验支付接口')
that.jweixin.checkJsApi({
jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function(res) {
console.log('checkjsapi Success')
console.log(res);
},
fail:function(res) {
console.log('res')
console.log(res);
}
});
console.log('支付开始2-开始支付')
that.jweixin.chooseWXPay({
timestamp: payData.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: payData.nonceStr, // 支付签名随机串,不长于 32 位
package: payData.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
signType: payData.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign:payData.paySign, // 支付签名
success:function(res) {
// 支付成功后的回调函数
console.log(res)
uni.showToast({
title:'支付成功',
icon:'none'
})
uni.redirectTo({
url:'/pages/order/index'
})
},
cancel: function(r) {
console.log(r)
uni.showToast({
title:'已经取消支付',
icon:'none'
})
},
fail:function(res) {
console.log('payfail')
console.log(res)
uni.showToast({
title:'已经取消支付',
icon:'none'
})
}
});
})
that.jweixin.error(function(res) {
console.log('error')
console.log(res)
// uni.showToast({
// icon: 'none',
// title: '支付失败了',
// duration: 4000
// });
});
}