Skip to content

Commit

Permalink
[打卡]更新架构
Browse files Browse the repository at this point in the history
  • Loading branch information
洪宇轩 committed Oct 11, 2023
1 parent 18728e1 commit ec19a80
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 124 deletions.
2 changes: 2 additions & 0 deletions ClassCheckin/add_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
$task_name = $_POST["task_name"];
$task_deadline = time() + $_POST["ddl"]*60;
$task_filename = "tasks/$task_name.txt";
$record_filename = "records/$task_name.txt";
file_put_contents($task_filename, $task_deadline);
file_put_contents($record_filename, "开始登记打卡\n");
echo "任务已添加成功!";
}
?>
136 changes: 135 additions & 1 deletion ClassCheckin/index.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,125 @@
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$selected_task = $_POST["selected_task"];
$username = $_POST["username"];
$timestamp = date("Y-m-d H:i:s");
$ip = $_SERVER["REMOTE_ADDR"];

// 检查用户是否存在
if (!isUserValid($username)) {
$error = "用户不存在!";
$err = "1";
}

function wgs84_to_gcj02($lat, $lon) {
$a = 6378245.0; // 长半轴
$ee = 0.00669342162296594323; // 扁率

if (out_of_china($lat, $lon)) {
return array('lat' => $lat, 'lon' => $lon);
}

$dLat = transformLat($lon - 105.0, $lat - 35.0);
$dLon = transformLon($lon - 105.0, $lat - 35.0);
$radLat = $lat / 180.0 * M_PI;
$magic = sin($radLat);
$magic = 1 - $ee * $magic * $magic;
$sqrtMagic = sqrt($magic);
$dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * M_PI);
$dLon = ($dLon * 180.0) / ($a / $sqrtMagic * cos($radLat) * M_PI);

$mgLat = $lat + $dLat;
$mgLon = $lon + $dLon;

return array('lat' => $mgLat, 'lon' => $mgLon);
}

function out_of_china($lat, $lon) {
return !($lon > 73.66 && $lon < 135.05 && $lat > 3.86 && $lat < 53.55);
}

function transformLat($x, $y) {
$ret = -100.0 + 2.0 * $x + 3.0 * $y + 0.2 * $y * $y + 0.1 * $x * $y + 0.2 * sqrt(abs($x));
$ret += (20.0 * sin(6.0 * $x * M_PI) + 20.0 * sin(2.0 * $x * M_PI)) * 2.0 / 3.0;
$ret += (20.0 * sin($y * M_PI) + 40.0 * sin($y / 3.0 * M_PI)) * 2.0 / 3.0;
$ret += (160.0 * sin($y / 12.0 * M_PI) + 320 * sin($y * M_PI / 30.0)) * 2.0 / 3.0;
return $ret;
}

function transformLon($x, $y) {
$ret = 300.0 + $x + 2.0 * $y + 0.1 * $x * $x + 0.1 * $x * $y + 0.1 * sqrt(abs($x));
$ret += (20.0 * sin(6.0 * $x * M_PI) + 20.0 * sin(2.0 * $x * M_PI)) * 2.0 / 3.0;
$ret += (20.0 * sin($x * M_PI) + 40.0 * sin($x / 3.0 * M_PI)) * 2.0 / 3.0;
$ret += (150.0 * sin($x / 12.0 * M_PI) + 300.0 * sin($x / 30.0 * M_PI)) * 2.0 / 3.0;
return $ret;
}

if (isset($_POST["latitude"]) && isset($_POST["longitude"])) {
// 获取经度和纬度
$result = wgs84_to_gcj02($_POST["latitude"], $_POST["longitude"]);

$latitude = number_format($result['lat'], 6);
$longitude = number_format($result['lon'] , 6);
// 构建高德地图API的逆地理编码请求URL
$api_url = "https://restapi.amap.com/v3/geocode/regeo?key=APIKEY&radius=10&extensions=0&roadlevel=0&location={$longitude},{$latitude}";
// 发送HTTP请求
$response = file_get_contents($api_url);
// 解析JSON响应
$data = json_decode($response, true);
if ($data["status"] === "1") {
// 提取出地点名称
$location = $data["regeocode"]["formatted_address"];
$location_mod = "位置:{$location}";
} else {
$location_mod = "无法获取地点信息";
}
} else {
$location_mod = "未收到经度和纬度信息";
}

// 获取任务截止时间
$task_deadline = file_get_contents("tasks/$selected_task.txt");
$current_time = time();

// 检查是否超过任务截止时间
if ($current_time > $task_deadline && (!isset($err))) {
$error = "任务已经过期,无法打卡!";
$err = "1";
}

// 任务的打卡记录文件
$record_filename = "records/$selected_task.txt";

$lat = $result['lat'];
$lon = $result['lon'];
// 打卡记录
$record_data = "用户:$username 时间:$timestamp 位置信息:经度$lon 纬度$lat $location_mod IP:$ip\n";

$records = file_get_contents($record_filename);

if (strpos($records, $username) !== false) {
$error = "请勿重复打卡!";
$err = "1";
} else {
if (!isset($err)) {
file_put_contents($record_filename, $record_data, FILE_APPEND);
}
}

if (!isset($err)) {
session_start();
$_SESSION['checkuser'] = $username;
$_SESSION['selected_task'] = $selected_task;
header("Location: success.php");
}
}

function isUserValid($username) {
$users = file("user.list", FILE_IGNORE_NEW_LINES);
return in_array($username, $users);
}
?>

<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -90,6 +212,12 @@
button:hover {
background-color: #0056b3;
}

notice {
color: red;
font-size: 20px;
font-weight: bold;
}
</style>
<script>
window.onload = getLocation;
Expand All @@ -111,7 +239,7 @@ function showPosition(position) {
</head>
<body>
<h1>计师2班打卡</h1>
<form action="record.php" method="post">
<form action="index.php" method="post">
<label for="selected_task">课程</label>
<select name="selected_task" id="selected_task">
<?php
Expand All @@ -133,9 +261,15 @@ function showPosition(position) {
<input type="hidden" id="latitude" name="latitude">
<input type="hidden" id="longitude" name="longitude">
<input type="submit" value="打卡">
<?php
if (isset($error)) {
echo "<notice>{$error}</notice><br>";
}
?>
<button onclick="location.href='backend.php'">后台管理</button>
</form>
<!-- 底部声明 -->
<div class="footer">平台开发:hyx<br>2023.10<br>温馨提示:有用户特征记录,请勿帮他人打卡</div>
</body>
</html>

123 changes: 0 additions & 123 deletions ClassCheckin/record.php

This file was deleted.

0 comments on commit ec19a80

Please sign in to comment.