CmsContentTemplateSetRepository.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsContentTemplate;
  4. use App\Models\CmsContentTemplateSet;
  5. use App\Models\CmsSubjectProduct;
  6. use App\Models\CmsSubject;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Redis;
  9. use Symfony\Component\HttpKernel\Exception\HttpException;
  10. use Tymon\JWTAuth\Facades\JWTAuth;
  11. class CmsContentTemplateSetRepository
  12. {
  13. public function __construct(CmsContentTemplateSet $cmsContentTemplateSet, CmsContentTemplate $cmsContentTemplate, CmsSubjectProduct $cmsSubjectProduct, CmsSubject $cmsSubject)
  14. {
  15. $this->cmsContentTemplateSet = $cmsContentTemplateSet;
  16. $this->cmsContentTemplate = $cmsContentTemplate;
  17. $this->cmsSubjectProduct = $cmsSubjectProduct;
  18. $this->cmsSubject = $cmsSubject;
  19. }
  20. /**
  21. * 内容预览
  22. */
  23. public function preview($request)
  24. {
  25. $where = [
  26. 'city_id' => $request['city_id'],
  27. 'apply_type' => $request['type'],
  28. 'is_open' => 1,
  29. 'status'=>1,
  30. 'deleted_at'=>null
  31. ];
  32. $temalates = $this->cmsContentTemplate->select('title', 'id')->where($where)->orderBy('id','desc')->first();
  33. if (!$temalates) {
  34. throw new HttpException(500, '没有找到对应模板');
  35. }
  36. $group_array = [];
  37. $group_key = env('REDIS_HOME_GROUP_KEY');
  38. $market_key = env('REDIS_HOME_MARKET_KEY');
  39. //团购首页
  40. if ($request['type'] == 0) {
  41. if (Cache::has($group_key)) {
  42. return Cache::store('redis')->get($group_key);
  43. }
  44. $group_array['apply_type'] = "group";
  45. $group_array['title'] = $temalates->title;
  46. $group_array['content'] = [];
  47. }else {//菜市场首页
  48. if (Cache::has($market_key)) {
  49. return Cache::store('redis')->get($market_key);
  50. }
  51. $group_array['apply_type'] = "market";
  52. $group_array['title'] = $temalates->title;
  53. $group_array['content'] = [];
  54. }
  55. $group_array['content'][0]['area_type'] = "banner";
  56. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0,'status'=>1])->get();
  57. $new_rule = [];
  58. foreach ($banner_rule->toArray() as $k => $v) {
  59. $decode_banner = \GuzzleHttp\json_decode($v['rule'], true);
  60. if (count($decode_banner)>0) {
  61. foreach ($decode_banner as $key=>$value){
  62. $new_rule[$key]['id'] = $key;
  63. $new_rule[$key]['rule'] = $value;
  64. $new_rule[$key]['rule']['b_id'] = $v['id'];
  65. }
  66. }
  67. }
  68. $group_array['content'][0]['rule'] = $new_rule;
  69. $subject_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 1,'status'=>1])->get();
  70. $new_rule1 = [];
  71. foreach ($subject_rule->toArray() as $k => $v) {
  72. $new_rule1[$k]['id'] = $v['id'];
  73. $new_rule1[$k]['rule'] = \GuzzleHttp\json_decode($v['rule'], true);
  74. }
  75. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2,'status'=>1])->get();
  76. $new_rule2 = [];
  77. foreach ($floor_rule->toArray() as $k => $v) {
  78. $new_rule2[$k]['id'] = $v['id'];
  79. $new_rule2[$k]['rule'] = \GuzzleHttp\json_decode($v['rule'], true);
  80. }
  81. foreach ($new_rule2 as $k => $v) {
  82. if ($v) {
  83. $rules = $v['rule'];
  84. $show_num = intval($rules['show_num']);
  85. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  86. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  87. $pro_array = $product->toArray();
  88. $res_id = implode(",", array_column($pro_array, 'product_id'));
  89. $new_rule2[$k]['product_id'] = $res_id;
  90. $new_rule2[$k]['subject_id'] = $rules['link_url'];
  91. $new_rule2[$k]['show_type'] = $show_type->show_type ?? '';
  92. unset($v['url']);
  93. unset($v['show_num']);
  94. unset($rules['link_url']);
  95. unset($v['link_type']);
  96. }
  97. }
  98. if ($request['type'] == 1) {
  99. $group_array['content'][1]['area_type'] = "category";
  100. $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 3,'status'=>1])->get();
  101. $new_rule3 = [];
  102. foreach ($category_rule->toArray() as $k => $v) {
  103. $new_rule3[$k]['id'] = $v['id'];
  104. $new_rule3[$k]['rule'] = \GuzzleHttp\json_decode($v['rule'], true);
  105. }
  106. $group_array['content'][1]['rule'] = $new_rule3;
  107. $group_array['content'][2]['area_type'] = "special";
  108. $group_array['content'][2]['rule'] = $new_rule1;
  109. $group_array['content'][3]['area_type'] = "floor";
  110. $group_array['content'][3]['rule'] = $new_rule2;
  111. if (!Cache::has($market_key)) {
  112. Cache::store('redis')->put($market_key, $group_array, 600);//10分钟过期
  113. }
  114. } else {
  115. $group_array['content'][1]['area_type'] = "special";
  116. $group_array['content'][1]['rule'] = $new_rule1;
  117. $group_array['content'][2]['area_type'] = "floor";
  118. $group_array['content'][2]['rule'] = $new_rule2;
  119. if (!Cache::has($group_key)) {
  120. Cache::store('redis')->put($group_key, $group_array, 600);
  121. }
  122. }
  123. return $group_array;
  124. }
  125. public function getTemplate($cityId)
  126. {
  127. return $this->cmsContentTemplate->where(['city_id' => $cityId, 'is_open' => 1,'status'=>1])->where('deleted_at', null)->select('id','title', 'apply_type')->orderBy('apply_type', 'asc')->get();
  128. }
  129. public function productList($request)
  130. {
  131. $area_type = '';
  132. if ($request['area_type'] == 'floor') {
  133. $area_type = 2;
  134. }
  135. $template_set = $this->cmsContentTemplateSet->select('rule')->where(['area_type' => $area_type, 'id' => $request['id']])->first();
  136. if (!$template_set) {
  137. throw new HttpException(500, '没有找到对应模板内容');
  138. }
  139. $template_array = json_decode($template_set->rule, true);
  140. $product = $this->cmsSubjectProduct->where('subject_id', $template_array['link_url'])->orderBy('sort', 'asc')->get();
  141. $pro_array = $product->toArray();
  142. $res_id = implode(",", array_column($pro_array, 'product_id'));
  143. try {
  144. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  145. $url = config("customer.app_service_url") . '/product/homeProduct';
  146. $array = [
  147. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  148. ];
  149. return http($url, $array, 'get');
  150. } catch (\Exception $e) {
  151. return [];
  152. }
  153. }
  154. }