110 lines
3.1 KiB
PHP
110 lines
3.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2021年10月15日
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
namespace app\models\common;
|
|
|
|
use Geocoder\Geocoder;
|
|
use app\models\District;
|
|
use app\models\Model;
|
|
|
|
class CommonGeocoderForm extends Model
|
|
{
|
|
public $lat;
|
|
public $lng;
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['lat', 'lng'], 'trim'],
|
|
[['lat', 'lng'], 'string'],
|
|
[['lat', 'lng'], 'required']
|
|
];
|
|
}
|
|
|
|
public function get_district_info()
|
|
{
|
|
if(!$this->validate()){
|
|
return $this->getModelError();
|
|
}
|
|
$provider = "qqmap";
|
|
$key = \Yii::$app->params['mapKey'][$provider];
|
|
$geocoder = new Geocoder($key,$provider);
|
|
$res = $geocoder->getAddress($this->lat, $this->lng);
|
|
$addr_info = $res['data']['address_component'];
|
|
$formatted_addr = $res['data']['formatted_addresses']['recommend'];
|
|
|
|
$province = District::find()
|
|
->where([
|
|
'name' => $addr_info['province'],
|
|
'level' => 'province',
|
|
'is_delete' => 0,
|
|
'parent_id' => 0
|
|
])
|
|
->select('id,parent_id,name,level')
|
|
->asArray()->one();
|
|
if($province == null){
|
|
return $this->apiReturnError("逆地址解析失败,对应省级行政区未找到");
|
|
}
|
|
|
|
$city = District::find()
|
|
->where([
|
|
'name' => $addr_info['city'],
|
|
'level' => 'city',
|
|
'is_delete' => 0,
|
|
'parent_id' => $province['id']
|
|
])
|
|
->select('id,parent_id,name,level')
|
|
->asArray()->one();
|
|
if($city == null){
|
|
return $this->apiReturnError("逆地址解析失败,对应市级行政区未找到");
|
|
}
|
|
|
|
$district = District::find()
|
|
->where([
|
|
'name' => $addr_info['district'],
|
|
'level' => 'district',
|
|
'is_delete' => 0,
|
|
'parent_id' => $city['id']
|
|
])
|
|
->select('id,parent_id,name,level')
|
|
->asArray()->one();
|
|
if($city == null){
|
|
return $this->apiReturnError("逆地址解析失败,对应区/县级行政区未找到");
|
|
}
|
|
|
|
$data = [
|
|
'location' => $res['data']['location'],
|
|
'address' => $res['data']['address'],
|
|
'province' => [
|
|
'id' => $province['id'],
|
|
'name' => $province['name'],
|
|
],
|
|
'city' => [
|
|
'id' => $city['id'],
|
|
'name' => $city['name'],
|
|
],
|
|
'district' => [
|
|
'id' => $district['id'],
|
|
'name' => $district['name'],
|
|
],
|
|
'street' => [
|
|
'name' => $addr_info['street'],
|
|
'street_number' => $addr_info['street_number'],
|
|
],
|
|
'formatted_addr' => $formatted_addr
|
|
];
|
|
return $this->apiReturnSuccess("ok", $data);
|
|
}
|
|
}
|
|
|
|
|
|
|