CmsContentTemplateSetRepository.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. $group_array['content'][0]['area_type'] = "banner";
  172. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0,'status'=>1])->get();
  173. $new_rule = [];
  174. foreach ($banner_rule->toArray() as $k => $v) {
  175. $decode_banner = \GuzzleHttp\json_decode($v['rule'], true);
  176. if (count($decode_banner)>0) {
  177. foreach ($decode_banner as $key=>$value){
  178. $new_rule[$key]['id'] = $key;
  179. $new_rule[$key]['rule'] = $value;
  180. $new_rule[$key]['rule']['b_id'] = intval($v['id']);
  181. }
  182. }
  183. }
  184. $group_array['content'][0]['rule'] = $new_rule;
  185. $subject_one = $this->cmsContentTemplateSet->select('id', 'rule','sort','name')->where(['tpl_id' => $temalates->id, 'area_type' => 4,'status'=>1])->orderBy('id', 'desc')->limit(1)->get();
  186. $subject_two = $this->cmsContentTemplateSet->select('id', 'rule','sort','name')->where(['tpl_id' => $temalates->id, 'area_type' => 5,'status'=>1])->limit(1)->get();
  187. $subject_one_sort = implode(',',array_column($subject_one->toArray(), 'sort'));
  188. $subject_two_sort = implode(',',array_column($subject_two->toArray(), 'sort'));
  189. if (intval($subject_one_sort) <= intval($subject_two_sort)){
  190. $group_array['content'][1]['area_type'] = "subject_one";
  191. $new_rule = [];
  192. foreach ($subject_one->toArray() as $k => $v) {
  193. $decode_subject_one = \GuzzleHttp\json_decode($v['rule'], true);
  194. if (count($decode_subject_one) > 0) {
  195. foreach ($decode_subject_one as $key=>$value) {
  196. $new_rule[$key]['id'] = $key;
  197. $new_rule[$key]['name'] = $v['name'];
  198. $new_rule[$key]['image'] = config('customer.cms_subject_one_bgimg');
  199. $new_rule[$key]['rule'] = $value;
  200. $new_rule[$key]['rule']['b_id'] = intval($v['id']);
  201. }
  202. }
  203. }
  204. $group_array['content'][1]['rule'] = $new_rule;
  205. $group_array['content'][2]['area_type'] = "subject_two";
  206. $new_rule = [];
  207. foreach ($subject_two->toArray() as $k => $v) {
  208. $decode_subject_two = \GuzzleHttp\json_decode($v['rule'], true);
  209. if (count(json_decode($v['rule'])) > 0) {
  210. foreach ($decode_subject_two as $key=>$value) {
  211. $new_rule[$key]['id'] = $key;
  212. $new_rule[$key]['name'] = $v['name'];
  213. $new_rule[$key]['image'] = config('customer.cms_subject_two_bgimg');
  214. $new_rule[$key]['rule'] = $value;
  215. $new_rule[$key]['rule']['b_id'] = intval($v['id']);
  216. }
  217. }
  218. }
  219. $group_array['content'][2]['rule'] = $new_rule;
  220. }else{
  221. $group_array['content'][1]['area_type'] = "subject_two";
  222. $new_rule = [];
  223. foreach ($subject_two->toArray() as $k => $v) {
  224. $decode_subject_two = \GuzzleHttp\json_decode($v['rule'], true);
  225. if (count(json_decode($v['rule'])) > 0) {
  226. foreach ($decode_subject_two as $key=>$value) {
  227. $new_rule[$key]['id'] = $key;
  228. $new_rule[$key]['name'] = $v['name'];
  229. $new_rule[$key]['image'] = config('customer.cms_subject_two_bgimg');
  230. $new_rule[$key]['rule'] = $value;
  231. $new_rule[$key]['rule']['b_id'] = intval($v['id']);
  232. }
  233. }
  234. }
  235. $group_array['content'][1]['rule'] = $new_rule;
  236. $group_array['content'][2]['area_type'] = "subject_one";
  237. $new_rule = [];
  238. foreach ($subject_one->toArray() as $k => $v) {
  239. $decode_subject_one = \GuzzleHttp\json_decode($v['rule'], true);
  240. if (count($decode_subject_one) > 0) {
  241. foreach ($decode_subject_one as $key=>$value) {
  242. $new_rule[$key]['id'] = $key;
  243. $new_rule[$key]['name'] = $v['name'];
  244. $new_rule[$key]['image'] = config('customer.cms_subject_one_bgimg');
  245. $new_rule[$key]['rule'] = $value;
  246. $new_rule[$key]['rule']['b_id'] = intval($v['id']);
  247. }
  248. }
  249. }
  250. $group_array['content'][2]['rule'] = $new_rule;
  251. }
  252. $group_array['content'][3]['area_type'] = "floor";
  253. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2,'status'=>1])->get();
  254. $new_rule2 = [];
  255. foreach ($floor_rule->toArray() as $k => $v) {
  256. $new_rule2[$k]['id'] = $v['id'];
  257. $decode_floor = \GuzzleHttp\json_decode($v['rule'], true);
  258. $new_rule2[$k]['rule'] = $decode_floor;
  259. }
  260. foreach ($new_rule2 as $k => $v) {
  261. if ($v) {
  262. $rules = $v['rule'];
  263. $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first();
  264. $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->get();
  265. $pro_array = $product->toArray();
  266. $res_id = implode(",", array_column($pro_array, 'product_id'));
  267. $get_product = $this->get_product($res_id);
  268. $new_rule2[$k]['products'] = $get_product['products'] ?? [];
  269. // $new_rule2[$k]['subject_id'] = intval($rules['link_url']);
  270. $new_rule2[$k]['show_type'] = intval($show_type->show_type) ?? '';
  271. unset($v['url']);
  272. unset($rules['link_url']);
  273. unset($v['link_type']);
  274. }
  275. }
  276. $group_array['content'][3]['rule'] = $new_rule2;
  277. return $group_array;
  278. }
  279. function get_product($res_id)
  280. {
  281. try {
  282. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  283. $url = config("customer.app_service_url") . '/product/exchange/subject';
  284. $array = [
  285. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  286. ];
  287. return http($url, $array, 'get');
  288. } catch (\Exception $e) {
  289. return [];
  290. }
  291. }
  292. public function getProducts($request)
  293. {
  294. $product = $this->cmsSubjectProduct->where('subject_id', $request['subject_id'])->orderBy('sort', 'asc')->get();
  295. $pro_array = $product->toArray();
  296. $res_id = implode(",", array_column($pro_array, 'product_id'));
  297. try {
  298. if (isset($request['field_order'])) {
  299. $field_order = $request['field_order'];
  300. $sign = generateSign(['ids' => $res_id, 'field_order' => $field_order], config('customer.app_secret'));
  301. $array = [
  302. 'json' => ['sign' => $sign, 'ids' => $res_id, 'field_order' => $field_order], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  303. ];
  304. } else {
  305. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  306. $array = [
  307. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  308. ];
  309. }
  310. $url = config("customer.app_service_url") . '/product/exchange/subject';
  311. return http($url, $array, 'get');
  312. } catch (\Exception $e) {
  313. return [];
  314. }
  315. }
  316. }