Ken 2 weeks ago
parent
commit
a784a74e20
1 changed files with 24 additions and 28 deletions
  1. 24 28
      app/Http/Controllers/admin/Keyboard.php

+ 24 - 28
app/Http/Controllers/admin/Keyboard.php

@@ -91,35 +91,31 @@ class Keyboard extends Controller
      */
     public function store()
     {
-        // try {
-
-        $id = request()->input('id', null);
-        $validator = [
-            // 'name' => 'required|string|max:50|alpha_dash',
-            // 'name' => 'required|string|max:50|alpha_dash|unique:keyboards,name',
-//            'button' => 'required|nullable|string|max:100',
-            'id' => ['required', 'integer'],
-            'reply' => 'required|string',
-            'image' => ['nullable', 'url'],
-            'buttons' => ['required', 'array'],
-        ];
-        if (!$id) {
-            unset($validator['id']);
-            $validator['button'] = ['required', 'string', 'min:1', 'max:100'];
-        }
-
-
-        $params = request()->validate($validator);
-
-        $ret = KeyboardService::submit($params);
-        if ($ret['code'] == KeyboardService::NOT) {
-            return $this->error($ret['code'], $ret['msg']);
+        try {
+            $id = request()->input('id', null);
+            $validator = [
+                'id' => ['required', 'integer'],
+                'reply' => 'required|string',
+                'image' => ['nullable', 'url'],
+                'buttons' => ['required', 'array'],
+            ];
+            if (empty($id)) {
+                unset($validator['id']);
+                $validator['button'] = ['required', 'string', 'min:1', 'max:100'];
+            }
+            $params = request()->validate($validator);
+            $ret = KeyboardService::submit($params);
+            if ($ret['code'] == KeyboardService::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) {
+            if ($e->getCode() == HttpStatus::CUSTOM_ERROR) {
+                return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
+            }
+            return $this->error(intval($e->getCode()));
         }
-        // } 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']);
 
     }