打卡优化

main
Jing Li 2024-05-01 13:36:54 +08:00
parent 498a2df89d
commit 655c2fa1d8
3 changed files with 22 additions and 12 deletions

View File

@ -40,7 +40,7 @@ class SettingController extends AdminController
amis()->SwitchControl('oss_config.use_ssl', '开启SSL')->value(false)->visibleOn('${upload_disk == "oss"}'),
]),
Tab::make()->title('打卡设置')->body([
amisMake()->NumberControl()->min(0)->name('sign.distance')->label('允许打卡的距离(公里)')
amis()->NumberControl()->min(0)->name('sign.distance')->label('允许打卡的距离()')
]),
])
);

View File

@ -91,7 +91,7 @@ class EmployeeSignService extends BaseService
/**
* 打卡
*
*
* @param Employee $user 用户
* @param SignTime $time 上班/下班 打卡
* @param mixed $date 打卡时间
@ -147,28 +147,28 @@ class EmployeeSignService extends BaseService
}
/**
* 计算两点之间的距离()
* 计算两点之间的距离()
*/
public function haversineDistance($lat1, $lon1, $lat2, $lon2)
{
// 地球半径(千米)
$R = 6371;
// 将角度转换为弧度
$lat1 = deg2rad($lat1);
$lon1 = deg2rad($lon1);
$lat2 = deg2rad($lat2);
$lon2 = deg2rad($lon2);
// 经纬度差值
$dlon = $lon2 - $lon1;
$dlat = $lat2 - $lat1;
// Calculate the Haversine formula
$a = pow(sin($dlat/2), 2) + cos($lat1) * cos($lat2) * pow(sin($dlon/2), 2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$distance = $R * $c;
return floor($distance * 1000) / 1000;
return floor($distance * 1000);
}
}

View File

@ -95,11 +95,21 @@ class SignController extends Controller
$type = SignType::from($request->input('type'));
// 根据定位的距离判断, 是否需要外勤打卡
$maxDistance = AdminSettingService::make()->arrayGet('sign', 'distance', 0);
$distance = $service->haversineDistance($request->input('position.lat'), $request->input('position.lon'), $store->lat, $store->lon);
if ($distance > $maxDistance && $type == SignType::Normal) {
throw new RuntimeException('当前位置不在考勤范围内, 请选择外勤打卡');
$maxDistance = (int) AdminSettingService::make()->arrayGet('sign', 'distance');
if ($maxDistance > 0 && $type == SignType::Normal) {
$distance = $service->haversineDistance(
$request->input('position.lat'),
$request->input('position.lon'),
$store->lat,
$store->lon,
);
if ($distance > $maxDistance) {
throw new RuntimeException('当前位置不在考勤范围内, 请选择外勤打卡');
}
}
try {
DB::beginTransaction();
if (!$service->signDay($user, $time, now(), $request->only(['remarks', 'position', 'type']))) {