Store.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\ApiController;
  4. use app\common\validate\IDMustBePositiveInt;
  5. class Store extends ApiController
  6. {
  7. /**
  8. * 获取附近的商家
  9. *
  10. * @url /store/nearby?lng=118.302416&lat=33.958887&page=1&limit=10
  11. *
  12. * @return bool|\think\response\Json
  13. * @throws \think\db\exception\BindParamException
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. * @throws \think\exception\PDOException
  18. */
  19. public function getNearbyCol()
  20. {
  21. // 1. 传入用户位置
  22. $param = $this->request->param();
  23. // 数据校验
  24. $valid = $this->validate($param, [
  25. 'lng' => ['require','regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
  26. 'lat' => ['require','regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
  27. 'page' => 'require',
  28. ],[
  29. 'lng.require' => '缺少经度参数',
  30. 'lng.regex' => '经度参数有误',
  31. 'lat.require' => '缺少维度参数',
  32. 'lat.regex' => '维度参数有误'
  33. ]);
  34. // 错误
  35. if (true !== $valid){
  36. return $this->ApiJson(-1,$valid);
  37. }
  38. $limit = 10;
  39. // 经纬度升序
  40. $sellers = model('Seller')
  41. ->fieldRaw("* , TRUNCATE(( 6371 * acos (
  42. cos ( radians(".$param['lat'].") )
  43. * cos( radians( lat ) )
  44. * cos( radians( lng ) - radians(".$param['lng'].") )
  45. + sin ( radians(".$param['lat'].") )
  46. * sin( radians( lat ) )
  47. )
  48. ), 2) AS distance")
  49. ->having('distance < 20')
  50. ->order(['distance' => 'ASC'])
  51. ->where(['is_allow' => 2])
  52. ->where(['status' => 1])
  53. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  54. ->select();
  55. return $this->ApiJson(0,'', $sellers);
  56. }
  57. /**
  58. * 获取商户详情
  59. *
  60. * @url /store/:id
  61. * @param $id
  62. *
  63. * @return \think\response\Json
  64. * @throws \Lettered\Support\Exceptions\EvidentException
  65. */
  66. public function getStoreById($id)
  67. {
  68. (new IDMustBePositiveInt())->valid();
  69. $seller = model('Seller')::get($id);
  70. if (!$seller){
  71. return $this->ApiJson(-1,'此商户不存在或已被禁封!');
  72. }
  73. return $this->ApiJson(0,'', $seller);
  74. }
  75. }