feat : city

This commit is contained in:
2024-10-31 16:52:32 +08:00
parent ceeeea7c34
commit 2d8f35ddf0
5 changed files with 100 additions and 3 deletions

View File

@@ -33,9 +33,9 @@ class CityCache
public function getCityList(int $deep = -1): array
{
$type = match ($deep) {
1 => 'province',
2 => 'city',
3 => 'district',
0 => 'province',
1 => 'city',
2 => 'district',
default => 'all',
};

View File

@@ -23,4 +23,11 @@ class CityCode
* 不删除
*/
const IS_NOT_DELETE = 1;
/**
* 深度 0 省 1 市 2 区
*/
const DEEP_PROVINCE = 0;
const DEEP_CITY = 1;
const DEEP_DISTRICT = 2;
}

View File

@@ -5,22 +5,50 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Service\Admin\System\ConfigService;
use App\Service\System\SystemService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
#[Controller(prefix: "admin/config")]
#[Middlewares([
JwtAuthMiddleware::class,
])]
class ConfigController extends AbstractController
{
#[RequestMapping(path: "module", methods: "GET")]
public function getConfigModule()
{
return (new ConfigService)->getConfigModule();
}
#[RequestMapping(path: "form", methods: "GET")]
public function getConfigForm()
{
return (new ConfigService)->getConfigForm();
}
#[RequestMapping(path: "update", methods: "POST")]
public function updateConfig()
{
return (new ConfigService)->handle();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
#[RequestMapping(path: "system/address_list", methods: "GET")]
public function getAddressList()
{
return (new SystemService)->address_list();
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\System;
use App\Cache\Redis\Common\CityCache;
use App\Constants\Common\CityCode;
use App\Service\Admin\BaseService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use RedisException;
class SystemService extends BaseService
{
/**
* @var CityCache
*/
#[Inject]
protected CityCache $cityCache;
public function handle()
{
return $this->return->success();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RedisException
*/
public function address_list(): array
{
$deep = (int)$this->request->input('deep');
$pid = $this->request->input('pid',0);
$deepList = $this->cityCache->getCityList($deep);
if (in_array($deep,[CityCode::DEEP_CITY,CityCode::DEEP_PROVINCE]) && $pid != 0) {
$column = 'pid';
$filteredData = array_values(array_filter($deepList, function($value) use ($column, $pid) {
return $value[$column] == $pid;
}));
}
return $this->return->success('success', ['list' => $filteredData ?? $deepList]);
}
}