123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- 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();
- }
- }
|