// +---------------------------------------------------------------------- namespace app\common\dao\store; use app\common\dao\BaseDao; use app\common\model\BaseModel; use app\common\model\store\CityArea; use app\common\model\store\GeoProvince; use app\common\model\store\GeoCity; use app\common\model\store\GeoArea; use app\common\model\store\GeoStreet; use app\common\model\store\GeoVillage; use app\common\model\store\GeoBrigade; class CityAreaDao extends BaseDao { protected function getModel(): string { return CityArea::class; } public function search(array $where) { return CityArea::getDB()->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) { $query->where('parent_id', $where['pid']); })->when(isset($where['address']) && $where['address'] !== '', function ($query) use ($where) { $address = explode('/', trim($where['address'], '/')); $p = array_shift($address); if (mb_strlen($p) - 1 === mb_strpos($p, '市')) { $p = mb_substr($p, 0, -1); } elseif (mb_strlen($p) - 1 === mb_strpos($p, '省')) { $p = mb_substr($p, 0, -1); } elseif (mb_strlen($p) - 3 === mb_strpos($p, '自治区')) { $p = mb_substr($p, 0, -3); } $pcity = $this->search([])->where('name', $p)->find(); $street = array_pop($address); if ($pcity) { $path = '/' . $pcity->id . '/'; $query->whereLike('path', "/{$pcity->id}/%"); foreach ($address as $item) { $id = $this->search([])->whereLike('path', $path . '%')->where('name', $item)->value('id'); if ($id) { $path .= $id . '/'; } else { break; } } } $query->getVillage('path', $path . '%')->where('name', $street); }); } public function searchGeo(array $where) { if (!empty($where['street_code'])) { return GeoVillage::getDB()->where('street_code', $where['street_code']); } if (!empty($where['area_code'])) { return GeoStreet::getDB()->where('area_code', $where['area_code']); } if (!empty($where['city_code'])) { return GeoArea::getDB()->where('city_code', $where['city_code']); } if (!empty($where['province_code'])) { return GeoCity::getDB()->where('province_code', $where['province_code']); } else { return GeoProvince::getDB(); } return false; } public function searchBrigade(array $where) { return GeoBrigade::getDB(); } public function getCityList(CityArea $city) { if (!$city->parent_id) return [$city]; $lst = $this->search([])->where('id', 'in', explode('/', trim($city->path, '/')))->order('id ASC')->select(); $lst[] = $city; return $lst; } /** * 获取经纬度 * @param $location * @param $streetCode * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getLngAndLat($location, $streetCode) { if (!empty($streetCode)) { $location = GeoStreet::where('street_code', $streetCode)->field('lng,lat')->find(); if (!empty($location)) { [$lat, $lng] = [$location['lat'], $location['lng']]; } } elseif (!empty($location) && $location != ',') { [$lat, $lng] = explode(',', $location); } if (empty($lat) || empty($lng)) { [$lat, $lng] = ['28.889137', '105.443352']; } return [$lat, $lng]; } }