HomePageConfigController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\homePageConfig\classes\HomePageConfigClass;
  6. use bizHd\homePageConfig\classes\HomePageDisplayClass;
  7. use bizHd\homePageConfig\classes\HomePageModuleClass;
  8. use bizMall\hd\classes\HdClass;
  9. use common\components\util;
  10. use mall\models\homePageConfig\GetSectionGoodsForm;
  11. use Yii;
  12. /**
  13. * 商城端首页配置读取接口
  14. * mallApp 通过 account(shopId) 获取配置,与 hd 管理端共用 Redis。
  15. * 具体的格式化/真实商品解析逻辑统一收敛在 HomePageDisplayClass,
  16. * 保证与 app-hd 的预览接口返回结构完全一致。
  17. * 登录态会解析/创建顾客与门店的 hd 绑定,把 hdId 带回给 mallApp 缓存并拼到跳转链接。
  18. */
  19. class HomePageConfigController extends BaseController
  20. {
  21. public $guestAccess = [
  22. 'get-banner',
  23. 'get-nav-grid',
  24. 'get-seckill',
  25. 'get-group-buy',
  26. 'get-hot',
  27. 'get-new',
  28. 'get-pull-goods',
  29. 'get-home',
  30. 'get-section-goods',
  31. ];
  32. /**
  33. * 一次拉取首页全部模块配置,便于 mallApp 首页组装。
  34. * 已登录情况下,会解析当前门店的 hd 绑定:已有则直接返回 hdId,没有则建客建 hd。
  35. */
  36. public function actionGetHome()
  37. {
  38. $shopId = intval($this->shopId);
  39. if ($shopId <= 0) {
  40. util::fail('shopId不存在');
  41. }
  42. $shop = $this->shop;
  43. if (empty($shop)) {
  44. // BaseController 在请求 hdId 与当前用户不匹配时会清空 shop,这里按 account 重新取门店
  45. $shop = ShopClass::getById($shopId, true);
  46. }
  47. if (empty($shop)) {
  48. util::fail('无效门店');
  49. }
  50. $mainId = intval($this->mainId);
  51. if ($mainId <= 0) {
  52. $mainId = intval($shop->mainId);
  53. }
  54. if ($mainId <= 0) {
  55. util::fail('mainId不存在');
  56. }
  57. $hdId = 0;
  58. $inviteBound = 0;
  59. if (intval($this->userId) > 0) {
  60. $resolved = $this->resolveLoggedInHd($shop, $shopId);
  61. $hdId = $resolved['hdId'];
  62. $inviteBound = $resolved['inviteBound'];
  63. }
  64. $re = HomePageDisplayClass::buildHome($mainId, $shopId);
  65. $re['hdId'] = $hdId;
  66. $re['inviteBound'] = $inviteBound;
  67. util::success($re);
  68. }
  69. public function actionGetBanner()
  70. {
  71. util::success(HomePageDisplayClass::formatBanner(HomePageConfigClass::getBanner($this->requireMainId(), $this->requireShopId())));
  72. }
  73. public function actionGetNavGrid()
  74. {
  75. util::success(HomePageDisplayClass::formatNavGrid(HomePageModuleClass::getNavGrid($this->requireMainId(), $this->requireShopId())));
  76. }
  77. public function actionGetSeckill()
  78. {
  79. util::success(HomePageDisplayClass::formatActivity(HomePageModuleClass::getSeckill($this->requireMainId(), $this->requireShopId(), true)));
  80. }
  81. public function actionGetGroupBuy()
  82. {
  83. util::success(HomePageDisplayClass::formatActivity(HomePageModuleClass::getGroupBuy($this->requireMainId(), $this->requireShopId(), true)));
  84. }
  85. public function actionGetHot()
  86. {
  87. util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'hot'));
  88. }
  89. public function actionGetNew()
  90. {
  91. util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'new'));
  92. }
  93. public function actionGetPullGoods()
  94. {
  95. util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'pullGoods'));
  96. }
  97. /**
  98. * 首页模块「更多」商品列表分页
  99. * moduleKey: seckill|groupBuy|hot|new|pullGoods
  100. */
  101. public function actionGetSectionGoods()
  102. {
  103. $form = new GetSectionGoodsForm();
  104. $form->loadAndValidate();
  105. util::success(HomePageDisplayClass::getSectionGoodsPage(
  106. $this->requireMainId(),
  107. $this->requireShopId(),
  108. $form->moduleKey,
  109. (int)$form->page,
  110. (int)$form->pageSize
  111. ));
  112. }
  113. /**
  114. * 登录态解析当前门店 hdId。
  115. * 已有 xhHd(门店已绑定该顾客)则直接取 id;没有则走 CustomClass::buildRelation,
  116. * 与 ShopController::actionInfo 在无 hdId 时的建客路径一致,避免首页先建客却漏掉分享拉新。
  117. * @param object|null $shop 门店对象,建客时必须有
  118. * @param int $shopId 门店 id(xhShop.id)
  119. * @return array{hdId:int,inviteBound:int}
  120. * @throws \yii\db\IntegrityException
  121. */
  122. private function resolveLoggedInHd($shop, $shopId)
  123. {
  124. $hd = HdClass::getByCondition(
  125. ['shopId' => $shopId, 'userId' => $this->userId],
  126. false,
  127. null,
  128. 'id, customId'
  129. );
  130. if (!empty($hd)) {
  131. return [
  132. 'hdId' => intval($hd['id']),
  133. 'inviteBound' => 0,
  134. ];
  135. }
  136. $user = $this->user;
  137. if (empty($shop) || empty($user)) {
  138. return ['hdId' => 0, 'inviteBound' => 0];
  139. }
  140. $inviterCustomId = (int)Yii::$app->request->get('inviterCustomId', 0);
  141. $respond = CustomClass::buildRelation($shop, $user, $inviterCustomId);
  142. if (empty($respond) || empty($respond['hd'])) {
  143. return ['hdId' => 0, 'inviteBound' => 0];
  144. }
  145. $hd = $respond['hd'];
  146. return [
  147. 'hdId' => intval(is_object($hd) ? $hd->id : ($hd['id'] ?? 0)),
  148. 'inviteBound' => !empty($respond['inviteBound']) ? (int)$respond['inviteBound'] : 0,
  149. ];
  150. }
  151. private function requireMainId()
  152. {
  153. $mainId = intval($this->mainId);
  154. if ($mainId <= 0) {
  155. util::fail('无效mainId');
  156. }
  157. return $mainId;
  158. }
  159. private function requireShopId()
  160. {
  161. $shopId = intval($this->shopId);
  162. if ($shopId <= 0) {
  163. util::fail('无效门店');
  164. }
  165. return $shopId;
  166. }
  167. }