微信公众号授权登录 tp

40 篇文章 0 订阅
26 篇文章 0 订阅

代码如下
先用前端请求一下 接口

	uni.request({
					url:'http://s***1.com/api/user/getcode',
					method:'GET',
					success(res) {
						window.location.href=res.data
					}
				})

1.php代码如下

    /**
     * 微信公众号登录
     */
    public function official_login()
    {
        $postData = input();
        $appid    = 'wxc3b******f59e0d';
        $secret   = 'c0544d****67ef915999';
        $code     = $postData['code'];

        //获取 openid access_token
        if ($code) {
            $url  = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
            $data = file_get_contents($url);
            $data = json_decode($data, true);
        }

        //获取用户信息
        $access_token = $data['access_token'];
//        if($access_token){
        $openid   = $data['openid'];
        $url      = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        $data     = file_get_contents($url);
        $postData = json_decode($data, true);
//        }

        $phone                       = $postData['unionid'] . '@.qq.com';
        $check_type                  = 'email';
        $account                     = $postData['nickname'];
        $password                    = 123456;
        $pay_password                = $password;
        $info                        = null;
        $verify                      = null;
        $postData['unionId']         = $postData['unionid'];//转大写
        $postData['head_img']        = $postData['headimgurl'];
        $postData['official_openId'] = $postData['openid'];
        unset($postData['unionid']);

		//自己的操作
        if ($phone != '') {
            $Users = new Users();

            if ($check_type == 'mobile') {
                $phone = str_replace(" ", '', $phone);
                if ($Users->check_mobile($phone)) {
                    return json(['code' => 0, 'msg' => '该手机号已被注册']);
                }
            } else {
                if ($Users->check_email($phone)) {
                    //已被注册那么就去登录
                    $data = $this->wx_login('email', $phone, '');
                    return json($data);
                }
            }

            $country_code = (get_phone_verify($phone, 'reg_'))['country_code'];

            $res = $Users->reg($account, $password, $pay_password, $info, $phone, $country_code, $check_type, $postData);


            if ($res) {
                del_phone_verify($phone, $verify, 'reg_');
                $data['code']                = 1;
                $data['msg']                 = '注册成功';
                $data['data']['app_downurl'] = config('config.app_downurl');
            } else {
                $data['code'] = 0;
                $data['msg']  = '注册失败';
            }
        } else {
            $data['code'] = 0;
            $data['msg']  = '参数错误';
        }
        return json($data);


    }

    //获取code值
    public function getcode()
    {
        $postData = input();
        if (!isset($postData['code'])) {
            $urls     = "http://syt0351.com/api/user/official_login";//回调地址
            $appid    = 'wxc3b***59e0d';
            $state    = 'page/my';//前端地址吧
            $url      = urlencode($urls);
            $gcodeurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$url}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";

            return json($gcodeurl);

        } else {
            return $postData['code'];
        }
    }

2.代码:


    /**
     * 微信公众号登录
     */
    public function official_login()
    {
        $postData = input();
        $appid    = 'wxa35e77624971a4d4';
        $secret   = '774adc248bb01db64739e78140b806cd';
        $code     = $postData['code'];

        //获取 openid access_token
        if ($code) {
            $url  = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
            $data = file_get_contents($url);
            $data = json_decode($data, true);
        }

        if (!$data) {
            $this->error('获取失败请重试');
        }

        //获取用户信息
        $access_token = $data['access_token'];
        if ($access_token) {
            $openid   = $data['openid'];
            $url      = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
            $data     = file_get_contents($url);
            $postData = json_decode($data, true);
        }
        else {
            $this->error('获取失败请重试');
        }
        $postData['type'] = 'wx_official';
        extract($postData);

        $token    = '';
        $Official = Official::where('openid', '=', $openid)->find();
        $user     = User::where('openid', '=', $openid)->find();

        if ($Official && $user) {
            //直接 登录
            $this->auth->direct($user->id);
            $data['data'] = $this->auth->getUserinfo();
            $data['code'] = 1;
        }
        else {
            //注册并登录
            $username = $nickname;
            $password = 123456;
            $email    = $openid . '@qq.com';
            $mobile   = '';

            $ret = $this->auth->register($username, $password, $email, $mobile, $postData);
            if ($ret) {
                $token               = $this->auth->getUserinfo();
                $postData['user_id'] = $token['id'];
                Official::UserAdd($postData);

                $data['data'] = $token;
                $data['code'] = 1;
            }
            else {
                $data['code'] = 0;
                $data['data'] = $this->auth->getError();
            }
        }
        return json($data);
    }

    //获取code值
    public function getcode()
    {
        $request = Request::instance();
        $url     = $request->domain();

        $postData = input();
        if (!isset($postData['code'])) {
            $urls     = $url . "/addons/greentown/login/official_login";//回调地址
            $appid    = 'wxa35e77624971a4d4';
            $state    = 'pages/index/index';//前端地址
            $url      = urlencode($urls);
            $gcodeurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$url}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
            return json($gcodeurl);
        }
        else {
            return $postData['code'];
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值