| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace mall\controllers;
- use biz\shop\classes\ShopClass;
- use bizHd\custom\classes\CustomClass;
- use bizHd\homePageConfig\classes\HomePageConfigClass;
- use bizHd\homePageConfig\classes\HomePageDisplayClass;
- use bizHd\homePageConfig\classes\HomePageModuleClass;
- use bizMall\hd\classes\HdClass;
- use common\components\util;
- use mall\models\homePageConfig\GetSectionGoodsForm;
- use Yii;
- /**
- * 商城端首页配置读取接口
- * mallApp 通过 account(shopId) 获取配置,与 hd 管理端共用 Redis。
- * 具体的格式化/真实商品解析逻辑统一收敛在 HomePageDisplayClass,
- * 保证与 app-hd 的预览接口返回结构完全一致。
- * 登录态会解析/创建顾客与门店的 hd 绑定,把 hdId 带回给 mallApp 缓存并拼到跳转链接。
- */
- class HomePageConfigController extends BaseController
- {
- public $guestAccess = [
- 'get-banner',
- 'get-nav-grid',
- 'get-seckill',
- 'get-group-buy',
- 'get-hot',
- 'get-new',
- 'get-pull-goods',
- 'get-home',
- 'get-section-goods',
- ];
- /**
- * 一次拉取首页全部模块配置,便于 mallApp 首页组装。
- * 已登录情况下,会解析当前门店的 hd 绑定:已有则直接返回 hdId,没有则建客建 hd。
- */
- public function actionGetHome()
- {
- $shopId = intval($this->shopId);
- if ($shopId <= 0) {
- util::fail('shopId不存在');
- }
- $shop = $this->shop;
- if (empty($shop)) {
- // BaseController 在请求 hdId 与当前用户不匹配时会清空 shop,这里按 account 重新取门店
- $shop = ShopClass::getById($shopId, true);
- }
- if (empty($shop)) {
- util::fail('无效门店');
- }
- $mainId = intval($this->mainId);
- if ($mainId <= 0) {
- $mainId = intval($shop->mainId);
- }
- if ($mainId <= 0) {
- util::fail('mainId不存在');
- }
- $hdId = 0;
- $inviteBound = 0;
- if (intval($this->userId) > 0) {
- $resolved = $this->resolveLoggedInHd($shop, $shopId);
- $hdId = $resolved['hdId'];
- $inviteBound = $resolved['inviteBound'];
- }
- $re = HomePageDisplayClass::buildHome($mainId, $shopId);
- $re['hdId'] = $hdId;
- $re['inviteBound'] = $inviteBound;
- util::success($re);
- }
- public function actionGetBanner()
- {
- util::success(HomePageDisplayClass::formatBanner(HomePageConfigClass::getBanner($this->requireMainId(), $this->requireShopId())));
- }
- public function actionGetNavGrid()
- {
- util::success(HomePageDisplayClass::formatNavGrid(HomePageModuleClass::getNavGrid($this->requireMainId(), $this->requireShopId())));
- }
- public function actionGetSeckill()
- {
- util::success(HomePageDisplayClass::formatActivity(HomePageModuleClass::getSeckill($this->requireMainId(), $this->requireShopId(), true)));
- }
- public function actionGetGroupBuy()
- {
- util::success(HomePageDisplayClass::formatActivity(HomePageModuleClass::getGroupBuy($this->requireMainId(), $this->requireShopId(), true)));
- }
- public function actionGetHot()
- {
- util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'hot'));
- }
- public function actionGetNew()
- {
- util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'new'));
- }
- public function actionGetPullGoods()
- {
- util::success(HomePageDisplayClass::formatGoodsSection($this->requireMainId(), $this->requireShopId(), 'pullGoods'));
- }
- /**
- * 首页模块「更多」商品列表分页
- * moduleKey: seckill|groupBuy|hot|new|pullGoods
- */
- public function actionGetSectionGoods()
- {
- $form = new GetSectionGoodsForm();
- $form->loadAndValidate();
- util::success(HomePageDisplayClass::getSectionGoodsPage(
- $this->requireMainId(),
- $this->requireShopId(),
- $form->moduleKey,
- (int)$form->page,
- (int)$form->pageSize
- ));
- }
- /**
- * 登录态解析当前门店 hdId。
- * 已有 xhHd(门店已绑定该顾客)则直接取 id;没有则走 CustomClass::buildRelation,
- * 与 ShopController::actionInfo 在无 hdId 时的建客路径一致,避免首页先建客却漏掉分享拉新。
- * @param object|null $shop 门店对象,建客时必须有
- * @param int $shopId 门店 id(xhShop.id)
- * @return array{hdId:int,inviteBound:int}
- * @throws \yii\db\IntegrityException
- */
- private function resolveLoggedInHd($shop, $shopId)
- {
- $hd = HdClass::getByCondition(
- ['shopId' => $shopId, 'userId' => $this->userId],
- false,
- null,
- 'id, customId'
- );
- if (!empty($hd)) {
- return [
- 'hdId' => intval($hd['id']),
- 'inviteBound' => 0,
- ];
- }
- $user = $this->user;
- if (empty($shop) || empty($user)) {
- return ['hdId' => 0, 'inviteBound' => 0];
- }
- $inviterCustomId = (int)Yii::$app->request->get('inviterCustomId', 0);
- $respond = CustomClass::buildRelation($shop, $user, $inviterCustomId);
- if (empty($respond) || empty($respond['hd'])) {
- return ['hdId' => 0, 'inviteBound' => 0];
- }
- $hd = $respond['hd'];
- return [
- 'hdId' => intval(is_object($hd) ? $hd->id : ($hd['id'] ?? 0)),
- 'inviteBound' => !empty($respond['inviteBound']) ? (int)$respond['inviteBound'] : 0,
- ];
- }
- private function requireMainId()
- {
- $mainId = intval($this->mainId);
- if ($mainId <= 0) {
- util::fail('无效mainId');
- }
- return $mainId;
- }
- private function requireShopId()
- {
- $shopId = intval($this->shopId);
- if ($shopId <= 0) {
- util::fail('无效门店');
- }
- return $shopId;
- }
- }
|