微信支付密码登录的巨坑

四、接口文档(官方文档)

4.1使用方法及参数

使用方法为:
需要将 <button> 组件 open-type 的值设置为 getRealnameAuthInfo,当用户点击并同意之后,可以通过 bindgetRealnameAuthInfo事件回调获取到微信服务器返回的auth_token,再用auth_token调用API来获取用户加密过后的实名信息

示例:
<button open-type=“getRealnameAuthInfo” bindgetRealnameAuthInfo=“authinfo” category-id="{{[99, 904]}}">实名授权</button>
调用参数为:(!!!!!!!!!标红为巨坑应该改为这个 bindgetrealnameauthinfo="getrealname"

wxml

<button class="payWay" open-type="getRealnameAuthInfo" bindgetrealnameauthinfo="getrealname" category-id="{{[52, 93]}}">dasds </button>

js

const app = getApp();

const api = require('../../utils/util');

import wxValidate from '../../utils/wxValidate';

var Validate = "";

// pages/login/index.js

Page({

data: {

canIUse: wx.canIUse('button.open-type.getUserInfo'),

isHide: false

},

onShow: function() {

this.setData({

cWidth: wx.getSystemInfoSync().windowWidth,

cHeight: wx.getSystemInfoSync().windowHeight,

})

var that = this;

// 查看是否授权

if (wx.getStorageSync("cleared")) {

that.setData({

isHide: true

});

wx.removeStorageSync("cleared")

} else {

that.getAuth(that);

}

},

onLoad: function() {

},

getPhoneNumber(e){

console.log(e);

},

bindGetUserInfo: function(e) {

var that = this;

that.getAuth(that);

if (e.detail.userInfo) {

//用户按了允许授权按钮

// 获取到用户的信息了,打印到控制台上看下

this.setData({

userObj: e.detail.userInfo

})

wx.setStorageSync("wechatData", e.detail.userInfo)

//授权成功后,通过改变 isHide 的值,让实现页面显示出来,把授权页面隐藏起来

that.setData({

isHide: false

});

} else {

//用户按了拒绝按钮

wx.showToast({

title: '您拒绝了授权,将无法继续使用',

icon:'none'

})

}

},

mobilePage: function() {

wx.navigateTo({

url: '/pages/login/mobileLogin',

})

},

logRequest(data) {

var that=this;

wx.showLoading({

title: '加载中',

mask:true

})

wx.cloud.callFunction({

name: 'getDecodedInfo',

data: {

auth_token: data.detail.auth_token

}

}).then(res => {

if(res.result.message){

that.prompt(res.result.message, that);

wx.hideLoading();

return false;

}

if (res.result.data) {

wx.setStorageSync('token', res.result.data.token)

wx.setStorageSync("payLoad", res.result.data.payload)

} else {

var rsp = JSON.parse(res.result);

if (rsp.message){

that.prompt(rsp.message,that);

wx.hideLoading();

return false;

}else{

var token=rsp.data.token

wx.setStorageSync("payLoad", rsp.data.payload)

wx.setStorageSync('token', token)

}

}

console.log("1232311322")

wx.reLaunch({

url: '/pages/home/home',

})

wx.hideLoading()

}).catch(console.error)

},

prompt: function (msg, that) {

that.setData({

toastText: msg,

toastShow: true

})

setTimeout(function () {

that.setData({

toastShow: false

})

}.bind(that), 1500)

},

getrealname(data) {

this.logRequest(data)

if (data.detail) {

if (!data.detail.auth_token) {

wx.navigateTo({

url: '/pages/login/changeLogWay',

})

return false;

} else {

wx.cloud.callFunction({

name: 'getDecodedInfo',

data: {

auth_token: data.detail.auth_token

},

}).then(res => {

wx.setStorageSync("userInfo", res);

wx.hideLoading();

}).catch(console.error)

}

}

},

getAuth(that){

wx.getSetting({

success: function (res) {

if (res.authSetting['scope.userInfo']) {

wx.getUserInfo({

success: function (res) {

wx.login({

success: res => {

// 获取到用户的 code 之后:res.code

// 可以传给后台,再经过解析获取用户的 openid

// 或者可以直接使用微信的提供的接口直接获取 openid ,方法如下:

// wx.request({

// // 自行补上自己的 APPID 和 SECRET

// url: 'https://api.weixin.qq.com/sns/jscode2session?appid=自己的APPID&secret=自己的SECRET&js_code=' + res.code + '&grant_type=authorization_code',

// success: res => {

// // 获取到用户的 openid

// console.log("用户的openid:" + res.data.openid);

// }

// });

wx.setStorageSync("code",res.code);

}

});

}

});

} else {

// 用户没有授权

// 改变 isHide 的值,显示授权页面

that.setData({

isHide: true

});

}

}

});

},

userProtocol(){

wx.navigateTo({

url: '/pages/protocol/protocol',

})

},

private() {

wx.navigateTo({

url: '/pages/protocol/private',

})

},

})

猜你喜欢

转载自blog.csdn.net/qq_38698753/article/details/100152385