Browse Source

登陸注冊

Jonlin 6 years ago
parent
commit
e688dab500
2 changed files with 189 additions and 0 deletions
  1. 77 0
      application/index/controller/Login.php
  2. 112 0
      application/index/controller/Register.php

+ 77 - 0
application/index/controller/Login.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * User: nickbai
+ * Date: 2017/10/24 10:46
+ * Email: 1902822973@qq.com
+ */
+namespace app\index\controller;
+
+use think\Controller;
+
+class Login extends Controller
+{
+    // 登录首页
+    public function index()
+    {
+        $this->assign([
+            'version' => config('version')
+        ]);
+
+        return $this->fetch();
+    }
+
+    // 处理登录
+    public function doLogin()
+    {
+        if(request()->isPost()){
+
+            $userName = input("param.user_name/s");
+            $password = input("param.password/s");
+
+            if(empty($userName)){
+                return json(['code' => -1, 'data' => '', 'msg' => '用户名或邮箱不能为空']);
+            }
+
+            if(empty($password)){
+                return json(['code' => -2, 'data' => '', 'msg' => '密码不能为空']);
+            }
+
+            $user_name = db('accounts')->where('user_name', $userName)->find();
+            $user_email = db('accounts')->where('user_name', $userName)->find();
+
+            if(empty($user_name) && empty($user_email)){
+                return json(['code' => -3, 'data' => '', 'msg' => '用户名或邮箱不存在']);
+            }
+            if(!empty($user_name)){$userInfo = $user_name;}
+            if(!empty($user_email)){$userInfo = $user_email;}
+
+            if(md5($password . config('salt')) != $userInfo['password']){
+                return json(['code' => -4, 'data' => '', 'msg' => '密码错误']);
+            }
+
+            if(1 != $userInfo['status']){
+                return json(['code' => -5, 'data' => '', 'msg' => '您已被禁用']);
+            }
+
+            // 记录用户状态
+            cookie('user_name', $userName, config('save_time'));
+            cookie('user_id', $userInfo['id'], config('save_time'));
+            // 更新用户状态
+            $param = [
+                'last_login_time' => time()
+            ];
+            db('accounts')->where('id', $userInfo['id'])->update($param);
+
+            return json(['code' => 1, 'data' => url('user/index'), 'msg' => '登陆成功']);
+        }
+    }
+
+    public function loginOut()
+    {
+        cookie('user_name', '');
+        cookie('user_id', '');
+
+        $this->redirect(url('login/index'));
+    }
+
+}

+ 112 - 0
application/index/controller/Register.php

@@ -0,0 +1,112 @@
+<?php
+/**
+ * User: nickbai
+ * Date: 2017/10/24 10:46
+ * Email: 1902822973@qq.com
+ */
+namespace app\index\controller;
+
+use think\Controller;
+use Xmail\PHPMailer;
+
+class Register extends Controller
+{
+    // 注册首页
+    public function index()
+    {
+        $this->assign([
+            'version' => config('version')
+        ]);
+
+        return $this->fetch();
+    }
+
+    // 处理注册
+    public function doRegister()
+    {
+        if(request()->isPost()){
+
+            $userName = input("param.user_name/s");
+            $userEmail = input("param.user_email/s");
+            $password = input("param.password/s");
+            $emailCode = input("param.code/s");
+
+            if(empty($userName)){
+                return json(['code' => -1, 'data' => '', 'msg' => '用户名不能为空']);
+            }
+
+            if(empty($userEmail)){
+                return json(['code' => -2, 'data' => '', 'msg' => '邮箱不能为空']);
+            }
+
+            if(empty($password)){
+                return json(['code' => -3, 'data' => '', 'msg' => '密码不能为空']);
+            }
+
+            if(empty($emailCode)){
+                return json(['code' => -4, 'data' => '', 'msg' => '验证码不能为空']);
+            }
+
+            if($emailCode != cookie('code')){
+                return json(['code' => -5, 'data' => '', 'msg' => '验证码不正确']);
+            }
+
+            $name = db('accounts')->where('user_name', $userName)->find();
+            if(!empty($name)){
+                return json(['code' => -6, 'data' => '', 'msg' => '用户名已存在']);
+            }
+
+            $email = db('accounts')->where('user_email', $userEmail)->find();
+            if(!empty($email)){
+                return json(['code' => -7, 'data' => '', 'msg' => '邮箱已存在']);
+            }
+
+            // 添加用户信息
+            $userInfo = [
+                'user_name' => $userName,
+                'user_email' => $userEmail,
+                'password' => md5($password . config('salt')),
+                'status' => 1,
+                'add_time' => time(),
+                'last_login_time' => time()
+            ];
+
+            $user_id = db('accounts')->insertGetId($userInfo);
+
+            // 记录用户状态
+            cookie('user_name', $userName, config('save_time'));
+            cookie('user_id', $user_id, config('save_time'));
+
+            return json(['code' => 1, 'data' => url('user/index'), 'msg' => '注册成功']);
+        }
+    }
+    public function email(){
+        $email=input("post.email");//获取收件人邮箱
+        $mail = new PHPMailer();
+        $mail->IsSMTP();
+        $mail->isHTML(true);
+        $mail->AltBody    = "更好地查看这封邮件,请打开HTML兼容视图"; // optional, comment out and test
+        $mail->CharSet = 'utf-8';   // 编码格式为utf8,不设置编码的话,中文会出现乱码
+        $mail->SMTPDebug  = 0;                     // enables SMTP debug information
+        $mail->SMTPAuth   = true;                  // enable SMTP authentication
+        $mail->Host       = "smtp.163.com"; // 发送方的SMTP服务器地址
+        $mail->Port       = 25;          // 端口
+        $mail->Password   = "jonlin2468";    //客户端授权密码,而不是邮箱的登录密码!
+        $mail->Username   = "jonlinhuang@163.com"; // 发件人邮箱
+        $mail->SetFrom('jonlinhuang@163.com', '安全猫-系统邮件'); // 安全猫科技有限公司co.ltd
+        $code=rand(100000,999999);
+        cookie('code', $code, config('save_time'));
+        $mail->Subject = '邮箱验证';
+        $mail->AddAddress($email);
+        $mail->MsgHTML('邮件内容是 <b>您的验证码是:'.$code.'</b>,如果非本人操作无需理会!');
+
+        $res = $mail->send();
+        if($res == true){
+            return "发送成功";
+        }else{
+            echo "Message could not be sent.";
+            echo "Mailer Error: " . $mail->ErrorInfo;// 输出错误信息
+        }
+    }
+
+}