Browse Source

完善功能

gorden 9 months ago
parent
commit
e5976006ba

+ 42 - 0
app/admin/controller/sys_manage/AdvController.php

@@ -5,6 +5,7 @@ namespace app\admin\controller\sys_manage;
 use app\admin\validate\sys_manage\AdvValidate;
 use app\controller\Curd;
 use app\model\Adv;
+use support\exception\BusinessException;
 use support\Request;
 use support\Response;
 
@@ -36,6 +37,23 @@ class AdvController extends Curd
         return $this->doFormat($query, $format, $limit);
     }
 
+    public function afterQuery($items)
+    {
+        foreach ($items as &$item) {
+            if (!empty($item->adv_media_json)) {
+                $mediaJson = json_decode($item->adv_media_json, true);
+                if (isset($mediaJson['type'])) {
+                    $item->adv_media_json = $mediaJson['type'];
+                }
+                if (empty($mediaJson)){
+                    $item->adv_media_json = "";
+                }
+            }
+        }
+
+        return $items;
+    }
+
     /**
      * @Desc
      * @Author Gorden
@@ -48,8 +66,32 @@ class AdvController extends Curd
     protected function insertInput(Request $request): array
     {
         $data = $this->inputFilter($request->post());
+        if (!empty($data['adv_media'])) {
+            $data['adv_media_json'] = json_encode(['type' => $data['adv_media_json']]);
+        } else {
+            $data['adv_media_json'] = '[]';
+        }
 //        $data['adv_category'] = 'adv';
 
         return $data;
     }
+
+    protected function updateInput(Request $request): array
+    {
+        $primary_key = $this->model->getKeyName();
+        $id = $request->post($primary_key);
+        $data = $this->inputFilter($request->post());
+
+        if (!empty($data['adv_media_json'])) {
+            $data['adv_media_json'] = json_encode(['type' => $data['adv_media_json']]);
+        } else {
+            $data['adv_media_json'] = '[]';
+        }
+        $model = $this->model->find($id);
+        if (!$model) {
+            throw new BusinessException('记录不存在', 2);
+        }
+        unset($data[$primary_key]);
+        return [$id, $data];
+    }
 }

+ 3 - 3
app/admin/validate/sys_manage/AdvValidate.php

@@ -14,7 +14,7 @@ class AdvValidate extends Validate
         'adv_name|名称' => 'require|max:64',
         'adv_title|标题' => 'require|max:64',
         'adv_carrier载体页面' => 'max:64',
-        'adv_media|多媒体信息' => 'isJson',
+//        'adv_media|多媒体信息' => 'isJson',
         'adv_href|跳转链接' => 'max:64',
         'adv_config_json|配置信息' => 'isJson',
         'content_sort|排序' => 'integer',
@@ -26,9 +26,9 @@ class AdvValidate extends Validate
     protected $message = [];
 
     protected $scene = [
-        'add' => ['join_adv_category_id', 'adv_status', 'adv_category', 'adv_name', 'adv_title', 'adv_carrier', 'adv_media',
+        'add' => ['join_adv_category_id', 'adv_status', 'adv_category', 'adv_name', 'adv_title', 'adv_carrier',
             'adv_href', 'adv_config_json', 'content_sort', 'content_groupby', 'content_remark', 'content_extend_json'],
-        'update' => ['adv_id', 'join_adv_category_id', 'adv_status', 'adv_category', 'adv_name', 'adv_title', 'adv_carrier', 'adv_media',
+        'update' => ['adv_id', 'join_adv_category_id', 'adv_status', 'adv_category', 'adv_name', 'adv_title', 'adv_carrier',
             'adv_href', 'adv_config_json', 'content_sort', 'content_groupby', 'content_remark', 'content_extend_json'],
     ];