|
|
@@ -0,0 +1,87 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+
|
|
|
+namespace app\api\controller\v1;
|
|
|
+
|
|
|
+
|
|
|
+use app\api\controller\ApiController;
|
|
|
+use app\common\validate\IDMustBePositiveInt;
|
|
|
+
|
|
|
+class Stores extends ApiController
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取附近的商家
|
|
|
+ *
|
|
|
+ * @url /store/nearby?lng=118.302416&lat=33.958887&page=1&limit=10
|
|
|
+ *
|
|
|
+ * @return bool|\think\response\Json
|
|
|
+ * @throws \think\db\exception\BindParamException
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ * @throws \think\exception\DbException
|
|
|
+ * @throws \think\exception\PDOException
|
|
|
+ */
|
|
|
+ public function getNearbyCol()
|
|
|
+ {
|
|
|
+ // 1. 传入用户位置
|
|
|
+ $param = $this->request->param();
|
|
|
+ // 数据校验
|
|
|
+ $valid = $this->validate($param, [
|
|
|
+ 'lng' => ['require','regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
|
|
|
+ 'lat' => ['require','regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
|
|
|
+ 'page' => 'require',
|
|
|
+ ],[
|
|
|
+ 'lng.require' => '缺少经度参数',
|
|
|
+ 'lng.regex' => '经度参数有误',
|
|
|
+ 'lat.require' => '缺少维度参数',
|
|
|
+ 'lat.regex' => '维度参数有误'
|
|
|
+ ]);
|
|
|
+ // 错误
|
|
|
+ if (true !== $valid){
|
|
|
+ return $this->ApiJson(-1,$valid);
|
|
|
+ }
|
|
|
+
|
|
|
+ $limit = 10;
|
|
|
+
|
|
|
+ // 经纬度升序
|
|
|
+ $lists = model('Stores')
|
|
|
+ ->fieldRaw("* , TRUNCATE(( 6371 * acos (
|
|
|
+ cos ( radians(".$param['lat'].") )
|
|
|
+ * cos( radians( latitude ) )
|
|
|
+ * cos( radians( longitude ) - radians(".$param['lng'].") )
|
|
|
+ + sin ( radians(".$param['lat'].") )
|
|
|
+ * sin( radians( latitude ) )
|
|
|
+ )
|
|
|
+ ), 2) AS distance")
|
|
|
+ ->having('distance < 20')
|
|
|
+ ->order(['distance' => 'ASC'])
|
|
|
+ ->where(['status' => 1])
|
|
|
+ ->limit((($param['page'] - 1) * $limit) . "," . $limit)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ return $this->ApiJson(0,'', $lists);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取门店详情
|
|
|
+ *
|
|
|
+ * @url /store/:id
|
|
|
+ * @param $id
|
|
|
+ *
|
|
|
+ * @return \think\response\Json
|
|
|
+ * @throws \Lettered\Support\Exceptions\EvidentException
|
|
|
+ */
|
|
|
+ public function getStoreById($id)
|
|
|
+ {
|
|
|
+ (new IDMustBePositiveInt())->valid();
|
|
|
+
|
|
|
+ $info = model('Stores')::get($id);
|
|
|
+ if (!$info){
|
|
|
+ return $this->ApiJson(-1,'此门店不存在或已被禁封!');
|
|
|
+ }
|
|
|
+ return $this->ApiJson(0,'', $info);
|
|
|
+ }
|
|
|
+}
|