- 新增上传分类归一化处理,统一白名单校验规则 - 新增COS存储记录同步逻辑 - 新增分享功能支持,配置多页面分享参数 - 新增订单案例分享按钮 - 新增订单自动校验和回收命令 - 新增测试脚本和图片同步脚本 - 优化后台上传分类配置,统一分类映射
190 行
6.5 KiB
PHP
190 行
6.5 KiB
PHP
<?php
|
||
|
||
namespace app\common\library;
|
||
|
||
use think\Config;
|
||
use think\Log;
|
||
|
||
/**
|
||
* 天地图(Tianditu)地理位置服务组件
|
||
* 提供高精度的正/逆地理编码、物理测距以及现场打卡异地防作弊核算
|
||
*/
|
||
class TiandituService
|
||
{
|
||
/** 默认天地图服务 Key(可在 application/config.php 或后台全局配置覆盖) */
|
||
const DEFAULT_KEY = '369ea1edc677b9e3746093f8d9dc4465';
|
||
|
||
/**
|
||
* 获取天地图 Key
|
||
* @return string
|
||
*/
|
||
public static function getKey()
|
||
{
|
||
$key = Config::get('site.tianditu_key');
|
||
if (empty($key)) {
|
||
$key = self::DEFAULT_KEY;
|
||
}
|
||
return $key;
|
||
}
|
||
|
||
/**
|
||
* 正地理编码:输入结构化地址,返回高精度经纬度
|
||
* @param string $address 详细地址,如“成都市武侯区天府三街腾讯大厦”
|
||
* @return array|null ['lng' => 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;
|
||
}
|
||
}
|