Index.php 4.0 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\Resource;
use app\common\model\Users;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * @ApiParams   (name="lng", type="integer", required=true, description="经度")
     * @ApiParams   (name="lat", type="integer", required=true, description="纬度")
     * @ApiParams   (name="radius", type="integer", required=true, description="半径[单位米]")
     * @ApiReturn   ({
    "code":1,
    "msg":"返回成功"
    "data": [
    {
    "id": "用户id",
    "name": "用户姓名",
    "avatar": "用户头像",
    "grade": "性别",
    "age": "年龄",
    "lng": "经度",
    "lat": "纬度",
    "address": "详细地址",
    "createtime": "创建时间",
    "updatetime": "更新时间",
    "distance": "距离我多远"
    },
    ]
    })
     */
    public function index()
    {
        $latitude = $this->request->param('lat');
        $longitude = $this->request->param('lng');
        $radius = $this->request->param('radius');
        $address = new Resource();
        $data = $address->getAround($latitude, $longitude, $radius);
        //dump($data);
        //查询数据库中经纬度符合的数据
        $model = new Users();
        $res = $model
            ->where('lng','between',[$data['minLng'],$data['maxLng']])
            ->where('lng','between',[$data['minLng'],$data['maxLng']])
            ->select();
        foreach ($res as $k => $v){
            $res[$k]['avatar'] = cdnurl($v['avatar']);
            //计算两地距离
            $res[$k]['distance'] = $address->getDistance($longitude,$latitude,$v['lng'],$v['lat']).'米';
        }
        $this->success('',$res);
    }

    /**
     * @ApiTitle (获取城市列表)
     */
    public function getCity(){
        $like = db('city')->field('*')->select();
        $data = [];
        $kone = 0;
        foreach ($like as $v) {
            if ($v['pid'] == 0) {
                $data[$kone] = $v;
                $ktwo = 0;
                foreach ($like as $v2) {
                    if ($v2['pid'] == $v['id']) {
                        $data[$kone]['subclass'][$ktwo] = $v2;
                        $kthree = 0;
                        foreach ($like as $v3){
                            if ($v3['pid'] == $v2['id']){
                                $data[$kone]['subclass'][$ktwo]['zsubclass'][$kthree] = $v3;
                                $kthree += 1;
                            }
                        }
                        $ktwo += 1;
                    }
                }
                $kone += 1;
            }
        }
        $this->success('返回成功', $data);
    }

    /**
     * 计算距离
     * @ApiParams   (name="lng1", type="integer", required=true, description="经度1")
     * @ApiParams   (name="lat1", type="integer", required=true, description="纬度1")
     * @ApiParams   (name="lng2", type="integer", required=true, description="经度2")
     * @ApiParams   (name="lat2", type="integer", required=true, description="纬度2")

     */
    public function test2()
    {
        $param = $this->request->param();
        $address = new Resource();
        $data = $address->getDistance($param['lng1'],$param['lat1'],$param['lng2'],$param['lat2'],1);
        $this->success('',$data);
    }


    /**
     * [正方形4个点]
     * @ApiParams   (name="lng", type="integer", required=true, description="经度")
     * @ApiParams   (name="lat", type="integer", required=true, description="纬度")
     * @ApiParams   (name="radius", type="integer", required=true, description="半径[单位千米]")
     */
    public function test4()
    {
        $latitude = $this->request->param('lat');
        $longitude = $this->request->param('lng');
        $radius = $this->request->param('radius');
        $address = new Resource();
        $data = $address->returnSquarePoint($longitude,$latitude, $radius);
        $this->success('',$data);
    }
}