Companys.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. class Companys extends Api
  6. {
  7. protected $noNeedLogin = [];
  8. protected $noNeedRight = '*';
  9. protected $model = null;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->model = Db::name('company');
  14. }
  15. /**
  16. * 列表
  17. * @return void
  18. */
  19. public function getList()
  20. {
  21. try {
  22. $page = $this->request->param('page',1);
  23. $limit = $this->request->param('listrow',15);
  24. $offset = ($page - 1) * $limit;
  25. //获取门店信息
  26. $params = [
  27. 'lng' => $this->request->param('lng',''),
  28. 'lat' => $this->request->param('lat',''),
  29. ];
  30. $companyList = $this->getCompanySort($params);
  31. $result = array_slice($companyList['data'],$offset,$limit);
  32. $this->success('获取成功',$result);
  33. } catch (Exception $e) {
  34. $this->error($e->getMessage());
  35. }
  36. }
  37. /**
  38. * 获取门店距离排序
  39. * @return void
  40. */
  41. public function getCompanySort($params=[])
  42. {
  43. try {
  44. $result = [
  45. 'status' => true,
  46. 'msg' => '获取成功',
  47. 'data' => [],
  48. ];
  49. $lng = isset($params['lng']) ? $params['lng'] : '';
  50. $lat = isset($params['lat']) ? $params['lat'] : '';
  51. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  52. $where['status'] = 1;
  53. $data = $this->model->where($where)->field($field)->select();
  54. if (!empty($data)) {
  55. foreach ($data as $key => &$value) {
  56. $value['distance'] = getDistance($lng, $lat, $value['longitude'], $value['latitude'], true, 2);
  57. $value['distance_text'] = !empty($value['distance']) ? $value['distance'].'km' : '';
  58. }
  59. $n = count($data);
  60. for($i=1;$i<$n;$i++) { //从第二个元素开始插入
  61. for($j=$i-1;$j>=0;$j--) { //与前面的数比较,找到插入的位置
  62. if($data[$j]['distance'] > $data[$j+1]['distance']) { //比前面的数小,交换位置
  63. $tmp = $data[$j];
  64. $data[$j] = $data[$j+1];
  65. $data[$j+1] = $tmp;
  66. } else { //大于或等于前面的数,表示已找到插入的位置
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. $result['data'] = $data;
  73. return $result;
  74. } catch (Exception $e) {
  75. $result['status'] = 0;
  76. $result['msg'] = $e->getMessage();
  77. return $result;
  78. }
  79. }
  80. }