Ken 3 روز پیش
والد
کامیت
5fa6c0c9ee
2فایلهای تغییر یافته به همراه33 افزوده شده و 38 حذف شده
  1. 31 36
      app/Http/Controllers/admin/Admin.php
  2. 2 2
      app/Http/Controllers/admin/Keyboard.php

+ 31 - 36
app/Http/Controllers/admin/Admin.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\admin;
 
 use App\Constants\HttpStatus;
 use App\Http\Controllers\Controller;
+use App\Services\BaseService;
 use App\Services\JwtService;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Cache;
@@ -226,45 +227,39 @@ class Admin extends Controller
      */
     public function store()
     {
-        // try {
-        $params = request()->all();
-        if (isset($params['id']) && $params['id'] == 1) {
-            return $this->error(HttpStatus::CUSTOM_ERROR, '超级管理员禁止操作');
-        }
-        $validator = [
-            'username' => 'required|string|min:5|max:50|alpha_dash|unique:admin,username',
-            'nickname' => 'required|string|max:100',
-            'password' => ['nullable', 'string', 'min:6', 'max:20'],
-            'cellphone' => ['required', 'string'],
-            'email' => ['required', 'email'],
-            'remarks' => ['nullable', 'string'],
-            'roles_ids' => ['nullable', 'array'],
+        try {
+            $id = request()->input('id');
+            $validator = [
+                'username' => 'required|string|min:5|max:50|alpha_dash|unique:admin,username',
+                'nickname' => 'required|string|max:100',
+                'password' => ['nullable', 'string', 'min:6', 'max:20'],
+                'cellphone' => ['required', 'string'],
+                'email' => ['required', 'email'],
+                'remarks' => ['nullable', 'string'],
+                'roles_ids' => ['nullable', 'array'],
+                'id' => ['nullable', 'integer', 'min:1'],
 
-        ];
-        if (isset($params['id']) && !empty($params['id'])) {
-            if ($params['id'] > 1) {
-                $validator['roles_ids'] = ['required', 'array', 'min:1'];
-                $validator['roles_ids.*'] = ['required', 'integer', 'min:1'];
-            }
-            $validator['username'] = ['required', 'string', 'max:50', 'alpha_dash',
-                Rule::unique('admin', 'username')->ignore($params['id']), // 忽略当前 ID
             ];
+            if (!empty($id)) {
+                if ($id > 1) {
+                    $validator['roles_ids'] = ['required', 'array', 'min:1'];
+                    $validator['roles_ids.*'] = ['required', 'integer', 'min:1'];
+                }
+                $validator['username'] = ['required', 'string', 'max:50', 'alpha_dash',
+                    Rule::unique('admin', 'username')->ignore($id), // 忽略当前 ID
+                ];
+            }
+            $params = request()->validate($validator);
+            $ret = AdminService::submit($params);
+            if ($ret['code'] == BaseService::NOT) {
+                throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
+            }
+        } catch (ValidationException $e) {
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
+        } catch (Exception $e) {
+            return $this->error($e->getCode(), $e->getMessage());
         }
-
-
-        request()->validate($validator);
-
-        $ret = AdminService::submit($params);
-        if ($ret['code'] == AdminService::NOT) {
-            return $this->error($ret['code'], $ret['msg']);
-        }
-        // } catch (ValidationException $e) {
-        //     return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
-        // } catch (Exception $e) {
-        //     return $this->error(intval($e->getCode()));
-        // }
-        return $this->success([], $ret['msg']);
-
+        return $this->success();
     }
 
     /**

+ 2 - 2
app/Http/Controllers/admin/Keyboard.php

@@ -51,9 +51,9 @@ class Keyboard extends Controller
             ]);
             $result = KeyboardService::paginate($search);
         } catch (ValidationException $e) {
-            return $this->error(HttpStatus::VALIDATION_FAILED, '', $e->errors());
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
         } catch (Exception $e) {
-            return $this->error(intval($e->getCode()));
+            return $this->error($e->getCode(), $e->getMessage());
         }
         return $this->success($result);
     }