123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace app\services\other;
- use app\dao\other\CityAreaDao;
- use app\services\BaseServices;
- use crmeb\exceptions\AdminException;
- use crmeb\services\CacheService;
- use crmeb\services\FormBuilder as Form;
- class CityAreaServices extends BaseServices
- {
-
- public $type = [
- '1' => 'province',
- '2' => 'city',
- '3' => 'area',
- '4' => 'street'
- ];
-
- public function __construct(CityAreaDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function getRelationCityIds(int $id)
- {
- $cityInfo = $this->dao->get($id);
- $ids = [];
- if ($cityInfo) {
- $ids = explode('/', trim($cityInfo['path'], '/'));
- }
- return array_merge([$id], $ids);
- }
-
- public function getRelationCityIdsCache(int $id, int $expire = 1800)
- {
- return CacheService::redisHandler('apiCity')->remember('city_ids_' . $id, function () use ($id) {
- $cityInfo = $this->dao->get($id);
- $ids = [];
- if ($cityInfo) {
- $ids = explode('/', trim($cityInfo['path'], '/'));
- }
- return array_merge([$id], $ids);
- }, $expire);
- }
-
- public function getCityTreeList(int $pid = 0, int $type = 0)
- {
- $cityList = $this->dao->cacheStrRemember('pid_' . $pid, function () use ($pid) {
- $parent_name = '中国';
- if ($pid) {
- $city = $this->dao->get($pid);
- $parent_name = $city ? $city['name'] : '';
- }
- $cityList = $this->dao->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']);
- }
- }
- return $cityList;
- });
- if ($cityList) {
- switch ($type) {
- case 0:
- case 3:
- break;
- case 1:
- 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;
- }
-
- public function createCityForm(int $parentId)
- {
- $info = [];
- if ($parentId) {
- $info = $this->dao->get($parentId);
- }
- $field[] = Form::hidden('level', $info['level'] ?? 0);
- $field[] = Form::hidden('parent_id', $info['id'] ?? 0);
- $field[] = Form::input('parent_name', '父类名称', $info['name'] ?? '中国')->disabled(true);
- $field[] = Form::input('name', '名称')->required('请填写城市名称');
- return create_form('添加城市', $field, $this->url('/setting/city/save'));
- }
-
- public function updateCityForm(int $id)
- {
- $info = $this->dao->get($id);
- if (!$info) {
- throw new AdminException('需改的数据不存在');
- }
- if ($info['parent_id']) {
- $city = $this->dao->get($info['parent_id']);
- $info['parent_name'] = $city['name'];
- }
- $info = $info->toArray();
- $field[] = Form::hidden('id', $info['id']);
- $field[] = Form::hidden('level', $info['level']);
- $field[] = Form::hidden('parent_id', $info['parent_id']);
- $field[] = Form::input('parent_name', '父类名称', $info['parent_name'] ?? '中国')->disabled(true);
- $field[] = Form::input('name', '名称', $info['name'])->required('请填写城市名称');
- return create_form('修改城市', $field, $this->url('/setting/city/save'));
- }
- }
|