From 1085ba14e84a1e006d1af0f369c25480fabf113c Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Thu, 27 Mar 2025 15:40:24 +0800 Subject: [PATCH] feat : banner --- app/Controller/Api/SystemController.php | 10 +++++ app/Service/Admin/BannerService.php | 7 +++ app/Service/Api/Good/OptionalListService.php | 45 +++++++++++++++++++- app/Service/Api/IndexService.php | 19 +++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 app/Service/Api/IndexService.php diff --git a/app/Controller/Api/SystemController.php b/app/Controller/Api/SystemController.php index d833a4c..7ae93e0 100644 --- a/app/Controller/Api/SystemController.php +++ b/app/Controller/Api/SystemController.php @@ -6,11 +6,13 @@ namespace App\Controller\Api; use App\Controller\AbstractController; use App\Middleware\Api\JwtAuthMiddleware; +use App\Service\Api\IndexService; use App\Service\Api\System\CityListService; use App\Service\Api\System\MiniWxConfigService; use App\Service\Api\System\SiteListService; use App\Service\Api\System\SystemConfigService; use Hyperf\HttpServer\Annotation\Controller; +use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middlewares; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\Validation\Annotation\Scene; @@ -46,4 +48,12 @@ class SystemController extends AbstractController { return (new SystemConfigService)->handle(); } + + #[RequestMapping(path: 'index',methods: 'GET')] + #[Scene(scene: 'index')] + #[Middleware(JwtAuthMiddleware::class)] + public function index() + { + return (new IndexService)->handle(); + } } diff --git a/app/Service/Admin/BannerService.php b/app/Service/Admin/BannerService.php index e71b0ef..db1327a 100644 --- a/app/Service/Admin/BannerService.php +++ b/app/Service/Admin/BannerService.php @@ -60,6 +60,8 @@ class BannerService extends BaseService $insertModel->type = $this->request->input('type'); $insertModel->value = $this->request->input('value'); + $this->updateOssObjects([$insertModel->image_id]); + if (!$insertModel->save()) throw new ErrException('添加错误'); return $this->return->success(); @@ -89,6 +91,11 @@ class BannerService extends BaseService $info->type = $this->request->input('type'); $info->value = $this->request->input('value'); + if ($info->image_id != $this->request->input('image_id')) { + $this->updateOssObjectsDisable([$info->image_id]); + $this->updateOssObjects([$this->request->input('image_id')]); + } + if (!$info->save()) throw new ErrException('修改错误'); return $this->return->success(); diff --git a/app/Service/Api/Good/OptionalListService.php b/app/Service/Api/Good/OptionalListService.php index 08d73f6..b91705f 100644 --- a/app/Service/Api/Good/OptionalListService.php +++ b/app/Service/Api/Good/OptionalListService.php @@ -14,8 +14,11 @@ use App\Cache\Redis\Api\ApiRedisKey; use App\Cache\Redis\Api\GoodCache; use App\Cache\Redis\Api\SiteCache; use App\Cache\Redis\RedisCache; +use App\Exception\ErrException; +use App\Model\Category; use App\Service\Api\BaseService; use App\Service\ServiceTrait\Common\CycleTrait; +use App\Service\ServiceTrait\Common\OssTrait; use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -42,6 +45,12 @@ class OptionalListService extends BaseService #[Inject] protected RedisCache $redisCache; + /** + * @var Category + */ + #[Inject] + protected Category $categoryModel; + /** * @return array * @throws ContainerExceptionInterface @@ -63,13 +72,45 @@ class OptionalListService extends BaseService if (empty($data)) return $this->return->success('success', ['list' => []]); - $stockKey = ApiRedisKey::goodStockKey((int)$cycleId,(int)$siteInfo['kitchen_id']); + $res = $this->buildData($data); + + return $this->return->success('success', ['list' => $res]); + } + + use OssTrait; + + /** + * @param $data + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + private function buildData($data): array + { + + $categoryIds = $this->categoryModel->whereIn('id',array_column($data,'category_id'))->get(); + if (empty($categoryIds)) throw new ErrException('数据错误'); + $categoryList = array_column($categoryIds->toArray(),null, 'id'); + $categoryImages = $this->getOssObjects(array_keys($categoryList)); + + $res = []; + $stockKey = ApiRedisKey::goodStockKey($this->goodCache->cycleId, $this->goodCache->kitchenId); foreach ($data as &$item) { foreach ($item['sku_list'] as &$v) { $v['surplus_stock'] = $this->redisCache->zScore($stockKey,$v['id']) ?? 0; } + + if (empty($res[$item['category_id']])) { + $res[$item['category_id']] = [ + 'category_name' => $categoryList[$item['category_id']]['name'] ?? '', + 'category_image' => $categoryImages[$item['category_id']]['url'] ?? '', + 'spu_list' => [] + ]; + } + + $res[$item['category_id']]['spu_list'][] = $item; } - return $this->return->success('success', ['list' => $data]); + return $res; } } \ No newline at end of file diff --git a/app/Service/Api/IndexService.php b/app/Service/Api/IndexService.php new file mode 100644 index 0000000..938f60c --- /dev/null +++ b/app/Service/Api/IndexService.php @@ -0,0 +1,19 @@ +