浏览代码

新增点击内容配置接口

duqinya 6 年之前
父节点
当前提交
6182c64264

+ 14 - 0
app/Http/Controllers/CmsContentTemplateSetController.php

@@ -20,6 +20,20 @@ class CmsContentTemplateSetController extends BaseController
     }
     }
 
 
 
 
+    //点击配置
+    public function set(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'tpl_id' => 'required',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->cmsContentSetTemplate->set($request->all());
+
+    }
+
     //banner设置
     //banner设置
     public function bannerSet(Request $request)
     public function bannerSet(Request $request)
     {
     {

+ 5 - 13
app/Repositories/CmsContentTemplateRepository.php

@@ -17,13 +17,13 @@ class CmsContentTemplateRepository {
         $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
         $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
         $where = [];
         $where = [];
         if(isset($request['id'])){
         if(isset($request['id'])){
-            $where[] = ['id', '=', $request['id']];
+            $where[] = ['id'=>$request['id'],'is_open'=>1];
         }
         }
         if (isset($request['is_open'])){
         if (isset($request['is_open'])){
             $where[] = ['is_open',$request['is_open']];
             $where[] = ['is_open',$request['is_open']];
         }
         }
         if (isset($request['city_id'])){
         if (isset($request['city_id'])){
-            $where[] = ['city_id',$request['city_id']];
+            $where[] = ['city_id'=>$request['city_id'],'is_open'=>1];
         }
         }
 
 
         return $this->cmsContentTemplate->where($where)->orderBy('id', 'asc')->paginate($perPage);
         return $this->cmsContentTemplate->where($where)->orderBy('id', 'asc')->paginate($perPage);
@@ -37,17 +37,9 @@ class CmsContentTemplateRepository {
     {
     {
         $where = ['city_id'=>$request['city_id'],'title'=>trim($request['title'])];
         $where = ['city_id'=>$request['city_id'],'title'=>trim($request['title'])];
         $template = $this->cmsContentTemplate->where($where)->get();
         $template = $this->cmsContentTemplate->where($where)->get();
-        foreach ($template->toArray() as $k=>$v){
-            if ($v['is_open']==1){
-                $template_id = $this->cmsContentTemplate->find($v['id']);
-                $template_id->is_open = 0;
-                $template_id->updated_at = date('Y-m-d H:i:s');
-                $res = $template_id->save();
-                if (!$res) {
-                    throw new HttpException(500, '模板状态修改失败');
-                }
-            }
-    }
+        if (count($template)>2){
+            throw new HttpException(500, '每个城市只能存在两个相同的模版');
+        }
 
 
         $subject = [
         $subject = [
             'title' => $request['title'],
             'title' => $request['title'],

+ 62 - 5
app/Repositories/CmsContentTemplateSetRepository.php

@@ -8,6 +8,8 @@ use App\Models\CmsContentTemplate;
 use App\Models\CmsContentTemplateSet;
 use App\Models\CmsContentTemplateSet;
 use App\Models\CmsSubjectProduct;
 use App\Models\CmsSubjectProduct;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Component\HttpKernel\Exception\HttpException;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Database\QueryException;
 
 
 class CmsContentTemplateSetRepository {
 class CmsContentTemplateSetRepository {
     public function __construct(CmsContentTemplateSet $cmsContentTemplateSet,CmsContentTemplate $cmsContentTemplate,CmsSubjectProduct $cmsSubjectProduct,CmsSubject $cmsSubject) {
     public function __construct(CmsContentTemplateSet $cmsContentTemplateSet,CmsContentTemplate $cmsContentTemplate,CmsSubjectProduct $cmsSubjectProduct,CmsSubject $cmsSubject) {
@@ -19,9 +21,67 @@ class CmsContentTemplateSetRepository {
     }
     }
 
 
     /**
     /**
-     * 点击配置
+     * 点击内容配置
      */
      */
- 
+    public function set($request)
+    {
+        $template = $this->cmsContentTemplate->where('id',$request['tpl_id'])->first();
+        $templates = $template->toArray();
+
+        $request = [];
+            if ($templates['is_open'] == 0){
+                $request['type'] = $templates['apply_type'];
+                $request['tpl_id'] = $templates['id'];
+                return $this->preview($request);
+            }
+
+            if ($templates['is_open'] == 1){
+                        $new_template = [
+                            'title' => $templates['title'],
+                            'city_id' => $templates['city_id'],
+                            'city_name' => $templates['city_name'],
+                            'apply_type' => $templates['apply_type'],
+                            'is_open' => 0,
+                        ];
+                        DB::beginTransaction();
+                        try {
+                            $res = $this->cmsContentTemplate->create($new_template);
+                            if (!$res) {
+                                throw new HttpException(500, '添加草稿模板失败');
+                            }
+
+                            $template_set = $this->cmsContentTemplateSet->where('tpl_id', $templates['id'])->get();
+                            if (count($template_set) > 0) {
+                                foreach ($template_set->toArray() as $key => $val) {
+                                    $copy_template = [
+                                        'rule' => $val['rule'],
+                                        'tpl_id' => $res->id,
+                                        'area_type' => $val['area_type'],
+                                        'status' => $val['status'],
+                                    ];
+                                    $result = $this->cmsContentTemplateSet->create($copy_template);
+                                    if (!$result) {
+                                        throw new HttpException(500, '生成模板内容失败');
+                                    }
+                                    $request['type'] = $res->apply_type;
+                                    $request['tpl_id'] = $result->tpl_id;
+                                }
+                            }
+                            DB::commit();
+                           return $this->preview($request);
+    //                        return Response::create();
+
+                        } catch (QueryException $exception) {
+                            DB::rollBack();
+                            return Response::create([
+                                'message' => '生成模板内容失败,请重试',
+                                'error' => $exception->getMessage(),
+                                'status_code' => 500
+                            ]);
+                        }
+                    }
+
+    }
 
 
     /**
     /**
      * banner配置
      * banner配置
@@ -29,7 +89,6 @@ class CmsContentTemplateSetRepository {
     public function bannerSet($request)
     public function bannerSet($request)
     {
     {
         $banner_id = $request['id']??'';
         $banner_id = $request['id']??'';
-//            $rules = json_decode($request['rule'],true);
             $rules = $request['rule'];
             $rules = $request['rule'];
             if (is_array($rules)){
             if (is_array($rules)){
                 if (count($rules)>10) {
                 if (count($rules)>10) {
@@ -242,9 +301,7 @@ class CmsContentTemplateSetRepository {
             }
             }
             $group_array['content'][3]['rule'] = $new_rule;
             $group_array['content'][3]['rule'] = $new_rule;
         }
         }
-
         return $group_array;
         return $group_array;
-
     }
     }
 
 
     public function getTemplate($cityId)
     public function getTemplate($cityId)