CmsContentTemplateSetRepository.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 Symfony\Component\HttpKernel\Exception\HttpException;
  9. use Tymon\JWTAuth\Facades\JWTAuth;
  10. class CmsContentTemplateSetRepository
  11. {
  12. public function __construct(CmsContentTemplateSet $cmsContentTemplateSet, CmsContentTemplate $cmsContentTemplate, CmsSubjectProduct $cmsSubjectProduct, CmsSubject $cmsSubject)
  13. {
  14. $this->cmsContentTemplateSet = $cmsContentTemplateSet;
  15. $this->cmsContentTemplate = $cmsContentTemplate;
  16. $this->cmsSubjectProduct = $cmsSubjectProduct;
  17. $this->cmsSubject = $cmsSubject;
  18. }
  19. /**
  20. * 内容预览
  21. */
  22. public function preview($request)
  23. {
  24. $where = [
  25. 'city_id' => $request['city_id'],
  26. 'apply_type' => $request['type'],
  27. 'is_open' => 1,
  28. 'status' => 1,
  29. 'deleted_at' => null
  30. ];
  31. $temalates = $this->cmsContentTemplate->select('title', 'id')->where($where)->orderBy('id', 'desc')->first();
  32. if (!$temalates) {
  33. throw new HttpException(500, '没有找到对应模板');
  34. }
  35. $group_array = [];
  36. $group_key = config('constants.CMS_GROUP');
  37. $market_key = config('constants.CMS_MARKET');
  38. //团购首页
  39. if ($request['type'] == 0) {
  40. if (Cache::has($group_key)) {
  41. return Cache::store('redis')->get($group_key);
  42. }
  43. $group_array['apply_type'] = "group";
  44. $group_array['title'] = $temalates->title;
  45. $group_array['content'] = [];
  46. } else {//菜市场首页
  47. if (Cache::has($market_key)) {
  48. return Cache::store('redis')->get($market_key);
  49. }
  50. $group_array['apply_type'] = "market";
  51. $group_array['title'] = $temalates->title;
  52. $group_array['content'] = [];
  53. }
  54. $group_array['content'][0]['area_type'] = "banner";
  55. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0, 'status' => 1])->get();
  56. $new_rule = [];
  57. foreach ($banner_rule->toArray() as $k => $v) {
  58. $decode_banner = \GuzzleHttp\json_decode($v['rule'], true);
  59. if (count($decode_banner) > 0) {
  60. foreach ($decode_banner as $key => $value) {
  61. $new_rule[$key]['id'] = $key;
  62. $new_rule[$key]['rule'] = $value;
  63. $new_rule[$key]['rule']['b_id'] = strval($v['id']);
  64. $new_rule[$key]['rule']['link_url'] = strval($value['link_url']);//强转link_url类型
  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. $decode_subject = \GuzzleHttp\json_decode($v['rule'], true);
  74. $decode_subject['link_url'] = strval($decode_subject['link_url']);//强转link_url类型
  75. $new_rule1[$k]['rule'] = $decode_subject;
  76. }
  77. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2, 'status' => 1])->get();
  78. $new_rule2 = [];
  79. foreach ($floor_rule->toArray() as $k => $v) {
  80. $new_rule2[$k]['id'] = $v['id'];
  81. $decode_floor = \GuzzleHttp\json_decode($v['rule'], true);
  82. $decode_floor['link_url'] = strval($decode_floor['link_url']);//强转link_url类型
  83. $new_rule2[$k]['rule'] = $decode_floor;
  84. }
  85. foreach ($new_rule2 as $k => $v) {
  86. if ($v) {
  87. $rules = $v['rule'];
  88. $show_num = intval($rules['show_num']);
  89. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  90. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  91. $pro_array = $product->toArray();
  92. $res_id = implode(",", array_column($pro_array, 'product_id'));
  93. $new_rule2[$k]['product_id'] = $res_id;
  94. $new_rule2[$k]['subject_id'] = strval($rules['link_url']);
  95. $new_rule2[$k]['show_type'] = $show_type->show_type ?? '';
  96. unset($v['url']);
  97. unset($v['show_num']);
  98. unset($rules['link_url']);
  99. unset($v['link_type']);
  100. }
  101. }
  102. if ($request['type'] == 1) {
  103. $group_array['content'][1]['area_type'] = "category";
  104. $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 3, 'status' => 1])->get();
  105. $new_rule3 = [];
  106. foreach ($category_rule->toArray() as $k => $v) {
  107. $new_rule3[$k]['id'] = $v['id'];
  108. $decode_category = \GuzzleHttp\json_decode($v['rule'], true);
  109. $decode_category['link_url'] = strval($decode_category['link_url']);//强转link_url类型
  110. $new_rule3[$k]['rule'] = $decode_category;
  111. }
  112. $group_array['content'][1]['rule'] = $new_rule3;
  113. $group_array['content'][2]['area_type'] = "special";
  114. $group_array['content'][2]['rule'] = $new_rule1;
  115. $group_array['content'][3]['area_type'] = "floor";
  116. $group_array['content'][3]['rule'] = $new_rule2;
  117. if (!Cache::has($market_key)) {
  118. Cache::store('redis')->put($market_key, $group_array, 600);//10分钟过期
  119. }
  120. } else {
  121. $group_array['content'][1]['area_type'] = "special";
  122. $group_array['content'][1]['rule'] = $new_rule1;
  123. $group_array['content'][2]['area_type'] = "floor";
  124. $group_array['content'][2]['rule'] = $new_rule2;
  125. if (!Cache::has($group_key)) {
  126. Cache::store('redis')->put($group_key, $group_array, 600);
  127. }
  128. }
  129. return $group_array;
  130. }
  131. public function getTemplate($cityId)
  132. {
  133. 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();
  134. }
  135. public function productList($request)
  136. {
  137. $product = $this->cmsSubjectProduct->where('subject_id', $request['subject_id'])->orderBy('sort', 'asc')->get();
  138. $pro_array = $product->toArray();
  139. $res_id = implode(",", array_column($pro_array, 'product_id'));
  140. try {
  141. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  142. $url = config("customer.app_service_url") . '/product/homeProduct';
  143. $array = [
  144. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  145. ];
  146. return http($url, $array, 'get');
  147. } catch (\Exception $e) {
  148. return [];
  149. }
  150. }
  151. /**
  152. * 内容预览
  153. */
  154. public function exchangeMall($request)
  155. {
  156. $where = [
  157. 'city_id' => 0,
  158. 'apply_type' => 2,
  159. 'is_open' => 1,
  160. 'status' => 1,
  161. 'deleted_at' => null
  162. ];
  163. $temalates = $this->cmsContentTemplate->select('title', 'id')->where($where)->orderBy('id', 'desc')->first();
  164. if (!$temalates) {
  165. throw new HttpException(500, '没有找到对应模板');
  166. }
  167. $group_array = [];
  168. $group_array['apply_type'] = "exchangeMall";
  169. $group_array['title'] = $temalates->title;
  170. $group_array['content'] = [];
  171. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0, 'status' => 1])->limit(1)->first();
  172. $new_rule = [];
  173. if (isset($banner_rule->rule)){
  174. $decode_banner = \GuzzleHttp\json_decode($banner_rule->rule, true);
  175. if (count($decode_banner) > 0){
  176. foreach ($decode_banner as $key => $value) {
  177. $new_rule[$key]['url'] = $value['url'];
  178. $new_rule[$key]['link_url'] = $value['link_url'];
  179. $new_rule[$key]['link_type'] = $value['link_type'];
  180. }
  181. }
  182. $group_array['content'][0]['area_type'] = "banner";
  183. $group_array['content'][0]['id'] = $banner_rule->id ?? 0;
  184. $group_array['content'][0]['rule'] = $new_rule;
  185. }
  186. $all_subject = $this->cmsContentTemplateSet->select('id', 'rule', 'sort', 'name', 'floor_img', 'area_type')->where(['tpl_id' => $temalates->id, 'status' => 1])->whereIn('area_type', [4, 5])->orderBy('sort', 'asc')->get();
  187. $all_subject = $all_subject->toArray();
  188. $count = count($all_subject);
  189. $new_rule = [];
  190. foreach ($all_subject as $k => $v) {
  191. $decode_subject = \GuzzleHttp\json_decode($v['rule'], true);
  192. if (count($decode_subject) > 0) {
  193. if ($v['area_type'] == 4) {
  194. $new_rule[$k]['area_type'] = "subject_one";
  195. } else {
  196. $new_rule[$k]['area_type'] = "subject_two";
  197. }
  198. $new_rule[$k]['floor_img'] = $v['floor_img'];
  199. $new_rule[$k]['id'] = intval($v['id']);
  200. $new_rule[$k]['rule'] = $decode_subject;
  201. }
  202. }
  203. foreach ($new_rule as $k => $v) {
  204. $group_array['content'][$k + 1]['area_type'] = $new_rule[$k]['area_type'];
  205. $group_array['content'][$k + 1]['id'] = $new_rule[$k]['id'];
  206. $group_array['content'][$k + 1]['floor_img'] = $new_rule[$k]['floor_img'];
  207. $group_array['content'][$k + 1]['rule'] = $new_rule[$k]['rule'];
  208. }
  209. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2, 'status' => 1])->orderBy('id', 'asc')->get();
  210. $new_rule = [];
  211. foreach ($floor_rule->toArray() as $key => $val) {
  212. $rule = \GuzzleHttp\json_decode($val['rule'], true);
  213. if (count($rule) > 0) {
  214. $show_type = $this->cmsSubject->select('show_type')->where('id', intval($rule['link_url']))->first();
  215. $product = $this->cmsSubjectProduct->where('subject_id', intval($rule['link_url']))->orderBy('sort', 'asc')->get();
  216. $pro_array = $product->toArray();
  217. $res_id = implode(",", array_column($pro_array, 'product_id'));
  218. $get_product = $this->get_product($res_id);
  219. $new_rule[$key]['id'] = $val['id'];
  220. $new_rule[$key]['show_type'] = intval($show_type->show_type) ?? '';
  221. $new_rule[$key]['url'] = $rule['url'];
  222. $new_rule[$key]['link_url'] = intval($rule['link_url']);
  223. $new_rule[$key]['link_type'] = $rule['link_type'];
  224. $new_rule[$key]['rule'] = $get_product['products'] ?? [];
  225. }
  226. }
  227. foreach ($new_rule as $key => $val) {
  228. $group_array['content'][$count + $key + 1]['area_type'] = "floor";
  229. $group_array['content'][$count + $key + 1]['id'] = $val['id'];
  230. $group_array['content'][$count + $key + 1]['show_type'] = $val['show_type'];
  231. $group_array['content'][$count + $key + 1]['floor_img'] = $val['url'];
  232. $group_array['content'][$count + $key + 1]['link_url'] = $val['link_url'];
  233. $group_array['content'][$count + $key + 1]['link_type'] = $val['link_type'];
  234. $group_array['content'][$count + $key + 1]['rule'] = $val['rule'];
  235. }
  236. return $group_array;
  237. }
  238. function get_product($res_id)
  239. {
  240. try {
  241. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  242. $url = config("customer.app_service_url") . '/product/exchange/subject';
  243. $array = [
  244. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  245. ];
  246. return http($url, $array, 'get');
  247. } catch (\Exception $e) {
  248. return [];
  249. }
  250. }
  251. public function getProducts($request)
  252. {
  253. $product = $this->cmsSubjectProduct->where('subject_id', $request['subject_id'])->orderBy('sort', 'asc')->get();
  254. $pro_array = $product->toArray();
  255. $res_id = implode(",", array_column($pro_array, 'product_id'));
  256. try {
  257. if (isset($request['field_order'])) {
  258. $field_order = $request['field_order'];
  259. $sign = generateSign(['ids' => $res_id, 'field_order' => $field_order], config('customer.app_secret'));
  260. $array = [
  261. 'json' => ['sign' => $sign, 'ids' => $res_id, 'field_order' => $field_order], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  262. ];
  263. } else {
  264. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  265. $array = [
  266. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  267. ];
  268. }
  269. $url = config("customer.app_service_url") . '/product/exchange/subject';
  270. return http($url, $array, 'get');
  271. } catch (\Exception $e) {
  272. return [];
  273. }
  274. }
  275. }