diff --git a/app/Cache/Redis/Common/CityCache.php b/app/Cache/Redis/Common/CityCache.php new file mode 100644 index 0000000..849c3cf --- /dev/null +++ b/app/Cache/Redis/Common/CityCache.php @@ -0,0 +1,56 @@ + 'province', + 2 => 'city', + 3 => 'district', + default => 'all', + }; + + $key = CommonRedisKey::getSystemRegionList($type); + + if ($this->redis->exists($key)) return json_decode($this->redis->get($key), true); + + if ($type == 'all') { + $data = $this->systemCityConfigModel->getAll(); + } else { + $data = $this->systemCityConfigModel->getListByDeep($deep); + } + + $this->redis->set($key, json_encode($data)); + + return $data; + } +} \ No newline at end of file diff --git a/app/Cache/Redis/Common/CommonRedisKey.php b/app/Cache/Redis/Common/CommonRedisKey.php new file mode 100644 index 0000000..0f41907 --- /dev/null +++ b/app/Cache/Redis/Common/CommonRedisKey.php @@ -0,0 +1,15 @@ +add(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + #[RequestMapping(path: "list", methods: "GET")] + #[Scene(scene: "list")] + public function list(): array + { + return (new CityService)->handle(); + } + + /** + * @return array + */ + #[RequestMapping(path: "edit", methods: "GET")] + #[Scene(scene: "edit")] + public function edit(): array + { + return (new CityService)->changeStatus(); + } + + /** + * @return array + */ + #[RequestMapping(path: "del", methods: "GET")] + #[Scene(scene: "del")] + public function delete(): array + { + return (new CityService)->delete(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + #[RequestMapping(path: "info", methods: "GET")] + #[Scene(scene: "info")] + public function info(): array + { + return (new CityService)->info(); + } +} diff --git a/app/Model/SystemCity.php b/app/Model/SystemCity.php new file mode 100644 index 0000000..54dd599 --- /dev/null +++ b/app/Model/SystemCity.php @@ -0,0 +1,61 @@ + 'integer', 'status' => 'integer', 'city_id' => 'integer', 'province_id' => 'integer', 'is_del' => 'integer']; + + const CREATED_AT = 'create_time'; + + const UPDATED_AT = 'update_time'; + + /** + * @param int $cityId + * @return Builder|\Hyperf\Database\Model\Model|null + */ + public function getInfoByCityId(int $cityId): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('city_id', $cityId)->where('is_del',CityCode::IS_NOT_DELETE)->first(); + } + + /** + * @param int $id + * @return Builder|\Hyperf\Database\Model\Model|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id', $id)->where('is_del',CityCode::IS_NOT_DELETE)->first(); + } +} diff --git a/app/Model/SystemCityConfig.php b/app/Model/SystemCityConfig.php new file mode 100644 index 0000000..2c5766a --- /dev/null +++ b/app/Model/SystemCityConfig.php @@ -0,0 +1,70 @@ + 'integer', 'pid' => 'integer', 'deep' => 'integer']; + + private array $selectFiled = ['id','pid','title']; + + /** + * @return array + */ + public function getAll(): array + { + return $this->get($this->selectFiled)->toArray(); + } + + /** + * @param int $deep + * @return array + */ + public function getListByDeep(int $deep): array + { + return $this->where('deep',$deep)->get($this->selectFiled)->toArray(); + } + + /** + * @param int $id + * @return Builder|\Hyperf\Database\Model\Model|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id',$id)->first(); + } + + /** + * @param int $id + * @param int $pid + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getAddressByIdAndPid(int $id, int $pid): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id',$id)->where('pid',$pid)->first(); + } +} diff --git a/app/Request/Admin/CityRequest.php b/app/Request/Admin/CityRequest.php new file mode 100644 index 0000000..b877429 --- /dev/null +++ b/app/Request/Admin/CityRequest.php @@ -0,0 +1,28 @@ +request->input('limit', 10); + + $list = $this->systemCityModel + ->where('is_del', CityCode::IS_NOT_DELETE) + ->paginate($limit,['id','title','status','city_id','province_id']) + ->toArray(); + + $allInfo = array_column($this->cityCache->getCityList(),'title','id'); + + if (!empty($list['data'])) { + foreach ($list['data'] as &$v) { + $v['city_name'] = $allInfo[$v['city_id']]; + $v['province_name'] = $allInfo[$v['province_id']]; + } + } + + return $this->return->success('success', ['list' => $list]); + } + + + /** + * @return array + */ + public function add(): array + { + $provinceId = (int)$this->request->input('province_id'); + $cityId = (int)$this->request->input('city_id'); + + $info = $this->systemCityConfigModel->getAddressByIdAndPid($cityId,$provinceId); + if (empty($info)) throw new AdminException('城市选择错误'); + + if ($this->systemCityModel->getInfoByCityId($cityId)) throw new AdminException('城市已存在'); + + $model = new SystemCity(); + + $model->province_id = $info->pid; + $model->city_id = $info->id; + $model->title = $info->title; + $model->status = $this->request->input('status', 1); + $model->is_del = 1; + + if (!$model->save()) throw new AdminException('添加失败'); + + return $this->return->success(); + } + + public function changeStatus(): array + { + $id = (int)$this->request->input('id'); + + $model = $this->systemCityModel->getInfoById($id); + + if (!$model) throw new AdminException('数据不存在'); + + $model->status = $model->status == CityCode::STATUS_ENABLE ? CityCode::STATUS_DISABLE : CityCode::STATUS_ENABLE; + + if (!$model->save()) throw new AdminException('修改失败'); + + return $this->return->success(); + } + + /** + * @return array + */ + public function delete(): array + { + $id = (int)$this->request->input('id'); + + $model = $this->systemCityModel->getInfoById($id); + + if (!$model) throw new AdminException('数据不存在'); + + $model->is_del = CityCode::IS_DELETE; + + if (!$model->save()) throw new AdminException('删除失败'); + + return $this->return->success(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws \RedisException + */ + public function info(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->systemCityModel->getInfoById($id); + + if (!$info) throw new AdminException('数据不存在'); + + $allInfo = $this->cityCache->getCityList(); + $allInfo = array_column($allInfo,'title','id'); + + $res = [ + 'id' => $info->id, + 'title' => $info->title, + 'status' => $info->status, + 'province_id' => $info->province_id, + 'province_name' => $allInfo[$info->province_id] ?? '', + 'city_id' => $info->city_id, + 'city_name' => $allInfo[$info->city_id]?? '', + ]; + + return $this->return->success('success',$res); + } +} \ No newline at end of file diff --git a/sync/http/admin/address.http b/sync/http/admin/address.http new file mode 100644 index 0000000..95b5f92 --- /dev/null +++ b/sync/http/admin/address.http @@ -0,0 +1,37 @@ +### 登录 +POST {{host}}/admin/login/user +Content-Type: application/x-www-form-urlencoded + +account=13632877014&password=123456 + +> {% + client.global.set("admin_token", response.body.data.token); +%} + + +###添加城市 +POST {{host}}/admin/city/add +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +city_id=1410&province_id=14&status=1 + +###城市详情 +GET {{host}}/admin/city/info?id=1 +Content-Type: application/json +Authorization: Bearer {{admin_token}} + +###城市列表 +GET {{host}}/admin/city/list?limit=10 +Content-Type: application/json +Authorization: Bearer {{admin_token}} + +###城市修改状态 +GET {{host}}/admin/city/edit?id=1 +Content-Type: application/json +Authorization: Bearer {{admin_token}} + +###城市删除 +GET {{host}}/admin/city/del?id=1 +Content-Type: application/json +Authorization: Bearer {{admin_token}}