如果发起请求不知道,怎么发起的话,参考微信小程序封装wx.request ,发起网络请求。封装为模块
1、调用 wx.login() 获取 临时登录凭证code ,并回传到开发者服务器。
2、调用 auth.code2Session 接口,换取 用户唯一标识 OpenID 和 会话密钥 session_key。
3、使用会话密钥 session_key解密button返回的手机号加密后的数据
一、调用 wx.login() 获取 临时登录凭证code ,并回传到开发者服务器
官方文档
调用接口获取登录凭证(code)。通过凭证进而换取用户登录态信息,包括用户的唯一标识(openid)及本次登录的会话密钥(session_key)等
onLoad: function () {
wx.login({
success: function (res) {
if (res.code) {//获取code之后,换取session_key
var url = app.globalData.swtBase + "A_login.php?act=main&code=" + res.code;
util.http(that, url, that.loginCallback);//请求反馈
}
console.log(res);
},
fail: function (res) {
console.log(res);
//return;
wx.hideLoading();
wx.showModal({
title: '提示',
content: '稍微等一小会再试.',
showCancel: false,
complete: function () {
wx.navigateBack({
delta: 1
})
}
})
return;
}
})
}
loginCallback: function(that, data) {
wx.hideLoading();
var openid = data.openid;//获取openid
var userSessionKey = data.userSessionKey;//获取session_key
that.setData({
openid: openid,
userSessionKey: userSessionKey
})
},
A_login.php换取session_key
function curl_get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//禁止调用时就输出获取到的数据
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);//HTTPS这个是重点。
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);//HTTPS这个是重点。
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$token_url = "https://api.weixin.qq.com/sns/jscode2session";
$token_url.= "?appid=" . AppID . "";
$token_url.= "&secret=" . AppSecret . "";
$token_url.= "&js_code=" . $_GET["code"] . "";
$token_url.= "&grant_type=authorization_code";
//$json_token = file_get_contents($token_url);
$json_token = curl_get($token_url);//curl_get请见
//echo $json_token;
$token = json_decode($json_token, true);
//echo '<pre>';
//print_r($token);
//exit;
到这里前面的一系列只是为了获取appid和session_key为解码做准备!
button请求
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">授权登录</button>
//获取用户手机号码
onGetPhoneNumber: function(e) {
console.log(e);
var that = this;
if (e.detail.errMsg == 'getPhoneNumber:ok') {
var url = app.globalData.swtBase + "A_login.php?act=PhoneNumber";//向服务器,发起转换请求
var htmlDatas = {
tjrhao: tjrhao,
openid: that.data.openid,
userSessionKey: that.data.userSessionKey,
iv: e.detail.iv,
encryptedData: e.detail.encryptedData
};
util.http(that, url, that.loginPhoneNumberCallback, null, htmlDatas);
}
return;
},
具体怎么解码的,官方给了文档,直接套用就行了