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['image'] = !empty($value['image']) ? cdnurl($value['image']) : ''; $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; } } /** * 详情 * @return void */ public function getInfo() { try { $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours'; $where['status'] = 1; $result = $this->model->where($where)->field($field)->select(); if (!empty($result)) { !empty($result['image']) && $result['image'] = cdnurl($result['image']); } $this->success('获取成功',$result); } catch (Exception $e) { $this->error($e->getMessage()); } } /** * 绑定门店 * @return void */ public function bind() { Db::startTrans(); try { $id = $this->request->param('id',0); $where['id'] = $id; $where['status'] = 1; $company = $this->model->where($where)->find(); if (empty($company)) { throw new Exception('未找到门店信息'); } $user = $this->auth->getUser(); $userId = $this->auth->id; if ($user['company_id'] != $id) {//更新绑定门店 $userWhere['id'] = $userId; $userData['company_id'] = $id; $userRes = model('User')->where($userWhere)->update($userData); if (!$userRes) { throw new Exception('绑定门店失败'); } } //用户门店钱包 $walletWhere['user_id'] = $userId; $walletWhere['company_id'] = $id; $userWallet = model('UserWallet')->where($walletWhere)->find(); if (empty($userWallet)) { $walletData = [ 'user_id' => $userId, 'company_id' => $id, 'money' => 0.00, ]; $walletRes = model('UserWallet')->insertGetId($walletData); if (!$walletRes) { throw new Exception('绑定失败'); } } Db::commit(); $this->success(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } }