当前位置:网站首页>TP5.0 小程序用户无需登录,直接获取用户手机号。
TP5.0 小程序用户无需登录,直接获取用户手机号。
2022-07-29 02:32:00 【你好骚啊】
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\User;
use think\Hook;
use think\Db;
use think\Cache;
/**
*
*/
class Wechat extends Api
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
public $AppId = '填你自己的';
public $Secret = '填你自己的';
//获取用户手机号
public function user_phone(){
//获取前端传过来的code,如果前端不知道code是啥,就刁他。
$post = $this->request->param();
if(!isset($post['code']) || empty($post['code'])){
$return['status'] = 222;
$return['msg'] = '非法请求';
return json_encode($return);
}
//获取accesstoken
$accessToken = $this->get_access_token();
//请求地址
$url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$accessToken;
//前端传递的code
$code = $post['code'];
//组成数组
$data=[
'code'=>$code,
];
//这里要把传递的参数给转成json,不然小程序接口会报数据类型错误。
$result = json_decode($this->curl_post_https($url,json_encode($data)),true);
//开始判断获取是否成功
if($result['errmsg'] == 0){
//获取成功
$phoen = $result['phone_info']['phoneNumber'];
$return['smg'] = '获取手机号成功!';
$return['code'] = 200;
$return['phone'] = $phoen;
//把手机号返回给前端,或者自己进行存储。看需求
//Db::name('user')->add();
return json_encode($return);
}else{
$return['smg'] = '获取手机号失败!';
$return['code'] = 201;
return json_encode($return);
}
}
//获取小程序二维码的token
public function get_access_token()
{
//先判断缓存里面的access_token过期了没有
if(Cache::get('access_token')){
//没过期直接拿出来用
$a = Cache::get('access_token');
return $a;
}else{
//过期了就重新获取
$appid = $this->AppId;
$secret = $this->Secret;
$url = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=$appid&secret=$secret";
//请求接口,获取accesstoken
$user_obj = $this->curlHttp($url);
//然后将accesstoken存入缓存里面,官方过期时间7200秒,缓存里面可以过期的早一点,自己把控
Cache::set('access_token',$user_obj['access_token'],7100);
return Cache::get('access_token');
}
}
/**
* 发送get请求
*/
public function curlHttp($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
return json_decode($output,true);
}
/**
* 发送post请求
*/
public function curl_post_https($url,$data,$header=[]){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}请求成功之后返回的数据。

非常简单!
边栏推荐
猜你喜欢

Shell script quick start-01

QT screen adaptive automatic layout, drag the window to automatically grow larger and smaller (I)

etcd实现大规模服务治理应用实战

golang 协程的实现原理

C语言:空心正方形图案

FTP protocol details

Ffmpeg+sdl+qt is a simple video player

【OpenCV】使用OpenCV调用手机摄像头

Day 10 notes

QT compilation of IOT management platform 48 characteristic function design
随机推荐
【OpenCV】使用OpenCV调用手机摄像头
Flink kernel source code (VII) Flink SQL submission process
Tesla neural network model hydranet
Redis queue realizes second kill
.NET 序列化枚举为字符串
Day 10 notes
STP协议(生成树协议)
Some records during the development of ros2/ros1
6-21漏洞利用-mysql弱口令破解
多重继承与派生类成员标识
The origin of Nacos' name
Only when you are far away will you miss
New conch movie theme template m3.1 fully decrypted version multifunctional apple cmsv10 background adaptive theme open source fully decrypted version
Family relationship calculator wechat applet source code
C language: judging letters
Really time NTP service startup command
12.书写规则-静态模式
Pytest environment deployment + use case execution management + use case parameterization
【无标题】
关于时间复杂度的一些新认识