Page.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\common\model\Page as PageModel;
  14. use app\api\model\Goods as GoodsModel;
  15. use app\api\model\Coupon as CouponModel;
  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 mixed
  37. * @throws \cores\exception\BaseException
  38. * @throws \think\Exception
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getPageData(int $pageId = null)
  44. {
  45. // 页面详情
  46. $detail = $pageId > 0 ? parent::detail($pageId) : parent::getHomePage();
  47. empty($detail) && throwError('很抱歉,未找到该页面');
  48. // 页面diy元素
  49. $pageData = $detail['page_data'];
  50. // 获取动态数据
  51. foreach ($pageData['items'] as &$item) {
  52. // 移出无效的数据
  53. $item = $this->removeInvalidData($item);
  54. if ($item['type'] === 'window') {
  55. $item['data'] = array_values($item['data']);
  56. } else if ($item['type'] === 'goods') {
  57. $item['data'] = $this->getGoodsList($item);
  58. } else if ($item['type'] === 'coupon') {
  59. $item['data'] = $this->getCouponList($item);
  60. } else if ($item['type'] === 'article') {
  61. $item['data'] = $this->getArticleList($item);
  62. } else if ($item['type'] === 'special') {
  63. $item['data'] = $this->getSpecialList($item);
  64. }
  65. }
  66. return $pageData;
  67. }
  68. /**
  69. * 移出无效的数据(默认的或demo)
  70. * @param array $item
  71. * @return array
  72. */
  73. private function removeInvalidData(array $item): array
  74. {
  75. if (array_key_exists('defaultData', $item)) {
  76. unset($item['defaultData']);
  77. }
  78. if (array_key_exists('demo', $item)) {
  79. unset($item['demo']);
  80. }
  81. return $item;
  82. }
  83. /**
  84. * 商品组件:获取商品列表
  85. * @param $item
  86. * @return array
  87. * @throws \think\db\exception\DbException
  88. */
  89. private function getGoodsList($item): array
  90. {
  91. // 获取商品数据
  92. $model = new GoodsModel;
  93. if ($item['params']['source'] === 'choice') {
  94. // 数据来源:手动
  95. $goodsIds = helper::getArrayColumn($item['data'], 'goods_id');
  96. if (empty($goodsIds)) return [];
  97. $goodsList = $model->getListByIdsFromApi($goodsIds);
  98. } else {
  99. // 数据来源:自动
  100. $goodsList = $model->getList([
  101. 'status' => 10,
  102. 'categoryId' => $item['params']['auto']['category'],
  103. 'sortType' => $item['params']['auto']['goodsSort'],
  104. ], $item['params']['auto']['showNum']);
  105. }
  106. if (empty($goodsList) && $goodsList->isEmpty()) {
  107. return [];
  108. }
  109. // 格式化商品列表
  110. $data = [];
  111. foreach ($goodsList as $goods) {
  112. $data[] = [
  113. 'goods_id' => $goods['goods_id'],
  114. 'goods_name' => $goods['goods_name'],
  115. 'selling_point' => $goods['selling_point'],
  116. 'goods_image' => $goods['goods_image'],
  117. 'goods_price_min' => $goods['goods_price_min'],
  118. 'goods_price_max' => $goods['goods_price_max'],
  119. 'line_price_min' => $goods['line_price_min'],
  120. 'line_price_max' => $goods['line_price_max'],
  121. 'goods_sales' => $goods['goods_sales'],
  122. ];
  123. }
  124. return $data;
  125. }
  126. /**
  127. * 优惠券组件:获取优惠券列表
  128. * @param $item
  129. * @return \think\Collection
  130. * @throws \cores\exception\BaseException
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. private function getCouponList($item): \think\Collection
  136. {
  137. // 获取优惠券数据
  138. return (new CouponModel)->getList($item['params']['showNum'], true);
  139. }
  140. /**
  141. * 文章组件:获取文章列表
  142. * @param $item
  143. * @return array
  144. * @throws \think\db\exception\DbException
  145. */
  146. private function getArticleList($item): array
  147. {
  148. // 获取文章数据
  149. $model = new Article;
  150. $articleList = $model->getList($item['params']['auto']['category'], $item['params']['auto']['showNum']);
  151. return $articleList->isEmpty() ? [] : $articleList->toArray()['data'];
  152. }
  153. /**
  154. * 头条快报:获取头条列表
  155. * @param $item
  156. * @return array
  157. * @throws \think\db\exception\DbException
  158. */
  159. private function getSpecialList($item): array
  160. {
  161. // 获取头条数据
  162. $model = new Article;
  163. $articleList = $model->getList($item['params']['auto']['category'], $item['params']['auto']['showNum']);
  164. return $articleList->isEmpty() ? [] : $articleList->toArray()['data'];
  165. }
  166. }