| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829 |
- <?php
- namespace bizHd\homePageConfig\classes;
- use bizHd\goods\classes\GoodsClass;
- use bizHd\goods\classes\GoodsCategoryClass;
- use bizHd\goods\classes\GoodsUseCaseClass;
- use common\components\imgUtil;
- use common\components\util;
- use Yii;
- /**
- * 门店首页其余模块配置(金刚区/秒杀/团购/热门/上新/下拉商品)
- * 数据按 mainId 存 Redis,供 hdApp 配置、mallApp 读取展示。
- */
- class HomePageModuleClass
- {
- const REDIS_NAV_GRID = '_home_page_config_nav_grid';
- const REDIS_SECKILL = '_home_page_config_seckill';
- const REDIS_GROUP_BUY = '_home_page_config_group_buy';
- const REDIS_HOT = '_home_page_config_hot';
- const REDIS_NEW = '_home_page_config_new';
- const REDIS_PULL_GOODS = '_home_page_config_pull_goods';
- const NAV_OSS_ROOT = 'uploads_home_nav';
- const MAX_NAV_COUNT = 20;
- /** 关联:商品 */
- const LINK_GOODS = 2;
- /** 关联:分类 */
- const LINK_CATEGORY = 3;
- /** 关联:场景 */
- const LINK_USE_CASE = 4;
- /** 排序:按商品排序 */
- const SORT_PRODUCT = 1;
- /** 排序:按销量 */
- const SORT_SALES = 2;
- /** 排序:按上新时间 */
- const SORT_NEW = 3;
- /** 排列规则合法取值:1排1列/1排2列/1排3列 */
- const LAYOUT_COLS_OPTIONS = [1, 2, 3];
- /** 排列规则默认值 */
- const DEFAULT_LAYOUT_COLS = 3;
- /** 首页展示商品解析默认条数上限 */
- const DISPLAY_GOODS_LIMIT = 20;
- public static function redisKey($mainId, $suffix)
- {
- return intval($mainId) . $suffix;
- }
- public static function getJson($mainId, $suffix)
- {
- $raw = Yii::$app->redis->executeCommand('GET', [self::redisKey($mainId, $suffix)]);
- if (empty($raw)) {
- return [];
- }
- $data = json_decode($raw, true);
- return is_array($data) ? $data : [];
- }
- public static function setJson($mainId, $suffix, $payload)
- {
- Yii::$app->redis->executeCommand('SET', [
- self::redisKey($mainId, $suffix),
- json_encode($payload, JSON_UNESCAPED_UNICODE),
- ]);
- }
- public static function decodeList($raw)
- {
- if (is_string($raw)) {
- $decoded = json_decode($raw, true);
- return is_array($decoded) ? $decoded : [];
- }
- return is_array($raw) ? $raw : [];
- }
- // -------------------- 金刚区 --------------------
- public static function getNavGrid($mainId)
- {
- $mainId = intval($mainId);
- if ($mainId <= 0) {
- util::fail('无效门店');
- }
- $saved = self::getJson($mainId, self::REDIS_NAV_GRID);
- $cols = isset($saved['cols']) ? intval($saved['cols']) : 5;
- if (!in_array($cols, [4, 5], true)) {
- $cols = 5;
- }
- $list = [];
- foreach (self::decodeList($saved['list'] ?? []) as $item) {
- if (!is_array($item)) {
- continue;
- }
- $list[] = [
- 'id' => strval($item['id'] ?? ''),
- 'name' => strval($item['name'] ?? ''),
- 'icon' => strval($item['icon'] ?? ''),
- 'iconType' => intval($item['iconType'] ?? 1),
- 'enabled' => !empty($item['enabled']) ? 1 : 0,
- 'type' => intval($item['type'] ?? self::LINK_CATEGORY),
- 'value' => strval($item['value'] ?? ''),
- ];
- }
- return [
- 'enabled' => HomePageConfigClass::getModuleEnabled($mainId, 'navGrid'),
- 'cols' => $cols,
- 'list' => $list,
- ];
- }
- public static function saveNavGrid($mainId, $data)
- {
- $mainId = intval($mainId);
- if ($mainId <= 0) {
- util::fail('无效门店');
- }
- $enabled = !empty($data['enabled']) ? 1 : 0;
- $cols = isset($data['cols']) ? intval($data['cols']) : 5;
- if (!in_array($cols, [4, 5], true)) {
- util::fail('排列规则无效');
- }
- $rawList = self::decodeList($data['list'] ?? []);
- if (count($rawList) > self::MAX_NAV_COUNT) {
- util::fail('最多可配置' . self::MAX_NAV_COUNT . '个导航');
- }
- $list = [];
- foreach ($rawList as $index => $item) {
- if (!is_array($item)) {
- continue;
- }
- $name = trim(strval($item['name'] ?? ''));
- $icon = trim(strval($item['icon'] ?? ''));
- $type = intval($item['type'] ?? 0);
- $value = trim(strval($item['value'] ?? ''));
- $no = $index + 1;
- if ($name === '' || $icon === '' || $value === '') {
- util::fail("请完善第{$no}个导航项");
- }
- if (!in_array($type, [self::LINK_CATEGORY, self::LINK_USE_CASE], true)) {
- util::fail("第{$no}个导航关联类型无效");
- }
- $list[] = [
- 'id' => strval($item['id'] ?? ('nav_' . ($index + 1))),
- 'name' => mb_substr($name, 0, 20),
- 'icon' => $icon,
- 'iconType' => intval($item['iconType'] ?? 1) === 2 ? 2 : 1,
- 'enabled' => !empty($item['enabled']) ? 1 : 0,
- 'type' => $type,
- 'value' => $value,
- ];
- }
- self::setJson($mainId, self::REDIS_NAV_GRID, ['cols' => $cols, 'list' => $list]);
- HomePageConfigClass::updateModuleEnabled($mainId, 'navGrid', $enabled);
- return true;
- }
- // -------------------- 秒杀 --------------------
- public static function getSeckill($mainId, $refreshStock = true)
- {
- $mainId = intval($mainId);
- if ($mainId <= 0) {
- util::fail('无效门店');
- }
- $saved = self::getJson($mainId, self::REDIS_SECKILL);
- $data = self::normalizeActivityBase($saved);
- $data['enabled'] = HomePageConfigClass::getModuleEnabled($mainId, 'seckill');
- $data['goods'] = self::normalizeSeckillGoods($saved['goods'] ?? [], $mainId, $refreshStock);
- // 按已添加商品数量 + 展开开关动态推导列数与展示数量(不依赖商家手动配置)
- $layout = self::resolveActivityLayout(count($data['goods']), $data['expand']);
- $data['layoutCols'] = $layout['layoutCols'];
- $data['displayCount'] = $layout['displayCount'];
- $data['status'] = self::calcActivityStatus($data['startTime'], $data['endTime'], $data['enabled']);
- return $data;
- }
- public static function saveSeckill($mainId, $data)
- {
- $mainId = intval($mainId);
- if ($mainId <= 0) {
- util::fail('无效门店');
- }
- $base = self::validateActivityBase($data);
- $goods = self::validateSeckillGoods($mainId, self::decodeList($data['goods'] ?? []));
- self::setJson($mainId, self::REDIS_SECKILL, array_merge($base, ['goods' => $goods]));
- HomePageConfigClass::updateModuleEnabled($mainId, 'seckill', !empty($data['enabled']) ? 1 : 0);
- return true;
- }
- /**
- * 校验并规范化秒杀商品;秒杀库存不得超过实际商品库存
- */
- public static function validateSeckillGoods($mainId, $rawList)
- {
- $list = [];
- foreach ($rawList as $index => $item) {
- if (!is_array($item)) {
- continue;
- }
- $no = $index + 1;
- $goodsId = intval($item['goodsId'] ?? 0);
- $price = floatval($item['price'] ?? 0);
- $stock = intval($item['stock'] ?? 0);
- $limit = intval($item['limit'] ?? 0);
- if ($goodsId <= 0) {
- util::fail("请选择第{$no}个秒杀商品");
- }
- if ($price <= 0) {
- util::fail("请填写第{$no}个秒杀价格");
- }
- if ($stock <= 0) {
- util::fail("请填写第{$no}个秒杀库存");
- }
- if ($limit <= 0) {
- util::fail("请填写第{$no}个单人限购");
- }
- $goods = GoodsClass::getById($goodsId, true);
- if (empty($goods) || intval($goods->mainId ?? 0) !== intval($mainId)) {
- util::fail("第{$no}个秒杀商品无效");
- }
- $realStock = intval($goods->stock ?? 0);
- // 秒杀库存不得超过实际库存
- if ($stock > $realStock) {
- util::fail("第{$no}个秒杀库存不能超过商品实际库存({$realStock})");
- }
- $status = !empty($item['status']) ? 1 : 0;
- // 实际库存已低于秒杀库存:自动下架该秒杀商品
- if ($realStock < $stock) {
- $status = 0;
- }
- $list[] = [
- 'goodsId' => $goodsId,
- 'price' => round($price, 2),
- 'stock' => $stock,
- 'limit' => $limit,
- 'status' => $status,
- 'name' => strval($goods->name ?? ($item['name'] ?? '')),
- 'cover' => strval($goods->shortCover ?? ($goods->cover ?? ($item['cover'] ?? ''))),
- 'originPrice' => floatval($goods->price ?? ($item['originPrice'] ?? 0)),
- ];
- }
- return $list;
- }
- /**
- * 读取时刷新秒杀商品状态:实际库存低于秒杀库存则下架,并回写 Redis
- */
- public static function normalizeSeckillGoods($rawList, $mainId, $refreshStock = true)
- {
- $list = [];
- $changed = false;
- foreach (self::decodeList($rawList) as $item) {
- if (!is_array($item) || empty($item['goodsId'])) {
- continue;
- }
- $row = [
- 'goodsId' => intval($item['goodsId']),
- 'price' => floatval($item['price'] ?? 0),
- 'stock' => intval($item['stock'] ?? 0),
- 'limit' => intval($item['limit'] ?? 0),
- 'status' => !empty($item['status']) ? 1 : 0,
- 'name' => strval($item['name'] ?? ''),
- 'cover' => strval($item['cover'] ?? ''),
- 'originPrice' => floatval($item['originPrice'] ?? 0),
- 'realStock' => intval($item['stock'] ?? 0),
- ];
- if ($refreshStock) {
- $goods = GoodsClass::getById($row['goodsId'], true);
- if (!empty($goods) && intval($goods->mainId ?? 0) === intval($mainId)) {
- $realStock = intval($goods->stock ?? 0);
- $row['realStock'] = $realStock;
- $row['name'] = strval($goods->name ?? $row['name']);
- $row['cover'] = strval($goods->shortCover ?? ($goods->cover ?? $row['cover']));
- $row['originPrice'] = floatval($goods->price ?? $row['originPrice']);
- if ($realStock < $row['stock'] && $row['status'] == 1) {
- $row['status'] = 0;
- $changed = true;
- }
- }
- }
- $list[] = $row;
- }
- if ($changed && $refreshStock) {
- $saved = self::getJson($mainId, self::REDIS_SECKILL);
- $persist = [];
- foreach ($list as $g) {
- $persist[] = [
- 'goodsId' => $g['goodsId'],
- 'price' => $g['price'],
- 'stock' => $g['stock'],
- 'limit' => $g['limit'],
- 'status' => $g['status'],
- 'name' => $g['name'],
- 'cover' => $g['cover'],
- 'originPrice' => $g['originPrice'],
- ];
- }
- $saved['goods'] = $persist;
- self::setJson($mainId, self::REDIS_SECKILL, $saved);
- }
- return $list;
- }
- // -------------------- 团购 --------------------
- public static function getGroupBuy($mainId, $refreshStock = true)
- {
- $mainId = intval($mainId);
- if ($mainId <= 0) {
- util::fail('无效门店');
- }
- $saved = self::getJson($mainId, self::REDIS_GROUP_BUY);
- $data = self::normalizeActivityBase($saved);
- $data['enabled'] = HomePageConfigClass::getModuleEnabled($mainId, 'groupBuy');
- $data['goods'] = self::normalizeGroupBuyGoods($saved['goods'] ?? [], $mainId, $refreshStock);
- // 按已添加商品数量 + 展开开关动态推导列数与展示数量(不依赖商家手动配置)
- $layout = self::resolveActivityLayout(count($data['goods']), $data['expand']);
- $data['layoutCols'] = $layout['layoutCols'];
- $data['displayCount'] = $layout['displayCount'];
- $data['status'] = self::calcActivityStatus($data['startTime'], $data['endTime'], $data['enabled']);
- return $data;
- }
- public static function saveGroupBuy($mainId, $data)
- {
- $mainId = intval($mainId);
- if ($mainId <= 0) {
- util::fail('无效门店');
- }
- $base = self::validateActivityBase($data);
- $goods = self::validateGroupBuyGoods($mainId, self::decodeList($data['goods'] ?? []));
- self::setJson($mainId, self::REDIS_GROUP_BUY, array_merge($base, ['goods' => $goods]));
- HomePageConfigClass::updateModuleEnabled($mainId, 'groupBuy', !empty($data['enabled']) ? 1 : 0);
- return true;
- }
- public static function validateGroupBuyGoods($mainId, $rawList)
- {
- $list = [];
- foreach ($rawList as $index => $item) {
- if (!is_array($item)) {
- continue;
- }
- $no = $index + 1;
- $goodsId = intval($item['goodsId'] ?? 0);
- $price = floatval($item['price'] ?? 0);
- $stock = intval($item['stock'] ?? 0);
- $limit = intval($item['limit'] ?? 0);
- $groupSize = intval($item['groupSize'] ?? 3);
- if (!in_array($groupSize, [2, 3, 5], true)) {
- util::fail("第{$no}个成团人数无效");
- }
- if ($goodsId <= 0 || $price <= 0 || $stock <= 0 || $limit <= 0) {
- util::fail("请完善第{$no}个团购商品");
- }
- $goods = GoodsClass::getById($goodsId, true);
- if (empty($goods) || intval($goods->mainId ?? 0) !== intval($mainId)) {
- util::fail("第{$no}个团购商品无效");
- }
- $realStock = intval($goods->stock ?? 0);
- if ($stock > $realStock) {
- util::fail("第{$no}个团购库存不能超过商品实际库存({$realStock})");
- }
- $virtualGroup = !empty($item['virtualGroup']) ? 1 : 0;
- $virtualMinutes = intval($item['virtualMinutes'] ?? 0);
- if ($virtualGroup && $virtualMinutes <= 0) {
- util::fail("请填写第{$no}个虚拟成团时间");
- }
- $list[] = [
- 'goodsId' => $goodsId,
- 'price' => round($price, 2),
- 'stock' => $stock,
- 'limit' => $limit,
- 'groupSize' => $groupSize,
- 'virtualGroup' => $virtualGroup,
- 'virtualMinutes' => $virtualMinutes,
- 'autoRefund' => !empty($item['autoRefund']) ? 1 : 0,
- 'status' => ($realStock < $stock) ? 0 : (!empty($item['status']) ? 1 : 0),
- 'name' => strval($goods->name ?? ($item['name'] ?? '')),
- 'cover' => strval($goods->shortCover ?? ($goods->cover ?? ($item['cover'] ?? ''))),
- 'originPrice' => floatval($goods->price ?? ($item['originPrice'] ?? 0)),
- ];
- }
- return $list;
- }
- public static function normalizeGroupBuyGoods($rawList, $mainId, $refreshStock = true)
- {
- $list = [];
- $changed = false;
- foreach (self::decodeList($rawList) as $item) {
- if (!is_array($item) || empty($item['goodsId'])) {
- continue;
- }
- $row = [
- 'goodsId' => intval($item['goodsId']),
- 'price' => floatval($item['price'] ?? 0),
- 'stock' => intval($item['stock'] ?? 0),
- 'limit' => intval($item['limit'] ?? 0),
- 'groupSize' => intval($item['groupSize'] ?? 3),
- 'virtualGroup' => !empty($item['virtualGroup']) ? 1 : 0,
- 'virtualMinutes' => intval($item['virtualMinutes'] ?? 0),
- 'autoRefund' => !empty($item['autoRefund']) ? 1 : 0,
- 'status' => !empty($item['status']) ? 1 : 0,
- 'name' => strval($item['name'] ?? ''),
- 'cover' => strval($item['cover'] ?? ''),
- 'originPrice' => floatval($item['originPrice'] ?? 0),
- 'realStock' => intval($item['stock'] ?? 0),
- ];
- if ($refreshStock) {
- $goods = GoodsClass::getById($row['goodsId'], true);
- if (!empty($goods) && intval($goods->mainId ?? 0) === intval($mainId)) {
- $realStock = intval($goods->stock ?? 0);
- $row['realStock'] = $realStock;
- $row['name'] = strval($goods->name ?? $row['name']);
- $row['cover'] = strval($goods->shortCover ?? ($goods->cover ?? $row['cover']));
- $row['originPrice'] = floatval($goods->price ?? $row['originPrice']);
- if ($realStock < $row['stock'] && $row['status'] == 1) {
- $row['status'] = 0;
- $changed = true;
- }
- }
- }
- $list[] = $row;
- }
- if ($changed && $refreshStock) {
- $saved = self::getJson($mainId, self::REDIS_GROUP_BUY);
- $persist = [];
- foreach ($list as $g) {
- unset($g['realStock']);
- $persist[] = $g;
- }
- $saved['goods'] = $persist;
- self::setJson($mainId, self::REDIS_GROUP_BUY, $saved);
- }
- return $list;
- }
- // -------------------- 热门/上新/下拉 --------------------
- public static function getGoodsSection($mainId, $moduleKey)
- {
- $mainId = intval($mainId);
- $suffix = self::sectionSuffix($moduleKey);
- $saved = self::getJson($mainId, $suffix);
- $defaults = [
- 'hot' => '热门推荐',
- 'new' => '今日上新',
- 'pullGoods' => '更多商品',
- ];
- return [
- 'enabled' => HomePageConfigClass::getModuleEnabled($mainId, $moduleKey),
- 'name' => strval($saved['name'] ?? ($defaults[$moduleKey] ?? '')),
- 'type' => intval($saved['type'] ?? self::LINK_GOODS),
- 'value' => strval($saved['value'] ?? ''),
- 'sort' => intval($saved['sort'] ?? self::SORT_PRODUCT),
- ];
- }
- public static function saveGoodsSection($mainId, $moduleKey, $data)
- {
- $mainId = intval($mainId);
- $suffix = self::sectionSuffix($moduleKey);
- $name = trim(strval($data['name'] ?? ''));
- $type = intval($data['type'] ?? 0);
- $value = trim(strval($data['value'] ?? ''));
- $sort = intval($data['sort'] ?? self::SORT_PRODUCT);
- // layoutCols/displayCount 由读取时按真实商品数量自动推导,保存时不再接收商家手动配置
- if ($name === '') {
- util::fail('请输入首页展示名称');
- }
- if (mb_strlen($name) > 20) {
- util::fail('展示名称不能超过20字');
- }
- if (!in_array($type, [self::LINK_GOODS, self::LINK_CATEGORY, self::LINK_USE_CASE], true)) {
- util::fail('关联类型无效');
- }
- if ($value === '') {
- util::fail('请选择关联内容');
- }
- if (!in_array($sort, [self::SORT_PRODUCT, self::SORT_SALES, self::SORT_NEW], true)) {
- util::fail('排序规则无效');
- }
- self::setJson($mainId, $suffix, [
- 'name' => $name,
- 'type' => $type,
- 'value' => $value,
- 'sort' => $sort,
- ]);
- HomePageConfigClass::updateModuleEnabled($mainId, $moduleKey, !empty($data['enabled']) ? 1 : 0);
- return true;
- }
- /**
- * 按真实商品总数与模块类型自动推导首页列数与展示数量
- * - 1个商品:1排1列,展示全部
- * - 2个商品:1排2列,展示全部
- * - ≥3个:hot/new 按1排3列且只展示3个;pullGoods 按1排2列且展示全部
- *
- * @param int $goodsTotal 关联配置解析出的真实商品总数
- * @param string $moduleKey hot|new|pullGoods
- * @return array{layoutCols:int,displayCount:int}
- */
- public static function resolveGoodsSectionLayout($goodsTotal, $moduleKey)
- {
- $goodsTotal = intval($goodsTotal);
- if ($goodsTotal <= 1) {
- return ['layoutCols' => 1, 'displayCount' => 0];
- }
- if ($goodsTotal === 2) {
- return ['layoutCols' => 2, 'displayCount' => 0];
- }
- if ($moduleKey === 'pullGoods') {
- return ['layoutCols' => 2, 'displayCount' => 0];
- }
- return ['layoutCols' => 3, 'displayCount' => 3];
- }
- /**
- * 规范化排列规则取值,非法/缺省时回退默认值
- */
- public static function normalizeLayoutCols($cols)
- {
- $cols = intval($cols);
- return in_array($cols, self::LAYOUT_COLS_OPTIONS, true) ? $cols : self::DEFAULT_LAYOUT_COLS;
- }
- /**
- * 将热门推荐/今日上新/下拉商品的关联配置(type+value)解析为真实商品列表,用于首页展示
- * type=2 商品:value 为商品ID逗号拼接,按选择顺序展示
- * type=3 分类:value 为分类ID逗号拼接,取分类下全部商品
- * type=4 场景:value 为场景ID逗号拼接,取场景下全部商品
- * sort=1 按上面解析出的原始顺序;sort=2 按销量(虚拟+实际)降序;sort=3 按上架时间降序
- *
- * @param int $mainId
- * @param int $type
- * @param string $value
- * @param int $sort
- * @param int $limit
- * @return array [{id,name,price,stock,cover,coverUrl,sold}]
- */
- public static function resolveDisplayGoods($mainId, $type, $value, $sort, $limit = self::DISPLAY_GOODS_LIMIT)
- {
- $paged = self::resolveDisplayGoodsPaged($mainId, $type, $value, $sort, 1, $limit);
- return $paged['list'];
- }
- /**
- * 匹配关联配置下的全部商品行(未截断、未格式化封面URL),供分页与总数统计复用
- *
- * @param int $mainId
- * @param int $type
- * @param string $value
- * @param int $sort
- * @return array
- */
- public static function matchGoodsRows($mainId, $type, $value, $sort)
- {
- $mainId = intval($mainId);
- $type = intval($type);
- $value = trim(strval($value));
- if ($mainId <= 0 || $value === '') {
- return [];
- }
- $ids = array_values(array_unique(array_filter(array_map('intval', explode(',', $value)))));
- if (empty($ids)) {
- return [];
- }
- $goodsIds = [];
- if ($type === self::LINK_GOODS) {
- // 商品类型:保留用户选择顺序
- $goodsIds = $ids;
- } elseif ($type === self::LINK_CATEGORY) {
- $rows = GoodsCategoryClass::getAllByCondition(
- ['cId' => ['in', $ids], 'delStatus' => 0],
- null,
- 'gId'
- );
- $goodsIds = array_values(array_unique(array_map('intval', array_column($rows, 'gId'))));
- } elseif ($type === self::LINK_USE_CASE) {
- $rows = GoodsUseCaseClass::getAllByCondition(
- ['useCaseId' => ['in', $ids]],
- null,
- 'goodsId'
- );
- $goodsIds = array_values(array_unique(array_map('intval', array_column($rows, 'goodsId'))));
- }
- if (empty($goodsIds)) {
- return [];
- }
- // 注意:Base::getByIds 传入 $order 会走到未定义的 order() 方法而报错,这里统一取回后在 PHP 侧排序
- $rows = GoodsClass::getByIds($goodsIds, null, null, 'id,name,price,stock,cover,sold,actualSold,status,delStatus,masterId,mainId,createTime');
- // 仅取当前门店、未删除、上架中的主规格商品
- $rows = array_values(array_filter($rows, function ($row) use ($mainId) {
- return intval($row['mainId'] ?? 0) === $mainId
- && intval($row['delStatus'] ?? 0) === 0
- && intval($row['status'] ?? 0) === 1
- && intval($row['masterId'] ?? 0) === 0;
- }));
- if ($sort == self::SORT_SALES) {
- usort($rows, function ($a, $b) {
- $soldA = floatval($a['actualSold'] ?? 0) + floatval($a['sold'] ?? 0);
- $soldB = floatval($b['actualSold'] ?? 0) + floatval($b['sold'] ?? 0);
- return $soldB <=> $soldA;
- });
- } elseif ($sort == self::SORT_NEW) {
- usort($rows, function ($a, $b) {
- return strtotime($b['createTime'] ?? '') <=> strtotime($a['createTime'] ?? '');
- });
- } elseif ($type === self::LINK_GOODS) {
- // 商品类型按用户选择顺序重新排列;分类/场景按商品排序时无自定义顺序可依,保持数据库默认返回顺序
- $indexBy = [];
- foreach ($rows as $row) {
- $indexBy[intval($row['id'])] = $row;
- }
- $ordered = [];
- foreach ($goodsIds as $gid) {
- if (isset($indexBy[$gid])) {
- $ordered[] = $indexBy[$gid];
- }
- }
- $rows = $ordered;
- }
- return $rows;
- }
- /**
- * 将原始商品行格式化为首页/列表展示结构(含 coverUrl)
- *
- * @param array $rows
- * @return array
- */
- public static function formatGoodsRows($rows)
- {
- $list = [];
- $goodsIds = [];
- foreach ($rows as $row) {
- $goodsIds[] = intval($row['id'] ?? 0);
- }
- $specEnabledMap = GoodsClass::getSpecEnabledMap($goodsIds);
- foreach ($rows as $row) {
- $id = intval($row['id']);
- $cover = strval($row['cover'] ?? '');
- $coverUrl = $cover !== '' ? imgUtil::groupImg($cover) . '?x-oss-process=image/resize,m_fill,h_700,w_700' : '';
- $list[] = [
- 'id' => $id,
- 'name' => strval($row['name'] ?? ''),
- 'price' => floatval($row['price'] ?? 0),
- 'stock' => intval($row['stock'] ?? 0),
- 'cover' => $cover,
- 'coverUrl' => $coverUrl,
- 'sold' => intval(bcadd($row['actualSold'] ?? 0, $row['sold'] ?? 0)),
- 'specEnabled' => intval($specEnabledMap[$id] ?? 0),
- ];
- }
- return $list;
- }
- /**
- * 分页解析关联配置下的商品列表,返回 list + total(真实总数,不受首页展示上限限制)
- *
- * @param int $mainId
- * @param int $type
- * @param string $value
- * @param int $sort
- * @param int $page
- * @param int $pageSize 传 0 表示不分页返回全部
- * @return array {list, total}
- */
- public static function resolveDisplayGoodsPaged($mainId, $type, $value, $sort, $page = 1, $pageSize = self::DISPLAY_GOODS_LIMIT)
- {
- $rows = self::matchGoodsRows($mainId, $type, $value, $sort);
- $total = count($rows);
- $page = max(1, intval($page));
- $pageSize = intval($pageSize);
- if ($pageSize > 0) {
- $rows = array_slice($rows, ($page - 1) * $pageSize, $pageSize);
- }
- return [
- 'list' => self::formatGoodsRows($rows),
- 'total' => $total,
- ];
- }
- /**
- * 根据 displayCount 计算首页实际截断条数:1-10 用配置值,0(全部)回退默认上限
- * 供秒杀/团购等活动模块使用
- *
- * @param mixed $displayCount
- * @return int
- */
- public static function resolveHomeDisplayLimit($displayCount)
- {
- $count = intval($displayCount);
- if ($count >= 1 && $count <= 10) {
- return $count;
- }
- return self::DISPLAY_GOODS_LIMIT;
- }
- public static function sectionSuffix($moduleKey)
- {
- $map = [
- 'hot' => self::REDIS_HOT,
- 'new' => self::REDIS_NEW,
- 'pullGoods' => self::REDIS_PULL_GOODS,
- ];
- if (!isset($map[$moduleKey])) {
- util::fail('无效模块');
- }
- return $map[$moduleKey];
- }
- // -------------------- 活动公共 --------------------
- public static function normalizeActivityBase($saved)
- {
- return [
- 'title' => strval($saved['title'] ?? ''),
- 'subtitle' => strval($saved['subtitle'] ?? ''),
- 'showCountdown' => !empty($saved['showCountdown']) ? 1 : 0,
- // 商品展开:开启后首页按1排1列展示全部商品,默认关闭
- 'expand' => !empty($saved['expand']) ? 1 : 0,
- 'startTime' => intval($saved['startTime'] ?? 0),
- 'endTime' => intval($saved['endTime'] ?? 0),
- 'desc' => strval($saved['desc'] ?? ''),
- ];
- }
- /**
- * 按已添加商品数量与「商品展开」开关自动推导首页列数与展示数量
- * - expand 开启:1排1列,展示全部
- * - 1个商品:1排1列;2个:1排2列;≥3个:1排3列且只展示3个
- *
- * @param int $goodsCount 已添加商品总数(含未上架)
- * @param int $expand 商品展开开关 0/1
- * @return array{layoutCols:int,displayCount:int}
- */
- public static function resolveActivityLayout($goodsCount, $expand)
- {
- if (!empty($expand)) {
- return ['layoutCols' => 1, 'displayCount' => 0];
- }
- $goodsCount = intval($goodsCount);
- if ($goodsCount <= 1) {
- return ['layoutCols' => 1, 'displayCount' => 0];
- }
- if ($goodsCount === 2) {
- return ['layoutCols' => 2, 'displayCount' => 0];
- }
- return ['layoutCols' => 3, 'displayCount' => 3];
- }
- public static function validateActivityBase($data)
- {
- $title = trim(strval($data['title'] ?? ''));
- $subtitle = trim(strval($data['subtitle'] ?? ''));
- $desc = trim(strval($data['desc'] ?? ''));
- $startTime = intval($data['startTime'] ?? 0);
- $endTime = intval($data['endTime'] ?? 0);
- // layoutCols/displayCount 由读取时按商品数量自动推导,保存时不再接收商家手动配置
- if ($title === '') {
- util::fail('请输入活动标题');
- }
- if (mb_strlen($title) > 10) {
- util::fail('活动标题不能超过10字');
- }
- if ($subtitle === '') {
- util::fail('请输入活动副标题');
- }
- if (mb_strlen($subtitle) > 20) {
- util::fail('活动副标题不能超过20字');
- }
- if ($startTime <= 0 || $endTime <= 0) {
- util::fail('请选择活动时间');
- }
- if ($endTime <= $startTime) {
- util::fail('结束时间必须大于开始时间');
- }
- if ($desc === '') {
- util::fail('请输入活动说明');
- }
- if (mb_strlen($desc) > 500) {
- util::fail('活动说明不能超过500字');
- }
- return [
- 'title' => $title,
- 'subtitle' => $subtitle,
- 'showCountdown' => !empty($data['showCountdown']) ? 1 : 0,
- 'expand' => !empty($data['expand']) ? 1 : 0,
- 'startTime' => $startTime,
- 'endTime' => $endTime,
- 'desc' => $desc,
- ];
- }
- /**
- * 活动状态:0未开始 1进行中 2已结束;模块关闭视为已结束展示用
- */
- public static function calcActivityStatus($startTime, $endTime, $enabled)
- {
- if (empty($enabled)) {
- return 2;
- }
- $now = time();
- $startTime = intval($startTime);
- $endTime = intval($endTime);
- if ($startTime <= 0 || $endTime <= 0) {
- return 0;
- }
- if ($now < $startTime) {
- return 0;
- }
- if ($now > $endTime) {
- return 2;
- }
- return 1;
- }
- }
|