Autosave: 20260322-051230
This commit is contained in:
parent
0741c9a2a3
commit
628da1f171
19
admin.php
19
admin.php
@ -506,7 +506,8 @@ $stats = [
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-footer bg-white p-3 border-top">
|
||||
<form id="adminChatForm" onsubmit="event.preventDefault(); sendMessage();" class="input-group">
|
||||
<input type="file" id="adminImageInput" style="display:none" accept="image/*">
|
||||
<button type="button" class="btn btn-outline-secondary border-end-0" onclick="document.getElementById('adminImageInput').click()"><i class="fas fa-image"></i></button> <form id="adminChatForm" onsubmit="event.preventDefault(); sendMessage();" class="input-group">
|
||||
<input type="text" id="adminMsgInput" class="form-control border-end-0" placeholder="输入回复内容..." autocomplete="off">
|
||||
<button type="submit" class="btn btn-primary px-4 border-start-0"><i class="fas fa-paper-plane"></i></button>
|
||||
</form>
|
||||
@ -563,6 +564,20 @@ $stats = [
|
||||
loadMessages();
|
||||
loadChatUsers();
|
||||
document.getElementById('adminMsgInput').focus();
|
||||
document.getElementById("adminImageInput").addEventListener("change", async (e) => {
|
||||
const file = e.target.files[0];
|
||||
if (!file) return;
|
||||
const formData = new FormData();
|
||||
formData.append("image", file);
|
||||
const res = await fetch("ajax_handler.php?action=upload_image", { method: "POST", body: formData });
|
||||
const data = await res.json();
|
||||
if (data.code === 0) {
|
||||
const msgData = new FormData();
|
||||
msgData.append("message", data.url);
|
||||
msgData.append("user_id", currentChatUser);
|
||||
await fetch("ajax_handler.php?action=send_message", { method: "POST", body: msgData });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function loadMessages() {
|
||||
@ -591,7 +606,7 @@ $stats = [
|
||||
|
||||
function appendMessageToUI(m) {
|
||||
const content = document.getElementById('chatContent');
|
||||
const isMe = m.sender === 'admin';
|
||||
const isMe = m.sender === "admin";
|
||||
const div = document.createElement('div');
|
||||
div.className = `message-row ${isMe ? 'me' : 'them'}`;
|
||||
|
||||
|
||||
@ -133,6 +133,17 @@ try {
|
||||
echo json_encode(['code' => 0, 'status' => 'pending']);
|
||||
break;
|
||||
|
||||
case "upload_image":
|
||||
$file = $_FILES["image"] ?? null;
|
||||
if ($file) {
|
||||
$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
|
||||
$name = "uploads/" . bin2hex(random_bytes(8)) . "." . $ext;
|
||||
move_uploaded_file($file["tmp_name"], __DIR__ . "/" . $name);
|
||||
echo json_encode(["code" => 0, "url" => $name]);
|
||||
} else {
|
||||
echo json_encode(["code" => 400, "msg" => "上传失败"]);
|
||||
}
|
||||
break;
|
||||
case 'send_message':
|
||||
$message = trim($_POST['message'] ?? '');
|
||||
$target_user_id = $_POST['user_id'] ?? $_SESSION['user_id'];
|
||||
|
||||
@ -204,7 +204,8 @@ $user = $stmt->fetch();
|
||||
<!-- Messages loaded via JS -->
|
||||
</div>
|
||||
<div class="chat-footer">
|
||||
<form id="chatForm" class="d-flex gap-2">
|
||||
<input type="file" id="imageInput" style="display:none" accept="image/*">
|
||||
<button type="button" class="btn btn-outline-secondary" onclick="document.getElementById('imageInput').click()"><i class="fas fa-image"></i></button> <form id="chatForm" class="d-flex gap-2">
|
||||
<input type="text" id="msgInput" class="form-control" placeholder="输入您的问题..." required autocomplete="off">
|
||||
<button type="submit" class="btn btn-send"><i class="fas fa-paper-plane"></i></button>
|
||||
</form>
|
||||
@ -253,9 +254,9 @@ $user = $stmt->fetch();
|
||||
|
||||
function appendMessage(msg) {
|
||||
const row = document.createElement('div');
|
||||
row.className = `message-row ${msg.sender === 'user' ? 'me' : 'them'}`;
|
||||
row.className = `message-row ${msg.sender === "user" ? "me" : "them"}`;
|
||||
|
||||
const time = new Date(msg.created_at).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
|
||||
const time = new Date(msg.created_at.replace(" ", "T")).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
|
||||
|
||||
row.innerHTML = `
|
||||
<div class="message-bubble">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user