Store.php
6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?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;
}
}