feat : city

This commit is contained in:
2024-10-31 16:30:29 +08:00
parent 39b5df9f0a
commit ceeeea7c34
9 changed files with 534 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Cache\Redis\Common;
use App\Cache\Redis\RedisCache;
use App\Model\SystemCityConfig;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CityCache
{
/**
* @var RedisCache
*/
#[Inject]
protected RedisCache $redis;
/**
* @var SystemCityConfig
*/
#[Inject]
protected SystemCityConfig $systemCityConfigModel;
/**
* 获取数据
* @param int $deep 0 = 省份 1 = 市 2 = 区 other = all
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
public function getCityList(int $deep = -1): array
{
$type = match ($deep) {
1 => '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;
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace App\Cache\Redis\Common;
class CommonRedisKey
{
/**
* @var $type
* @return string
*/
public static function getSystemRegionList($type = 'all')
{
return '__system:address:list:'.$type;
}
}