微信支付密碼登錄的巨坑

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

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',

})

},

})

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章