feat : city
This commit is contained in:
56
app/Cache/Redis/Common/CityCache.php
Normal file
56
app/Cache/Redis/Common/CityCache.php
Normal 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;
|
||||
}
|
||||
}
|
||||
15
app/Cache/Redis/Common/CommonRedisKey.php
Normal file
15
app/Cache/Redis/Common/CommonRedisKey.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user