float, 'lat' => float, 'level' => string] */ public static function geocoder($address) { $address = trim((string)$address); if (empty($address)) { return null; } $key = self::getKey(); $url = 'https://api.tianditu.gov.cn/geocoder?ds=' . urlencode(json_encode(['keyWord' => $address])) . '&tk=' . $key; $response = self::httpGet($url); if (!$response) { return null; } $data = json_decode($response, true); if (isset($data['status']) && $data['status'] === '0' && !empty($data['location'])) { return [ 'lng' => (float)$data['location']['lon'], 'lat' => (float)$data['location']['lat'], 'level' => isset($data['location']['level']) ? $data['location']['level'] : '', ]; } Log::record('[TiandituService] geocoder failed for address: ' . $address . ', resp: ' . $response, 'notice'); return null; } /** * 逆地理编码:输入经纬度,返回行政区划与标准门牌地址 * @param float $lat 纬度 * @param float $lng 经度 * @return array|null */ public static function regeocoder($lat, $lng) { $lat = (float)$lat; $lng = (float)$lng; if (abs($lat) < 0.0001 && abs($lng) < 0.0001) { return null; } $key = self::getKey(); $postData = [ 'postStr' => json_encode([ 'lon' => $lng, 'lat' => $lat, 'ver' => 1 ]), 'type' => 'geocode', 'tk' => $key ]; $url = 'https://api.tianditu.gov.cn/geocoder?' . http_build_query($postData); $response = self::httpGet($url); if (!$response) { return null; } $data = json_decode($response, true); if (isset($data['status']) && $data['status'] === '0' && isset($data['result'])) { $comp = isset($data['result']['addressComponent']) ? $data['result']['addressComponent'] : []; return [ 'formatted_address' => isset($data['result']['formatted_address']) ? $data['result']['formatted_address'] : '', 'province' => isset($comp['province']) ? $comp['province'] : '', 'city' => isset($comp['city']) ? $comp['city'] : '', 'district' => isset($comp['county']) ? $comp['county'] : '', 'poi' => isset($comp['poi']) ? $comp['poi'] : '', 'road' => isset($comp['road']) ? $comp['road'] : '', ]; } Log::record("[TiandituService] regeocoder failed for ({$lat}, {$lng}), resp: " . $response, 'notice'); return null; } /** * 高精度两点大圆球面距离计算(Haversine Formula) * @param float $lat1 起点纬度 * @param float $lng1 起点经度 * @param float $lat2 终点纬度 * @param float $lng2 终点经度 * @return float 距离(单位:米,保留1位小数) */ public static function distance($lat1, $lng1, $lat2, $lng2) { $lat1 = (float)$lat1; $lng1 = (float)$lng1; $lat2 = (float)$lat2; $lng2 = (float)$lng2; if (abs($lat1 - $lat2) < 0.0000001 && abs($lng1 - $lng2) < 0.0000001) { return 0.0; } $earthRadius = 6378137.0; // 地球半径(米) $radLat1 = deg2rad($lat1); $radLat2 = deg2rad($lat2); $deltaLat = $radLat1 - $radLat2; $deltaLng = deg2rad($lng1) - deg2rad($lng2); $val = 2 * asin(sqrt(pow(sin($deltaLat / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($deltaLng / 2), 2))); $dist = $val * $earthRadius; return round($dist, 1); } /** * 异地打卡防作弊检测 * 判断师傅现场签到 GPS 与订单下单地址物理距离是否在允许安全半径内 * @param float $orderLat 订单设定的纬度 * @param float $orderLng 订单设定的经度 * @param float $punchLat 师傅签到的纬度 * @param float $punchLng 师傅签到的经度 * @param float $allowedDistance 允许的最大打卡偏差(米),默认 1000 米 * @return array ['is_pass' => bool, 'distance' => float, 'reason' => string] */ public static function checkArriveDistance($orderLat, $orderLng, $punchLat, $punchLng, $allowedDistance = 1000.0) { // 若订单未维护或无经纬度,宽容放行但记录无基准 if (abs((float)$orderLat) < 0.0001 || abs((float)$orderLng) < 0.0001) { return [ 'is_pass' => true, 'distance' => 0.0, 'reason' => '订单无基准经纬度,跳过距离校验' ]; } $dist = self::distance($orderLat, $orderLng, $punchLat, $punchLng); $isPass = ($dist <= $allowedDistance); return [ 'is_pass' => $isPass, 'distance' => $dist, 'reason' => $isPass ? '签到位置在合理范围内' : "签到定位与订单地址相距 {$dist} 米,超过 {$allowedDistance} 米限制" ]; } /** * 发送轻量 HTTP GET 请求 */ protected static function httpGet($url, $timeout = 3) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $result = curl_exec($ch); curl_close($ch); return $result; } }