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. use cores\exception\BaseException;
  18. /**
  19. * 页面模型
  20. * Class Page
  21. * @package app\api\model
  22. */
  23. class Page extends PageModel
  24. {
  25. /**
  26. * 隐藏字段
  27. * @var array
  28. */
  29. protected $hidden = [
  30. 'store_id',
  31. 'create_time',
  32. 'update_time'
  33. ];
  34. /**
  35. * DIY页面详情
  36. * @param int|null $pageId 页面ID
  37. * @return array
  38. * @throws \app\common\exception\BaseException
  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. if (empty($detail)) {
  48. throwError('很抱歉,未找到该页面');
  49. }
  50. // 页面diy元素
  51. $pageData = $detail['page_data'];
  52. // 获取动态数据
  53. foreach ($pageData['items'] as &$item) {
  54. // 移出默认数据
  55. if (array_key_exists('defaultData', $item)) {
  56. unset($item['defaultData']);
  57. }
  58. if ($item['type'] === 'window') {
  59. $item['data'] = array_values($item['data']);
  60. } else if ($item['type'] === 'goods') {
  61. $item['data'] = $this->getGoodsList($item);
  62. } else if ($item['type'] === 'coupon') {
  63. $item['data'] = $this->getCouponList($item);
  64. } else if ($item['type'] === 'article') {
  65. $item['data'] = $this->getArticleList($item);
  66. } else if ($item['type'] === 'special') {
  67. $item['data'] = $this->getSpecialList($item);
  68. }
  69. }
  70. return $pageData;
  71. }
  72. /**
  73. * 商品组件:获取商品列表
  74. * @param $item
  75. * @return array
  76. * @throws \think\db\exception\DbException
  77. */
  78. private function getGoodsList($item)
  79. {
  80. // 获取商品数据
  81. $model = new GoodsModel;
  82. if ($item['params']['source'] === 'choice') {
  83. // 数据来源:手动
  84. $goodsIds = helper::getArrayColumn($item['data'], 'goods_id');
  85. if (empty($goodsIds)) return [];
  86. $goodsList = $model->getListByIdsFromApi($goodsIds);
  87. } else {
  88. // 数据来源:自动
  89. $goodsList = $model->getList([
  90. 'status' => 10,
  91. 'categoryId' => $item['params']['auto']['category'],
  92. 'sortType' => $item['params']['auto']['goodsSort'],
  93. ], $item['params']['auto']['showNum']);
  94. }
  95. if ($goodsList->isEmpty()) return [];
  96. // 格式化商品列表
  97. $data = [];
  98. foreach ($goodsList as $goods) {
  99. $data[] = [
  100. 'goods_id' => $goods['goods_id'],
  101. 'goods_name' => $goods['goods_name'],
  102. 'selling_point' => $goods['selling_point'],
  103. 'goods_image' => $goods['goods_images'][0]['preview_url'],
  104. 'goods_price_min' => $goods['goods_price_min'],
  105. 'goods_price_max' => $goods['goods_price_max'],
  106. 'line_price_min' => $goods['line_price_min'],
  107. 'line_price_max' => $goods['line_price_max'],
  108. 'goods_sales' => $goods['goods_sales'],
  109. ];
  110. }
  111. return $data;
  112. }
  113. /**
  114. * 优惠券组件:获取优惠券列表
  115. * @param $item
  116. * @return array|mixed
  117. * @throws BaseException
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. private function getCouponList($item)
  123. {
  124. // 获取优惠券数据
  125. return (new CouponModel)->getList($item['params']['limit'], true);
  126. }
  127. /**
  128. * 文章组件:获取文章列表
  129. * @param $item
  130. * @return array
  131. * @throws \think\db\exception\DbException
  132. */
  133. private function getArticleList($item)
  134. {
  135. // 获取文章数据
  136. $model = new Article;
  137. $articleList = $model->getList($item['params']['auto']['category'], $item['params']['auto']['showNum']);
  138. return $articleList->isEmpty() ? [] : $articleList->toArray()['data'];
  139. }
  140. /**
  141. * 头条快报:获取头条列表
  142. * @param $item
  143. * @return array
  144. * @throws \think\db\exception\DbException
  145. */
  146. private function getSpecialList($item)
  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. }