| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\ApiController;
- use app\common\validate\IDMustBePositiveInt;
- class Store 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;
- // 经纬度升序
- $sellers = model('Seller')
- ->fieldRaw("* , TRUNCATE(( 6371 * acos (
- cos ( radians(".$param['lat'].") )
- * cos( radians( lat ) )
- * cos( radians( lng ) - radians(".$param['lng'].") )
- + sin ( radians(".$param['lat'].") )
- * sin( radians( lat ) )
- )
- ), 2) AS distance")
- ->having('distance < 20')
- ->order(['distance' => 'ASC'])
- ->where(['is_allow' => 2])
- ->where(['status' => 1])
- ->limit((($param['page'] - 1) * $limit) . "," . $limit)
- ->select();
- return $this->ApiJson(0,'', $sellers);
- }
- /**
- * 获取商户详情
- *
- * @url /store/:id
- * @param $id
- *
- * @return \think\response\Json
- * @throws \Lettered\Support\Exceptions\EvidentException
- */
- public function getStoreById($id)
- {
- (new IDMustBePositiveInt())->valid();
- $seller = model('Seller')::get($id);
- if (!$seller){
- return $this->ApiJson(-1,'此商户不存在或已被禁封!');
- }
- return $this->ApiJson(0,'', $seller);
- }
- }
|