引用用微信 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
// });
});
}