From d84ffd4c9ee4c761eb2d7d2bdc84af9f191ae19c Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Mon, 31 Mar 2025 15:20:51 +0800 Subject: [PATCH] fix : site --- app/Cache/Redis/Api/SiteCache.php | 2 +- app/Controller/Api/UserController.php | 24 +++++++++++++- app/Model/UserSite.php | 11 +++++- app/Service/Api/User/MyPageService.php | 4 ++- app/Service/Api/User/SiteService.php | 46 ++++++++++++++++++++++++-- 5 files changed, 81 insertions(+), 6 deletions(-) diff --git a/app/Cache/Redis/Api/SiteCache.php b/app/Cache/Redis/Api/SiteCache.php index d65d17f..8cc3ff0 100644 --- a/app/Cache/Redis/Api/SiteCache.php +++ b/app/Cache/Redis/Api/SiteCache.php @@ -84,7 +84,7 @@ class SiteCache if ($info->status == SiteCode::SITE_DISABLE || $info->is_del == SiteCode::SITE_DEL) return; $this->redis->hMset($siteKey, [ - 'id' => $info->id, + 'site_id' => $info->id, 'name' => $info->name, 'kitchen_id' => $info->kitchen_id, 'city_id' => $info->city_id, diff --git a/app/Controller/Api/UserController.php b/app/Controller/Api/UserController.php index e31067e..91bfa41 100644 --- a/app/Controller/Api/UserController.php +++ b/app/Controller/Api/UserController.php @@ -83,6 +83,28 @@ class UserController extends AbstractController return (new SiteService)->del(); } + /** + * @return array + */ + #[RequestMapping(path: 'site/set_default',methods: 'post')] + #[Scene(scene: 'set_default_site')] + public function setDefaultSite() + { + return (new SiteService)->setDefault(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + #[RequestMapping(path: 'site/get_default',methods: 'GET')] + #[Scene(scene: 'get_default_site')] + public function getDefaultSite() + { + return (new SiteService)->getDefault(); + } + /** * @return array */ @@ -117,6 +139,6 @@ class UserController extends AbstractController public function updateProfile() { - + } } diff --git a/app/Model/UserSite.php b/app/Model/UserSite.php index 98c5606..39985f8 100644 --- a/app/Model/UserSite.php +++ b/app/Model/UserSite.php @@ -80,7 +80,16 @@ class UserSite extends Model * @param int $userId * @return BuildsQueries|Builder|\Hyperf\Database\Model\Model|object|null */ - public function getUserNoDefaultIdByUserId(int $userId) + public function getUserNoDefaultIdByUserId(int $userId): \Hyperf\Database\Model\Model|Builder|BuildsQueries|null + { + return $this->where('user_id', $userId)->where('is_default',SiteCode::USER_NO_DEFAULT)->first(); + } + + /** + * @param int $userId + * @return BuildsQueries|Builder|\Hyperf\Database\Model\Model|object|null + */ + public function getUserDefaultInfoByUserId(int $userId) { return $this->where('user_id', $userId)->where('is_default',SiteCode::USER_NO_DEFAULT)->first(); } diff --git a/app/Service/Api/User/MyPageService.php b/app/Service/Api/User/MyPageService.php index 7157114..1e53b8b 100644 --- a/app/Service/Api/User/MyPageService.php +++ b/app/Service/Api/User/MyPageService.php @@ -37,7 +37,7 @@ class MyPageService extends BaseService $adminInfo = $this->adminUserModel->getAdminInfoByBindUserId($this->userId); - return [ + $res = [ 'id' => $this->userId, 'nickname' => $userInfo->nickname, 'avatar' => $this->getOssObjectById($userInfo->avatar_id) ?? '', @@ -47,6 +47,8 @@ class MyPageService extends BaseService 'invite_num' => 0, 'role_id' => (!empty($adminInfo) && $adminInfo->role_id > 0) ? $adminInfo->role_id : 0, ]; + + return $this->return->success('success', $res); } /** diff --git a/app/Service/Api/User/SiteService.php b/app/Service/Api/User/SiteService.php index 1d73315..9d10c8f 100644 --- a/app/Service/Api/User/SiteService.php +++ b/app/Service/Api/User/SiteService.php @@ -50,13 +50,13 @@ class SiteService extends BaseService */ public function handle(): array { - $siteIds = $this->userSiteModel->where('user_id', $this->userId)->select(['id','is_default'])->get(); + $siteIds = $this->userSiteModel->where('user_id', $this->userId)->select(['id','site_id','is_default'])->get(); if (empty($siteIds)) $this->return->success('success', ['list' => []]); $siteIds = $siteIds->toArray(); foreach ($siteIds as &$siteId) { - $siteId = array_merge($siteId,$this->siteCache->getSiteInfo($siteId['id'])); + $siteId['info'] = $this->siteCache->getSiteInfo($siteId['site_id']); } return $this->return->success('success', ['list' => $siteIds]); @@ -119,4 +119,46 @@ class SiteService extends BaseService return $this->return->success(); } + + /** + * @return array + */ + public function setDefault(): array + { + $id = (int)$this->request->input('site_id'); + + $info = $this->userSiteModel->getInfoByUserIdAndId($this->userId,$id); + + if ($info->is_default == SiteCode::USER_DEFAULT) throw new ErrException('该地址已经是默认地址'); + + $updateInfo = $this->userSiteModel->getUserDefaultInfoByUserId($this->userId); + if (!empty($updateInfo)) { + if ($updateInfo->id == $id) throw new ErrException('该地址已经是默认地址'); + + $updateInfo->is_default = SiteCode::USER_NO_DEFAULT; + + if(!$updateInfo->save()) throw new ErrException('修改失败-修改联动数据失败'); + } + + $info->is_default = SiteCode::USER_DEFAULT; + if (!$info->save()) throw new ErrException('修改失败'); + + return $this->return->success(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function getDefault(): array + { + $info = $this->userSiteModel->getUserDefaultInfoByUserId($this->userId); + if (empty($info)) return $this->return->success(); + + $info = $info->toArray(); + $info['info'] = $this->siteCache->getSiteInfo($info['site_id']); + + return $this->return->success('success', $info); + } } \ No newline at end of file