// +---------------------------------------------------------------------- namespace app\controller\merchant\store\shipping; use app\common\repositories\store\CityAreaRepository; use think\App; use crmeb\basic\BaseController; use app\common\repositories\store\shipping\CityRepository as repository; use think\facade\Log; use Overtrue\Pinyin\Pinyin; class City extends BaseController { protected $repository; /** * City constructor. * @param App $app * @param repository $repository */ public function __construct(App $app, repository $repository) { parent::__construct($app); $this->repository = $repository; } /** * @Author:Qinii * @Date: 2020/5/8 * @Time: 14:40 * @return mixed */ public function lst() { return app('json')->success($this->repository->getFormatList([['is_show', '=', 1],['level','<',2]])); } public function lstV2($pid) { return app('json')->success(app()->make(CityAreaRepository::class)->getChildren(intval($pid))); } public function lstV3() { $provinceCode = $this->request->param('province_code') ?? ''; $cityCode = $this->request->param('city_code') ?? ''; $areaCode = $this->request->param('area_code') ?? ''; $streetCode = $this->request->param('street_code') ?? ''; $pinyin = $this->request->param('pinyin') ?? 0; $list = app()->make(CityAreaRepository::class)->getGeoChildren(['province_code' => $provinceCode, 'city_code' => $cityCode, 'area_code' => $areaCode, 'street_code' => $streetCode]); $geoList = []; $pyList = []; foreach ($list as $v) { $temp = []; if (!empty($v['village_id'])) { if ($pinyin == 1) { $py = strtoupper((new Pinyin)->abbr($v['village_name'])[0] ?? '-'); $pyList[$py][] = [ 'type' => 'village', 'id' => $v['village_id'], 'level' => 5, 'name' => $v['village_name'] ?? '', 'code' => $v['village_code'] ?? '' ]; } else { $temp = [ 'type' => 'village', 'id' => $v['village_id'], 'level' => 5, 'name' => $v['village_name'] ?? '', 'code' => $v['village_code'] ?? '' ]; $geoList[] = $temp; } } if (!empty($v['street_id'])) { if ($pinyin == 1) { $py = strtoupper((new Pinyin)->abbr($v['street_name'])[0] ?? '-'); $pyList[$py][] = [ 'type' => 'street', 'id' => $v['street_id'], 'level' => 4, 'name' => $v['street_name'] ?? '', 'code' => $v['street_code'] ?? '' ]; } else { $temp = [ 'type' => 'street', 'id' => $v['street_id'], 'level' => 4, 'name' => $v['street_name'] ?? '', 'code' => $v['street_code'] ?? '' ]; $geoList[] = $temp; } } if (!empty($v['area_id'])) { if ($pinyin == 1) { $py = strtoupper((new Pinyin)->abbr($v['area_name'])[0] ?? '-'); $pyList[$py][] = [ 'type' => 'area', 'id' => $v['area_id'], 'level' => 3, 'name' => $v['area_name'] ?? '', 'code' => $v['area_code'] ?? '' ]; } else { $temp = [ 'type' => 'area', 'id' => $v['area_id'], 'level' => 3, 'name' => $v['area_name'] ?? '', 'code' => $v['area_code'] ?? '' ]; $geoList[] = $temp; } } if (!empty($v['city_id'])) { if ($pinyin == 1) { $py = strtoupper((new Pinyin)->abbr($v['city_name'])[0] ?? '-'); $pyList[$py][] = [ 'type' => 'city', 'id' => $v['city_id'], 'level' => 2, 'name' => $v['city_name'] ?? '', 'code' => $v['city_code'] ?? '' ]; } else { $temp = [ 'type' => 'city', 'id' => $v['city_id'], 'level' => 2, 'name' => $v['city_name'] ?? '', 'code' => $v['city_code'] ?? '', ]; $geoList[] = $temp; } } if (!empty($v['province_id'])) { if ($pinyin == 1) { $py = strtoupper((new Pinyin)->abbr($v['province_name'])[0] ?? '-'); $pyList[$py][] = [ 'type' => 'province', 'id' => $v['province_id'], 'level' => 1, 'name' => $v['province_name'] ?? '', 'code' => $v['province_code'] ?? '' ]; } else { $temp = [ 'type' => 'province', 'id' => $v['province_id'], 'level' => 1, 'name' => $v['province_name'] ?? '', 'code' => $v['province_code'] ?? '' ]; $geoList[] = $temp; } } } if ($pinyin == 1) { $geoList = []; foreach($pyList as $k=>$v) { $temp = []; $temp['pinyin'] = $k; $temp['data'] = $pyList[$k]; $geoList[] = $temp; } } return app('json')->success($geoList); } public function brigade() { $list = app()->make(CityAreaRepository::class)->getBrigade([]); $geoList = []; foreach ($list as $v) { $temp = [ 'type' => 'brigade', 'id' => $v['id'], 'level' => 6, 'name' => $v['brigade_name'] ?? '', 'code' => $v['brigade_name'] ?? '' ]; $geoList[] = $temp; } return app('json')->success($geoList); } public function cityList() { $address = $this->request->param('address'); if (!$address) return app('json')->fail('地址不存在'); $make = app()->make(CityAreaRepository::class); $city = $make->search(compact('address'))->order('id DESC')->find(); if (!$city){ Log::info('用户定位对比失败,请在城市数据中增加:'.var_export($address,true)); return app('json')->fail('地址不存在'); } return app('json')->success($make->getCityList($city)); } /** * @return mixed * @author Qinii */ public function getlist() { return app('json')->success($this->repository->getFormatList(['is_show' => 1])); } }