where('is_check', '=', $is_check); // 商家id $shop_supplier_id && $model = $model->where('shop_supplier_id', '=', $shop_supplier_id); // 获取数量 $limit != false && $model = $model->limit($limit); // 获取门店列表数据 $data = $model->where('is_delete', '=', '0') ->where('status', '=', '1') ->order(['sort' => 'asc', 'create_time' => 'desc']) ->select(); // 根据距离排序 return $this->sortByDistance($data, $longitude, $latitude); } /** * 获取平台门店列表 */ public function getChoiceLists($is_check = null, $longitude = '', $latitude = '', $limit = false, $shop_supplier_id = 0) { $model = $this; // 是否支持自提核销 $is_check && $model = $model->where('is_check', '=', $is_check); // 商家id $shop_supplier_id && $model = $model->where('shop_supplier_id', '=', $shop_supplier_id); // 获取数量 $limit != false && $model = $model->limit($limit); $distanceSql = ''; $sort = ['sort' => 'asc', 'create_time' => 'desc']; if (!empty($longitude) && !empty($latitude)) { $fieldSql = "*,".\think\facade\Db::Raw("(6378.138 * 2 * asin(sqrt(pow(sin((latitude * pi() / 180 - " . $latitude . " * pi() / 180) / 2),2) + cos(latitude * pi() / 180) * cos(" . $latitude . " * pi() / 180) * pow(sin((longitude * pi() / 180 - " . $longitude . " * pi() / 180) / 2),2))) * 1000) as distance"); $sort['distance'] = 'asc'; }else { $fieldSql = '*'; } // 获取门店列表数据 $list = $model->with(['logo','supplier']) ->where('is_delete', '=', '0') ->where('status', '=', '1') ->field($fieldSql) ->order($sort) ->paginate() ->each(function ($store, $k){ $store['distance'] = round($store['distance'], 3); $store['shop_business'] = 0; $shop_hours = isset($store['shop_hours'])? $store['shop_hours'] : ''; $shop_hours = explode('~', $shop_hours); $start = isset($shop_hours[0])? $shop_hours[0] : ''; $end = isset($shop_hours[1])? $shop_hours[1] : ''; if($start && date('H:i') >= $start){ if($end && date('H:i') <= $end || empty($end)){ $store['shop_business'] = 1; } } if($store['distance'] > 0){ if ($store['distance'] >= 1000) { $distance = bcdiv($store['distance'], 1000, 2); $store['distance_unit'] = $distance . 'km'; } else $store['distance_unit'] = round($store['distance'], 2) . 'm'; }else{ $store['distance_unit'] = '未知'; } }); return $list; } /** * 根据距离排序 */ private function sortByDistance(&$data, $longitude, $latitude) { // 根据距离排序 $list = $data->isEmpty() ? [] : $data->toArray(); $sortArr = []; foreach ($list as &$store) { /* $logo_image_id = isset($store['logo_image_id'])? $store['logo_image_id'] : 0; $store['logo_image'] = ''; if($logo_image_id){ $store['logo_image'] = }*/ if (!empty($longitude) && !empty($latitude)) { // 计算距离 $distance = self::getDistance($longitude, $latitude, $store['longitude'], $store['latitude']); // 排序列 $sortArr[] = $distance; $store['distance'] = $distance; if ($distance >= 1000) { $distance = bcdiv($distance, 1000, 2); $store['distance_unit'] = $distance . 'km'; } else $store['distance_unit'] = $distance . 'm'; } else { $store['distance'] = '0.00'; $store['distance_unit'] = '未知'; } } if (!empty($longitude) && !empty($latitude)) { // 根据距离排序 array_multisort($sortArr, SORT_ASC, $list); } return $list; } /** * 获取两个坐标点的距离 */ private static function getDistance($ulon, $ulat, $slon, $slat) { // 地球半径 $R = 6378137; // 将角度转为狐度 $radLat1 = deg2rad($ulat); $radLat2 = deg2rad($slat); $radLng1 = deg2rad($ulon); $radLng2 = deg2rad($slon); // 结果 $s = acos(cos($radLat1) * cos($radLat2) * cos($radLng1 - $radLng2) + sin($radLat1) * sin($radLat2)) * $R; // 精度 $s = round($s * 10000) / 10000; return round($s); } /** * 根据门店id集获取门店列表 */ public function getListByIds($storeIds) { $model = $this; // 筛选条件 $filter = ['store_id' => ['in', $storeIds]]; if (!empty($storeIds)) { $model = $model->orderRaw('field(store_id, ' . implode(',', $storeIds) . ')'); } // 获取商品列表数据 return $model->with(['logo']) ->where('is_delete', '=', '0') ->where('status', '=', '1') ->where($filter) ->select(); } }