// +---------------------------------------------------------------------- namespace app\services\system; use app\model\system\CityArea; use Exception; use qiniu\basic\BaseServices; use qiniu\services\CacheService; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\Model; use Throwable; /** * 城市数据(街道) * Class CityAreaServices * @package app\services\other */ class CityAreaServices extends BaseServices { /** * 城市类型 * @var string[] */ public $type = [ '1' => 'province', '2' => 'city', '3' => 'area', '4' => 'street' ]; /** * CityAreaServices constructor. * @param CityArea $model */ public function __construct(CityArea $model) { $this->model = $model; } /** * 获取某一个城市id相关上级所有ids * @param int $id * @return array|int[] * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getRelationCityIds(int $id) { $cityInfo = $this->model->get($id); $ids = []; if ($cityInfo) { $ids = explode('/', trim($cityInfo['path'], '/')); } return array_merge([$id], $ids); } /** * @param int $id * @param int $expire * @return bool|mixed|null * @throws Throwable */ public function getRelationCityIdsCache(int $id, int $expire = 1800) { return CacheService::redisHandler('apiCity')->remember('city_ids_' . $id, function () use ($id) { $cityInfo = $this->model->get($id); $ids = []; if ($cityInfo) { $ids = explode('/', trim($cityInfo['path'], '/')); } return array_merge([$id], $ids); }, $expire); } /** * 获取城市数据 * @param int $pid * @param int $type 1:省市 2:省市区 0、3:省市区街道 * @return false|mixed|string|null * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getCityTreeList(int $pid = 0, int $type = 0) { $parent_name = '中国'; if ($pid) { $city = $this->model->get($pid); $parent_name = $city ? $city['name'] : ''; } $cityList = $this->getCityList(['parent_id' => $pid], 'id as value,id,name as label,parent_id as pid,level', ['children']); foreach ($cityList as &$item) { $item['parent_name'] = $parent_name; if (isset($item['children']) && $item['children']) { $item['children'] = []; $item['loading'] = false; $item['_loading'] = false; } else { unset($item['children']); } } if ($cityList) { switch ($type) { case 0: case 3: break; case 1://控制children 前端不能请求下一级数据 foreach ($cityList as &$item) { if ($item['level'] == 2) { unset($item['children'], $item['loading'], $item['_loading']); } } break; case 2: foreach ($cityList as &$item) { if ($item['level'] == 3) { unset($item['children'], $item['loading'], $item['_loading']); } } break; case 4: foreach ($cityList as &$item) { if ($item['level'] == 1) { unset($item['children'], $item['loading'], $item['_loading']); } } break; } } return $cityList; } /** * 搜索某个地址 * @param array $where * @return array|Model|null * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function searchCity(array $where) { return $this->model->search($where)->order('id DESC')->find(); } /** * 获取地址 * @param array $where * @param string $field * @param array $with * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getCityList(array $where, string $field = '*', array $with = []) { return $this->model->where($where)->field($field)->with($with)->order('id asc')->select()->toArray(); } /** * 删除上级城市和当前城市id * @param int $cityId * @return bool * @throws Exception */ public function deleteCity(int $cityId) { return $this->model->where('id', $cityId)->whereOr('parent_id', $cityId)->delete(); } }