Selaa lähdekoodia

6-15引入validate验证机制

Home 5 vuotta sitten
vanhempi
commit
c485b16564
2 muutettua tiedostoa jossa 55 lisäystä ja 7 poistoa
  1. 18 7
      app/admin/controller/Login.php
  2. 37 0
      app/admin/validate/AdminUser.php

+ 18 - 7
app/admin/controller/Login.php

@@ -8,6 +8,7 @@
 
 namespace app\admin\controller;
 
+use app\admin\validate\AdminUser as AdminUserValidate;
 use app\common\model\mysql\AdminUser;
 use think\facade\View;
 
@@ -17,7 +18,7 @@ class Login extends AdminBase
     public function initialize()
     {
         if ($this->isLogin()) {
-            return $this->redirect(url('index/index'),302);
+            return $this->redirect(url('index/index'), 302);
         }
     }
 
@@ -42,14 +43,24 @@ class Login extends AdminBase
         $username = $this->request->param('username', '', 'trim');
         $password = $this->request->param('password', '', 'trim');
         $captcha = $this->request->param('captcha', '', 'trim');
-        if (empty($username) || empty($password) || empty($captcha)) {
-            return show(config('status.error'), '参数不能为空');
+
+        $data = [
+            'username' => $username,
+            'password' => $password,
+            'captcha' => $captcha,
+        ];
+        $validate = new AdminUserValidate();
+        if ($validate->check($data)) {
+            return show(config('status.error'), $validate->getError());
         }
+//        if (empty($username) || empty($password) || empty($captcha)) {
+//            return show(config('status.error'), '参数不能为空');
+//        }
         // 验证码校验
-        if (!captcha_check($captcha)) {
-            // 验证码校验失败
-            return show(config('status.error'), '验证码不正确');
-        }
+//        if (!captcha_check($captcha)) {
+//            // 验证码校验失败
+//            return show(config('status.error'), '验证码不正确');
+//        }
 
         try {
             $adminUserObj = new AdminUser();

+ 37 - 0
app/admin/validate/AdminUser.php

@@ -0,0 +1,37 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/1/4 11:17
+ */
+
+namespace app\admin\validate;
+
+
+use think\Validate;
+
+class AdminUser extends Validate
+{
+    protected $rule = [
+        'username' => 'require',
+        'password' => 'require',
+        'captcha' => 'require|checkCaptcha',
+    ];
+
+    protected $message = [
+        'username' => '用户名必须',
+        'password' => '密码必须',
+        'captcha' => '验证码必须',
+    ];
+
+    protected function checkCaptcha($value, $rule, $data = [])
+    {
+        if (!captcha_check($value)) {
+            // 验证码校验失败
+            return '验证码不正确';
+        }
+        return true;
+    }
+}