### [小程序如何获取手机号( thinkphp3.2.3框架)](https://www.prosaas.cn/article/17133)
**Published:** 2018-08-14T09:12:50
**Author:** 5201314
**Excerpt:** 本篇文章给大家带来的内容是关于小程序如何获取手机号( thinkphp3.2.3框架),有一定的参考价值,有需…
本篇文章给大家带来的内容是关于小程序如何获取手机号( thinkphp3.2.3框架),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、直接上代码php
namespace Home\\Controller;
use Think\\Controller;
class ApiController extends Controller {
/\*\*
\* error code 说明.
\*
\* - -41001: encodingAesKey 非法
\* - -41003: aes 解密失败
\* - -41004: 解密后得到的buffer非法
\* - -41005: base64加密失败
\* - -41016: base64解密失败
\*
\*/
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
// 小程序
public static $appid = 'XXX'; //小程序appid
public static $secret = 'XXX'; //小程序秘钥
public $sessionKey ='';
// 获取openId session-key 等
public function getopenId($value='')
{
$code = I('post.code');
$appid = self::$appid;
$secret = self::$secret;
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='. $appid.'&secret='.$secret.'&js\_code='.$code.'&grant\_type=authorization\_code';
$result = httpGet($url);
$res = json\_decode($result);
// session(\['sessionKey'=>$res,'expire'=>7200\]);
$this->ajaxReturn($res);
}
// 获取小程序手机号api 接口,对应下面小程序 js
public function getPhoneNumber($value='')
{
$encryptedData = I('get.encryptedData');
$iv = I('get.iv');
$this->sessionKey=I('get.session\_key');
$res = $this->decryptData($encryptedData, $iv);
// $res = json\_decode($res);
if($res->phoneNumber){
// $res->phoneNumbe 就是手机号可以 写入数据库或者做其他操作
}
$this->ajaxReturn(\['msg'=>$res,'status'=>'1'\]); //把手机号返回
}
// 小程序解密
public function decryptData($encryptedData, $iv)
{
if (strlen($this->sessionKey) != 24) {
return self::$IllegalAesKey;
}
$aesKey=base64\_decode($this->sessionKey);
if (strlen($iv) != 24) {
return self::$IllegalIv;
}
$aesIV=base64\_decode($iv);
$aesCipher=base64\_decode($encryptedData);
$result=openssl\_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj=json\_decode( $result );
if( $dataObj == NULL )
{
return self::$IllegalBuffer;
}
if( $dataObj->watermark->appid != self::$appid )
{
return self::$IllegalBuffer;
}
return $dataObj;
// return self::$OK;
}
function httpGet($url) {
$curl = curl\_init();
curl\_setopt($curl, CURLOPT\_RETURNTRANSFER, true);
curl\_setopt($curl, CURLOPT\_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl\_setopt($curl, CURLOPT\_SSL\_VERIFYPEER, false);
curl\_setopt($curl, CURLOPT\_SSL\_VERIFYHOST, false);
curl\_setopt($curl, CURLOPT\_URL, $url);
$res = curl\_exec($curl);
curl\_close($curl);
return $res;
}
}
//2、小程序
2.1在app.js 启动页面里先login
// 登录
// if (!wx.getStorageSync('session\_key') || wx.getStorageSync('time') < Date.parse(new Date())){ // 判断session\_key是不是存在获者过期
wx.login({
success: res => {
console.log(res)
// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.request({
url: 'https://www.zhixiaobing.com/index.php?m=&c=api&a=getopenId',
header: { "Content-Type": "application/x-www-form-urlencoded" },
method: 'post',
data: { code: res.code },
success: function (res) {
console.log(res.data);
wx.setStorageSync('openid', res.data.openid)
wx.setStorageSync('session\_key', res.data.session\_key)
wx.setStorageSync('time', parseInt(Date.parse(new Date())) + 7200)
}
})
}
})
//2.2 在小程序模板里写组件