<?php
/**
* Created by PhpStorm.
* User: wudi
* Date: 2019/1/28
* Time: 10:31
*/
namespace app\index\controller;
use app\BaseController;
use think\facade\view;
use think\facade\DB;
class Wxgetuserinfo extends BaseController
{
/**
* 调用方法
* 具体参考tp5的命名空间 https://www.kancloud.cn/manual/thinkphp5/118014
* $class = new \app\card\controller\Wxgetuserinfo();
* $class->index();
*/
/**
* 1、获取用户信息
*/
public function delsession()
{
session('userinfo', null);
}
public function index()
{
$appid = config('user.appid');
$secret = config('user.secret');
if (!session('userinfo')) {
if (!isset($_GET['code'])) { //没有code,去微信接口获取code码
$request = request();
$callback = $request->url(true); //微信服务器回调url,这里是本页url
// $this->get_code($callback);
$redirect_uri = urlencode($callback);
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
header("location:$url");
exit();
} else { //获取code后跳转回来到这里了
$code = $_GET['code'];
$data = $this->get_access_token($code); //获取网页授权access_token和用户openid
$data_all = $this->get_user_info($data['access_token'], $data['openid']); //获取微信用户信息
session('userinfo', $data_all);
$this->checkuser($data_all['openid'], $data_all);
return json($data_all);
}
} else {
$ret = session('userinfo');
return json($ret); //返回的获取到的微信用户信息
}
}
/**
* 3、使用code换取access_token
* @param string 用于换取access_token的code,微信提供
* @return array access_token和用户openid数组
*/
private function get_access_token($code)
{
$arrContextOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
];
$appid = config('user.appid');
$appsecret = config('user.secret');
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
$user = json_decode(file_get_contents($url,false,stream_context_create($arrContextOptions)));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode . '<hr>msg :' . $user->errmsg;
exit;
}
$data = json_decode(json_encode($user), true); //返回的json数组转换成array数组
return $data;
}
/**
* 4、使用access_token获取用户信息
* @param string access_token
* @param string 用户的openid
* @return array 用户信息数组
*/
private function get_user_info($access_token, $openid)
{
$arrContextOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
];
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
$user = json_decode(file_get_contents($url,false,stream_context_create($arrContextOptions)));
if (isset($user->errcode)) {
// echo 'error:' . $user->errcode.'<hr>msg :' . $user->errmsg;exit;
}
$data = json_decode(json_encode($user), true); //返回的json数组转换成array数组
return $data;
}
/**
* 检查用户信息
* TODO 在这个地方如果在用户表加一个时间字段,就可以每隔多少次对数据库进行一次操作,而不是一直更新操作。
*/
public function checkuser($openid, $data)
{
$check = Db::name('phone_user')->where(array('open_id' => $openid))->find();
if ($check) {
/*更新用户信息*/
$updata = array();
$updata['user_pic'] = $data['headimgurl'];
$updata['create_time'] = time();
$updata['username'] = $data['nickname'];
Db::name('phone_user')->where('open_id', $openid)->update($updata);
/*获取最新用户信息并存入缓存*/
$newinfo = Db::name('phone_user')->where(array('id' => $check['id']))->find();
session('userinfo', $newinfo);
} else {
/*新增用户信息*/
$intdata = array();
// $intdata = ['username' => $data['nickname'], 'user_pic' => $data['headimgurl'], 'create_time' => time(), 'openid' => $data['openid'], 'gender' => $data['sex'], 'city' => $data['city'], 'province' => $data['province'], 'county' => $data['country']];
$intdata = ['username' => $data['nickname'], 'user_pic' => $data['headimgurl'], 'create_time' => time(), 'open_id' => $data['openid'], 'status' => 1, 'update_time' => time()];
Db::name('phone_user')->insert($intdata);
$userId = Db::name('phone_user')->getLastInsID();
/*获取最新用户信息并存入缓存*/
if (!session('?userinfo')) {
$newinfo = Db::name('phone_user')->where(array('id' => $userId))->find();
$dataSava = [
's_uid' => $newinfo['id'],
'c_uid' => $newinfo['id'],
];
Db::name('code_record')->insert($dataSava);
//$newinfo=Db::name('phone_user')->where('id',$userId)->find();array('openid'=>$openid,'delstatus'=>1)
session('userinfo', $newinfo);
}
}
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
/*检测是否关注公众号*/
/*subscribe 用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。*/
/*https://www.cnblogs.com/mracale/p/9318349.html*/
public function checkisgz()
{
$request = request();
$callback = $request->url(true); //微信服务器回调url,这里是本页url
$appid = config('user.appid');
$secret = config('user.secret');
//微信网页授权获取openid
$web_url = $callback;
if (!isset($_GET['code'])) {
$redirect_uri = urlencode($web_url);
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
header("location:$url");
exit();
}
$code = trim($_GET['code']);
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
$arrContextOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
];
$access = file_get_contents($url,false,stream_context_create($arrContextOptions));
$data = json_decode($access, true);
// dump($data);
if(isset($data['errcode'])){
echo "<script>WeixinJSBridge.call('closeWindow');</script>";
echo "<script>document.addEventListener('WeixinJSBridgeReady', function(){ WeixinJSBridge.call('closeWindow'); }, false);</script>";
exit;
}
$access_token = $data['access_token'];
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=OPENID&lang=zh_CN';
$user = file_get_contents($url,false,stream_context_create($arrContextOptions));
$arr = json_decode($user, true);
//获取用户的openid
$openid = $arr['openid'];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
$access = file_get_contents($url,false,stream_context_create($arrContextOptions));
$access_arr = json_decode($access, true);
//非网页的access_token
$access_token = $access_arr['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
$res = file_get_contents($url,false,stream_context_create($arrContextOptions));
return json_decode($res);
}
}
PHP H5微信授权
最新推荐文章于 2024-04-16 18:45:00 发布