User.php 11.4 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/28
 * Time: 17:44
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\index\model\Code;
use fast\Random;
use think\captcha\Captcha;
use think\Cookie;
use think\Db;

class User extends Frontend
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 登录页
     * @return mixed
     */
    public function login_view(){
        $this->assign('title',"登录");
        return $this->fetch();
    }

    /**
     * 注册页
     * @return mixed
     */
    public function register_view(){
        $this->assign('title',"注册");
        return $this->fetch();
    }

    /**
     * 注册
     */
    public function register_login(){
        $username = $this->request->param('username');
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');
        $password = $this->request->param('password');
        if(empty($mobile) || empty($password) || empty($username) || empty($code)){
            $this->error('缺少必要参数');
        }
        $codeModel = new Code();
        $data = $codeModel->findData(['mobile'=>$mobile]);
        if(!$data){
            $this->error('请先获取验证码');
        }
        if($data['is_use'] != 2){
            $this->error('验证码已失效');
        }
        if($data['pasttime'] < time()){
            $this->error('验证码已过期');
        }
        if($data['code'] != $code){
            $this->error('验证码错误');
        }

        $userModel = new \app\index\model\User();
        $result = $userModel->where(['mobile'=>$mobile])->find();
        if($result){
            $this->error('账号已存在');
        }
        Db::startTrans();
        $salt = Random::alnum();
        $arr3['username'] = $username;
        $arr3['avatar'] = "/assets/img/avatar.png";
        $arr3['mobile'] = $mobile;
        $arr3['salt'] = $salt;
        $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);
        $result3 = $userModel->insertData($arr3);
        if(empty($result3)){
            Db::rollback();
            $this->error('sql执行失败');
        }
        $result4 = $codeModel->updateData(['id'=>$data['id']],['is_use'=>1]);
        if(empty($result4)){
            Db::rollback();
            $this->error('sql执行失败');
        }
        Db::commit();
//        $token = generate_user_token($result3);
//        session('token',$token);
        $this->auth->direct($result3);
        $token = $this->auth->getToken();
        session('token',$token);
        Cookie::set('token',$token);
        $this->success('SUCCESS');
    }

    /**
     * 密码登录
     */
    public function password_login(){
        $mobile = $this->request->param('mobile');
        $password = $this->request->param('password');
        if(empty($mobile) || empty($password)){
            $this->error('缺少必要参数');
        }
        $userModel = new \app\index\model\User();
        $result = $userModel->findData(['mobile'=>$mobile]);
        if(empty($result)){
            $this->error('账号错误');
        }
        $password = md5(md5($password) . $result['salt']);
        if($result['password'] != $password){
            $this->error('密码错误');
        }
        $arr3['logintime'] = time();
        $arr3['loginip'] = $this->request->ip(0, true);
        $result3 = $userModel->updateData(['id'=>$result['id']],$arr3);
        if(empty($result3)){
            $this->error('sql执行失败');
        }
//        $token = generate_user_token($result['id']);
//        session('token',$token);
        $this->auth->direct($result['id']);
        $token = $this->auth->getToken();
        session('token',$token);
        Cookie::set('token',$token);
        $this->success('SUCCESS');
    }

    /**
     * 短信验证码登录
     */
    public function code_login(){
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');
        if(empty($mobile) || empty($code)){
            $this->error('缺少必要参数');
        }
        $userModel = new \app\index\model\User();
        $result1 = $userModel->findData(['mobile'=>$mobile]);
        if(empty($result1)){
            $this->error('账号错误');
        }
        $codeModel = new Code();
        $data = $codeModel->findData(['mobile'=>$mobile]);
        if(empty($data)){
            $this->error('请先获取验证码');
        }
        if($data['is_use'] != 2){
            $this->error('验证码已失效');
        }
        if($data['pasttime'] < time()){
            $this->error('验证码已过期');
        }
        if($data['code'] != $code){
            $this->error('验证码错误');
        }
        $arr3['logintime'] = time();
        $arr3['loginip'] = $this->request->ip(0, true);
        $result3 = $userModel->updateData(['id'=>$result1['id']],$arr3);
        if(empty($result3)){
            $this->error('sql执行失败');
        }
        $result4 = $codeModel->updateData(['id'=>$data['id']],['is_use'=>1]);
        if(empty($result4)){
            Db::rollback();
            $this->error('sql执行失败');
        }
//        $token = generate_user_token($result1['id']);
//        session('token',$token);
        $this->auth->direct($result1['id']);
        $token = $this->auth->getToken();
        session('token',$token);
        Cookie::set('token',$token);
        $this->success('SUCCESS');
    }

    /**
     * 获取短信验证码
     */
    public function get_code1(){
        $mobile = $this->request->param('mobile');
        if (empty($mobile)) {
            $this->error('缺少必要参数');
        }
        $code = Random::numeric(6);
        $codeModel = new Code();
        $data = $codeModel->findData(['mobile' => $mobile]);
        $arr1['mobile'] = $mobile;
        $arr1['code'] = $code;
        $arr1['pasttime'] = time() + 600;
        $arr1['is_use'] = 2;
        Db::startTrans();
        if (empty($data)) {
            $arr1['createtime'] = time();
            $result1 = $codeModel->insertData($arr1);
        } else {
            $arr1['updatetime'] = time();
            $result1 = $codeModel->updateData(['mobile' => $mobile], $arr1);
        }
        if (empty($result1)) {
            $this->error('sql执行失败');
        }
        $content = array(
            'content' => "【工品达】您的验证码是:" . $code . ",请于10分钟内使用,如非本人操作,可忽略此消息。",//短信内容
            'mobile' => $mobile,//手机号码
            'tKey' => time(),
        );
        $result2 = json_decode(send_sms2($content),true);
        if ($result2['code'] != 200) {
            Db::rollback();
            $this->error('发送失败');
        }
        Db::commit();
        $this->success('SUCCESS','',['code'=>$code]);
    }

    /**
     * 忘记密码
     * @return mixed
     */
    public function forget_password_view(){
        $this->assign('title',"忘记密码");
        return $this->fetch();
    }

    /**
     * 获取短信验证码(需验证图形验证码)
     */
    public function get_code2(){
        $mobile = $this->request->param('mobile');
        $image_code = $this->request->param('image_code');
        if (empty($mobile) || empty($image_code)) {
            $this->error('缺少必要参数');
        }
        $captcha = new Captcha();
        if(!$captcha->check($image_code)){
            $this->error('图形验证码错误');
        }
        $code = Random::numeric(6);
        $codeModel = new Code();
        $data = $codeModel->findData(['mobile' => $mobile]);
        $arr1['mobile'] = $mobile;
        $arr1['code'] = $code;
        $arr1['pasttime'] = time() + 600;
        $arr1['is_use'] = 2;
        Db::startTrans();
        if (empty($data)) {
            $arr1['createtime'] = time();
            $result1 = $codeModel->insertData($arr1);
        } else {
            $arr1['updatetime'] = time();
            $result1 = $codeModel->updateData(['mobile' => $mobile], $arr1);
        }
        if (empty($result1)) {
            $this->error('sql执行失败');
        }
        $content = array(
            'content' => "【工品达】您的验证码是:" . $code . ",请于10分钟内使用,如非本人操作,可忽略此消息。",//短信内容
            'mobile' => $mobile,//手机号码
            'tKey' => time(),
        );
        $result2 = json_decode(send_sms2($content),true);
        if ($result2['code'] != 200) {
            Db::rollback();
            $this->error('发送失败');
        }
        Db::commit();
        $this->success('SUCCESS','',['code'=>$code]);
    }

    /**
     * 下一步
     */
    public function next(){
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');
        if(empty($mobile) || empty($code)){
            $this->error('缺少必要参数');
        }
        $userModel = new \app\index\model\User();
        $result1 = $userModel->findData(['mobile'=>$mobile]);
        if(empty($result1)){
            $this->error('账号错误');
        }
        $codeModel = new Code();
        $data = $codeModel->findData(['mobile'=>$mobile]);
        if(empty($data)){
            $this->error('请先获取验证码');
        }
        if($data['is_use'] != 2){
            $this->error('验证码已失效');
        }
        if($data['pasttime'] < time()){
            $this->error('验证码已过期');
        }
        if($data['code'] != $code){
            $this->error('验证码错误');
        }
        $this->success('SUCCESS');
    }

    /**
     * 设置密码
     */
    public function setting_password(){
        $mobile = $this->request->param('mobile');
        $password = $this->request->param('password');
        $affirm_password = $this->request->param('affirm_password');
        if(empty($mobile) || empty($password) || empty($affirm_password)){
            $this->error('缺少必要参数');
        }
        if($password != $affirm_password){
            $this->error('两次密码输入不一致');
        }
        $userModel = new \app\index\model\User();
        $data = $userModel->findData(['mobile'=>$mobile]);
        if(empty($data)){
            $this->error('该账户尚未注册');
        }
        Db::startTrans();
        $arr1['password'] = md5(md5($password) . $data['salt']);
        $arr1['updatetime'] = time();
        $result1 = $userModel->updateData(['id'=>$data['id']],$arr1);
        if(empty($result1)){
            Db::rollback();
            $this->error('sql执行失败');
        }
//        $token = generate_user_token($result1);
//        session('token',$token);
        $this->auth->direct($result1);
        $token = $this->auth->getToken();
        session('token',$token);
        Cookie::set('token',$token);
        Db::commit();
        $this->success('SUCCESS','user/login_view');
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        //注销本站
        $this->auth->logout();
        session('token',null);
        Cookie::delete('token');
        $this->redirect(url('index/index'));
//        $this->success(__('Logout successful'), url('index/index'));
    }
}