WxappPage.php 11 KB

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