CmsContentTemplateSetRepository.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 = config('constants.CMS_GROUP');
  38. $market_key = config('constants.CMS_MARKET');
  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'] = strval($v['id']);
  65. $new_rule[$key]['rule']['link_url'] = strval($value['link_url']);//强转link_url类型
  66. }
  67. }
  68. }
  69. $group_array['content'][0]['rule'] = $new_rule;
  70. $subject_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 1, 'status' => 1])->get();
  71. $new_rule1 = [];
  72. foreach ($subject_rule->toArray() as $k => $v) {
  73. $new_rule1[$k]['id'] = $v['id'];
  74. $decode_subject = \GuzzleHttp\json_decode($v['rule'], true);
  75. $decode_subject['link_url'] = strval($decode_subject['link_url']);//强转link_url类型
  76. $new_rule1[$k]['rule'] = $decode_subject;
  77. }
  78. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2, 'status' => 1])->get();
  79. $new_rule2 = [];
  80. foreach ($floor_rule->toArray() as $k => $v) {
  81. $new_rule2[$k]['id'] = $v['id'];
  82. $decode_floor = \GuzzleHttp\json_decode($v['rule'], true);
  83. $decode_floor['link_url'] = strval($decode_floor['link_url']);//强转link_url类型
  84. $new_rule2[$k]['rule'] = $decode_floor;
  85. }
  86. foreach ($new_rule2 as $k => $v) {
  87. if ($v) {
  88. $rules = $v['rule'];
  89. $show_num = intval($rules['show_num']);
  90. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  91. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  92. $pro_array = $product->toArray();
  93. $res_id = implode(",", array_column($pro_array, 'product_id'));
  94. $new_rule2[$k]['product_id'] = $res_id;
  95. $new_rule2[$k]['subject_id'] = strval($rules['link_url']);
  96. $new_rule2[$k]['show_type'] = $show_type->show_type ?? '';
  97. unset($v['url']);
  98. unset($v['show_num']);
  99. unset($rules['link_url']);
  100. unset($v['link_type']);
  101. }
  102. }
  103. if ($request['type'] == 1) {
  104. $group_array['content'][1]['area_type'] = "category";
  105. $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 3, 'status' => 1])->get();
  106. $new_rule3 = [];
  107. foreach ($category_rule->toArray() as $k => $v) {
  108. $new_rule3[$k]['id'] = $v['id'];
  109. $decode_category = \GuzzleHttp\json_decode($v['rule'], true);
  110. $decode_category['link_url'] = strval($decode_category['link_url']);//强转link_url类型
  111. $new_rule3[$k]['rule'] = $decode_category;
  112. }
  113. $group_array['content'][1]['rule'] = $new_rule3;
  114. $group_array['content'][2]['area_type'] = "special";
  115. $group_array['content'][2]['rule'] = $new_rule1;
  116. $group_array['content'][3]['area_type'] = "floor";
  117. $group_array['content'][3]['rule'] = $new_rule2;
  118. if (!Cache::has($market_key)) {
  119. Cache::store('redis')->put($market_key, $group_array, 600);//10分钟过期
  120. }
  121. } else {
  122. $group_array['content'][1]['area_type'] = "special";
  123. $group_array['content'][1]['rule'] = $new_rule1;
  124. $group_array['content'][2]['area_type'] = "floor";
  125. $group_array['content'][2]['rule'] = $new_rule2;
  126. if (!Cache::has($group_key)) {
  127. Cache::store('redis')->put($group_key, $group_array, 600);
  128. }
  129. }
  130. return $group_array;
  131. }
  132. public function getTemplate($cityId)
  133. {
  134. 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();
  135. }
  136. public function productList($request)
  137. {
  138. $product = $this->cmsSubjectProduct->where('subject_id', $request['subject_id'])->orderBy('sort', 'asc')->get();
  139. $pro_array = $product->toArray();
  140. $res_id = implode(",", array_column($pro_array, 'product_id'));
  141. try {
  142. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  143. $url = config("customer.app_service_url") . '/product/homeProduct';
  144. $array = [
  145. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  146. ];
  147. return http($url, $array, 'get');
  148. } catch (\Exception $e) {
  149. return [];
  150. }
  151. }
  152. /**
  153. * 内容预览
  154. */
  155. public function exchangeMall($request)
  156. {
  157. $where = [
  158. 'city_id' => 0,
  159. 'apply_type' => 2,
  160. 'is_open' => 1,
  161. 'status' => 1,
  162. 'deleted_at' => null
  163. ];
  164. $temalates = $this->cmsContentTemplate->select('title', 'id')->where($where)->orderBy('id', 'desc')->first();
  165. if (!$temalates) {
  166. throw new HttpException(500, '没有找到对应模板');
  167. }
  168. $group_array = [];
  169. $group_array['apply_type'] = "exchangeMall";
  170. $group_array['title'] = $temalates->title;
  171. $group_array['content'] = [];
  172. $exchange_key = config('constants.CMS_EXCHANGE');
  173. if (Cache::has($exchange_key)) {
  174. return Cache::store('redis')->get($exchange_key);
  175. }
  176. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0, 'status' => 1])->limit(1)->first();
  177. $new_rule = [];
  178. if (isset($banner_rule->rule)){
  179. $decode_banner = \GuzzleHttp\json_decode($banner_rule->rule, true);
  180. if (count($decode_banner) > 0){
  181. foreach ($decode_banner as $key => $value) {
  182. $new_rule[$key]['url'] = $value['url'];
  183. $new_rule[$key]['link_url'] = $value['link_url'];
  184. $new_rule[$key]['link_type'] = $value['link_type'];
  185. }
  186. }
  187. $group_array['content'][0]['area_type'] = "banner";
  188. $group_array['content'][0]['id'] = $banner_rule->id ?? 0;
  189. $group_array['content'][0]['rule'] = $new_rule;
  190. }
  191. $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();
  192. $all_subject = $all_subject->toArray();
  193. $count = count($all_subject);
  194. $new_rule = [];
  195. foreach ($all_subject as $k => $v) {
  196. $decode_subject = \GuzzleHttp\json_decode($v['rule'], true);
  197. if (count($decode_subject) > 0) {
  198. if ($v['area_type'] == 4) {
  199. $new_rule[$k]['area_type'] = "subject_one";
  200. } else {
  201. $new_rule[$k]['area_type'] = "subject_two";
  202. }
  203. $new_rule[$k]['floor_img'] = $v['floor_img'];
  204. $new_rule[$k]['id'] = intval($v['id']);
  205. $new_rule[$k]['rule'] = $decode_subject;
  206. }
  207. }
  208. foreach ($new_rule as $k => $v) {
  209. $group_array['content'][$k + 1]['area_type'] = $new_rule[$k]['area_type'];
  210. $group_array['content'][$k + 1]['id'] = $new_rule[$k]['id'];
  211. $group_array['content'][$k + 1]['floor_img'] = $new_rule[$k]['floor_img'];
  212. $group_array['content'][$k + 1]['rule'] = $new_rule[$k]['rule'];
  213. }
  214. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2, 'status' => 1])->orderBy('id', 'asc')->get();
  215. $new_rule = [];
  216. foreach ($floor_rule->toArray() as $key => $val) {
  217. $rule = \GuzzleHttp\json_decode($val['rule'], true);
  218. if (count($rule) > 0) {
  219. $show_type = $this->cmsSubject->select('show_type')->where('id', intval($rule['link_url']))->first();
  220. $product = $this->cmsSubjectProduct->where('subject_id', intval($rule['link_url']))->orderBy('sort', 'asc')->get();
  221. $pro_array = $product->toArray();
  222. $res_id = implode(",", array_column($pro_array, 'product_id'));
  223. $get_product = $this->get_product($res_id);
  224. if ($get_product['products']){
  225. $get_product['products'] = array_slice($get_product['products'],0,20);//楼层仅展示20个商品
  226. }
  227. $new_rule[$key]['id'] = $val['id'];
  228. $new_rule[$key]['show_type'] = intval($show_type->show_type) ?? '';
  229. $new_rule[$key]['url'] = $rule['url'];
  230. $new_rule[$key]['link_url'] = intval($rule['link_url']);
  231. $new_rule[$key]['link_type'] = $rule['link_type'];
  232. $new_rule[$key]['rule'] = $get_product['products'] ?? [];
  233. }
  234. }
  235. foreach ($new_rule as $key => $val) {
  236. $group_array['content'][$count + $key + 1]['area_type'] = "floor";
  237. $group_array['content'][$count + $key + 1]['id'] = $val['id'];
  238. $group_array['content'][$count + $key + 1]['show_type'] = $val['show_type'];
  239. $group_array['content'][$count + $key + 1]['floor_img'] = $val['url'];
  240. $group_array['content'][$count + $key + 1]['link_url'] = $val['link_url'];
  241. $group_array['content'][$count + $key + 1]['link_type'] = $val['link_type'];
  242. $group_array['content'][$count + $key + 1]['rule'] = $val['rule'];
  243. }
  244. if (!Cache::has($exchange_key)) {
  245. Cache::store('redis')->put($exchange_key, $group_array, 600);//10分钟过期
  246. }
  247. return $group_array;
  248. }
  249. function get_product($res_id)
  250. {
  251. try {
  252. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  253. $url = config("customer.app_service_url") . '/product/exchange/subject';
  254. $array = [
  255. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  256. ];
  257. return http($url, $array, 'get');
  258. } catch (\Exception $e) {
  259. return [];
  260. }
  261. }
  262. public function getProducts($request)
  263. {
  264. $product = $this->cmsSubjectProduct->where('subject_id', $request['subject_id'])->orderBy('sort', 'asc')->get();
  265. $pro_array = $product->toArray();
  266. $res_id = implode(",", array_column($pro_array, 'product_id'));
  267. $product_key = config('constants.CMS_PRODUCTS');
  268. $product_key = sprintf($product_key,md5($res_id));
  269. if (Cache::has($product_key)) {
  270. return Cache::store('redis')->get($product_key);
  271. }
  272. try {
  273. if (isset($request['field_order'])) {
  274. $field_order = $request['field_order'];
  275. $sign = generateSign(['ids' => $res_id, 'field_order' => $field_order], config('customer.app_secret'));
  276. $array = [
  277. 'json' => ['sign' => $sign, 'ids' => $res_id, 'field_order' => $field_order], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  278. ];
  279. } else {
  280. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  281. $array = [
  282. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  283. ];
  284. }
  285. $url = config("customer.app_service_url") . '/product/exchange/subject';
  286. $http = http($url, $array, 'get');
  287. if (!Cache::has($product_key)) {
  288. Cache::store('redis')->put($product_key, $http, 600);//10分钟过期
  289. }
  290. return $http;
  291. } catch (\Exception $e) {
  292. return [];
  293. }
  294. }
  295. }