HomePageConfigController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace mall\controllers;
  3. use bizHd\homePageConfig\classes\HomePageConfigClass;
  4. use bizHd\homePageConfig\classes\HomePageDisplayClass;
  5. use bizHd\homePageConfig\classes\HomePageModuleClass;
  6. use common\components\util;
  7. use mall\models\homePageConfig\GetSectionGoodsForm;
  8. /**
  9. * 商城端首页配置读取接口
  10. * mallApp 通过 account(shopId) 获取配置,与 hd 管理端共用 Redis。
  11. * 具体的格式化/真实商品解析逻辑统一收敛在 HomePageDisplayClass,
  12. * 保证与 app-hd 的预览接口返回结构完全一致。
  13. * 登录态会解析/创建顾客与门店的 hd 绑定,把 hdId 带回给 mallApp 缓存并拼到跳转链接。
  14. */
  15. class HomePageConfigController extends BaseController
  16. {
  17. public $guestAccess = [
  18. 'get-banner',
  19. 'get-nav-grid',
  20. 'get-seckill',
  21. 'get-group-buy',
  22. 'get-hot',
  23. 'get-new',
  24. 'get-pull-goods',
  25. 'get-home',
  26. 'get-section-goods',
  27. ];
  28. /**
  29. * 一次拉取首页全部模块配置,便于 mallApp 首页组装。
  30. * 已登录情况下,会解析当前门店的 hd 绑定:已有则直接返回 hdId,没有则建客建 hd。
  31. */
  32. public function actionGetHome()
  33. {
  34. // 换店/分享直达时请求可能仍带旧 hdId,先按 account 恢复门店再解析当前店绑定
  35. $ctx = $this->recoverShopAndResolveHd();
  36. $re = HomePageDisplayClass::buildHome($ctx['mainId'], $ctx['shopId']);
  37. $re['hdId'] = $ctx['hdId'];
  38. $re['inviteBound'] = $ctx['inviteBound'];
  39. util::success($re);
  40. }
  41. public function actionGetBanner()
  42. {
  43. util::success(HomePageDisplayClass::formatBanner(HomePageConfigClass::getBanner($this->requireMainId(), $this->requireShopId())));
  44. }
  45. public function actionGetNavGrid()
  46. {
  47. util::success(HomePageDisplayClass::formatNavGrid(HomePageModuleClass::getNavGrid($this->requireMainId(), $this->requireShopId())));
  48. }
  49. public function actionGetSeckill()
  50. {
  51. util::success(HomePageDisplayClass::formatActivity(HomePageModuleClass::getSeckill($this->requireMainId(), $this->requireShopId(), true)));
  52. }
  53. public function actionGetGroupBuy()
  54. {
  55. util::success(HomePageDisplayClass::formatActivity(HomePageModuleClass::getGroupBuy($this->requireMainId(), $this->requireShopId(), true)));
  56. }
  57. public function actionGetHot()
  58. {
  59. util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'hot'));
  60. }
  61. public function actionGetNew()
  62. {
  63. util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'new'));
  64. }
  65. public function actionGetPullGoods()
  66. {
  67. util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'pullGoods'));
  68. }
  69. /**
  70. * 首页模块「更多」商品列表分页
  71. * moduleKey: seckill|groupBuy|hot|new|pullGoods
  72. */
  73. public function actionGetSectionGoods()
  74. {
  75. $form = new GetSectionGoodsForm();
  76. $form->loadAndValidate();
  77. util::success(HomePageDisplayClass::getSectionGoodsPage(
  78. $this->requireMainId(),
  79. $this->requireShopId(),
  80. $form->moduleKey,
  81. (int)$form->page,
  82. (int)$form->pageSize
  83. ));
  84. }
  85. private function requireMainId()
  86. {
  87. $mainId = intval($this->mainId);
  88. if ($mainId <= 0) {
  89. util::fail('无效mainId');
  90. }
  91. return $mainId;
  92. }
  93. private function requireShopId()
  94. {
  95. $shopId = intval($this->shopId);
  96. if ($shopId <= 0) {
  97. util::fail('无效门店');
  98. }
  99. return $shopId;
  100. }
  101. }