上传文件至 /

This commit is contained in:
2026-11-27 15:42:12 +08:00
parent ab9c22aae7
commit e8677d6b8f
5 changed files with 563 additions and 0 deletions

338
gallery.php Normal file
View File

@@ -0,0 +1,338 @@
<?php
require_once __DIR__ . '/includes/layout.php';
require_once __DIR__ . '/includes/zones.php';
$user = require_password_ready($conn, require_login($conn));
if ($user['is_admin']) {
header('Location: admin.php');
exit;
}
// 可推送的分区:只列当前用户看得见的,前端不拼数据
$push_zones = visible_zones($conn, $user);
$stmt = $conn->prepare("SELECT i.image_id, i.image_path, sq.status AS square_status, sq.zone_id AS square_zone, sq.title AS square_title
FROM images i LEFT JOIN photos_square sq ON sq.image_id = i.image_id
WHERE i.user_id = ? AND i.is_deleted = 0 ORDER BY i.image_id DESC");
$stmt->bind_param("i", $user['user_id']);
$stmt->execute();
$images = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
$count = count($images);
$points = (int)$user['points'];
render_header('我的图库', $user, 'gallery.php', '共 ' . $count . ' 张图片');
?>
<div class="page-head">
<div>
<div class="page-head__title">我的图库</div>
<div class="page-head__desc">剩余可上传 <strong id="remain"><?php echo $points; ?></strong> 张 · 点击图片查看大图</div>
</div>
<div class="page-head__actions">
<a href="upload.php" class="btn btn--primary" id="uploadNewImage"><?php echo icon('upload', 16); ?>上传新图片</a>
</div>
</div>
<?php if ($count === 0): ?>
<div class="empty">
<div class="empty__icon"><?php echo icon('image', 32); ?></div>
<div class="empty__title">还没有上传任何图片</div>
<div class="empty__desc">上传第一张照片,开始建立你的私人相册。</div>
<a href="upload.php" class="btn btn--primary btn--lg"><?php echo icon('upload', 16); ?>上传第一张</a>
</div>
<?php else: ?>
<div class="masonry" id="grid">
<?php foreach ($images as $img): ?>
<?php $sq = $img['square_status'] ?? null;
$sq_label = ['pending' => '审核中', 'approved' => '广场已上架', 'rejected' => '广场被驳回'][$sq] ?? '';
$sq_badge = ['pending' => 'warning', 'approved' => 'success', 'rejected' => 'danger'][$sq] ?? ''; ?>
<figure class="masonry__item" data-id="<?php echo (int)$img['image_id']; ?>" data-path="<?php echo h($img['image_path']); ?>" data-square="<?php echo $sq ? h($sq) : ''; ?>"
data-zone="<?php echo (int)$img['square_zone']; ?>" data-title="<?php echo h((string)$img['square_title']); ?>">
<img src="<?php echo h($img['image_path']); ?>" alt="图片" loading="lazy" class="masonry__img" onclick="previewImage(this)">
<?php if ($sq_label): ?>
<span class="square-tag badge badge--<?php echo $sq_badge; ?>"><?php echo $sq_label; ?></span>
<?php endif; ?>
<div class="masonry__bar-overlay" role="toolbar">
<button class="icon-btn<?php echo $sq === 'approved' || $sq === 'pending' ? ' is-on' : ''; ?>" onclick="event.stopPropagation();toggleSquare(this)" title="<?php echo $sq === 'approved' || $sq === 'pending' ? '撤回广场推送' : '推送到照片广场'; ?>" aria-label="广场"><?php echo icon('globe', 15); ?></button>
<button class="icon-btn" onclick="event.stopPropagation();copyImage(this)" title="复制图片外链" aria-label="复制图片外链"><?php echo icon('copy', 15); ?></button>
<button class="icon-btn icon-btn--danger" onclick="event.stopPropagation();deleteOne(this)" title="删除" aria-label="删除"><?php echo icon('trash', 15); ?></button>
</div>
<span class="masonry__check-wrap" title="选择">
<input type="checkbox" class="masonry__check" aria-label="选择这张图片">
</span>
</figure>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="selection-bar" id="selectionBar">
<span class="selection-bar__count" id="selCount">已选 0 张</span>
<button class="btn btn--sm btn--danger-solid" id="batchDelete"><?php echo icon('trash', 14); ?>删除所选</button>
<button class="btn btn--sm btn--ghost" id="clearSel">取消</button>
</div>
<div class="modal" id="pushModal">
<div class="modal__dialog" style="max-width:460px">
<div class="modal__head">
<span class="modal__title">推送到照片广场</span>
<button type="button" class="close-btn" data-modal-close aria-label="关闭"><?php echo icon('close'); ?></button>
</div>
<div class="modal__body">
<div class="field">
<label class="label" for="pus_title">标题(可留空)</label>
<input type="text" id="pus_title" class="input" maxlength="40" placeholder="最多 40 个字">
</div>
<div class="field">
<label class="label" for="pus_zone">推送到分区</label>
<select id="pus_zone" class="input"></select>
<div class="help">只能推送到你可见的分区;已上架的作品改了标题或分区会重新进入审核。</div>
</div>
<div class="toolbar" style="margin:0">
<button class="btn btn--primary" id="pus_save" type="button"><?php echo icon('send', 15); ?>推送</button>
<button class="btn btn--danger" id="pus_cancel" type="button" style="display:none"><?php echo icon('trash', 15); ?>从广场撤回</button>
<button class="btn btn--ghost" type="button" data-modal-close>取消</button>
</div>
</div>
</div>
</div>
<div class="modal" id="imageModal">
<div class="modal__dialog">
<div class="modal__head">
<span class="modal__title">图片预览</span>
<button type="button" class="close-btn" data-modal-close aria-label="关闭"><?php echo icon('close'); ?></button>
</div>
<div class="modal__body">
<img id="modalImage" src="" alt="预览">
<div class="copy-row">
<input type="text" id="imageURL" class="input" readonly>
<button class="btn btn--primary" id="copyFromModal" type="button"><?php echo icon('copy', 15); ?>复制</button>
</div>
</div>
</div>
</div>
<script>
var ABS_BASE = location.href.replace(/gallery\.php.*$/, '');
function absoluteUrl(p) {
if (/^https?:\/\//.test(p)) return p;
return ABS_BASE + p;
}
function previewImage(el) {
var item = el.closest('.masonry__item');
document.getElementById('modalImage').src = item.dataset.path;
document.getElementById('imageURL').value = absoluteUrl(item.dataset.path);
openModal(document.getElementById('imageModal'));
}
document.getElementById('copyFromModal').addEventListener('click', function () {
copyText(document.getElementById('imageURL').value)
.then(function () { toast('图片地址已复制'); })
.catch(function () { toast('复制失败,请手动选择', 'danger'); });
});
function copyImage(btn) {
var item = btn.closest('.masonry__item');
copyText(absoluteUrl(item.dataset.path))
.then(function () { toast('图片地址已复制'); })
.catch(function () { toast('复制失败', 'danger'); });
}
function postDelete(ids) {
return $.ajax({
url: 'delete_image.php',
type: 'POST',
data: { ids: ids.join(','), csrf_token: window.CSRF_TOKEN }
});
}
function deleteOne(btn) {
var item = btn.closest('.masonry__item');
if (!confirm('确定删除这张图片吗?删除后会返还 1 次上传额度。')) return;
postDelete([item.dataset.id]).then(function (resp) {
var r = parseResp(resp);
if (r.ok) {
item.remove();
updateAfterDelete(1);
toast('已删除,返还 1 分');
if (!document.querySelector('.masonry__item')) location.reload();
} else {
toast(r.msg, 'danger');
}
});
}
document.getElementById('batchDelete').addEventListener('click', function () {
var ids = Array.prototype.map.call(
document.querySelectorAll('.masonry__item.is-selected'),
function (t) { return t.dataset.id; }
);
if (!ids.length) return;
if (!confirm('确定删除选中的 ' + ids.length + ' 张图片吗?删除后返还 ' + ids.length + ' 分。')) return;
postDelete(ids).then(function (resp) {
var r = parseResp(resp);
if (r.ok) {
toast('已删除 ' + r.deleted + ' 张,返还 ' + r.deleted + ' 分');
setTimeout(function () { location.reload(); }, 700);
} else {
toast(r.msg, 'danger');
}
});
});
document.getElementById('clearSel').addEventListener('click', function () {
document.querySelectorAll('.masonry__check').forEach(function (c) { c.checked = false; });
refreshSelection();
});
function parseResp(data) {
if (typeof data === 'object') return data;
try { return JSON.parse(data); } catch (e) { return { ok: false, msg: '服务器返回异常' }; }
}
function updateAfterDelete(n) {
['remain', 'pointsNow'].forEach(function (id) {
var el = document.getElementById(id);
if (el) el.textContent = parseInt(el.textContent, 10) + n;
});
refreshSelection();
}
function refreshSelection() {
var items = document.querySelectorAll('.masonry__item.is-selected');
var n = items.length;
document.getElementById('selCount').textContent = '已选 ' + n + ' 张';
document.getElementById('selectionBar').classList.toggle('is-open', n > 0);
}
var grid = document.getElementById('grid');
grid && grid.addEventListener('change', function (e) {
if (!e.target.classList.contains('masonry__check')) return;
e.target.closest('.masonry__item').classList.toggle('is-selected', e.target.checked);
refreshSelection();
});
// 推送 / 撤回 照片广场
function toggleSquare(btn) {
var item = btn.closest('.masonry__item');
var id = item.dataset.id;
var st = item.dataset.square || '';
// 已上架:再点地球 = 改标题/换分区(换分区会重新审核),撤回走弹窗里的按钮
if (st === 'approved') {
openPush(item);
return;
}
if (st === 'pending') {
if (!confirm('确定把这张图从照片广场撤回吗?图片仍保留在你的图库。')) return;
$.ajax({ url: 'square_cancel.php', type: 'POST', data: { image_id: id, csrf_token: window.CSRF_TOKEN } })
.done(function (r) {
if (!r.ok) return toast(r.msg, 'danger');
setSquareState(item, '');
toast('已从广场撤回');
}).fail(function () { toast('操作失败', 'danger'); });
return;
}
openPush(item);
}
// 推送弹窗:标题 + 分区。分区列表由服务端算好,这里只渲染
var PUSH_ZONES = <?php echo json_encode(array_map(function ($z) {
return [
'id' => (int)$z['id'],
'name' => $z['name'],
'vis' => zone_visibility_label($z),
];
}, $push_zones), JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?>;
var pushItem = null;
function openPush(item) {
pushItem = item;
var box = document.getElementById('pus_zone');
box.innerHTML = '';
var def = document.createElement('option');
def.value = '0';
def.textContent = '默认分区(综合广场)';
box.appendChild(def);
PUSH_ZONES.forEach(function (z) {
var o = document.createElement('option');
o.value = String(z.id);
o.textContent = z.name + '(' + z.vis + ')';
box.appendChild(o);
});
var cur = parseInt(item.dataset.zone || '0', 10) || 0;
box.value = String(cur);
if (box.value !== String(cur)) box.value = '0'; // 原分区已不可见,退回默认
document.getElementById('pus_title').value = item.dataset.title || '';
// 已推送过的作品才给撤回入口
var st = item.dataset.square || '';
document.getElementById('pus_cancel').style.display = (st === 'pending' || st === 'approved') ? '' : 'none';
openModal(document.getElementById('pushModal'));
}
document.getElementById('pus_cancel').addEventListener('click', function () {
if (!pushItem) return;
if (!confirm('确定从照片广场撤回吗?图片仍保留在你的图库。')) return;
var item = pushItem;
$.ajax({ url: 'square_cancel.php', type: 'POST', data: { image_id: item.dataset.id, csrf_token: window.CSRF_TOKEN } })
.done(function (r) {
if (!r.ok) return toast(r.msg, 'danger');
setSquareState(item, '');
closeModal(document.getElementById('pushModal'));
toast('已从广场撤回');
}).fail(function () { toast('操作失败', 'danger'); });
});
document.getElementById('pus_save').addEventListener('click', function () {
if (!pushItem) return;
var item = pushItem;
var title = document.getElementById('pus_title').value;
var zone = document.getElementById('pus_zone').value;
var btn = this;
btn.disabled = true;
$.ajax({
url: 'square_submit.php',
type: 'POST',
data: { image_id: item.dataset.id, title: title, zone_id: zone, csrf_token: window.CSRF_TOKEN }
}).done(function (r) {
btn.disabled = false;
if (!r.ok) return toast(r.msg, 'danger');
item.dataset.zone = zone === '0' ? '0' : zone;
item.dataset.title = title;
setSquareState(item, r.status);
closeModal(document.getElementById('pushModal'));
toast(r.msg || (r.status === 'approved' ? '已更新' : '已提交,等待管理员审核'));
}).fail(function () {
btn.disabled = false;
toast('操作失败', 'danger');
});
});
function setSquareState(item, st) {
item.dataset.square = st || '';
var btn = item.querySelector('.icon-btn');
btn.classList.toggle('is-on', st === 'pending' || st === 'approved');
btn.title = (st === 'pending' || st === 'approved') ? '撤回广场推送' : '推送到照片广场';
var tag = item.querySelector('.square-tag');
var map = { pending: ['审核中', 'warning'], approved: ['广场已上架', 'success'], rejected: ['广场被驳回', 'danger'] };
if (!st) { if (tag) tag.remove(); return; }
if (!tag) {
tag = document.createElement('span');
item.appendChild(tag);
}
tag.className = 'square-tag badge badge--' + map[st][1];
tag.textContent = map[st][0];
}
// 积分耗尽时拦在点击之前
document.getElementById('uploadNewImage').addEventListener('click', function (e) {
if (parseInt(document.getElementById('pointsNow').textContent, 10) <= 0) {
e.preventDefault();
toast('积分不足,无法上传新图片', 'warning');
}
});
</script>
<?php render_footer($user, 'gallery.php');