浏览代码

6-16控制器业务代码抽离到business层

Home 5 年之前
父节点
当前提交
f70986f703
共有 3 个文件被更改,包括 91 次插入43 次删除
  1. 68 0
      app/admin/business/AdminUser.php
  2. 9 29
      app/admin/controller/Login.php
  3. 14 14
      public/static/admin/js/login.js

+ 68 - 0
app/admin/business/AdminUser.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * Created by PhpStorm
+ * User:林志杰
+ * Email:[email protected]
+ * Motto:纵有疾风起,人生不言弃!
+ * Time:2020/1/10 20:46
+ */
+
+namespace app\admin\business;
+
+use app\common\model\mysql\AdminUser as AdminUserModel;
+use think\db\exception\DataNotFoundException;
+use think\db\exception\DbException;
+use think\db\exception\ModelNotFoundException;
+use think\Exception;
+
+class AdminUser
+{
+    public static function login($data)
+    {
+        $adminUser = self::getAdminUserByUsername($data['username']);
+        if (empty($adminUser)) {
+            throw new Exception('不存在该用户');
+        }
+        // 判断密码是否正确
+        if ($adminUser['password'] !== md5($data['password'] . '_singwa_abc')) {
+            throw new Exception('密码错误');
+            // return show(config('status.error'), '密码错误');
+        }
+
+        $adminUserObj = new AdminUserModel();
+        // 记录信息到mysql中
+        $updateData = [
+            'last_login_time' => time(),
+            'last_login_ip' => request()->ip(),
+            'update_time' => time(),
+        ];
+        $res = $adminUserObj->updateById($adminUser['id'], $updateData);
+        if (empty($res)) {
+            throw new Exception('登陆失败');
+            // return show(config('status.error'), '登陆失败');
+        }
+        // 记录session
+        session(config('admin.session_admin'), $adminUser);
+        return true;
+    }
+
+    /**
+     * 通过用户名获取用户数据
+     * @param $username
+     * @return array|bool|\think\Model|null
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     */
+    public static function getAdminUserByUsername($username)
+    {
+        $adminUserObj = new AdminUserModel();
+        $adminUser = $adminUserObj->getAdminUserByUsername($username);
+
+        if (empty($adminUser) || $adminUser->status != config('status.mysql.table_normal')) {
+            return false;
+        }
+        $adminUser = $adminUser->toArray();
+        return $adminUser;
+    }
+}

+ 9 - 29
app/admin/controller/Login.php

@@ -8,8 +8,8 @@
 
 namespace app\admin\controller;
 
+use app\admin\business\AdminUser as AdminUserBusiness;
 use app\admin\validate\AdminUser as AdminUserValidate;
-use app\common\model\mysql\AdminUser;
 use think\facade\View;
 
 
@@ -50,7 +50,7 @@ class Login extends AdminBase
             'captcha' => $captcha,
         ];
         $validate = new AdminUserValidate();
-        if ($validate->check($data)) {
+        if (!$validate->check($data)) {
             return show(config('status.error'), $validate->getError());
         }
 //        if (empty($username) || empty($password) || empty($captcha)) {
@@ -63,34 +63,14 @@ class Login extends AdminBase
 //        }
 
         try {
-            $adminUserObj = new AdminUser();
-            $adminUser = $adminUserObj->getAdminUserByUsername($username);
-            if (empty($adminUser) || $adminUser->status != config('status.mysql.table_normal')) {
-                return show(config('status.error'), '不存在该用户');
-            }
-            $adminUser = $adminUser->toArray();
-            if ($adminUser['password'] !== md5($password . '_singwa_abc')) {
-                return show(config('status.error'), '密码错误');
-            }
-
-            // 记录session
-            session(config('admin.session_admin'), $adminUser);
-
-            // 记录信息到mysql中
-            $updateData = [
-                'last_login_time' => time(),
-                'last_login_ip' => request()->ip(),
-                'update_time' => time(),
-            ];
-            $res = $adminUserObj->updateById($adminUser['id'], $updateData);
-            if (empty($res)) {
-                return show(config('status.error'), '登陆失败');
-            }
+            $result = AdminUserBusiness::login($data);
         } catch (\Exception $e) {
-            // todo 记录日志 $e->getMessage()
-            return show(config('status.error'), '内部异常,登陆失败');
+            return show(config('status.error'), $e->getMessage());
+        }
+        if ($result) {
+            return show(config('status.success'), '登录成功');
+        } else {
+            return show(config('status.error'), '登录失败');
         }
-
-        return show(config('status.success'), '登陆成功');
     }
 }

+ 14 - 14
public/static/admin/js/login.js

@@ -9,20 +9,20 @@ layui.use(['form'], function () {
     // 进行登录操作
     form.on('submit(login)', function (data) {
         data = data.field;
-        if (data.username == '') {
-            layer.msg('用户名不能为空');
-            return false;
-        }
-        if (data.password == '') {
-            layer.msg('密码不能为空');
-            return false;
-        }
-
-
-        if (data.captcha == '') {
-            layer.msg('验证码不能为空');
-            return false;
-        }
+        // if (data.username == '') {
+        //     layer.msg('用户名不能为空');
+        //     return false;
+        // }
+        // if (data.password == '') {
+        //     layer.msg('密码不能为空');
+        //     return false;
+        // }
+        //
+        //
+        // if (data.captcha == '') {
+        //     layer.msg('验证码不能为空');
+        //     return false;
+        // }
         // 验证码 校验
         // var val = $(".input-val").val().toLowerCase();
         // var num = show_num.join("");