tthinkphp3.2 后台实现微信小程序登录接口

ajaxReturn($return_data);
         } else {
             $appid = C('APPLET_APPID');// appid
             $secret = C('APPLET_APPKEY');// app密钥
             $code = $_POST['code']; // 小程序传来的code值
             $curl = curl_init();
             $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
             curl_setopt($curl, CURLOPT_URL, $url);
             curl_setopt($curl, CURLOPT_HEADER, 0);
             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
             $result = curl_exec($curl);
             $json = json_decode($result); // 对json数据解码
             $arr = get_object_vars($json);
             $openid = $arr['openid'];
             $session_key = $arr['session_key'];
             curl_close($curl);
             // 检验是否已经授权登录
             $Applet_user = M('Applet_user');
             // 构造查询条件
             $where = array();
             $where['openid'] = $openid;
             $user = $Applet_user->where($where)->find();
             if ($user) {
                 $return_data = array();
                 $return_data['error_code'] = 0;
                 $return_data['msg'] = '登录成功';
                 $return_data['data']['userid'] = $user['userid'];
                 $return_data['data']['username'] = $user['username'];
                 $return_data['data']['face_url'] = $user['face_url'];
                 $return_data['data']['session_key'] = $session_key;
                 $this->ajaxReturn($return_data);
             } else {
                 $return_data = array();
                 $return_data['error_code'] = 1;
                 $return_data['msg'] = '不存在该用户,请授权登录';
                 $this->ajaxReturn($return_data);
             }
         }
     }

    /**
     *首次授权登录
     * @return [type] [description]
     */
    public function wxsign()
    {
        // 校验参数是否存在
        if (!$_POST['code']) {
            $return_data = array();
            $return_data['error_code'] = 2;
            $return_data['msg'] = '参数不足: code';
            $this->ajaxReturn($return_data);
        } else if (!$_POST['name']) {
            $return_data = array();
            $return_data['error_code'] = 2;
            $return_data['msg'] = '参数不足: name';
            $this->ajaxReturn($return_data);
        } else if (!$_POST['avatarUrl']) {
            $return_data = array();
            $return_data['error_code'] = 2;
            $return_data['msg'] = '参数不足: avatarUrl';
            $this->ajaxReturn($return_data);
        } else {
            $appid = C('APPLET_APPID');// appid
            $secret = C('APPLET_APPKEY');// app密钥
            $code = $_POST['code']; // 小程序传来的code值
            $curl = curl_init();
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($curl);
            $json = json_decode($result); // 对json数据解码
            $arr = get_object_vars($json);
            $openid = $arr['openid'];
            $session_key = $arr['session_key'];
            curl_close($curl);
            // 检验是否已经授权过
            $Applet_user = M('Applet_user');
            // 构造查询条件
            $where = array();
            $where['openid'] = $openid;
            $user = $Applet_user->field(true)->where($where)->find();
            if ($user) {
                //已授权登录过
                TODO::可以在这儿写一个更新头像和昵称的操作
                //直接返回参数
                $result = $user;
                $this->response(showMessage(1, '授权成功', $result), 'json');
            } else {
                //首次授权登录
                //创建用户小程序信息
                $data['openid'] = $openid;
                $data['session_key']=$session_key;
                $data['username'] = $_POST['name'];
                $data['face_url'] = $_POST['avatarUrl'];
                $data['createtime'] = time();
                $data['last_login_time'] = time();
                $result = $Applet_user->add($data);
                //判断是否成功
                if ($result) {
                    $this->response(showMessage(1, '授权成功', $data), 'json');
                }else{
                    $this->response(showMessage(102, '授权失败,请重试!', ''), 'json');
                }
            }
        }
    }




}

你可能感兴趣的:(tthinkphp3.2 后台实现微信小程序登录接口)