62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\api\server\CityService;
|
|
use app\api\model\User;
|
|
|
|
class City extends ApiBase
|
|
{
|
|
public $like_not_need_login = ['index'];
|
|
|
|
// 类型属性声明
|
|
protected CityService $cityService;
|
|
|
|
/**
|
|
* 构造函数 - 必须初始化类型属性
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
// 初始化服务
|
|
$this->cityService = new CityService();
|
|
}
|
|
public function index()
|
|
{
|
|
$array = $this->cityService->list();
|
|
return $this->_success('成功',$array);
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据经纬度获取所在城市
|
|
* @return void
|
|
*/
|
|
public function getCity()
|
|
{
|
|
$lat = $this->request->param('lat', '');
|
|
$lng = $this->request->param('lng', '');
|
|
|
|
if (empty($lat) || empty($lng)) {
|
|
return $this->_error('请提供经纬度参数');
|
|
}
|
|
$uid = $this->user_id;
|
|
//查询用户信息
|
|
$userInfo = User::find($uid);
|
|
if ($userInfo) {
|
|
if ($userInfo->longitude == '' || $userInfo->latitude == '')
|
|
{
|
|
$userInfo->longitude = $lng; // 经度
|
|
$userInfo->latitude = $lat; // 纬度
|
|
$userInfo->save();
|
|
}
|
|
}
|
|
$result = $this->cityService->getCityByLocation($lat, $lng);
|
|
|
|
if ($result['success']) {
|
|
return $this->_success($result['msg'], $result['data']);
|
|
} else {
|
|
return $this->_error($result['msg'], $result['data']);
|
|
}
|
|
}
|
|
} |