CmsContentTemplateSetRepository.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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\Log;
  9. use Illuminate\Support\Facades\Redis;
  10. use Symfony\Component\HttpKernel\Exception\HttpException;
  11. use Tymon\JWTAuth\Facades\JWTAuth;
  12. class CmsContentTemplateSetRepository
  13. {
  14. public function __construct(CmsContentTemplateSet $cmsContentTemplateSet, CmsContentTemplate $cmsContentTemplate, CmsSubjectProduct $cmsSubjectProduct, CmsSubject $cmsSubject)
  15. {
  16. $this->cmsContentTemplateSet = $cmsContentTemplateSet;
  17. $this->cmsContentTemplate = $cmsContentTemplate;
  18. $this->cmsSubjectProduct = $cmsSubjectProduct;
  19. $this->cmsSubject = $cmsSubject;
  20. }
  21. /**
  22. * 内容预览
  23. */
  24. public function preview($request)
  25. {
  26. $where = [
  27. 'city_id' => $request['city_id'],
  28. 'apply_type' => $request['type'],
  29. 'is_open' => 1,
  30. 'status' => 1,
  31. 'deleted_at' => null
  32. ];
  33. $temalates = $this->cmsContentTemplate->select('title', 'id')->where($where)->orderBy('id', 'desc')->first();
  34. if (!$temalates) {
  35. throw new HttpException(500, '没有找到对应模板');
  36. }
  37. $group_array = [];
  38. $group_key = config('constants.CMS_GROUP');
  39. $market_key = config('constants.CMS_MARKET');
  40. //团购首页
  41. if ($request['type'] == 0) {
  42. // if (Cache::has($group_key)) {
  43. // return Cache::store('redis')->get($group_key);
  44. // }
  45. Log::info(Redis::exists($group_key));
  46. if (Redis::exists($group_key)) {
  47. $result = Redis::get($group_key);
  48. $res = json_decode($result, true);
  49. Log::info($res);
  50. return $res;
  51. }
  52. $group_array['apply_type'] = "group";
  53. $group_array['title'] = $temalates->title;
  54. $group_array['content'] = [];
  55. } else {//菜市场首页
  56. // if (Cache::has($market_key)) {
  57. // return Cache::store('redis')->get($market_key);
  58. // }
  59. Log::info(Redis::exists($market_key));
  60. if (Redis::exists($market_key)) {
  61. $result = Redis::get($market_key);
  62. $res = json_decode($result, true);
  63. Log::info($res);
  64. return $res;
  65. }
  66. $group_array['apply_type'] = "market";
  67. $group_array['title'] = $temalates->title;
  68. $group_array['content'] = [];
  69. }
  70. $group_array['content'][0]['area_type'] = "banner";
  71. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0, 'status' => 1])->get();
  72. $new_rule = [];
  73. foreach ($banner_rule->toArray() as $k => $v) {
  74. $decode_banner = \GuzzleHttp\json_decode($v['rule'], true);
  75. if (count($decode_banner) > 0) {
  76. foreach ($decode_banner as $key => $value) {
  77. $new_rule[$key]['id'] = $key;
  78. $new_rule[$key]['rule'] = $value;
  79. $new_rule[$key]['rule']['b_id'] = strval($v['id']);
  80. $new_rule[$key]['rule']['link_url'] = strval($value['link_url']);//强转link_url类型
  81. }
  82. }
  83. }
  84. $group_array['content'][0]['rule'] = $new_rule;
  85. $subject_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 1, 'status' => 1])->get();
  86. $new_rule1 = [];
  87. foreach ($subject_rule->toArray() as $k => $v) {
  88. $new_rule1[$k]['id'] = $v['id'];
  89. $decode_subject = \GuzzleHttp\json_decode($v['rule'], true);
  90. $decode_subject['link_url'] = strval($decode_subject['link_url']);//强转link_url类型
  91. $new_rule1[$k]['rule'] = $decode_subject;
  92. }
  93. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2, 'status' => 1])->get();
  94. $new_rule2 = [];
  95. foreach ($floor_rule->toArray() as $k => $v) {
  96. $new_rule2[$k]['id'] = $v['id'];
  97. $decode_floor = \GuzzleHttp\json_decode($v['rule'], true);
  98. $decode_floor['link_url'] = strval($decode_floor['link_url']);//强转link_url类型
  99. $new_rule2[$k]['rule'] = $decode_floor;
  100. }
  101. foreach ($new_rule2 as $k => $v) {
  102. if ($v) {
  103. $rules = $v['rule'];
  104. $show_num = intval($rules['show_num']);
  105. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  106. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get();
  107. $pro_array = $product->toArray();
  108. $res_id = implode(",", array_column($pro_array, 'product_id'));
  109. $new_rule2[$k]['product_id'] = $res_id;
  110. $new_rule2[$k]['subject_id'] = strval($rules['link_url']);
  111. $new_rule2[$k]['show_type'] = $show_type->show_type ?? '';
  112. unset($v['url']);
  113. unset($v['show_num']);
  114. unset($rules['link_url']);
  115. unset($v['link_type']);
  116. }
  117. }
  118. if ($request['type'] == 1) {
  119. $group_array['content'][1]['area_type'] = "category";
  120. $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 3, 'status' => 1])->get();
  121. $new_rule3 = [];
  122. foreach ($category_rule->toArray() as $k => $v) {
  123. $new_rule3[$k]['id'] = $v['id'];
  124. $decode_category = \GuzzleHttp\json_decode($v['rule'], true);
  125. $decode_category['link_url'] = strval($decode_category['link_url']);//强转link_url类型
  126. $new_rule3[$k]['rule'] = $decode_category;
  127. }
  128. $group_array['content'][1]['rule'] = $new_rule3;
  129. $group_array['content'][2]['area_type'] = "special";
  130. $group_array['content'][2]['rule'] = $new_rule1;
  131. $group_array['content'][3]['area_type'] = "floor";
  132. $group_array['content'][3]['rule'] = $new_rule2;
  133. // if (!Cache::has($market_key)) {
  134. // Cache::store('redis')->put($market_key, $group_array, 600);//10分钟过期
  135. // }
  136. if (!Redis::exists($market_key)) {
  137. Redis::setex($market_key, 600, json_encode($group_array));//10分钟过期
  138. Log::debug('菜市场首页缓存存储成功-' . json_encode($group_array));
  139. }
  140. } else {
  141. $group_array['content'][1]['area_type'] = "special";
  142. $group_array['content'][1]['rule'] = $new_rule1;
  143. $group_array['content'][2]['area_type'] = "floor";
  144. $group_array['content'][2]['rule'] = $new_rule2;
  145. // if (!Cache::has($group_key)) {
  146. // Cache::store('redis')->put($group_key, $group_array, 600);
  147. // }
  148. if (!Redis::exists($group_key)) {
  149. Redis::setex($group_key, 600, json_encode($group_array));//10分钟过期
  150. Log::debug('团购首页缓存存储成功-' . json_encode($group_array));
  151. }
  152. }
  153. return $group_array;
  154. }
  155. public function getTemplate($cityId)
  156. {
  157. 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();
  158. }
  159. public function productList($request)
  160. {
  161. $product = $this->cmsSubjectProduct->where('subject_id', $request['subject_id'])->orderBy('sort', 'asc')->get();
  162. $pro_array = $product->toArray();
  163. $res_id = implode(",", array_column($pro_array, 'product_id'));
  164. try {
  165. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  166. $url = config("customer.app_service_url") . '/product/homeProduct';
  167. $array = [
  168. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  169. ];
  170. return http($url, $array, 'get');
  171. } catch (\Exception $e) {
  172. return [];
  173. }
  174. }
  175. /**
  176. * 内容预览
  177. */
  178. public function exchangeMall($request)
  179. {
  180. $where = [
  181. 'city_id' => 0,
  182. 'apply_type' => 2,
  183. 'is_open' => 1,
  184. 'status' => 1,
  185. 'deleted_at' => null
  186. ];
  187. $temalates = $this->cmsContentTemplate->select('title', 'id')->where($where)->orderBy('id', 'desc')->first();
  188. if (!$temalates) {
  189. throw new HttpException(500, '没有找到对应模板');
  190. }
  191. $group_array = [];
  192. $group_array['apply_type'] = "exchangeMall";
  193. $group_array['title'] = $temalates->title;
  194. $group_array['content'] = [];
  195. $exchange_key = config('constants.CMS_EXCHANGE');
  196. // if (Cache::has($exchange_key)) {
  197. // return Cache::store('redis')->get($exchange_key);
  198. // }
  199. Log::info(Redis::exists($exchange_key));
  200. if (Redis::exists($exchange_key)) {
  201. $result = Redis::get($exchange_key);
  202. $res = json_decode($result, true);
  203. Log::info($res);
  204. return $res;
  205. }
  206. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0, 'status' => 1])->limit(1)->first();
  207. $new_rule = [];
  208. if (isset($banner_rule->rule)){
  209. $decode_banner = \GuzzleHttp\json_decode($banner_rule->rule, true);
  210. if (count($decode_banner) > 0){
  211. foreach ($decode_banner as $key => $value) {
  212. $new_rule[$key]['url'] = $value['url'];
  213. $new_rule[$key]['link_url'] = $value['link_url'];
  214. $new_rule[$key]['link_type'] = $value['link_type'];
  215. }
  216. }
  217. $group_array['content'][0]['area_type'] = "banner";
  218. $group_array['content'][0]['id'] = $banner_rule->id ?? 0;
  219. $group_array['content'][0]['rule'] = $new_rule;
  220. }
  221. $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();
  222. $all_subject = $all_subject->toArray();
  223. $count = count($all_subject);
  224. $new_rule = [];
  225. foreach ($all_subject as $k => $v) {
  226. $decode_subject = \GuzzleHttp\json_decode($v['rule'], true);
  227. if (count($decode_subject) > 0) {
  228. if ($v['area_type'] == 4) {
  229. $new_rule[$k]['area_type'] = "subject_one";
  230. } else {
  231. $new_rule[$k]['area_type'] = "subject_two";
  232. }
  233. $new_rule[$k]['floor_img'] = $v['floor_img'];
  234. $new_rule[$k]['id'] = intval($v['id']);
  235. $new_rule[$k]['rule'] = $decode_subject;
  236. }
  237. }
  238. foreach ($new_rule as $k => $v) {
  239. $group_array['content'][$k + 1]['area_type'] = $new_rule[$k]['area_type'];
  240. $group_array['content'][$k + 1]['id'] = $new_rule[$k]['id'];
  241. $group_array['content'][$k + 1]['floor_img'] = $new_rule[$k]['floor_img'];
  242. $group_array['content'][$k + 1]['rule'] = $new_rule[$k]['rule'];
  243. }
  244. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2, 'status' => 1])->orderBy('id', 'asc')->get();
  245. $new_rule = [];
  246. foreach ($floor_rule->toArray() as $key => $val) {
  247. $rule = \GuzzleHttp\json_decode($val['rule'], true);
  248. if (count($rule) > 0) {
  249. $show_type = $this->cmsSubject->select('show_type')->where('id', intval($rule['link_url']))->first();
  250. $product = $this->cmsSubjectProduct->where('subject_id', intval($rule['link_url']))->orderBy('sort', 'asc')->get();
  251. $pro_array = $product->toArray();
  252. $res_id = implode(",", array_column($pro_array, 'product_id'));
  253. $get_product = $this->get_product($res_id);
  254. if ($get_product['products']){
  255. $get_product['products'] = array_slice($get_product['products'],0,20);//楼层仅展示20个商品
  256. }
  257. $new_rule[$key]['id'] = $val['id'];
  258. $new_rule[$key]['show_type'] = intval($show_type->show_type) ?? '';
  259. $new_rule[$key]['url'] = $rule['url'];
  260. $new_rule[$key]['link_url'] = intval($rule['link_url']);
  261. $new_rule[$key]['link_type'] = $rule['link_type'];
  262. $new_rule[$key]['rule'] = $get_product['products'] ?? [];
  263. }
  264. }
  265. foreach ($new_rule as $key => $val) {
  266. $group_array['content'][$count + $key + 1]['area_type'] = "floor";
  267. $group_array['content'][$count + $key + 1]['id'] = $val['id'];
  268. $group_array['content'][$count + $key + 1]['show_type'] = $val['show_type'];
  269. $group_array['content'][$count + $key + 1]['floor_img'] = $val['url'];
  270. $group_array['content'][$count + $key + 1]['link_url'] = $val['link_url'];
  271. $group_array['content'][$count + $key + 1]['link_type'] = $val['link_type'];
  272. $group_array['content'][$count + $key + 1]['rule'] = $val['rule'];
  273. }
  274. // if (!Cache::has($exchange_key)) {
  275. // Cache::store('redis')->put($exchange_key, $group_array, 600);//10分钟过期
  276. // }
  277. if (!Redis::exists($exchange_key)) {
  278. // Redis::setex($exchange_key, 600, json_encode($group_array));//10分钟过期
  279. Redis::setex($exchange_key, 60, json_encode($group_array));//1分钟过期
  280. Log::debug('兑换商城缓存存储成功-' . json_encode($group_array));
  281. }
  282. return $group_array;
  283. }
  284. function get_product($res_id)
  285. {
  286. try {
  287. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  288. $url = config("customer.app_service_url") . '/product/exchange/subject';
  289. $array = [
  290. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  291. ];
  292. return http($url, $array, 'get');
  293. } catch (\Exception $e) {
  294. return [];
  295. }
  296. }
  297. public function getProducts($request)
  298. {
  299. $product = $this->cmsSubjectProduct->where('subject_id', $request['subject_id'])->orderBy('sort', 'asc')->get();
  300. $pro_array = $product->toArray();
  301. $res_id = implode(",", array_column($pro_array, 'product_id'));
  302. $field_order = $request['field_order'] ?? '';
  303. $product_key = config('constants.CMS_PRODUCTS');
  304. $product_keys = sprintf($product_key,md5($res_id.$field_order));
  305. // if (Cache::has($product_keys)) {
  306. // return Cache::store('redis')->get($product_key);
  307. // }
  308. Log::info(Redis::exists($product_keys));
  309. if (Redis::exists($product_keys)) {
  310. $result = Redis::get($product_keys);
  311. $res = json_decode($result, true);
  312. Log::info($res);
  313. return $res;
  314. }
  315. try {
  316. if (isset($request['field_order'])) {
  317. $field_order = $request['field_order'];
  318. $sign = generateSign(['ids' => $res_id, 'field_order' => $field_order], config('customer.app_secret'));
  319. $array = [
  320. 'json' => ['sign' => $sign, 'ids' => $res_id, 'field_order' => $field_order], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  321. ];
  322. } else {
  323. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  324. $array = [
  325. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  326. ];
  327. }
  328. $url = config("customer.app_service_url") . '/product/exchange/subject';
  329. $http = http($url, $array, 'get');
  330. // if (!Cache::has($product_keys)) {
  331. // Cache::store('redis')->put($product_keys, $http, 600);//10分钟过期
  332. // }
  333. if (!Redis::exists($product_keys)) {
  334. Redis::setex($product_keys, 300, json_encode($http));//5分钟过期
  335. Log::debug('专题商品缓存存储成功-' . json_encode($http));
  336. }
  337. return $http;
  338. } catch (\Exception $e) {
  339. return [];
  340. }
  341. }
  342. }