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

发布时间:2024年01月19日
基于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;
}
文章来源:https://blog.csdn.net/qq_16659821/article/details/135691301
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。