Page.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\api\model\Goods as GoodsModel;
  14. use app\api\model\Coupon as CouponModel;
  15. use app\common\model\Page as PageModel;
  16. use app\common\library\helper;
  17. /**
  18. * 页面模型
  19. * Class Page
  20. * @package app\api\model
  21. */
  22. class Page extends PageModel
  23. {
  24. /**
  25. * 隐藏字段
  26. * @var array
  27. */
  28. protected $hidden = [
  29. 'store_id',
  30. 'create_time',
  31. 'update_time'
  32. ];
  33. /**
  34. * DIY页面详情
  35. * @param int|null $pageId 页面ID
  36. * @return array
  37. * @throws \app\common\exception\BaseException
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function getPageData(int $pageId = null)
  43. {
  44. // 页面详情
  45. $detail = $pageId > 0 ? parent::detail($pageId) : parent::getHomePage();
  46. if (empty($detail)) {
  47. throwError('很抱歉,未找到该页面');
  48. }
  49. // 页面diy元素
  50. $pageData = $detail['page_data'];
  51. // 获取动态数据
  52. foreach ($pageData['items'] as &$item) {
  53. // 移出默认数据
  54. if (array_key_exists('defaultData', $item)) {
  55. unset($item['defaultData']);
  56. }
  57. if ($item['type'] === 'window') {
  58. $item['data'] = array_values($item['data']);
  59. } else if ($item['type'] === 'goods') {
  60. $item['data'] = $this->getGoodsList($item);
  61. } else if ($item['type'] === 'coupon') {
  62. $item['data'] = $this->getCouponList($item);
  63. } else if ($item['type'] === 'article') {
  64. $item['data'] = $this->getArticleList($item);
  65. } else if ($item['type'] === 'special') {
  66. $item['data'] = $this->getSpecialList($item);
  67. }
  68. }
  69. return $pageData;
  70. }
  71. /**
  72. * 商品组件:获取商品列表
  73. * @param $item
  74. * @return array
  75. * @throws \think\db\exception\DbException
  76. */
  77. private function getGoodsList($item)
  78. {
  79. // 获取商品数据
  80. $model = new GoodsModel;
  81. if ($item['params']['source'] === 'choice') {
  82. // 数据来源:手动
  83. $goodsIds = helper::getArrayColumn($item['data'], 'goods_id');
  84. if (empty($goodsIds)) return [];
  85. $goodsList = $model->getListByIdsFromApi($goodsIds);
  86. } else {
  87. // 数据来源:自动
  88. $goodsList = $model->getList([
  89. 'status' => 10,
  90. 'categoryId' => $item['params']['auto']['category'],
  91. 'sortType' => $item['params']['auto']['goodsSort'],
  92. ], $item['params']['auto']['showNum']);
  93. }
  94. if ($goodsList->isEmpty()) return [];
  95. // 格式化商品列表
  96. $data = [];
  97. foreach ($goodsList as $goods) {
  98. $data[] = [
  99. 'goods_id' => $goods['goods_id'],
  100. 'goods_name' => $goods['goods_name'],
  101. 'selling_point' => $goods['selling_point'],
  102. 'goods_image' => $goods['goods_images'][0]['preview_url'],
  103. 'goods_price_min' => $goods['goods_price_min'],
  104. 'goods_price_max' => $goods['goods_price_max'],
  105. 'line_price_min' => $goods['line_price_min'],
  106. 'line_price_max' => $goods['line_price_max'],
  107. 'goods_sales' => $goods['goods_sales'],
  108. ];
  109. }
  110. return $data;
  111. }
  112. /**
  113. * 优惠券组件:获取优惠券列表
  114. * @param $item
  115. * @return \think\Collection
  116. * @throws \app\common\exception\BaseException
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. private function getCouponList($item)
  122. {
  123. // 获取优惠券数据
  124. return (new CouponModel)->getList($item['params']['limit'], true);
  125. }
  126. /**
  127. * 文章组件:获取文章列表
  128. * @param $item
  129. * @return array
  130. * @throws \think\db\exception\DbException
  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. * @param $item
  142. * @return array
  143. * @throws \think\db\exception\DbException
  144. */
  145. private function getSpecialList($item)
  146. {
  147. // 获取头条数据
  148. $model = new Article;
  149. $articleList = $model->getList($item['params']['auto']['category'], $item['params']['auto']['showNum']);
  150. return $articleList->isEmpty() ? [] : $articleList->toArray()['data'];
  151. }
  152. }