Page.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\api\model\page;
  3. use app\api\model\plus\live\WxLive;
  4. use app\api\model\product\Product as ProductModel;
  5. use app\api\model\plus\article\Article;
  6. use app\api\model\store\Store as StoreModel;
  7. use app\common\model\page\Page as PageModel;
  8. use app\api\model\plus\coupon\Coupon;
  9. use app\api\model\plus\seckill\Product as SeckillProductModel;
  10. use app\api\model\plus\seckill\Active as SeckillActiveModel;
  11. use app\api\model\plus\assemble\Product as AssembleProductModel;
  12. use app\api\model\plus\assemble\Active as AssembleActiveModel;
  13. use app\api\model\plus\bargain\Product as BargainProductModel;
  14. use app\api\model\plus\bargain\Active as BargainActiveModel;
  15. /**
  16. * 首页模型
  17. */
  18. class Page extends PageModel
  19. {
  20. /**
  21. * 隐藏字段
  22. */
  23. protected $hidden = [
  24. 'app_id',
  25. 'create_time',
  26. 'update_time'
  27. ];
  28. /**
  29. * DIY页面详情
  30. */
  31. public static function getPageData($user, $page_id = null)
  32. {
  33. // 页面详情
  34. $detail = $page_id > 0 ? parent::detail($page_id) : parent::getHomePage();
  35. // 页面diy元素
  36. $items = $detail['page_data']['items'];
  37. // 页面顶部导航
  38. isset($detail['page_data']['page']) && $items['page'] = $detail['page_data']['page'];
  39. // 获取动态数据
  40. $model = new self;
  41. foreach ($items as $key => $item) {
  42. unset($items[$key]['defaultData']);
  43. if ($item['type'] === 'window') {
  44. $items[$key]['data'] = array_values($item['data']);
  45. } else if ($item['type'] === 'product') {
  46. $items[$key]['data'] = $model->getProductList($user, $item);
  47. } else if ($item['type'] === 'coupon') {
  48. $items[$key]['data'] = $model->getCouponList($user, $item);
  49. } else if ($item['type'] === 'article') {
  50. $items[$key]['data'] = $model->getArticleList($item);
  51. } else if ($item['type'] === 'special') {
  52. $items[$key]['data'] = $model->getSpecialList($item);
  53. } else if ($item['type'] === 'store') {
  54. $items[$key]['data'] = $model->getStoreList($item);
  55. } else if ($item['type'] === 'seckillProduct') {
  56. // 如果没有活动,则不显示
  57. $item_data = $model->getSeckillList($item);
  58. if(empty($item_data)){
  59. unset($items[$key]);
  60. }else{
  61. $items[$key]['data'] = $item_data;
  62. }
  63. } else if ($item['type'] === 'assembleProduct') {
  64. // 如果没有活动,则不显示
  65. $item_data = $model->getAssembleList($item);
  66. if(empty($item_data)){
  67. unset($items[$key]);
  68. }else{
  69. $items[$key]['data'] = $item_data;
  70. }
  71. } else if ($item['type'] === 'bargainProduct') {
  72. // 如果没有活动,则不显示
  73. $item_data = $model->getBargainList($item);
  74. if(empty($item_data)){
  75. unset($items[$key]);
  76. }else{
  77. $items[$key]['data'] = $item_data;
  78. }
  79. } else if ($item['type'] === 'wxlive') {
  80. $items[$key]['data'] = $model->getWxLiveList($item);
  81. }else if ($item['type'] === 'banner') {
  82. $items[$key]['data'] = $model->getBannerList($item);
  83. }
  84. }
  85. return ['page' => $items['page'], 'items' => $items];
  86. }
  87. /**
  88. * 商品组件:获取商品列表
  89. */
  90. private function getBannerList($item)
  91. {
  92. if(!empty($item['data'])){
  93. $data = [];
  94. foreach ($item['data'] as $key => $val) {
  95. if(!empty($val['name']) && empty($val['linkUrl'])){
  96. $val['linkUrl'] = $val['name'];
  97. }
  98. $data[] = $val;
  99. }
  100. return $data;
  101. }else{
  102. return [];
  103. }
  104. }
  105. /**
  106. * 商品组件:获取商品列表
  107. */
  108. private function getProductList($user, $item)
  109. {
  110. // 获取商品数据
  111. $model = new ProductModel;
  112. if ($item['params']['source'] === 'choice') {
  113. // 数据来源:手动
  114. $productIds = array_column($item['data'], 'product_id');
  115. $productList = $model->getListByIdsFromApi($productIds, $user);
  116. } else {
  117. // 数据来源:自动
  118. $productList = $model->getList([
  119. 'type' => 'sell',
  120. 'category_id' => $item['params']['auto']['category'],
  121. 'sortType' => $item['params']['auto']['productSort'],
  122. 'list_rows' => $item['params']['auto']['showNum']
  123. ], $user);
  124. }
  125. if ($productList->isEmpty()) return [];
  126. // 格式化商品列表
  127. $data = [];
  128. foreach ($productList as $product) {
  129. $show_sku = ProductModel::getShowSku($product);
  130. $data[] = [
  131. 'product_id' => $product['product_id'],
  132. 'product_name' => $product['product_name'],
  133. 'selling_point' => $product['selling_point'],
  134. 'image' => $product['image'][0]['file_path'],
  135. 'product_image' => $product['image'][0]['file_path'],
  136. 'product_price' => $show_sku['product_price'],
  137. 'line_price' => $show_sku['line_price'],
  138. 'product_sales' => $product['product_sales'],
  139. ];
  140. }
  141. return $data;
  142. }
  143. /**
  144. * 优惠券组件:获取优惠券列表
  145. */
  146. private function getCouponList($user, $item)
  147. {
  148. // 获取优惠券数据
  149. return (new Coupon)->getList($user, $item['params']['limit'], true);
  150. }
  151. /**
  152. * 文章组件:获取文章列表
  153. */
  154. private function getArticleList($item)
  155. {
  156. // 获取文章数据
  157. $model = new Article;
  158. $articleList = $model->getList($item['params']['auto']['category'], $item['params']['auto']['showNum']);
  159. return $articleList->isEmpty() ? [] : $articleList->toArray()['data'];
  160. }
  161. /**
  162. * 头条快报:获取头条列表
  163. */
  164. private function getSpecialList($item)
  165. {
  166. // 获取头条数据
  167. $model = new Article;
  168. $articleList = $model->getList($item['params']['auto']['category'], $item['params']['auto']['showNum']);
  169. return $articleList->isEmpty() ? [] : $articleList->toArray()['data'];
  170. }
  171. /**
  172. * 线下门店组件:获取门店列表
  173. */
  174. private function getStoreList($item)
  175. {
  176. // 获取商品数据
  177. $model = new StoreModel;
  178. if ($item['params']['source'] === 'choice') {
  179. // 数据来源:手动
  180. $storeIds = array_column($item['data'], 'store_id');
  181. $storeList = $model->getListByIds($storeIds);
  182. } else {
  183. // 数据来源:自动
  184. $storeList = $model->getList(null, false, false, $item['params']['auto']['showNum']);
  185. }
  186. if ($storeList->isEmpty()) return [];
  187. // 格式化商品列表
  188. $data = [];
  189. foreach ($storeList as $store) {
  190. $data[] = [
  191. 'store_id' => $store['store_id'],
  192. 'store_name' => $store['store_name'],
  193. 'logo_image' => $store['logo']['file_path'],
  194. 'phone' => $store['phone'],
  195. 'region' => $store['region'],
  196. 'address' => $store['address'],
  197. ];
  198. }
  199. return $data;
  200. }
  201. /**
  202. * 获取限时秒杀
  203. */
  204. private function getSeckillList($item)
  205. {
  206. // 获取秒杀数据
  207. $seckill = SeckillActiveModel::getActive();
  208. if($seckill){
  209. $product_model = new SeckillProductModel;
  210. $seckill['product_list'] = $product_model->getProductList($seckill['seckill_activity_id'], $item['params']['showNum']);
  211. }
  212. return $seckill;
  213. }
  214. /**
  215. * 获取限时拼团
  216. */
  217. private function getAssembleList($item)
  218. {
  219. // 获取拼团数据
  220. $assemble = AssembleActiveModel::getActive();
  221. if($assemble){
  222. $assemble->visible(['assemble_activity_id','title', 'start_time', 'end_time']);
  223. $product_model = new AssembleProductModel;
  224. $assemble['product_list'] = $product_model->getProductList($assemble['assemble_activity_id'], $item['params']['showNum']);
  225. }
  226. return $assemble;
  227. }
  228. /**
  229. * 获取限时砍价
  230. */
  231. private function getBargainList($item)
  232. {
  233. // 获取拼团数据
  234. $bargain = BargainActiveModel::getActive();
  235. if($bargain){
  236. $bargain->visible(['bargain_activity_id','title', 'start_time', 'end_time']);
  237. $product_model = new BargainProductModel;
  238. $bargain['product_list'] = $product_model->getProductList($bargain['bargain_activity_id'], $item['params']['showNum']);
  239. }
  240. return $bargain;
  241. }
  242. /**
  243. * 微信直播
  244. */
  245. private function getWxLiveList($item)
  246. {
  247. // 获取头条数据
  248. $model = new WxLive();
  249. $liveList = $model->getList($item['params']['showNum']);
  250. return $liveList->isEmpty() ? [] : $liveList->toArray()['data'];
  251. }
  252. }