审查视图

app/portal/controller/LoginController.php 6.4 KB
景龙 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/**
 * 登录注册
 * Author: xiaojie
 * DateTime: 2018/11/26 13:50
 */
namespace app\portal\controller;

use app\portal\model\UserModel;
use app\portal\validate\UsersValidate;
use app\user\model\CommentModel;
use cmf\controller\HomeBaseController;
use think\Cookie;
use think\Session;
use anerg\OAuth2\OAuth;
16
use think\Config;
景龙 authored
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

class LoginController extends HomeBaseController
{
    private $config;
    /**
     * 登录,注册页面
     */
    public function login(){
//        $login_type = $this->request->param('login_type','1','intval');
//        //1->登录  2->注册
//        if($login_type == 1){
//            $login_title = '登录';
//        }elseif($login_type == 2){
//            $login_title = '注册';
//        }else{
//            $this->redirect(url('index/index'));
//        }
//        $user_login_info = Cookie::get('user_login_info');
//        if($user_login_info){
//            $mobile = $user_login_info['mobile'];
//            $password = $user_login_info['password'];
//            $is_remember_password = 1;
//        }
//        $this->assign('login_type',$login_type);
//        $this->assign('login_title',$login_title);
//        $this->assign('mobile',isset($mobile)?$mobile:'');
//        $this->assign('password',isset($password)?$password:'');
//        $this->assign('is_remember_password',isset($is_remember_password)?$is_remember_password:'0');
        return $this->fetch();
    }

    /**
     * 手机号登录
     */
    public function ajax_login(){
        //提交参数手机号(mobile),密码(user_pass)
        $param = $this->request->param();
        $validate = new UsersValidate();
        $userModel = new UserModel();
        $map = [
            'mobile' => $param['mobile'],
            'user_pass' => cmf_password($param['user_pass']),
            'user_type' => 2,
            'user_status' => 1,
        ];
        $userInfo = $userModel->where($map)->find();
        if(!$userInfo){
            $this->apiResponse(0,'账号或密码错误');
        }
        $ip = get_client_ip();
        $data = [
            'id' => $userInfo['id'],
            'last_login_time' => time(),
            'last_login_ip' => $ip,
        ];

        if(!$validate->scene('edit')->check($data)){
            $this->apiResponse(0,$validate->getError());
        }
        $res = $userModel->isUpdate(true)->save($data);
        if($res){
            $session = new Session();
            $session->set('userInfo',$userInfo);
            $this->apiResponse(1,'登录成功');
        }
        $this->apiResponse(0,'未知错误');
    }

    /**
     * 注册
     */
   public function ajax_register(){
        //提交参数手机号(mobile),验证码(mobile_code),密码(user_pass)
        $param = $this->request->param();
        //验证验证码是否正确
        $common = new CommonController();
        $common->validateMobileCode($param);
        //验证场景add
        $validate = new UsersValidate();
        if(!$validate->scene('add')->check($param)){
            $this->apiResponse(0,$validate->getError());
        }
        if(empty($param['user_pass'])){
            $this->apiResponse(0,'密码不能为空!');
        }
        //是否已注册
        $userModel = new UserModel();
        $userInfo = $userModel->where(['mobile'=>$param['mobile'],'user_type'=>2])->find();
        if($userInfo){
            $this->apiResponse(0,'此账号已被注册');
        }
        //新增注册信息
        $info['mobile'] = $param['mobile'];
        $info['user_pass'] = cmf_password($param['user_pass']);
        $info['user_type'] = 2;
        $info['create_time'] = time();
        $res = $userModel->allowField(true)->save($info);
        if($res){
            $this->apiResponse(1,'注册成功');
        }
        $this->apiResponse(0,'未知错误');
   }

//   public function updateInfo(){
//       $param = $this->request->param();
//       $data = [
//           'id' => $userInfo['id'],
//           'last_login_time' => time(),
//           'last_login_ip' => $ip,
//       ];
//
//       if(!$validate->scene('edit')->check($data)){
//           $this->apiResponse(0,$validate->getError());
//       }
//       $res = $userModel->isUpdate(true)->save($data);
//   }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

    //第三方微信登录
    public function wx_login(){
        $config = Config::get('wx_login');
        $app_id = $config['app_id'];
        $redirect_uri = $config['redirect_uri'];
        $scope = $config['scope'];
        $state = md5(uniqid(rand(), TRUE));
        $url = 'https://open.weixin.qq.com/connect/qrconnect?appid='.$app_id.'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
        $res = $this->http_get($url);
        var_dump(11);
        var_dump($res);exit;
    }

    //curl  get请求
    public function http_get($url){
        $curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); //执行操作
        curl_close($curl);
        return $result;
    }

    //curl post请求
    public function http_post($url,$data,$headers){
        $curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, true); // 开启header
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); //执行操作
        curl_close($curl);
        return $result;
    }
景龙 authored
177
}