diff --git a/app/Constants/Common/BannerCode.php b/app/Constants/Common/BannerCode.php new file mode 100644 index 0000000..82d4bdd --- /dev/null +++ b/app/Constants/Common/BannerCode.php @@ -0,0 +1,12 @@ +handle(); } diff --git a/app/Service/Api/Good/OptionalListService.php b/app/Service/Api/Good/OptionalListService.php index b91705f..874ce7a 100644 --- a/app/Service/Api/Good/OptionalListService.php +++ b/app/Service/Api/Good/OptionalListService.php @@ -111,6 +111,6 @@ class OptionalListService extends BaseService $res[$item['category_id']]['spu_list'][] = $item; } - return $res; + return array_values($res); } } \ No newline at end of file diff --git a/app/Service/Api/IndexService.php b/app/Service/Api/IndexService.php index 938f60c..2f98ebe 100644 --- a/app/Service/Api/IndexService.php +++ b/app/Service/Api/IndexService.php @@ -10,10 +10,82 @@ declare(strict_types=1); namespace App\Service\Api; +use App\Constants\Common\BannerCode; +use App\Constants\Common\CouponCode; +use App\Model\Banner; +use App\Model\UserCoupon; +use App\Service\ServiceTrait\Api\GetUserInfoTrait; +use App\Service\ServiceTrait\Common\OssTrait; +use Hyperf\Di\Annotation\Inject; + class IndexService extends BaseService { - public function handle() + use OssTrait; + use GetUserInfoTrait; + + /** + * @return array + */ + public function handle(): array { - + $userInfo = $this->getUserInfo($this->userId); + + $avatar = $this->getOssObjectById($userInfo->avatar_id); + + $res = [ + 'banner' => $this->getBanner(), + 'nickname' => $userInfo->nickname, + 'avatar' => $avatar, + 'point' => 0, + 'coupon_num' => $this->getCouponNum() + ]; + + return $this->return->success('success',$res); + } + + /** + * @return int + */ + private function getCouponNum(): int + { + return $this->userCouponModel + ->where('user_id',$this->userId) + ->where('status',CouponCode::COUPON_STATUS_UNUSED) + ->where('validity_end_time','<',date('Y-m-d H:i:s')) + ->count() ?? 0; + } + + /** + * @var Banner + */ + #[Inject] + protected Banner $bannerModel; + + /** + * @var UserCoupon + */ + #[Inject] + private UserCoupon $userCouponModel; + + /** + * @return array + */ + private function getBanner(): array + { + $res = $this->bannerModel + ->where('city_id',$this->request->input('city_id')) + ->where('status',BannerCode::DISPLAYED) + ->orderBy('sort') + ->get(); + if ($res->isEmpty()) return []; + $res = $res->toArray(); + + $imageList = $this->getOssObjects(array_column($res, 'image_id')); + + foreach ($res as &$v) { + $v['url'] = $imageList[$v['image_id']]['url'] ?? ''; + } + + return $res; } } \ No newline at end of file