PHP解密微信小程序手机号简单DEMO
//第一步 调用一遍wx.login获取Code
var that = this;
wx.login({
success: function (ret) {
//获取微信登陆后的code
that.data.code = ret.code;
wx.request({
url: app.config.apiUrl + "/api/wechatlogin?code=" + ret.code,
success: function (result) {
//获得服务端返回的session_key和openid
that.data.openid = result.data.openid;
that.data.session_key = result.data.session_key;
}
})
}
})<?php
//第二步 调用微信API获取Session_key和OPENID openid在这里没啥用。
//我这里是TP5框架
public function wechatLogin(){
$appid = '';//小程序APPID
$appkey = '';//小程序的APPKEY
if(input("?code")){
$code = input("code");
$ret = curlGet("https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appkey."&js_code=".$code."&grant_type=authorization_code");
try{
$ret = json_decode($ret);
if(property_exists($ret,'session_key')){
$session_key = $ret->session_key;
$openid = $ret->openid;
return json(["code"=>0,"session_key"=>$session_key,"openid"=>$openid]);
}else{
return json(["code"=>1,"msg"=>"获取session_key失败"]);
}
}catch(\Exception $e){
return json(["code"=>1,"msg"=>"微信返回数据异常"]);
}
}else{
return json(["code"=>1,"msg"=>"你应该传code给我。"]);
}
}完毕!
到此,解密手机号,用手机号登录就完成了