微信公众号网页授权登录获取用户信息存入数据库

基于thinkphp5微信公众号授权
public function h5_authorization()
{
    $appid = $this->config['app_id'];
    $myurl=urlencode(""); //回调域名
    $wxurl='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$myurl.'&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect';
    header('Location:'.$wxurl);
}

/**
 * 微信公众号授权
 */
public function wxAuthorize(){
    // 获取页面URL的CODE参数,判断是否有值
    if(isset($_GET['code'])) {
        // 获取openid和access_token
        $app_id = $this->config['app_id'];
        $app_secret = $this->config['app_secret'];
        $code = $_GET['code'];
        // 发送请求,获取用户openid和access_token
        $data = $this->get_by_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$app_id.'&secret='.$app_secret.'&code='.$code.'&grant_type=authorization_code');
        $data = json_decode($data);
        // 防止第二次访问动态连接报错
        // 判断是否获取到当前用户的openid
        if(isset($data->openid)){
            $open_id = $data->openid;
            $access_token = $data->access_token;
            // 获取当前用户信息
            $user_info = $this->get_by_curl('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$open_id.'&lang=zh_CN');
            $user_info = json_decode($user_info);
            $user_openid = $user_info->openid;
            $user_nickname = $user_info->nickname;
            $user_sex = $user_info->sex;
            $user_province = $user_info->province;
            $user_city = $user_info->city;
            $user_headimgurl = $user_info->headimgurl;
            // 判断用户是否存在
            $data_user = $this->user
                ->where('openid','=',$user_openid)
                ->find();
            if(empty($data_user)){
                $this->user->data([
                    'openid' =>  $user_openid,
                    'nickname'  =>  $user_nickname,
                    'gender'  =>  $user_sex == '男' ? 1 : 0,
                    'province'  =>  $user_province,
                    'city'  =>  $user_city,
                    'avatar'  =>  $user_headimgurl,
                    'createtime' => time(),
                ])->save();
                // 新增用户到数据库
            }
            $cur_user = $this->user
                ->where('openid','=',$user_openid)
                ->find();
            // 判断当前用户是否修改过信息,如有变更则更新
            if(strcmp($cur_user->nickname,$user_nickname)!=0||strcmp($cur_user->avatar,$user_headimgurl)!=0){
                $cur_user->nickname = $user_nickname;
                $cur_user->avatar = $user_headimgurl;
                // 更新当前用户信息
                $cur_user->save();
            }
            // 将当前用户openid做为标识存到session里
            Session::set('open_id', $cur_user->openid);
            return $this->view->fetch('index');
        }
    }

}

/**
 * 请求微信接口获取数据
 * @param $url
 * @param bool $post
 * @return bool|string
 */
function get_by_curl($url,$post = false){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if($post){
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
    }
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值