123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- class Companys extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = Db::name('company');
- }
- /**
- * 列表
- * @return void
- */
- public function getList()
- {
- try {
- $page = $this->request->param('page',1);
- $limit = $this->request->param('listrow',15);
- $offset = ($page - 1) * $limit;
- //获取门店信息
- $params = [
- 'lng' => $this->request->param('lng',''),
- 'lat' => $this->request->param('lat',''),
- ];
- $companyList = $this->getCompanySort($params);
- $result = array_slice($companyList['data'],$offset,$limit);
- $this->success('获取成功',$result);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
-
- /**
- * 获取门店距离排序
- * @return void
- */
- public function getCompanySort($params=[])
- {
- try {
- $result = [
- 'status' => true,
- 'msg' => '获取成功',
- 'data' => [],
- ];
- $lng = isset($params['lng']) ? $params['lng'] : '';
- $lat = isset($params['lat']) ? $params['lat'] : '';
- $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
- $where['status'] = 1;
- $data = $this->model->where($where)->field($field)->select();
- if (!empty($data)) {
- foreach ($data as $key => &$value) {
- $value['distance'] = getDistance($lng, $lat, $value['longitude'], $value['latitude'], true, 2);
- $value['distance_text'] = !empty($value['distance']) ? $value['distance'].'km' : '';
- }
- $n = count($data);
- for($i=1;$i<$n;$i++) { //从第二个元素开始插入
- for($j=$i-1;$j>=0;$j--) { //与前面的数比较,找到插入的位置
- if($data[$j]['distance'] > $data[$j+1]['distance']) { //比前面的数小,交换位置
- $tmp = $data[$j];
- $data[$j] = $data[$j+1];
- $data[$j+1] = $tmp;
- } else { //大于或等于前面的数,表示已找到插入的位置
- break;
- }
- }
- }
- }
- $result['data'] = $data;
- return $result;
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- return $result;
- }
- }
- }
|