CmsContentTemplateSetRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsSubject;
  4. use Dingo\Api\Http\Response;
  5. use App\Models\CmsContentTemplate;
  6. use App\Models\CmsContentTemplateSet;
  7. use App\Models\CmsSubjectProduct;
  8. use Symfony\Component\HttpKernel\Exception\HttpException;
  9. class CmsContentTemplateSetRepository {
  10. public function __construct(CmsContentTemplateSet $cmsContentTemplateSet,CmsContentTemplate $cmsContentTemplate,CmsSubjectProduct $cmsSubjectProduct,CmsSubject $cmsSubject) {
  11. $this->cmsContentTemplateSet = $cmsContentTemplateSet;
  12. $this->cmsContentTemplate = $cmsContentTemplate;
  13. $this->cmsSubjectProduct = $cmsSubjectProduct;
  14. $this->cmsSubject = $cmsSubject;
  15. }
  16. /**
  17. * banner配置
  18. */
  19. public function bannerSet($request)
  20. {
  21. $rules = json_decode($request['rule'],true);
  22. if (is_array($rules)){
  23. if (count($rules)>10) {
  24. throw new HttpException(500, '最多只能添加10个banner海报');
  25. }
  26. $subject = [
  27. 'tpl_id' => $request['tpl_id'],
  28. 'rule' => $request['rule'],
  29. 'area_type' => $request['area_type'],
  30. 'status' => 0,
  31. ];
  32. if (!$this->cmsContentTemplateSet->create($subject)) {
  33. throw new HttpException(500, '添加失败');
  34. }
  35. }else{
  36. throw new HttpException(500, '参数格式有误');
  37. }
  38. }
  39. /**
  40. * 专题广告配置
  41. */
  42. public function advertisementSet($request)
  43. {
  44. $rules = json_decode($request['rule'],true);
  45. if (is_array($rules)){
  46. $subject = [
  47. 'tpl_id' => $request['tpl_id'],
  48. 'rule' => $request['rule'],
  49. 'area_type' => $request['area_type'],
  50. 'status' => 0,
  51. ];
  52. if (!$this->cmsContentTemplateSet->create($subject)) {
  53. throw new HttpException(500, '添加失败');
  54. }
  55. }else{
  56. throw new HttpException(500, '参数格式有误');
  57. }
  58. }
  59. /**
  60. * 商品楼层配置
  61. */
  62. public function floorSet($request)
  63. {
  64. $rules = json_decode($request['rule'],true);
  65. if (is_array($rules)){
  66. $subject = [
  67. 'tpl_id' => $request['tpl_id'],
  68. 'rule' => $request['rule'],
  69. 'area_type' => $request['area_type'],
  70. 'status' => 0,
  71. ];
  72. if (!$this->cmsContentTemplateSet->create($subject)) {
  73. throw new HttpException(500, '添加失败');
  74. }
  75. }else{
  76. throw new HttpException(500, '参数格式有误');
  77. }
  78. }
  79. /**
  80. * 分类专题配置
  81. */
  82. public function categorySet($request)
  83. {
  84. $rules = json_decode($request['rule'],true);
  85. if (is_array($rules)){
  86. $subject = [
  87. 'tpl_id' => $request['tpl_id'],
  88. 'rule' => $request['rule'],
  89. 'area_type' => $request['area_type'],
  90. 'status' => 0,
  91. ];
  92. if (!$this->cmsContentTemplateSet->create($subject)) {
  93. throw new HttpException(500, '添加失败');
  94. }
  95. }else{
  96. throw new HttpException(500, '参数格式有误');
  97. }
  98. }
  99. /**
  100. * 内容发布
  101. */
  102. public function release($request)
  103. {
  104. //同一个城市同一个模板(团购/菜市场) 只能有一种状态(草稿/发布),已发布的一旦被编辑把之前的直接删掉
  105. $templateSet = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->select('id','status')->get();
  106. $tem_array = $templateSet->toArray();
  107. foreach ($tem_array as $k=>$v) {
  108. if ($v['status'] == 1) {
  109. $result = $this->cmsContentTemplateSet->where('id', $v['id'])->delete();
  110. if (!$result) {
  111. return Response::create([
  112. 'message' => '删除失败,请重试',
  113. 'status_code' => 500
  114. ]);
  115. }
  116. }
  117. }
  118. $res = $this->cmsContentTemplateSet->where('tpl_id',$request['tpl_id'])->update(['status'=>1]);
  119. if (!$res){
  120. return Response::create([
  121. 'message' => '修改失败,请重试',
  122. 'status_code' => 500
  123. ]);
  124. }
  125. }
  126. /**
  127. * 内容发布
  128. */
  129. public function preview($request)
  130. {
  131. //团购首页
  132. $group_array = [];
  133. if ($request['type'] == 0){
  134. $group_array['apply_type'] ="group";
  135. $group_array['title'] ="今日团购";
  136. $group_array['content'] =[];
  137. }else{//菜市场首页
  138. $group_array['apply_type'] ="market";
  139. $group_array['title'] ="菜市场";
  140. $group_array['content'] =[];
  141. }
  142. $group_array['content'][0]['area_type'] ="banner";
  143. $banner_rule = $this->cmsContentTemplateSet->select('rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>0])->first();
  144. $group_array['content'][0]['rule'] = $banner_rule->rule;
  145. $group_array['content'][1]['area_type'] ="special";
  146. $subject_rule = $this->cmsContentTemplateSet->select('rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>1])->first();
  147. $group_array['content'][1]['rule'] = $subject_rule->rule;
  148. $group_array['content'][2]['area_type'] ="floor";
  149. $floor_rule = $this->cmsContentTemplateSet->select('rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>2])->first();
  150. $decode_floor = json_decode($floor_rule['rule'],true);
  151. foreach ($decode_floor as &$v){
  152. $show_type = $this->cmsSubject->select('show_type')->where('id',$v['link_url'])->first();
  153. $product = $this->cmsSubjectProduct->where('subject_id',$v['link_url'])->orderBy('sort','asc')->get();
  154. $pro_array = $product->toArray();
  155. $res_id = implode(",",array_column($pro_array,'id'));
  156. $v['product_id'] = $res_id;
  157. $v['subject_id'] = $v['link_url'];
  158. $v['show_type'] = $show_type->show_type;
  159. unset($v['url']);
  160. unset($v['show_num']);
  161. unset($v['link_url']);
  162. unset($v['link_type']);
  163. }
  164. $group_array['content'][2]['rule'] = json_encode($decode_floor,JSON_UNESCAPED_UNICODE);
  165. if ($request['type'] == 1){
  166. $group_array['content'][3]['area_type'] ="category";
  167. $category_rule = $this->cmsContentTemplateSet->select('rule')->where(['tpl_id'=>$request['tpl_id'],'area_type'=>3])->first();
  168. $group_array['content'][3]['rule'] = $category_rule->rule;
  169. }
  170. foreach ($group_array['content'] as $key=>$value){
  171. $rule = json_decode($value['rule'],true);
  172. $group_array['content'][$key]['rule'] = $rule;
  173. }
  174. $group_json = json_encode($group_array,JSON_UNESCAPED_UNICODE);
  175. return $group_json;
  176. }
  177. }