瀏覽代碼

Merge branch 'develop'

xielin 6 年之前
父節點
當前提交
790cc23c44

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

@@ -39,7 +39,6 @@ class CmsContentTemplateSetController extends BaseController
     public function bannerSet(Request $request)
     {
         $validator = Validator::make($request->all(), [
-            'rule' => 'required',
             'tpl_id' => 'required|integer',
             'area_type' => ['required', Rule::in(0,1,2,3)],
         ]);

+ 1 - 8
app/Http/Controllers/CmsSubjectController.php

@@ -31,14 +31,7 @@ class CmsSubjectController extends BaseController
         $cmsSubject = $this->cmsSubjectRepository->index($request->all());
         if (count($cmsSubject)>0) {
             foreach ($cmsSubject as $k => $v) {
-                $v->product_count = 0;
-                $product = CmsSubjectProduct::where('subject_id',$v['id'])->get();
-                $count = $product->toArray();
-                foreach ($count as $key => $value){
-                    if ($value['subject_id'] == $v['id']){
-                        $v->product_count ++ ;
-                    }
-                }
+                $v->product_count = CmsSubjectProduct::where('subject_id', $v['id'])->count();
             }
         }
 

+ 14 - 0
app/Repositories/CmsContentTemplateRepository.php

@@ -5,6 +5,7 @@ namespace App\Repositories;
 
 use App\Models\CmsContentTemplate;
 use App\Models\CmsContentTemplateSet;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Log;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 
@@ -73,6 +74,9 @@ class CmsContentTemplateRepository {
      */
     public function edit($request)
     {
+        $group_key = config('constants.CMS_GROUP');
+        $market_key = config('constants.CMS_MARKET');
+
         $template_id = $this->cmsContentTemplate->find($request['id']);
         if ($request['city_id'] == 610100 && $request['is_open'] == 0){
             throw new HttpException(500, '城市为西安的模板不允许手动关闭');
@@ -89,9 +93,19 @@ class CmsContentTemplateRepository {
         $template_id->updated_at = date('Y-m-d H:i:s');
 
         $res = $template_id->save();
+
         if (!$res) {
             throw new HttpException(500, '修改状态失败');
         }
+        if ($template_id->apply_type == 0){
+            if (Cache::has($group_key)) {
+                Cache::forget($group_key);
+            }
+        }elseif ($template_id->apply_type == 1){
+            if (Cache::has($market_key)) {
+                Cache::forget($market_key);
+            }
+        }
 
     }
 

+ 40 - 23
app/Repositories/CmsContentTemplateSetRepository.php

@@ -7,6 +7,7 @@ use Dingo\Api\Http\Response;
 use App\Models\CmsContentTemplate;
 use App\Models\CmsContentTemplateSet;
 use App\Models\CmsSubjectProduct;
+use Illuminate\Support\Facades\Cache;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Database\QueryException;
@@ -119,38 +120,40 @@ class CmsContentTemplateSetRepository {
                 $old_subject_id = $this->cmsContentTemplateSet->select('rule')->find($banner_id);
                 $subject_id_array = json_decode($old_subject_id['rule'], true);
                 $request_rule = $request['rule'];
-
-                foreach ($request_rule as $k => $v) {
-                    if ($v['link_type'] == 1) {
-                        $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
-                        $templates->used_count += 1;
-                        $templates->save();
+                if (count($request_rule)>0){
+                    foreach ($request_rule as $k => $v) {
+                        if (isset($v['link_type']) && $v['link_type'] == 1) {
+                            $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
+                            $templates->used_count += 1;
+                            $templates->save();
+                        }
                     }
                 }
-
-                foreach ($subject_id_array as $val) {
-                    if ($val['link_type'] == 1) {
-                        $templates = $this->cmsSubject->where('id', intval($val['link_url']))->first();
-                        $update_template = [
-                            $templates->used_count -= 1
-                        ];
-                        $templates->update($update_template);
+                if (count($subject_id_array)>0) {
+                    foreach ($subject_id_array as $val) {
+                        if (isset($val['link_type']) && $val['link_type'] == 1) {
+                            $templates = $this->cmsSubject->where('id', intval($val['link_url']))->first();
+                            $update_template = [
+                                $templates->used_count -= 1
+                            ];
+                            $templates->update($update_template);
+                        }
                     }
                 }
-
                 $banners = $this->cmsContentTemplateSet->where('id', $banner_id)->update($subject);
                 if (!$banners) {
                     throw new HttpException(500, '更新失败');
                 }
             } else {
-                foreach ($request['rule'] as $v) {
-                    if ($v['link_type'] == 1) {
-                        $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
-                        $templates->used_count += 1;
-                        $templates->save();
+                if (count($request['rule'])>0) {
+                    foreach ($request['rule'] as $v) {
+                        if (isset($v['link_type']) && $v['link_type'] == 1) {
+                            $templates = $this->cmsSubject->where('id', intval($v['link_url']))->first();
+                            $templates->used_count += 1;
+                            $templates->save();
+                        }
                     }
                 }
-
                 if (!$this->cmsContentTemplateSet->create($subject)) {
                     throw new HttpException(500, '添加失败');
                 }
@@ -292,6 +295,9 @@ class CmsContentTemplateSetRepository {
      */
     public function release($request)
     {
+        $group_key = config('constants.CMS_GROUP');
+        $market_key = config('constants.CMS_MARKET');
+
         //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
         $template = $this->cmsContentTemplate->select('id','city_name','city_id','title','apply_type','is_open','status')->where('id',$request['tpl_id'])->first();
         $where = [
@@ -347,6 +353,15 @@ class CmsContentTemplateSetRepository {
                 return Response::create();
             }
         }
+        if ($template->apply_type == 0){
+            if (Cache::has($group_key)) {
+                Cache::forget($group_key);
+            }
+        }elseif ($template->apply_type == 1){
+            if (Cache::has($market_key)) {
+                Cache::forget($market_key);
+            }
+        }
 
     }
 
@@ -378,8 +393,10 @@ class CmsContentTemplateSetRepository {
         $banner_rule = $this->cmsContentTemplateSet->select('id','rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>0])->orderBy('id', 'desc')->limit(1)->get();
         $new_rule = [];
         foreach ($banner_rule->toArray() as $k=>$v){
-            $new_rule[$k]['id'] = $v['id'];
-            $new_rule[$k]['rule'] = json_decode($v['rule'],true);
+            if (count(json_decode($v['rule']))>0){
+                $new_rule[$k]['id'] = $v['id'];
+                $new_rule[$k]['rule'] = json_decode($v['rule'],true);
+            }
         }
         $group_array['content'][0]['rule'] = $new_rule;
         $group_array['content'][1]['area_type'] = 1;

+ 13 - 8
app/Repositories/CmsSubjectRepository.php

@@ -69,17 +69,19 @@ class CmsSubjectRepository {
                     throw new HttpException(500, '请检查商品与排序数目是否对应');
                 }
 
-                $subject_product = new CmsSubjectProduct();
+                $resert_data = [];
                 for ($i = 0;$i < $product_num;$i++){
-                    $resert_data =[
+                    $resert_data[] =[
                         'product_id' => $products[$i],
                         'sort' => $sort[$i],
                         'subject_id' => $res['id'],
                         'created_at' => $date,
                         'updated_at' => $date,
                     ];
-                 $subject_product->insert($resert_data);
-
+                }
+                $result = $this->cmsSubjectProduct->insert($resert_data);
+                if (!$result){
+                    throw new HttpException(500, '专题商品添加失败');
                 }
             }
             DB::commit();
@@ -124,16 +126,19 @@ class CmsSubjectRepository {
                     throw new HttpException(500, '请检查商品与排序数目是否对应');
                 }
 
-                $subject_product = new CmsSubjectProduct();
-                for ($i = 0; $i < $product_num; $i++) {
-                    $resert_data = [
+                $update_data = [];
+                for ($i = 0;$i < $product_num;$i++){
+                    $update_data[] =[
                         'product_id' => $products[$i],
                         'sort' => $sort[$i],
                         'subject_id' => $subject->id,
                         'created_at' => $date,
                         'updated_at' => $date,
                     ];
-                    $subject_product->insert($resert_data);
+                }
+                $result = $this->cmsSubjectProduct->insert($update_data);
+                if (!$result){
+                    throw new HttpException(500, '专题商品添加失败');
                 }
             }
             DB::commit();

+ 3 - 0
bootstrap/app.php

@@ -29,6 +29,7 @@ $app->configure('apollo');
 $app->configure('auth');
 $app->configure('jwt');
 $app->configure('database');
+$app->configure('constants');
 
 /*
 |--------------------------------------------------------------------------
@@ -87,6 +88,8 @@ $app->register(App\Providers\AuthServiceProvider::class);
 
 $app->register(Dingo\Api\Provider\LumenServiceProvider::class);
 $app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
+$app->register(\Illuminate\Redis\RedisServiceProvider::class);
+
 /*
 |--------------------------------------------------------------------------
 | Load The Application Routes

+ 3 - 1
composer.json

@@ -13,7 +13,9 @@
         "laravel/lumen-framework": "5.8.*",
         "tymon/jwt-auth": "1.0.0-rc.4.1",
         "multilinguals/apollo-client": "^0.1.2",
-        "vlucas/phpdotenv": "^3.3"
+        "vlucas/phpdotenv": "^3.3",
+        "illuminate/redis": "^5.8",
+        "predis/predis": "^1.1"
     },
     "require-dev": {
         "fzaninotto/faker": "^1.4",

+ 10 - 0
config/constants.php

@@ -0,0 +1,10 @@
+<?php
+
+/**
+ * 常量配置
+ */
+
+return [
+    'CMS_GROUP' => env('REDIS_CMS_GROUP_KEY', 'cms_shop_group'),
+    'CMS_MARKET' => env('REDIS_CMS_MARKET_KEY', 'cms_shop_market'),
+];