Created by PhpStorm.
User: 水木易安 i@yangyongan.com Date: 2018/5/12 20:47 高德地图相关
Amap |
<?php
/**
* Created by PhpStorm.
* User: 水木易安 <i@yangyongan.com>
* Date: 2018/5/12 20:47
* 高德地图相关
*/
namespace app\index\controller;
use service\HttpService;
use think\Controller;
use think\Db;
use think\facade\Request;
class Amap extends Controller
{
function districts($city = '', $level = 1)
{
if ($city) {
$url = 'http://restapi.amap.com/v3/config/district?key=' . sysconf('amap_key') . '&keywords=' . $city . '&subdistrict=' . $level . '&extensions=base';
$res = HttpService::get($url);
$this->success('地区', '', json_decode($res));
} else {
$this->error("请填写地区参数");
}
}
function pos()
{
if (input('location/a')) {
if (input('distance')) {
$distance = input('distance');
} else {
$distance = false;
}
if (uid()) {
attract_form_id(input('form_id'));
$map['user_id'] = uid();
$location = input('location/a');
$has = db('user_location')->where($map)
->where([
['lat', '>', $location['lat'] - 0.0005],
['lat', '<', $location['lat'] + 0.0005],
['lng', '>', $location['lng'] - 0.0005],
['lng', '<', $location['lng'] + 0.0005]
])
->find();
$loc_data['lat'] = $location['lat'];
$loc_data['lng'] = $location['lng'];
$address = location2address($location['lat'], $location['lng']);
$loc_data['address'] = $address['simple'];
$loc_data['last_time'] = time();
$loc_data['clear'] = 0;
if ($has) {
$ret = db('user_location')->where($map)->update($loc_data);
} else {
$loc_data['user_id'] = uid();
$ret = db('user_location')->insert($loc_data);
}
if ($ret) {
$from_scope = input('from_scope');
if ($from_scope == 1) {
$where['u.city'] = session('user.city');
} else if ($from_scope == 2) {
$where['u.province'] = session('user.province');
} else {
$where['u.district'] = session('user.district');
}
$time_scope = input('time_scope');
if ($time_scope == 1) {
$last_time = time() - 24 * 3600;//一天
} else if ($time_scope == 2) {
$last_time = time() - 24 * 3600 * 3;//3天
} else if ($time_scope == 3) {
$last_time = time() - 24 * 3600 * 7;//7天
} else if ($time_scope == 4) {
$last_time = 0;//无限远
} else {
$last_time = time() - 2 * 3600;//2小时
}
$where['l.clear'] = 0;
$squares = square_point((double)$location['lng'], (double)$location['lat'], (double)$distance);
$users = db('user_location')
->alias('l')->join('system_user u', 'l.user_id = u.id')->where($where)
->where($distance ? [
['l.lat', '>', $squares['right-bottom']['lat']],
['l.lat', '<', $squares['left-top']['lat']],
['l.lng', '>', $squares['left-top']['lng']],
['l.lng', '<', $squares['right-bottom']['lng']]
] : true)
->where('l.last_time', '>', $last_time)
->field('u.id,u.nickname,u.avatar,u.province,u.town,u.city,u.district,u.desc,l.last_time,l.lat,l.lng,u.gender')
->order('l.last_time', 'desc')->limit(9999)->buildSql();
$users = Db::table($users . 'u')
->group('u.id')
->select();
$this->success($address, '', $users);
} else {
$this->error("保存位置失败");
}
} else {
$this->error("请登录");
}
} else {
$this->error("请传入位置信息");
}
}
function location_ip()
{
$ip_str = (getIp() == '127.0.0.1' ? '' : 'ip='.getIp().'&');
$res = HttpService::get('https://restapi.amap.com/v3/ip?' . $ip_str . 'output=json&key=' . sysconf('amap_key'));
$res = json_decode($res, true);
if ($res['status'] == 1) {
$this->success(getIp(), '', ['city' => $res['city'], 'province' => $res['province']]);
} else {
$this->error("IP定位失败");
}
}
}