Store.php 6.9 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 门店接口
 */
class Store extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获得门店列表
     *
     * @ApiTitle    (获得门店列表)
     * @ApiSummary  (获得门店列表)
     * @ApiMethod   (POST)
     * @ApiParams   (name="lng", type="string", required=true, description="lng")
     * @ApiParams   (name="lat", type="string", required=true, description="lat")
     * @ApiParams   (name="value", type="string", required=true, description="地区值")
     * @ApiParams   (name="type", type="string", required=true, description="地区类型")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'prepay_id':'123','options':{},'order':{}}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     */
    public function get_List()
    {
        $lng = $this->request->request('lng');
        $lat = $this->request->request('lat');
        $value = $this->request->request('value');
        $type = $this->request->request('type');

        $where = [];
        if(!empty($value)&&!empty($type)){
            $where[$type] = $value;
        }
        $where['status'] = 'enable';
        $store = new \app\admin\model\Store();
        $store = $store->where($where)->select();

        foreach ($store as &$k){
            $banner = explode(",", $k['banner']);
            foreach ($banner as &$v){
                $v = cdnurl($v,true);
            }
            $k['banner'] = $banner;
            $k['cover'] = cdnurl($k['cover'],true);
            $k['distance'] = $this->GetDistance($lat,$lng,$k['lat'],$k['lng']);
        }

        if(!empty($store)){
            //排序
            foreach ($store as $key => $row) {
                $distance[$key] = $row['distance'];
            }
            array_multisort($distance, SORT_ASC, $store);
        }

        $this->success('请求成功',$store);
    }

    /**
     * 计算两组经纬度坐标 之间的距离
     *  $lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
     *
     * @param $lat1
     * @param $lng1
     * @param $lat2
     * @param $lng2
     * @param int $len_type
     * @param int $decimal
     * @return false|float
     */
    private function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 2, $decimal = 2)
    {
        $EARTH_RADIUS = 6378.137;//地球半径
        $PI = 3.1415926;

        $radLat1 = $lat1 * $PI / 180.0;
        $radLat2 = $lat2 * $PI / 180.0;
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * $PI / 180.0) - ($lng2 * $PI / 180.0);
        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
        $s = $s * $EARTH_RADIUS;
        $s = round($s * 1000);
        if ($len_type > 1)
        {
            $s /= 1000;
        }
        return round($s, $decimal);
    }


    /**
     * 获得门店
     *
     * @ApiTitle    (获得门店)
     * @ApiSummary  (获得门店)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="string", required=true, description="ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'prepay_id':'123','options':{},'order':{}}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     */
    public function get_id()
    {
        $id = $this->request->request('id');
        if(!$id){
            $this->error(__('Invalid parameters'));
        }

        $store = new \app\admin\model\Store();
        $store = $store->where(['id'=>$id,'status'=>'enable'])->find();
        if(empty($store)){
            $this->error(__('Invalid parameters'));
        }
        $store = $store->toArray();

        $banner = explode(",", $store['banner']);
        foreach ($banner as &$v){
            $v = cdnurl($v,true);
        }
        $store['banner'] = $banner;
        $store['cover'] = cdnurl($store['cover'],true);

        //查询门店课程
        $courseStore = new \app\admin\model\CourseStore();
        $where['store_id'] = $id;
        $where['status'] = 'confirmed';
        $courseStore = $courseStore->with('course')->where($where)->order('date','desc')->select();
        $course_id = [];
        $course = [];
        foreach ($courseStore as $k){
            if(!isset($course_id[$k['course_id']])){
                $course_id[$k['course_id']] = $k['course_id'];

                $course1 = new \app\admin\model\Course();
                $course1 = $course1->where(['id'=>$k['course_id']])->find();
                if(empty($course1)){
                    continue;
                }
                $course[$k['course_id']] = $course1->toArray();
            }
        }

        foreach ($course_id as $v){
            //查询门店课程
            $courseStore = new \app\admin\model\CourseStore();
            $where['store_id'] = $id;
            $where['course_id'] = $v;
            $where['status'] = 'confirmed';
            $where['is_end'] = false;
            $where['end'] = ['>',date('Y-m-d H:i:s')];
            $courseStore = $courseStore->with('course')->where($where)->order('date','desc')->select();
            foreach ($courseStore as $k){
                $sign = new \app\admin\model\CourseSign();
                $k['course']['sign_count'] =  $sign->where(['course_id'=>$k['course_id'],'store_id'=>$id,'publish_time'=>['>',date('Y-m-d H:i:s')]])->count();
                $course[$k['course_id']] = $k['course'];
            }
        }

        $store['course'] = array_values($course);

        $this->success('请求成功',$store);
    }


    /**
     *  *计算某个经纬度的周围某段距离的正方形的四个点
     *
     * @param    $lng   float 经度
     * @param    $lat   float 纬度
     * @param    $distance float 该点所在圆的半径,该圆与此正方形内切,默认值为5公里
     *
     * @return array 正方形的四个点的经纬度坐标
     */
    private function returnSquarePoint($lng, $lat, $distance = 5.0)
  {
      //$lng 、$lat 经纬度
      $half = 6371;
      $d_lng = 2 * asin(sin($distance / (2 * $half)) / cos(deg2rad($lat)));
      $d_lng = rad2deg($d_lng);
      $d_lat = $distance / $half;
      $d_lat = rad2deg($d_lat);
      $four_point = array(
          'left-top'     => array('lat' => $lat + $d_lat, 'lng' => $lng - $d_lng),
          'right-top'    => array('lat' => $lat + $d_lat, 'lng' => $lng + $d_lng),
          'left-bottom'  => array('lat' => $lat - $d_lat, 'lng' => $lng - $d_lng),
          'right-bottom' => array('lat' => $lat - $d_lat, 'lng' => $lng + $d_lng),
      );

      return $four_point;
  }

}