xielin 6 years ago
parent
commit
5e7339bd48

+ 2 - 2
app/Http/Controllers/CmsContentTemplateSetController.php

@@ -26,10 +26,10 @@ class CmsContentTemplateSetController extends BaseController
             'type' => 'required|integer',
         ]);
         if ($validator->fails()) {
-            return $this->response->error($validator->errors()->first(), 500);
+            return $this->jsonError($validator->errors()->first());
         }
 
-        return  $this->cmsContentSetTemplate->preview($request->all());
+        return  $this->jsonSuccess($this->cmsContentSetTemplate->preview($request->all()));
 
     }
 

+ 44 - 0
app/Http/Controllers/Controller.php

@@ -6,5 +6,49 @@ use Illuminate\Routing\Controller as BaseController;
 
 class Controller extends BaseController
 {
+    use Helpers;
 
+    public function jsonSuccess($data = [], $msg = "成功")
+    {
+        $response = array(
+            'code' => 0,
+            'msg' => $msg,
+            'data' => []
+        );
+        if ($data) {
+            if (is_array($data)) {
+                //带有分页格式转换
+                if (isset($data['meta'])) {
+                    // 更改元数据格式,全部包含在data下
+                    $temp = array(
+                        'data' => array(
+                            'data' => $data['data'],
+                            'pagination' => $data['meta']['pagination']
+                        )
+                    );
+                    $response = array_merge($response, $temp);
+                } elseif (isset($data['data'])) {
+                    $response = array_merge($response, $data);
+                } else {
+                    $temp = array(
+                        'data' => $data
+                    );
+                    $response = array_merge($response, $temp);
+                }
+            } else {
+                $response['data'] = $data;
+            }
+        }
+        return $response;
+    }
+
+    public function jsonError($msg)
+    {
+        $response = array(
+            'code' => 1,
+            'msg' => $msg,
+            'data' => ""
+        );
+        return $response;
+    }
 }