WxappPage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace app\api\model;
  3. use app\api\model\Goods as GoodsModel;
  4. use app\api\model\store\Shop as ShopModel;
  5. use app\api\model\sharing\Goods as SharingGoodsModel;
  6. use app\api\model\bargain\Active as BargainActiveModel;
  7. use app\api\service\sharp\Active as SharpActiveService;
  8. use app\common\model\WxappPage as WxappPageModel;
  9. use app\common\library\helper;
  10. /**
  11. * 微信小程序diy页面模型
  12. * Class WxappPage
  13. * @package app\api\model
  14. */
  15. class WxappPage extends WxappPageModel
  16. {
  17. /**
  18. * 隐藏字段
  19. * @var array
  20. */
  21. protected $hidden = [
  22. 'wxapp_id',
  23. 'create_time',
  24. 'update_time'
  25. ];
  26. /**
  27. * DIY页面详情
  28. * @param $user
  29. * @param null $page_id
  30. * @return array
  31. * @throws \think\Exception
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \think\exception\DbException
  35. */
  36. public static function getPageData($user, $page_id = null)
  37. {
  38. // 页面详情
  39. $detail = $page_id > 0 ? parent::detail($page_id) : parent::getHomePage();
  40. // 页面diy元素
  41. $items = $detail['page_data']['items'];
  42. // 页面顶部导航
  43. isset($detail['page_data']['page']) && $items['page'] = $detail['page_data']['page'];
  44. // 获取动态数据
  45. $model = new self;
  46. foreach ($items as $key => $item) {
  47. if ($item['type'] === 'window') {
  48. $items[$key]['data'] = array_values($item['data']);
  49. } else if ($item['type'] === 'goods') {
  50. $items[$key]['data'] = $model->getGoodsList($user, $item);
  51. } else if ($item['type'] === 'sharingGoods') {
  52. $items[$key]['data'] = $model->getSharingGoodsList($user, $item);
  53. } else if ($item['type'] === 'bargainGoods') {
  54. $items[$key]['data'] = $model->getBargainGoodsList($item);
  55. } else if ($item['type'] === 'sharpGoods') {
  56. $items[$key]['data'] = $model->getSharpGoodsList($item);
  57. } else if ($item['type'] === 'coupon') {
  58. $items[$key]['data'] = $model->getCouponList($user, $item);
  59. } else if ($item['type'] === 'article') {
  60. $items[$key]['data'] = $model->getArticleList($item);
  61. } else if ($item['type'] === 'special') {
  62. $items[$key]['data'] = $model->getSpecialList($item);
  63. } else if ($item['type'] === 'shop') {
  64. $items[$key]['data'] = $model->getShopList($item);
  65. }
  66. }
  67. return ['page' => $items['page'], 'items' => $items];
  68. }
  69. /**
  70. * 商品组件:获取商品列表
  71. * @param $user
  72. * @param $item
  73. * @return array
  74. * @throws \think\exception\DbException
  75. */
  76. private function getGoodsList($user, $item)
  77. {
  78. // 获取商品数据
  79. $model = new GoodsModel;
  80. if ($item['params']['source'] === 'choice') {
  81. // 数据来源:手动
  82. $goodsIds = array_column($item['data'], 'goods_id');
  83. $goodsList = $model->getListByIdsFromApi($goodsIds, $user);
  84. } else {
  85. // 数据来源:自动
  86. $goodsList = $model->getList([
  87. 'status' => 10,
  88. 'category_id' => $item['params']['auto']['category'],
  89. 'sortType' => $item['params']['auto']['goodsSort'],
  90. 'listRows' => $item['params']['auto']['showNum']
  91. ], $user);
  92. }
  93. if ($goodsList->isEmpty()) return [];
  94. // 格式化商品列表
  95. $data = [];
  96. foreach ($goodsList as $goods) {
  97. $data[] = [
  98. 'goods_id' => $goods['goods_id'],
  99. 'goods_name' => $goods['goods_name'],
  100. 'selling_point' => $goods['selling_point'],
  101. 'image' => $goods['image'][0]['file_path'],
  102. 'goods_image' => $goods['image'][0]['file_path'],
  103. 'goods_price' => $goods['sku'][0]['goods_price'],
  104. 'line_price' => $goods['sku'][0]['line_price'],
  105. 'goods_sales' => $goods['goods_sales'],
  106. ];
  107. }
  108. return $data;
  109. }
  110. /**
  111. * 商品组件:获取拼团商品列表
  112. * @param $user
  113. * @param $item
  114. * @return array
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. * @throws \think\exception\DbException
  118. */
  119. private function getSharingGoodsList($user, $item)
  120. {
  121. // 获取商品数据
  122. $model = new SharingGoodsModel;
  123. if ($item['params']['source'] === 'choice') {
  124. // 数据来源:手动
  125. $goodsIds = array_column($item['data'], 'goods_id');
  126. $goodsList = $model->getListByIdsFromApi($goodsIds, $user);
  127. } else {
  128. // 数据来源:自动
  129. $goodsList = $model->getList([
  130. 'status' => 10,
  131. 'category_id' => $item['params']['auto']['category'],
  132. 'sortType' => $item['params']['auto']['goodsSort'],
  133. 'listRows' => $item['params']['auto']['showNum']
  134. ], $user);
  135. }
  136. if ($goodsList->isEmpty()) return [];
  137. // 格式化商品列表
  138. $data = [];
  139. foreach ($goodsList as $goods) {
  140. $data[] = [
  141. 'goods_id' => $goods['goods_id'],
  142. 'goods_name' => $goods['goods_name'],
  143. 'selling_point' => $goods['selling_point'],
  144. 'people' => $goods['people'],
  145. 'goods_sales' => $goods['goods_sales'],
  146. 'image' => $goods['image'][0]['file_path'],
  147. 'goods_image' => $goods['image'][0]['file_path'],
  148. 'sharing_price' => $goods['sku'][0]['sharing_price'],
  149. 'goods_price' => $goods['sku'][0]['goods_price'],
  150. 'line_price' => $goods['sku'][0]['line_price'],
  151. ];
  152. }
  153. return $data;
  154. }
  155. /**
  156. * 商品组件:获取拼团商品列表
  157. * @param $item
  158. * @return array
  159. * @throws \think\Exception
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. * @throws \think\exception\DbException
  163. */
  164. private function getBargainGoodsList($item)
  165. {
  166. // 获取商品数据
  167. $model = new BargainActiveModel;
  168. if ($item['params']['source'] === 'choice') {
  169. // 数据来源:手动
  170. $activeIds = helper::getArrayColumn($item['data'], 'active_id');
  171. $activeList = $model->getListByIds($activeIds);
  172. } else {
  173. // 数据来源:自动
  174. $activeList = $model->getHallList([
  175. 'sortType' => $item['params']['auto']['goodsSort'],
  176. 'listRows' => $item['params']['auto']['showNum']
  177. ]);
  178. }
  179. if ($activeList->isEmpty()) return [];
  180. // 格式化商品列表
  181. $data = [];
  182. foreach ($activeList as $item) {
  183. $data[] = [
  184. 'active_id' => $item['active_id'],
  185. 'goods_name' => $item['goods']['goods_name'],
  186. 'goods_image' => $item['goods']['goods_image'],
  187. 'floor_price' => $item['floor_price'],
  188. 'original_price' => $item['goods']['goods_sku']['goods_price'],
  189. 'peoples' => $item['peoples'],
  190. 'helps_count' => $item['helps_count'],
  191. 'helps' => $item['helps'],
  192. ];
  193. }
  194. return $data;
  195. }
  196. /**
  197. * 秒杀商品组件:获取秒杀活动
  198. * @param $item
  199. * @return array
  200. * @throws \think\db\exception\DataNotFoundException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. * @throws \think\exception\DbException
  203. */
  204. private function getSharpGoodsList($item)
  205. {
  206. // 获取商品数据
  207. $service = new SharpActiveService;
  208. // 获取秒杀活动及商品列表
  209. return $service->getSharpModular(['limit' => $item['params']['showNum']]);
  210. }
  211. /**
  212. * 优惠券组件:获取优惠券列表
  213. * @param $user
  214. * @param $item
  215. * @return false|\PDOStatement|string|\think\Collection
  216. * @throws \think\db\exception\DataNotFoundException
  217. * @throws \think\db\exception\ModelNotFoundException
  218. * @throws \think\exception\DbException
  219. */
  220. private function getCouponList($user, $item)
  221. {
  222. // 获取优惠券数据
  223. return (new Coupon)->getList($user, $item['params']['limit'], true);
  224. }
  225. /**
  226. * 文章组件:获取文章列表
  227. * @param $item
  228. * @return array
  229. * @throws \think\exception\DbException
  230. */
  231. private function getArticleList($item)
  232. {
  233. // 获取文章数据
  234. $model = new Article;
  235. $articleList = $model->getList($item['params']['auto']['category'], $item['params']['auto']['showNum']);
  236. return $articleList->isEmpty() ? [] : $articleList->toArray()['data'];
  237. }
  238. /**
  239. * 头条快报:获取头条列表
  240. * @param $item
  241. * @return array
  242. * @throws \think\exception\DbException
  243. */
  244. private function getSpecialList($item)
  245. {
  246. // 获取头条数据
  247. $model = new Article;
  248. $articleList = $model->getList($item['params']['auto']['category'], $item['params']['auto']['showNum']);
  249. return $articleList->isEmpty() ? [] : $articleList->toArray()['data'];
  250. }
  251. /**
  252. * 线下门店组件:获取门店列表
  253. * @param $item
  254. * @return array
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\ModelNotFoundException
  257. * @throws \think\exception\DbException
  258. */
  259. private function getShopList($item)
  260. {
  261. // 获取商品数据
  262. $model = new ShopModel;
  263. if ($item['params']['source'] === 'choice') {
  264. // 数据来源:手动
  265. $shopIds = array_column($item['data'], 'shop_id');
  266. $shopList = $model->getListByIds($shopIds);
  267. } else {
  268. // 数据来源:自动
  269. $shopList = $model->getList(null, false, false, $item['params']['auto']['showNum']);
  270. }
  271. if ($shopList->isEmpty()) return [];
  272. // 格式化商品列表
  273. $data = [];
  274. foreach ($shopList as $shop) {
  275. $data[] = [
  276. 'shop_id' => $shop['shop_id'],
  277. 'shop_name' => $shop['shop_name'],
  278. 'logo_image' => $shop['logo']['file_path'],
  279. 'phone' => $shop['phone'],
  280. 'region' => $shop['region'],
  281. 'address' => $shop['address'],
  282. ];
  283. }
  284. return $data;
  285. }
  286. }