Page.php 7.6 KB

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