Files
hyperf_service/app/Service/Api/User/IndexService.php
2025-04-01 10:02:10 +08:00

92 lines
2.1 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\User;
use App\Constants\Common\BannerCode;
use App\Constants\Common\CouponCode;
use App\Model\Banner;
use App\Model\UserCoupon;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
use App\Service\ServiceTrait\Common\OssTrait;
use Hyperf\Di\Annotation\Inject;
class IndexService extends BaseService
{
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;
}
}