From 3be21ed1b6aebf1f5f4d8c7cbe848858d682eae9 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Thu, 27 Feb 2025 17:21:21 +0800 Subject: [PATCH] feat : site --- app/Constants/Common/SiteCode.php | 7 ++ app/Controller/Api/SystemController.php | 25 +++++ app/Controller/Api/UserController.php | 35 +++++++ app/Model/UserSite.php | 77 +++++++++++++++ app/Service/Api/System/SiteListService.php | 53 ++++++++++ app/Service/Api/User/SiteService.php | 108 +++++++++++++++++++++ 6 files changed, 305 insertions(+) create mode 100644 app/Controller/Api/SystemController.php create mode 100644 app/Model/UserSite.php create mode 100644 app/Service/Api/System/SiteListService.php create mode 100644 app/Service/Api/User/SiteService.php diff --git a/app/Constants/Common/SiteCode.php b/app/Constants/Common/SiteCode.php index ddb3fe8..2857b4b 100644 --- a/app/Constants/Common/SiteCode.php +++ b/app/Constants/Common/SiteCode.php @@ -53,4 +53,11 @@ class SiteCode * @var string 站点不删除 */ const SITE_NO_DEL = 1; + + + /** + * @var int 用户默认地址 1=否 2=是 + */ + CONST INT USER_NO_DEFAULT = 1; + CONST INT USER_DEFAULT = 2; } \ No newline at end of file diff --git a/app/Controller/Api/SystemController.php b/app/Controller/Api/SystemController.php new file mode 100644 index 0000000..4f08d03 --- /dev/null +++ b/app/Controller/Api/SystemController.php @@ -0,0 +1,25 @@ +handle(); + } +} diff --git a/app/Controller/Api/UserController.php b/app/Controller/Api/UserController.php index 9722ea3..7123fd4 100644 --- a/app/Controller/Api/UserController.php +++ b/app/Controller/Api/UserController.php @@ -8,6 +8,7 @@ use App\Controller\AbstractController; use App\Middleware\Api\JwtAuthMiddleware; use App\Request\Api\UserRequest; use App\Service\Api\User\BindPhoneByWxService; +use App\Service\Api\User\SiteService; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Middlewares; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -33,4 +34,38 @@ class UserController extends AbstractController { return (new BindPhoneByWxService)->handle(); } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + #[RequestMapping(path: 'site/list',methods: 'post')] + #[Scene(scene: 'site_list')] + public function userSiteList() + { + return (new SiteService)->handle(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + #[RequestMapping(path: 'site/add',methods: 'post')] + #[Scene(scene: 'add_site')] + public function addSite() + { + return (new SiteService)->add(); + } + + /** + * @return array + */ + #[RequestMapping(path: 'site/del',methods: 'post')] + #[Scene(scene: 'del_site')] + public function delSite() + { + return (new SiteService)->del(); + } } diff --git a/app/Model/UserSite.php b/app/Model/UserSite.php new file mode 100644 index 0000000..a073fae --- /dev/null +++ b/app/Model/UserSite.php @@ -0,0 +1,77 @@ + 'integer', 'site_id' => 'integer', 'user_id' => 'integer', 'is_default' => 'integer']; + + const string CREATED_AT = 'created_time'; + const string UPDATED_AT = 'updated_time'; + + /** + * @param int $id + * @return UserSite|UserSite[]|Collection|\Hyperf\Database\Model\Model|null + */ + public function getInfoById(int $id): Collection|\Hyperf\Database\Model\Model|UserSite|array|null + { + return $this->find($id); + } + + /** + * @param int $userId + * @return array + */ + public function getSiteIdsByUserId(int $userId): array + { + return $this->where('user_id', $userId)->pluck('site_id')->toArray(); + } + + /** + * @param int $userId + * @return int + */ + public function getUserDefaultIdByUserId(int $userId): int + { + return $this->where('user_id', $userId)->where('is_default',SiteCode::USER_DEFAULT)->value('site_id') ?? 0; + } + + /** + * @param int $userId + * @return BuildsQueries|Builder|\Hyperf\Database\Model\Model|object|null + */ + public function getUserNoDefaultIdByUserId(int $userId) + { + return $this->where('user_id', $userId)->where('is_default',SiteCode::USER_NO_DEFAULT)->first(); + } +} diff --git a/app/Service/Api/System/SiteListService.php b/app/Service/Api/System/SiteListService.php new file mode 100644 index 0000000..95aac2e --- /dev/null +++ b/app/Service/Api/System/SiteListService.php @@ -0,0 +1,53 @@ +request->input('limit', 20); + + $list = $this->siteModel + ->when($name = $this->request->input('search_value'), function ($query) use ($name) { + $query->where('name', 'like', "%$name%"); + }) +// ->where('name', 'like', '%'.$this->request->input('search_value').'%') + ->paginate($limit) + ->toArray(); + + if (empty($list['data'])) return $this->return->success('success', ['list' => $list]); + + foreach ($list['data'] as &$item) { + $item['gap'] = SystemUtil::calculateDistance( + ['lng' => $this->request->input('lng'),'lat' => $this->request->input('lat')], + ['lng' => $item['lng'], 'lat' => $item['lat']] + ); + } + + return $this->return->success('success', ['list' => $list]); + } +} \ No newline at end of file diff --git a/app/Service/Api/User/SiteService.php b/app/Service/Api/User/SiteService.php new file mode 100644 index 0000000..0097a0e --- /dev/null +++ b/app/Service/Api/User/SiteService.php @@ -0,0 +1,108 @@ +userSiteModel->where('user_id', $this->userId)->pluck('site_id')->toArray(); + + $res = []; + foreach ($siteIds as $siteId) { + $res[] = $this->siteCache->getSiteInfo($siteId); + } + + return $this->return->success('success', ['list' => $res]); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function add(): array + { + $id = (int)$this->request->input('site_id'); + + if (empty($this->siteCache->getSiteInfo($id))) throw new ErrException('该地点不存在,请联系客服'); + + $siteIds = $this->userSiteModel->getSiteIdsByUserId($this->userId); + + if (in_array($id, $siteIds)) throw new ErrException('已添加该地点'); + + $insertModel = new UserSite(); + + $insertModel->user_id = $this->userId; + $insertModel->site_id = $id; + $insertModel->is_default = SiteCode::USER_NO_DEFAULT; + + if ($this->userSiteModel->getUserDefaultIdByUserId($this->userId) == 0) { + $insertModel->is_default = SiteCode::USER_DEFAULT; + } + + if (!$insertModel->save()) throw new ErrException('添加失败'); + + return $this->return->success(); + } + + /** + * @return array + */ + public function del(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->userSiteModel->getInfoById($id); + + Db::transaction(function () use ($info) { + if ($info->is_default == SiteCode::USER_DEFAULT) { + $updateInfo = $this->userSiteModel->getUserNoDefaultIdByUserId($this->userId); + + $updateInfo->is_default = SiteCode::USER_DEFAULT; + + if(!$updateInfo->save()) throw new ErrException('删除失败-修改联动数据失败'); + } + + if (!$info->delete()) throw new ErrException('删除失败'); + }); + + return $this->return->success(); + } +} \ No newline at end of file