CmsContentTemplateSetRepository.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. $exchange_key = config('constants.CMS_EXCHANGE');
  172. if (Cache::has($exchange_key)) {
  173. return Cache::store('redis')->get($exchange_key);
  174. }
  175. $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0, 'status' => 1])->limit(1)->first();
  176. $new_rule = [];
  177. if (isset($banner_rule->rule)){
  178. $decode_banner = \GuzzleHttp\json_decode($banner_rule->rule, true);
  179. if (count($decode_banner) > 0){
  180. foreach ($decode_banner as $key => $value) {
  181. $new_rule[$key]['url'] = $value['url'];
  182. $new_rule[$key]['link_url'] = $value['link_url'];
  183. $new_rule[$key]['link_type'] = $value['link_type'];
  184. }
  185. }
  186. $group_array['content'][0]['area_type'] = "banner";
  187. $group_array['content'][0]['id'] = $banner_rule->id ?? 0;
  188. $group_array['content'][0]['rule'] = $new_rule;
  189. }
  190. $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();
  191. $all_subject = $all_subject->toArray();
  192. $count = count($all_subject);
  193. $new_rule = [];
  194. foreach ($all_subject as $k => $v) {
  195. $decode_subject = \GuzzleHttp\json_decode($v['rule'], true);
  196. if (count($decode_subject) > 0) {
  197. if ($v['area_type'] == 4) {
  198. $new_rule[$k]['area_type'] = "subject_one";
  199. } else {
  200. $new_rule[$k]['area_type'] = "subject_two";
  201. }
  202. $new_rule[$k]['floor_img'] = $v['floor_img'];
  203. $new_rule[$k]['id'] = intval($v['id']);
  204. $new_rule[$k]['rule'] = $decode_subject;
  205. }
  206. }
  207. foreach ($new_rule as $k => $v) {
  208. $group_array['content'][$k + 1]['area_type'] = $new_rule[$k]['area_type'];
  209. $group_array['content'][$k + 1]['id'] = $new_rule[$k]['id'];
  210. $group_array['content'][$k + 1]['floor_img'] = $new_rule[$k]['floor_img'];
  211. $group_array['content'][$k + 1]['rule'] = $new_rule[$k]['rule'];
  212. }
  213. $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2, 'status' => 1])->orderBy('id', 'asc')->get();
  214. $new_rule = [];
  215. foreach ($floor_rule->toArray() as $key => $val) {
  216. $rule = \GuzzleHttp\json_decode($val['rule'], true);
  217. if (count($rule) > 0) {
  218. $show_type = $this->cmsSubject->select('show_type')->where('id', intval($rule['link_url']))->first();
  219. $product = $this->cmsSubjectProduct->where('subject_id', intval($rule['link_url']))->orderBy('sort', 'asc')->get();
  220. $pro_array = $product->toArray();
  221. $res_id = implode(",", array_column($pro_array, 'product_id'));
  222. $get_product = $this->get_product($res_id);
  223. $new_rule[$key]['id'] = $val['id'];
  224. $new_rule[$key]['show_type'] = intval($show_type->show_type) ?? '';
  225. $new_rule[$key]['url'] = $rule['url'];
  226. $new_rule[$key]['link_url'] = intval($rule['link_url']);
  227. $new_rule[$key]['link_type'] = $rule['link_type'];
  228. $new_rule[$key]['rule'] = $get_product['products'] ?? [];
  229. }
  230. }
  231. foreach ($new_rule as $key => $val) {
  232. $group_array['content'][$count + $key + 1]['area_type'] = "floor";
  233. $group_array['content'][$count + $key + 1]['id'] = $val['id'];
  234. $group_array['content'][$count + $key + 1]['show_type'] = $val['show_type'];
  235. $group_array['content'][$count + $key + 1]['floor_img'] = $val['url'];
  236. $group_array['content'][$count + $key + 1]['link_url'] = $val['link_url'];
  237. $group_array['content'][$count + $key + 1]['link_type'] = $val['link_type'];
  238. $group_array['content'][$count + $key + 1]['rule'] = $val['rule'];
  239. }
  240. if (!Cache::has($exchange_key)) {
  241. Cache::store('redis')->put($exchange_key, $group_array, 600);//10分钟过期
  242. }
  243. return $group_array;
  244. }
  245. function get_product($res_id)
  246. {
  247. try {
  248. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  249. $url = config("customer.app_service_url") . '/product/exchange/subject';
  250. $array = [
  251. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  252. ];
  253. return http($url, $array, 'get');
  254. } catch (\Exception $e) {
  255. return [];
  256. }
  257. }
  258. public function getProducts($request)
  259. {
  260. $product = $this->cmsSubjectProduct->where('subject_id', $request['subject_id'])->orderBy('sort', 'asc')->get();
  261. $pro_array = $product->toArray();
  262. $res_id = implode(",", array_column($pro_array, 'product_id'));
  263. try {
  264. if (isset($request['field_order'])) {
  265. $field_order = $request['field_order'];
  266. $sign = generateSign(['ids' => $res_id, 'field_order' => $field_order], config('customer.app_secret'));
  267. $array = [
  268. 'json' => ['sign' => $sign, 'ids' => $res_id, 'field_order' => $field_order], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  269. ];
  270. } else {
  271. $sign = generateSign(['ids' => $res_id], config('customer.app_secret'));
  272. $array = [
  273. 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  274. ];
  275. }
  276. $url = config("customer.app_service_url") . '/product/exchange/subject';
  277. return http($url, $array, 'get');
  278. } catch (\Exception $e) {
  279. return [];
  280. }
  281. }
  282. }