文件
mashangyoujiu/_ai_archive/scripts/test_fence_api.php

103 行
3.1 KiB
PHP

<?php
/**
* 电子围栏功能测试文件
* 用于测试点在多边形内的算法和接口功能
*/
// 引入必要的文件
require_once './Application/SmallProgramApi/Helper/FenceHelper.class.php';
// 测试数据
$testPoints = [
['lng' => 116.397428, 'lat' => 39.90923, 'name' => '北京天安门'],
['lng' => 121.473701, 'lat' => 31.230416, 'name' => '上海外滩'],
['lng' => 113.280637, 'lat' => 23.125178, 'name' => '广州塔'],
];
// 测试多边形(北京市中心区域)
$testPolygon = [
['lng' => 116.380, 'lat' => 39.900],
['lng' => 116.420, 'lat' => 39.900],
['lng' => 116.420, 'lat' => 39.920],
['lng' => 116.380, 'lat' => 39.920],
];
// 测试围栏数据(新格式)
$testFences = [
[
'fence_name' => '北京配送区域',
'fence_type' => 1,
'is_active' => 1,
'fence_data' => [
[
'id' => 'polygon_1',
'paths' => [
['lat' => 39.900, 'lng' => 116.380],
['lat' => 39.900, 'lng' => 116.420],
['lat' => 39.920, 'lng' => 116.420],
['lat' => 39.920, 'lng' => 116.380],
]
]
]
]
];
echo "<h2>电子围栏功能测试</h2>\n";
// 测试1: 基础点在多边形内算法
echo "<h3>测试1: 基础点在多边形内算法</h3>\n";
foreach ($testPoints as $point) {
$result = \SmallProgramApi\Helper\FenceHelper::isPointInPolygon(
$point['lng'],
$point['lat'],
$testPolygon
);
echo "点 {$point['name']} ({$point['lng']}, {$point['lat']}) ";
echo $result ? "在多边形内" : "不在多边形内";
echo "<br>\n";
}
// 测试2: 围栏检查功能
echo "<h3>测试2: 围栏检查功能</h3>\n";
foreach ($testPoints as $point) {
$result = \SmallProgramApi\Helper\FenceHelper::checkPointInFences(
$point['lng'],
$point['lat'],
$testFences
);
echo "点 {$point['name']} ({$point['lng']}, {$point['lat']}) ";
if ($result['in_fence']) {
echo "在围栏内 - {$result['fence_name']}";
} else {
echo "不在围栏内";
}
echo "<br>\n";
}
// 测试3: 边界情况
echo "<h3>测试3: 边界情况测试</h3>\n";
$boundaryPoints = [
['lng' => 116.380, 'lat' => 39.900, 'name' => '左下角'],
['lng' => 116.420, 'lat' => 39.920, 'name' => '右上角'],
['lng' => 116.400, 'lat' => 39.910, 'name' => '中心点'],
];
foreach ($boundaryPoints as $point) {
$result = \SmallProgramApi\Helper\FenceHelper::checkPointInFences(
$point['lng'],
$point['lat'],
$testFences
);
echo "边界点 {$point['name']} ({$point['lng']}, {$point['lat']}) ";
if ($result['in_fence']) {
echo "在围栏内 - {$result['fence_name']}";
} else {
echo "不在围栏内";
}
echo "<br>\n";
}
echo "<h3>测试完成</h3>\n";
echo "如果看到此消息,说明电子围栏辅助类已成功创建并可以正常工作。<br>\n";
echo "接下来可以通过实际的API接口进行测试。<br>\n";
?>