根据用户当前定位经纬度获取附近的店铺

记录:TP5 根据用户当前定位经纬度获取附近的店铺

代码示例:
    /**
     * 门店列表
     */
    public function index()
    {
        try {
            $validate = new Validate([
                'keyword' => 'max:25', // 搜索词长度不能超过25个字符
                'page' => 'integer|min:1',
                'longitude' => 'require',
                'latitude' => 'require',
            ]);
            $validate->message([
                'keyword.max' => '搜索词长度不能超过25个字符!',
                'page.integer' => '页码需为整数!',
                'longitude.require' => '经度不能为空!',
                'latitude.require' => '纬度不能为空!',
            ]);
            $param = $this->request->param();
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
            //用户当前经纬度
            $longitude = $this->request->param('longitude/f',0);
            $latitude = $this->request->param('latitude/f',0);

            //查询条件
            $where = "a.check_status=1";
            $keyword = $this->request->param('keyword', '');
            if($keyword){
            	$where .= " and a.name like '%$keyword%'";
            }
            //根据用户选择的距离筛选数据
            $distance = $this->request->param('distance/d',0);
            if($distance && ($longitude || $latitude)){
                $where .= " and ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((:latitude1 * PI() / 180 - a.latitude * PI() / 180) / 2),2) + COS(:latitude2 * PI() / 180) * COS(a.latitude * PI() / 180) * POW(SIN((:longitude * PI() / 180 - a.longitude * PI() / 180) / 2),2))),2) <= :distance";
    
                $bindValue['latitude1'] = $latitude;
                $bindValue['latitude2'] = $latitude;
                $bindValue['longitude'] = $longitude;
                $bindValue['distance'] = $distance;
            }

            //查询字段
            $field = "a.*, u.nickname as user_nickname";
            $field .= ",ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-a.latitude*PI()/180)/2),2) + COS($latitude*PI()/180)*COS(a.latitude*PI()/180)*POW(SIN(($longitude*PI()/180-a.longitude * PI()/180)/2),2))),2) as distance";

            //排序
            $order_field = ['distance'=>'ASC','a.createtime'=>'DESC'];

            //分页参数
            $iPage = $this->request->param('page', 1, 'int');
            $iPageSize = 10;

            $articleData = Db::name('user_store')
                ->field($field)
                ->alias('a')
                ->join('user u','a.user_id = u.id')
                ->where("a.status = 1 and u.status = 'normal'")
                ->where($where)
                ->order($order_field)
                ->paginate($iPageSize, false, ['page'=>$iPage, 'list_rows'=>$iPageSize])
                ->each(function($item,$key) {
                    //判断是否营业
                    $timedate = date('H:i:s');
                    $yingye_status = 0;
                    if($timedate >= $item['open_time'] && $timedate <= $item['close_time']){
                        $yingye_status = 1;
                    }
                    $item['yingye_status'] = $yingye_status;
                    //门店照片
                    if($item['photos']){
                        $item['photo_url'] = cdnurl(explode(",",$item['photos'])[0], true);
                    }else{
                        $item['photo_url'] = cdnurl('/assets/img/avatar.png',true);
                    }
                    return $item;
                })
                ->toArray();
            $total = $articleData['total'] ? $articleData['total'] : 0;
            $page = empty($articleData['last_page']) ? 0 : $articleData['last_page'] ;
            $this->success("OK", ['list' => $articleData, 'total_page' => $page, 'total' => $total]);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值