首先获取微信公众号的appID和secret
点击先获取code,用code去拿access_token
然后在用access_token去获取用户信息
在存用户信息到数据库
这里拿需要检测一下用户是否关注公众号
PHP
public function index(){
// $id = $this->request->param('id');
$appid = 'wxc8cdb6991cff2fca';
$secret = '3d0e67625628d5d75f9dfd3f7ffcad81';
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);
$user_id = Db::name('user')->where('openid',$data_all['openid'])->find();
if($user_id){
Session::set('user_id',$user_id['id']);
$this->redirect('index/index/user');
}else{
echo'';
}
}
}else{
$ret=session('userinfo');
// var_dump($ret);die();
return json($ret); //返回的获取到的微信用户信息
}
}
/**
* 3、使用code换取access_token
* @param string 用于换取access_token的code,微信提供
* @return array access_token和用户openid数组
*/
private function get_access_token($code){
// $appid = config('appid');
// $appsecret = config('secret');
// $appid = 'wx65ac021e2de709df';
// $appsecret = '806e5a9f22135c58d69975336e801a88';
$appid = 'wxc8cdb6991cff2fca';
$appsecret = '3d0e67625628d5d75f9dfd3f7ffcad81';
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
// var_dump($url);die();
$user = json_decode(file_get_contents($url));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode.'
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){
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
$user = json_decode(file_get_contents($url));
if (isset($user->errcode)) {
echo 'error:' . $user->errcode.'
msg :' . $user->errmsg;exit;
}
$data = json_decode(json_encode($user),true);//返回的json数组转换成array数组
return $data;
}
/**
* 检查用户信息
* TODO 在这个地方如果在用户表加一个时间字段,就可以每隔多少次对数据库进行一次操作,而不是一直更新操作。
*/
public function checkuser($openid,$data){
$check=Db::name('user')->where(array('openid'=>$openid))->find();
if($check){
/*更新用户信息*/
$updata=array();
$updata['image']=$data['headimgurl'];
$updata['create_time']=time();
$updata['username']=$data['nickname'];
Db::name('user')->where('openid',$openid)->update($updata);
/*获取最新用户信息并存入缓存*/
$newinfo=Db::name('user')->where(array('id'=>$check['id']))->find();
session('userinfo',$newinfo);
}else{
/*新增用户信息*/
$intdata=array();
$intdata = ['username' =>$data['nickname'], 'image' => $data['headimgurl'],'create_time'=>time(),'openid' => $data['openid'],'sex' => $data['sex'],'city' => $data['city'],'province' => $data['province'],'county' => $data['country']];
Db::name('user')->insert($intdata);
$userId = Db::name('user')->getLastInsID();
/*获取最新用户信息并存入缓存*/
if(!session('?userinfo')){
$newinfo=Db::name('user')->where(array('id'=>$userId))->find();
// $newinfo=Db::name('wxuser')->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 = 'wxd25f887839ae5f30';
// $secret = '71961adca3a8013a2791dc044fbf9439';
$appid = 'wxc8cdb6991cff2fca';
$secret = '3d0e67625628d5d75f9dfd3f7ffcad81';
//微信网页授权获取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';
$access=file_get_contents($url);
$data=json_decode($access,true);
$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);
$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);
$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);
// var_dump($res);die();
return $res['subscribe'];
}
HTML
点击去指向index方法获取code
<script>
layui.use(['layer','upload', 'element'],function() {
var form = layui.form;
var layer = layui.layer;
$('#SubmitAdd').click(function(){
var index=layer.load();
// var phone = $('#phone').val();
// var password = $('#password').val();
var id= 1;
$.ajax({
url:"{:url('index/Wxgetuserinfo/index')}",
type:'get',
data:{id:id},
dataType:'json',
success:function(res) {
layer.close(index);
console.log(res);
if ( res.code == 1 ) {
layer.msg(''+res.msg+'');
setTimeout(function(){
location.href=res.url;
},2000)
}else{
layer.msg(''+res.msg+'');
}
}
});
return false;
});
})
script>
中间参数正确,待用户同意授权的,存用户信息,存Session
进行其他操作
0条评论