uni-appH5授权登录⽀付
引⽤⽤ js-sdk官⽅⽂档 //NPM安装⽅式(不会⽤NPM就不要⽤这种⽅式)
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 = "open.weixin.qq/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.QueryString('code'))
//获取code传递给后端进⾏判断是否进⾏过⼿机号绑定,未绑定去进⾏⼿机号绑定已经绑定后正常货token 存储跳转代码中有从新拼接跳转原因是获取code是?拼接的,所以从新拼接路径删去?跳转到相应的页⾯Vue.prototype.weixinH5Login = function(code){
var that = this
that.$('/api/Account/WebWechatLogin', {
params: {
'code': code,
}
}).then(res => {
// 登陆成功
de==1){
var href=window.location.host
var pathname = window.location.pathname
if(ken){
uni.setStorageSync("goble_accessTokenExpires",ken.accessTokenExpires)
if(!res.data.passwordStatus){
window.location.href=''+href+pathname+'#/pages/registerLogin/setPwd';
return false
}
if(!quirementStatus){
window.location.href=''+href+pathname+'#/pages/registerLogin/userInfo';
return false
}
window.location.href=''+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=''+href+pathname+'#/pages/registerLogin/weachtBindMobile';                            }else{
window.location.href=''+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,
app登录界面"totalfee": that.projectInfo.ultimatelyPrice
}).then(res => {
//获取⽀付数据
that.jsdkZfData = res.data
//权限数据获取
})
}
},
/* 权限获取 */
getJsApiSignatureg:function(){
var that = this
that.$http.post('/api/Payment/GetJsApiSignature',{}).then(res => {
console.log(res)
de==1){
that.jsdkData = res.data
//开始调取⽀付接⼝
that.payH5Request(that.jsdkData,that.jsdkZfData)
},
//⽀付开始
Vue.prototype.payH5Request=function(verifyData,payData){
var that = this
console.log('权限校验开始1')
fig({
debug: false, // 开启调试模式,调⽤的所有api的返回值会在客户端alert出来,若要查看传⼊的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId:verifyData.appId, // 必填,的唯⼀标识
timestamp:verifyData.timestamp, // 必填,⽣成签名的时间戳
Str, // 必填,⽣成签名的随机串
signature:verifyData.signature, // 必填,签名,见附录1
jsApiList: ['chooseWXPay'] // 必填,需要使⽤的JS接⼝列表,所有JS接⼝列表见附录2
});
console.log('权限校验结束')
ady(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: Str, // ⽀付签名随机串,不长于 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'
})
url:'/pages/order/index'
})
},
cancel: function(r) {
console.log(r)
uni.showToast({
title:'已经取消⽀付',
},
fail:function(res) {
console.log('payfail')                                    console.log(res)
uni.showToast({
title:'已经取消⽀付',                                        icon:'none'
})
}
});
})
(function(res) {
console.log('error')
console.log(res)
// uni.showToast({
//    icon: 'none',
//    title: '⽀付失败了',
//    duration: 4000
// });
});
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。