Login.php 15.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/2
 * Time: 15:12
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\common\library\Auth;
use Endroid\QrCode\QrCode;
use fast\Random;
use think\captcha\Captcha;
use think\Cookie;
use think\Db;
use think\Validate;
use think\Cache;


/**
 * 登录接口
 */
class Login extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (手机号登录)
     * @ApiSummary  (手机号登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/code_login)
     *
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="code", type="string", required=true, description="验证码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    ]
    }
    })
     */
    public function code_login(){
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');
        if(empty($mobile) || empty($code)){
            $this->error('缺少必要参数');
        }
        $result1 = Db::name('user')->where('mobile',$mobile)->find();
        if(empty($result1)){
            $this->error('账号错误');
        }

        //验证手机号验证码是否正确
        $yuancode = Cache::get($mobile);
        if($code != $yuancode){
            $this->error('验证码错误或者失效,请重新发送');
        }

        $user = Db::name('user')->where(['id'=>$result1['id']])->find();
        if($user['status'] != 'normal'){
            $this->error('抱歉,您已被加入黑名单');
        }
        $arr3['logintime'] = time();
        $arr3['loginip'] = $this->request->ip(0, true);
        $result3 = Db::name('user')->where('id',$result1['id'])->update($arr3);
        if(empty($result3)){
            $this->error('sql执行失败');
        }
//        $token = generate_user_token($result1['id']);
//        session('token',$token);
        $auth =  Auth::instance();
        $this->auth->direct($result1['id']);
        $token = $this->auth->getToken();
        session('token',$token);
        Cookie::set('token',$token);
        $this->success('SUCCESS',['userInfo'=>$auth->getUserinfo()]);
    }

    /**
     * @ApiTitle    (账号密码登录)
     * @ApiSummary  (账号密码登录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/password_login)
     *
     * @ApiParams   (name="username", type="string", required=true, description="用户名")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    ]
    }
    })
     */
    public function password_login(){
        $username = $this->request->param('username');
        $password = $this->request->param('password');
        if(empty($username) || empty($password)){
            $this->error('缺少必要参数');
        }
       $result = Db::name('user')->where('username',$username)->find();
        if(empty($result)){
            $this->error('账号错误');
        }
        $password = md5(md5($password) . $result['salt']);
        if($result['password'] != $password){
            $this->error('密码错误');
        }
        $user = Db::name('user')->where(['id'=>$result['id']])->find();
        if($user['status'] != 'normal'){
            $this->error('抱歉,您已被加入黑名单');
        }
        $arr3['logintime'] = time();
        $arr3['loginip'] = $this->request->ip(0, true);

        $result3 = Db::name('user')->where('id',$result['id'])->update($arr3);
        if(empty($result3)){
            $this->error('sql执行失败');
        }
//        $token = generate_user_token($result['id']);
//        session('token',$token);

        $auth =  Auth::instance();
        $this->auth->direct($result['id']);
        $token = $this->auth->getToken();
        session('token',$token);
        Cookie::set('token',$token);
        $this->success('SUCCESS',['userInfo'=>$auth->getUserinfo()]);
    }

    /**
     * @ApiTitle    (注册)
     * @ApiSummary  (注册)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/register_login)
     *
     * @ApiParams   (name="username", type="string", required=true, description="用户名")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiParams   (name="respassword", type="string", required=true, description="确认密码")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="code", type="string", required=true, description="验证码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    ]
    }
    })
     */
    public function register_login(){
        $username = $this->request->param('username');
        $password = $this->request->param('password');
        $respassword = $this->request->param('respassword');
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');

        if(empty($mobile) || empty($password) || empty($respassword) || empty($username) || empty($code)){
            $this->error('缺少必要参数');
        }
        //验证手机号验证码是否正确
        $yuancode = Cache::get($mobile);
        if($code != $yuancode){
            $this->error('验证码错误或者失效,请重新发送');
        }
        //判断该手机号是否被注册
        $result = Db::name('user')->where('mobile',$mobile)->find();
        if($result){
            $this->error('该手机号已经注册');
        }
        Db::startTrans();
        //查看后台是否需要审核
        $data = Db::name('audit')->where('id',1)->find();
        if($data['is_audit'] == 0){
            //不需要
            $arr3['audit'] = 1;
        }else{
            //需要
            $arr3['audit'] = 0;
        }
        $salt = Random::alnum();
        $arr3['username'] = $username;
        $arr3['avatar'] = "/assets/img/avatar.png";
        $arr3['mobile'] = $mobile;
        $arr3['salt'] = $salt;
        $arr3['pwd'] = $password;
        $arr3['password'] = md5(md5($password) . $salt);
        $arr3['logintime'] = time();
        $arr3['loginip'] = $this->request->ip(0, true);
        $arr3['joinip'] = $this->request->ip(0, true);
        $arr3['createtime'] = time();
        $arr3['logintime'] = time();
        $arr3['loginip'] = $this->request->ip(0, true);
        $arr3['status'] = 'normal';
        $result3 = Db::name('user')->insertGetId($arr3);
        if(empty($result3)){
            Db::rollback();
            $this->error('sql执行失败');
        }
        Db::commit();

        //生成二维码
        $page = '/pages/money/give/give?user_id='.$result3;
        $thumbnail = $this->qrcode($page,$result3);
        Db::name('user')->where('id',$result3)->update(['thumbnail'=>$thumbnail]);

        $auth = Auth::instance();
        $this->auth->direct($result3);
        $token = $this->auth->getToken();
        session('token',$token);
        Cookie::set('token',$token);
        $this->success('SUCCESS',['userInfo'=>$auth->getUserinfo()]);
    }

    /**
     * @ApiTitle    (重新注册)
     * @ApiSummary  (重新注册)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/register_again)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="username", type="string", required=true, description="用户名")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiParams   (name="respassword", type="string", required=true, description="确认密码")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="code", type="string", required=true, description="验证码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    ]
    }
    })
     */
    public function register_again()
    {
        $user_id = $this->auth->id;
        $username = $this->request->param('username');
        $password = $this->request->param('password');
        $respassword = $this->request->param('respassword');
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');

        if(empty($mobile) || empty($password) || empty($respassword) || empty($username) || empty($code)){
            $this->error('缺少必要参数');
        }
        //验证手机号验证码是否正确
        $yuancode = Cache::get($mobile);
        if($code != $yuancode){
            $this->error('验证码错误或者失效,请重新发送');
        }
        $salt = Random::alnum();
        $arr3['username'] = $username;
        $arr3['salt'] = $salt;
        $arr3['pwd'] = $password;
        $arr3['password'] = md5(md5($password) . $salt);
        $data = Db::name('user')->where('id',$user_id)->update(['username'=>$username,'pwd'=>$arr3['pwd'],'password'=>$arr3['password'],'salt'=>$salt,'mobile'=>$mobile]);
        if(empty($data)){
            $this->error('编辑失败');
        }else{
            $this->success('success');
        }
    }

    /**
     * @ApiTitle    (获取验证码)
     * @ApiSummary  (获取验证码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/getcode)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="mobile", type="string", required=false, description="手机号")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {

    }
    })
     */
    public function getcode()
    {
        $phone = $this->request->param('mobile');
        if (empty($phone)) {
            $this->error(['code' => 40005, 'msg' => '手机号不能为空']);
        }
        if (!preg_match('/^1[0-9]{10}$/', $phone)) {
            $this->error(['code' => 40005, 'msg' => "请输入正确的手机格式!"]);
        }
        //生成验证码
        $number = generateCode();
        //发送验证码
        $data = array(
            'content' => "【彩云盘APP】提醒您,您的验证码是:" .$number.",十分钟之内有效,请勿向他人泄漏您的验证码",//短信内容
            'mobile' => $phone,//手机号码
            'productid' => '676767',//产品id
            'xh' => ''//小号
        );
        $result = send_sms($data);
        if (substr($result, 0, strpos($result, ',')) != "1") {
            $this->error(['code' => 42001, 'msg' => $result]);
        }
        Cache::set($phone,$number,600);
        $this->success('SUCCESS');
    }

    /**
     * @ApiTitle    (验证验证码)
     * @ApiSummary  (验证验证码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/verify)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="code", type="string", required=true, description="验证码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {

    }
    })
     */
    public function verify()
    {
        $param = $this->request->param();
        $validate = new Validate([
            'mobile' => 'require|max:11',
            'code'=>'require'
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $code = Cache::get($param['mobile']);
        if($param['code'] == $code){
            $this->success('SUCCESS');
        }else{
            $this->error('验证码错误或者失效,请重新发送');
        }

    }

    /**
     * @ApiTitle    (文案)
     * @ApiSummary  (文案)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/official)
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
          "id"://id
          "content"://用户协议
          "desc"://会员权益
          "explain"://操作说明
    }
    })
     */
    public function official()
    {
        $data = Db::name('official')->where('id',1)->field('id,content,desc,explain')->find();
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (查看个人信息)
     * @ApiSummary  (查看个人信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/selstatus)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
        "id": //用户id,
        "username": //用户名,
        "avatar"://头像
        "pwd": //密码,
        "mobile": //手机号,
        "audit": //审核状态0待审核1审核通过2审核未通过,
        "identity": //身份1普通会员2黄金会员3钻石会员
        "file_pwd"://文件密码
    }
    })
     */
    public function selstatus()
    {
        $user_id = $this->auth->id;
        $data = Db::name('user')->field('id,username,avatar,pwd,mobile,audit,identity')->where('id',$user_id)->find();
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (修改个人信息)
     * @ApiSummary  (修改个人信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/login/edit)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="username", type="string", required=true, description="用户名")
     * @ApiParams   (name="avatar", type="string", required=true, description="头像")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams   (name="file_pwd", type="string", required=true, description="文件密码")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    ]
    }
    })
     */
    public function edit()
    {
        $user_id = $this->auth->id;
        $param = $this->request->param();
        $validate = new Validate([
            'username' => 'require',
            'avatar'=>'require',
            'password'=>'require',
            'mobile'=>'require',
            'file_pwd'=>'require',
        ]);
        $validate->message([
            'username'=>'用户名不能为空',
            'avatar'=>'头像不能为空',
            'password'=>'密码不能为空',
            'mobile'=>'手机号不能为空',
            'file_pwd'=>'文件查看密码不能为空',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $param['salt'] = Random::alnum();
        $param['pwd'] = $param['password'];
        $param['password'] = md5(md5($param['pwd']) . $param['salt']);
        $data = Db::name('user')->where('id',$user_id)->update($param);
        if(empty($data)){
            $this->error('修改失败');
        }else{
            $this->success('SUCCESS');
        }

    }


    //生成二维码
    public function qrcode($url,$user_id)
    {
        $qrCode = new QrCode('Life is too short to be generating QR codes');
        $qrCode
            ->setText($url)
            ->setSize(450)
            ->setPadding(10)
            ->setErrorCorrection('high')
            ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
            ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
            ->setImageType(QrCode::IMAGE_TYPE_PNG);
        $file_path = "code".$user_id.".png";

        $qrCode->save(ROOT_PATH . 'public' . DS . 'upload/code' . DS . $file_path);
        $qrcodeurl = request()->domain().'/upload/code/code'.$user_id.'.png';
        return $qrcodeurl;
    }




}