tp5微信公众号获取用户openid_tp5.1公众号获取用户基本信息

namespace app\index\controller;

use app\admin\model\User;

use think\Controller;

use think\Db;

class Index extends Controller

{

public function initialize (){

//parent::__construct();

//parent::initialize();

if(session('openid')==""||session('openid')==null){

$get=input('get.');

$whereAB['id']=1;

$wechatRes=Db::name('weixin')->where($whereAB)->find();

$appid=$wechatRes['appid'];

$appsecret=$wechatRes['appsecret'];

if (!isset($get['code'])) {

//生成state 通过session验证

$state = self::getState(25, 32);

session('state',$state);

$redirect_uri = urlencode ('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

$getCodeUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='. $appid .'&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=' . $state . '#wechat_redirect';

$this->redirect($getCodeUrl);

}else{

if ($get['state'] === session('state')){

$getAccessTokenUrl ='https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $get['code'] . '&grant_type=authorization_code';

$data = json_decode(self::curlRequest($getAccessTokenUrl), true);

if (isset($data['openid'])) {

//获取用户信息

$getUserInfoUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $data['access_token'] . '&openid=' . $data['openid'] . '&lang=zh_CN';

$user_info = json_decode(self::curlRequest($getUserInfoUrl), true);

session('openid',$user_info['openid']);

session('nickname',$user_info['nickname']);

session('headimgurl',$user_info['headimgurl']);

//判断数据库是否有用户

$whereAA['openid']=session('openid');

$member=Db::name('user')->where($whereAA)->find();

$dataaa['openid']=session('openid');

$dataaa['nickname']=session('nickname');

$dataaa['headimgurl']=session('headimgurl');

if(empty($member)){

Db::name('user')->insert($dataaa);

}else{

if($member['nickname']==null||$member['nickname']==""||$member['nickname']!=session('nickname')){

Db::name('user')->where($whereAA)->update($dataaa);

}

if($member['headimgurl']==null||$member['headimgurl']==""||$member['headimgurl']!=session('headimgurl')){

Db::name('user')->where($whereAA)->update($dataaa);

}

}

}

}

}

}

}

private static function getState($minLength, $maxLength)

{

$length = mt_rand($minLength, $maxLength);

$letters = '1234567890bcdfghjklmnpqrstvwxyz';

$vowels = 'aeiou';

$code = '';

for ($i = 0; $i < $length; ++$i) {

if ($i % 2 && mt_rand(0, 10) > 2 || !($i % 2) && mt_rand(0, 10) > 9) {

$code .= $vowels[mt_rand(0, 4)];

} else {

$code .= $letters[mt_rand(0, 20)];

}

}

return $code;

}

public function curlRequest($url, $data = null)

{

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

if (!empty($data)) {

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

}

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curl);

curl_close($curl);

return $response;

}

//+-----------------------------------------------------------------------------------------

public function index() //首页

{

//用户信息(ID、头像、昵称)

$where['openid']=session('openid');

$userRes=Db::name('user')->where($where)->find();

$user['user_id']=$userRes['id'];

$user['nickname']=session('nickname');

$user['headimgurl']=session('headimgurl');

$this->assign('userData',$user);

return $this->fetch();

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值