WorkLog 全量合并:密码登录体系、每日自动备份、发信日志、活动邮件通知一键参加
- 认证:注册设密码(双次确认,>=10 位)、密码/验证码双模式登录、邮箱验证码找回重置密码 - 备份:服务内置每天 23:00 自动备份到 data/backups/(默认保留 14 份),backup_db.py 支持手动/crontab - 发信日志:logs/mail-YYYYMM.log 记录每次发信成败与失败原因(535 认证失败/断连细分),按月滚动保留 6 个月 - 通知:发起活动群发邮件(参与不发),邮件内一键参加(一次性令牌、7 天有效、私密活动不群发) - 前端:报错细分(字段/类型/状态码全中文)、验证码按钮 60 秒防连点 - 部署:Dockerfile + entrypoint(seed 机制)、限流/唯一索引/令牌摘要等安全加固 - 工具:repair_db/clear_db 的 SQL 全部常量化,无动态拼接 - 测试:43 项全覆盖(注册/登录/活动/通知/备份/日志/安全)
This commit is contained in:
Vendored
+416
@@ -0,0 +1,416 @@
|
||||
/* WorkLog 活动记录 — 样式 */
|
||||
|
||||
:root {
|
||||
--bg: #f4f6fa;
|
||||
--card: #ffffff;
|
||||
--ink: #1f2430;
|
||||
--muted: #6b7280;
|
||||
--line: #e5e8ef;
|
||||
--primary: #4361ee;
|
||||
--primary-ink: #ffffff;
|
||||
--primary-soft: #eef1fd;
|
||||
--ok: #12805c;
|
||||
--ok-soft: #e2f5ec;
|
||||
--warn: #b45309;
|
||||
--warn-soft: #fdf1e0;
|
||||
--bad: #b42323;
|
||||
--bad-soft: #fdeaea;
|
||||
--radius: 14px;
|
||||
--shadow: 0 1px 2px rgba(17, 24, 39, .05), 0 8px 24px -12px rgba(17, 24, 39, .12);
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: var(--bg);
|
||||
color: var(--ink);
|
||||
font-family: "PingFang SC", "Microsoft YaHei", "Segoe UI", system-ui, sans-serif;
|
||||
font-size: 15px;
|
||||
line-height: 1.55;
|
||||
}
|
||||
|
||||
.hidden { display: none !important; }
|
||||
.muted { color: var(--muted); font-size: 13px; }
|
||||
|
||||
button { font: inherit; cursor: pointer; }
|
||||
input, select, textarea {
|
||||
font: inherit;
|
||||
color: var(--ink);
|
||||
background: #fff;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 10px;
|
||||
padding: 9px 12px;
|
||||
outline: none;
|
||||
}
|
||||
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(67, 97, 238, .15); }
|
||||
textarea { width: 100%; resize: vertical; }
|
||||
|
||||
.btn {
|
||||
border: 1px solid var(--line);
|
||||
background: #fff;
|
||||
color: var(--ink);
|
||||
border-radius: 10px;
|
||||
padding: 9px 16px;
|
||||
font-weight: 500;
|
||||
transition: .15s;
|
||||
}
|
||||
.btn:hover { border-color: var(--primary); color: var(--primary); }
|
||||
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
|
||||
.btn.primary:hover { filter: brightness(1.08); color: #fff; }
|
||||
.btn.danger { color: var(--bad); border-color: #f2c4c4; }
|
||||
.btn.danger:hover { background: var(--bad-soft); }
|
||||
.btn.ghost { border-color: transparent; background: transparent; }
|
||||
.btn.ghost:hover { background: var(--primary-soft); }
|
||||
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
|
||||
.btn.block { width: 100%; }
|
||||
.btn:disabled { opacity: .55; cursor: not-allowed; }
|
||||
|
||||
/* ---------- 顶栏 ---------- */
|
||||
#topbar {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 50;
|
||||
background: rgba(255, 255, 255, .92);
|
||||
backdrop-filter: blur(8px);
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
.topbar-inner {
|
||||
max-width: 1080px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 18px;
|
||||
padding: 10px 20px;
|
||||
}
|
||||
.brand { font-weight: 700; font-size: 17px; white-space: nowrap; }
|
||||
#nav { display: flex; gap: 4px; flex: 1; }
|
||||
#nav a {
|
||||
color: var(--muted);
|
||||
text-decoration: none;
|
||||
padding: 7px 12px;
|
||||
border-radius: 9px;
|
||||
font-weight: 500;
|
||||
}
|
||||
#nav a:hover { color: var(--ink); background: var(--primary-soft); }
|
||||
#nav a.active { color: var(--primary); background: var(--primary-soft); }
|
||||
.topbar-user { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
|
||||
#topbar-name { font-weight: 600; font-size: 14px; }
|
||||
.menu-toggle { display: none; background: none; border: none; font-size: 20px; }
|
||||
|
||||
/* ---------- 布局 ---------- */
|
||||
.container { max-width: 1080px; margin: 0 auto; padding: 24px 20px 60px; }
|
||||
.container.narrow { max-width: 640px; }
|
||||
.page-head {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.page-head h2 { margin: 0 0 2px; font-size: 22px; }
|
||||
.page-head p { margin: 0; }
|
||||
.filters { display: flex; gap: 8px; flex-wrap: wrap; }
|
||||
.filters input, .filters select { min-width: 130px; }
|
||||
|
||||
.stats-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 18px; }
|
||||
.stat {
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
padding: 10px 16px;
|
||||
min-width: 96px;
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
.stat b { display: block; font-size: 20px; }
|
||||
.stat span { color: var(--muted); font-size: 12px; }
|
||||
|
||||
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
|
||||
.chip {
|
||||
border: 1px solid var(--line);
|
||||
background: #fff;
|
||||
border-radius: 999px;
|
||||
padding: 4px 12px;
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
|
||||
.chip:hover { border-color: var(--primary); color: var(--primary); }
|
||||
.chip.active:hover { color: #fff; }
|
||||
|
||||
/* ---------- 卡片列表 ---------- */
|
||||
.card-list { display: flex; flex-direction: column; gap: 12px; }
|
||||
.acard {
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
padding: 16px 18px;
|
||||
box-shadow: var(--shadow);
|
||||
transition: .15s;
|
||||
}
|
||||
.acard:hover { border-color: #c9d4f5; }
|
||||
.acard-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
|
||||
.acard h3 { margin: 0; font-size: 17px; cursor: pointer; }
|
||||
.acard h3:hover { color: var(--primary); }
|
||||
.acard-meta { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; align-items: center; }
|
||||
.meta-item { color: var(--muted); font-size: 13px; display: inline-flex; align-items: center; gap: 4px; }
|
||||
.acard-desc { margin: 10px 0 0; color: #3d4351; font-size: 14px; white-space: pre-wrap; }
|
||||
.acard-foot {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 12px;
|
||||
padding-top: 12px;
|
||||
border-top: 1px dashed var(--line);
|
||||
}
|
||||
.faces { display: flex; align-items: center; }
|
||||
.face {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 50%;
|
||||
background: var(--primary);
|
||||
color: #fff;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border: 2px solid #fff;
|
||||
margin-left: -8px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.face:first-child { margin-left: 0; }
|
||||
.face.more { background: #e7eaf3; color: var(--muted); }
|
||||
.face:hover { transform: translateY(-2px); }
|
||||
.faces-label { margin-left: 8px; font-size: 13px; color: var(--muted); }
|
||||
.acard-actions { display: flex; gap: 8px; flex-wrap: wrap; }
|
||||
|
||||
.tag {
|
||||
display: inline-block;
|
||||
padding: 2px 10px;
|
||||
border-radius: 999px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
}
|
||||
.tag.cat { background: var(--primary-soft); color: var(--primary); }
|
||||
.tag.pub { background: #e9f0fb; color: #275bd6; }
|
||||
.tag.pri { background: #f0e9fb; color: #6d3fc0; }
|
||||
.tag.joined { background: #e8f1fd; color: #1d5bbf; }
|
||||
.tag.attended { background: var(--ok-soft); color: var(--ok); }
|
||||
.tag.absent { background: var(--bad-soft); color: var(--bad); }
|
||||
.tag.full { background: #f1f2f6; color: var(--muted); }
|
||||
|
||||
/* ---------- 成员 ---------- */
|
||||
.member-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
.mcard {
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
padding: 16px;
|
||||
box-shadow: var(--shadow);
|
||||
cursor: pointer;
|
||||
transition: .15s;
|
||||
}
|
||||
.mcard:hover { border-color: #c9d4f5; transform: translateY(-2px); }
|
||||
.mcard-top { display: flex; align-items: center; gap: 12px; }
|
||||
.avatar {
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
border-radius: 50%;
|
||||
background: linear-gradient(135deg, #4361ee, #7c3aed);
|
||||
color: #fff;
|
||||
font-weight: 600;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 17px;
|
||||
flex: none;
|
||||
}
|
||||
.mcard b { display: block; }
|
||||
.mcard .muted { margin-top: 2px; }
|
||||
.mcard-stats { display: flex; gap: 14px; margin-top: 12px; }
|
||||
.mcard-stats div b { font-size: 16px; }
|
||||
.mcard-stats div span { display: block; font-size: 12px; color: var(--muted); }
|
||||
.mcard-bio { margin-top: 10px; font-size: 13px; color: #3d4351; }
|
||||
|
||||
/* ---------- 个人主页 ---------- */
|
||||
.profile-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
padding: 20px;
|
||||
box-shadow: var(--shadow);
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
.profile-header .avatar { width: 60px; height: 60px; font-size: 24px; }
|
||||
.profile-header h2 { margin: 0; font-size: 20px; }
|
||||
.profile-header p { margin: 3px 0 0; }
|
||||
|
||||
/* ---------- 认证页 ---------- */
|
||||
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
|
||||
.auth-card {
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 18px;
|
||||
padding: 32px 28px;
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
.auth-title { margin: 0; font-size: 24px; text-align: center; }
|
||||
.auth-sub { text-align: center; color: var(--muted); margin: 6px 0 18px; font-size: 13px; }
|
||||
.tabs { display: flex; background: var(--bg); border-radius: 10px; padding: 4px; margin-bottom: 18px; }
|
||||
.tab { flex: 1; border: none; background: transparent; padding: 8px; border-radius: 8px; font-weight: 500; color: var(--muted); }
|
||||
.tab.active { background: #fff; color: var(--ink); box-shadow: var(--shadow); }
|
||||
.auth-modes { display: flex; gap: 8px; margin-top: 14px; }
|
||||
.auth-mode { flex: 1; border: 1px solid var(--line); background: #fff; color: var(--muted); border-radius: 9px; padding: 8px; }
|
||||
.auth-mode.active { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); font-weight: 600; }
|
||||
.auth-links { display: flex; justify-content: flex-end; margin-top: 5px; }
|
||||
.text-link { border: 0; padding: 3px 0; background: transparent; color: var(--primary); font: inherit; font-size: 13px; cursor: pointer; }
|
||||
.text-link:hover { text-decoration: underline; }
|
||||
|
||||
/* ---------- 表单 ---------- */
|
||||
form label, .form-card label { display: block; margin-top: 14px; font-size: 13px; color: #3d4351; font-weight: 500; }
|
||||
form input, form select, form textarea, .form-card input, .form-card select { width: 100%; margin-top: 5px; }
|
||||
.hint-inline { color: var(--muted); font-weight: 400; }
|
||||
.form-error { color: var(--bad); font-size: 13px; min-height: 18px; margin-top: 8px; }
|
||||
.form-hint { font-size: 12px; margin-top: -6px; }
|
||||
.code-row { display: flex; gap: 8px; margin-top: 5px; }
|
||||
.code-row input { flex: 1; margin-top: 0; }
|
||||
.code-row button { flex: none; white-space: nowrap; }
|
||||
.code-row button:disabled { opacity: .55; cursor: not-allowed; }
|
||||
.form-card { margin-bottom: 14px; }
|
||||
.form-card h3 { margin: 0 0 4px; font-size: 16px; }
|
||||
.form-row { display: flex; gap: 10px; }
|
||||
.form-row > * { flex: 1; }
|
||||
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }
|
||||
.checkline { display: flex; align-items: center; gap: 8px; margin-top: 14px; font-size: 14px; }
|
||||
.checkline input { width: auto; margin: 0; }
|
||||
|
||||
/* ---------- 弹层 ---------- */
|
||||
.modal-mask {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(15, 23, 42, .45);
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
padding: 40px 16px;
|
||||
z-index: 100;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.modal {
|
||||
background: var(--card);
|
||||
border-radius: 18px;
|
||||
width: 100%;
|
||||
max-width: 560px;
|
||||
padding: 24px;
|
||||
box-shadow: 0 24px 60px -12px rgba(15, 23, 42, .35);
|
||||
animation: pop .18s ease-out;
|
||||
}
|
||||
@keyframes pop { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }
|
||||
.modal h3 { margin: 0 0 4px; font-size: 18px; }
|
||||
.modal .modal-sub { margin: 0 0 10px; }
|
||||
|
||||
/* 详情 */
|
||||
.detail-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
|
||||
.detail-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; margin: 14px 0; font-size: 14px; }
|
||||
.detail-meta span.lbl { color: var(--muted); font-size: 12px; display: block; }
|
||||
.detail-desc {
|
||||
background: var(--bg);
|
||||
border-radius: 10px;
|
||||
padding: 12px 14px;
|
||||
font-size: 14px;
|
||||
white-space: pre-wrap;
|
||||
margin: 12px 0;
|
||||
}
|
||||
.p-list { margin: 8px 0 0; padding: 0; list-style: none; }
|
||||
.p-list li {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 8px 0;
|
||||
border-top: 1px dashed var(--line);
|
||||
font-size: 14px;
|
||||
}
|
||||
.p-list li:first-child { border-top: none; }
|
||||
.p-list .grow { flex: 1; }
|
||||
.p-list .who { cursor: pointer; }
|
||||
.p-list .who:hover { color: var(--primary); }
|
||||
.p-list select { padding: 4px 8px; font-size: 13px; width: auto; }
|
||||
.p-list .note { color: var(--muted); font-size: 12px; }
|
||||
.add-member-row { display: flex; gap: 8px; margin-top: 12px; }
|
||||
.add-member-row input { flex: 1; }
|
||||
|
||||
/* ---------- 空态 / 加载 ---------- */
|
||||
.empty {
|
||||
text-align: center;
|
||||
color: var(--muted);
|
||||
padding: 60px 20px;
|
||||
border: 1px dashed var(--line);
|
||||
border-radius: var(--radius);
|
||||
background: rgba(255, 255, 255, .6);
|
||||
}
|
||||
|
||||
/* ---------- Toast ---------- */
|
||||
.toast {
|
||||
position: fixed;
|
||||
left: 50%;
|
||||
bottom: 36px;
|
||||
transform: translateX(-50%);
|
||||
background: #1f2430;
|
||||
color: #fff;
|
||||
padding: 10px 18px;
|
||||
border-radius: 999px;
|
||||
font-size: 14px;
|
||||
z-index: 200;
|
||||
box-shadow: 0 10px 30px -8px rgba(0, 0, 0, .4);
|
||||
}
|
||||
.toast.err { background: var(--bad); }
|
||||
|
||||
/* ---------- 响应式 ---------- */
|
||||
@media (max-width: 760px) {
|
||||
#nav {
|
||||
display: none;
|
||||
position: absolute;
|
||||
top: 100%;
|
||||
left: 0;
|
||||
right: 0;
|
||||
background: #fff;
|
||||
border-bottom: 1px solid var(--line);
|
||||
flex-direction: column;
|
||||
padding: 8px;
|
||||
}
|
||||
#nav.open { display: flex; }
|
||||
.menu-toggle { display: block; margin-left: auto; }
|
||||
.topbar-user { display: none; }
|
||||
#new-activity-btn { display: none; }
|
||||
.detail-meta { grid-template-columns: 1fr; }
|
||||
}
|
||||
|
||||
/* 成员简表 */
|
||||
#members-table { width: 100%; border-collapse: collapse; background: #fff; border: 1px solid var(--border, #e5e7eb); border-radius: 10px; overflow: hidden; }
|
||||
#members-table th, #members-table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border, #e5e7eb); font-size: 14px; }
|
||||
#members-table th { background: #f9fafb; color: #6b7280; font-weight: 600; font-size: 13px; }
|
||||
#members-table tbody tr:last-child td { border-bottom: none; }
|
||||
#members-table tbody tr:hover { background: #f9fafb; }
|
||||
|
||||
/* 审核 */
|
||||
.badge { display: inline-block; min-width: 16px; padding: 0 5px; margin-left: 4px; border-radius: 8px; background: #ef4444; color: #fff; font-size: 11px; line-height: 16px; text-align: center; }
|
||||
.review-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid #f0f0f0; }
|
||||
.review-row:last-child { border-bottom: none; }
|
||||
.review-row .grow { flex: 1; }
|
||||
.tag.pending { background: #fef3c7; color: #92400e; }
|
||||
.review-add { display: flex; align-items: center; gap: 8px; margin: 12px 0; }
|
||||
.review-add input { flex: 1; margin-top: 0; }
|
||||
Vendored
+210
@@ -0,0 +1,210 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WorkLog · 活动记录</title>
|
||||
<link rel="stylesheet" href="/assets/styles.css?v=6" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="app">
|
||||
<!-- 顶栏 -->
|
||||
<header id="topbar" class="hidden">
|
||||
<div class="topbar-inner">
|
||||
<div class="brand" id="brand">📘 WorkLog</div>
|
||||
<nav id="nav">
|
||||
<a href="#" data-view="feed">活动墙</a>
|
||||
<a href="#" data-view="mine">我的记录</a>
|
||||
<a href="#" data-view="members">成员</a>
|
||||
<a href="#" data-view="profile">设置</a>
|
||||
</nav>
|
||||
<div class="topbar-user">
|
||||
<span id="topbar-name"></span>
|
||||
<button class="btn ghost sm" id="logout-btn">退出</button>
|
||||
</div>
|
||||
<button class="btn primary" id="new-activity-btn">+ 记录活动</button>
|
||||
<button class="menu-toggle" id="menu-toggle">☰</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<!-- 登录 / 注册 -->
|
||||
<section id="auth-view" class="view hidden">
|
||||
<div class="auth-wrap">
|
||||
<div class="auth-card">
|
||||
<h1 class="auth-title">📘 WorkLog</h1>
|
||||
<p class="auth-sub">用一条条活动记录,攒下一起做过的事</p>
|
||||
<div class="tabs">
|
||||
<button class="tab active" data-auth="login">登录</button>
|
||||
<button class="tab" data-auth="register">注册新账号</button>
|
||||
</div>
|
||||
<form id="login-form">
|
||||
<label>邮箱 *<input name="email" type="email" required maxlength="255" autocomplete="email" placeholder="注册时使用的邮箱" /></label>
|
||||
<div class="auth-modes" role="group" aria-label="选择登录方式">
|
||||
<button type="button" class="auth-mode active" data-login-mode="password" aria-pressed="true">密码登录</button>
|
||||
<button type="button" class="auth-mode" data-login-mode="code" aria-pressed="false">验证码登录</button>
|
||||
</div>
|
||||
<div id="password-login-fields">
|
||||
<label>密码 *<input name="password" type="password" required minlength="10" maxlength="128" autocomplete="current-password" placeholder="至少 10 位" /></label>
|
||||
<div class="auth-links"><button type="button" class="text-link" id="forgot-password-btn">忘记密码?</button></div>
|
||||
</div>
|
||||
<div id="code-login-fields" class="hidden">
|
||||
<label>邮箱验证码 *
|
||||
<div class="code-row">
|
||||
<input name="email_code" type="text" minlength="6" maxlength="6" pattern="[0-9]{6}" inputmode="numeric" autocomplete="one-time-code" title="请输入 6 位数字验证码" placeholder="6 位验证码" />
|
||||
<button type="button" class="btn" id="login-code-btn">获取验证码</button>
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
||||
<div class="form-error" id="login-error" role="alert"></div>
|
||||
<div class="form-hint muted" id="login-hint" role="status"></div>
|
||||
<button class="btn primary block" type="submit" id="login-submit">登录</button>
|
||||
</form>
|
||||
<form id="reset-form" class="hidden">
|
||||
<h3>重设密码</h3>
|
||||
<p class="muted">输入注册邮箱并验证后设置新密码。</p>
|
||||
<label>邮箱 *<input name="email" type="email" required maxlength="255" autocomplete="email" placeholder="注册时使用的邮箱" /></label>
|
||||
<label>邮箱验证码 *
|
||||
<div class="code-row">
|
||||
<input name="email_code" type="text" required minlength="6" maxlength="6" pattern="[0-9]{6}" inputmode="numeric" autocomplete="one-time-code" title="请输入 6 位数字验证码" placeholder="6 位验证码" />
|
||||
<button type="button" class="btn" id="reset-code-btn">获取验证码</button>
|
||||
</div>
|
||||
</label>
|
||||
<label>新密码 *<input name="password" type="password" required minlength="10" maxlength="128" autocomplete="new-password" placeholder="至少 10 位" /></label>
|
||||
<label>确认新密码 *<input name="confirm_password" type="password" required minlength="10" maxlength="128" autocomplete="new-password" placeholder="再输入一次新密码" /></label>
|
||||
<div class="form-error" id="reset-error" role="alert"></div>
|
||||
<div class="form-hint muted" id="reset-hint" role="status"></div>
|
||||
<button class="btn primary block" type="submit">保存新密码</button>
|
||||
<div class="auth-links"><button type="button" class="text-link" id="back-to-login-btn">返回登录</button></div>
|
||||
</form>
|
||||
<form id="register-form" class="hidden">
|
||||
<label>姓名 *<input name="display_name" required maxlength="32" autocomplete="name" placeholder="真实姓名,展示用" /></label>
|
||||
<label>所属部门 *<select name="department" required><option value="" disabled selected>请选择部门</option></select></label>
|
||||
<label>邮箱 *<input name="email" type="email" required maxlength="255" autocomplete="email" placeholder="用于登录和接收验证码" /></label>
|
||||
<label>手机号 *<input name="phone" type="tel" required minlength="11" maxlength="11" pattern="1[3-9][0-9]{9}" inputmode="numeric" autocomplete="tel-national" title="请填写正确的 11 位手机号" placeholder="11 位手机号" /></label>
|
||||
<label>密码 *<input name="password" type="password" required minlength="10" maxlength="128" autocomplete="new-password" placeholder="至少 10 位" /></label>
|
||||
<label>确认密码 *<input name="confirm_password" type="password" required minlength="10" maxlength="128" autocomplete="new-password" placeholder="再输入一次密码" /></label>
|
||||
<p class="form-hint muted">姓名和部门向成员展示;邮箱、手机号仅自己可见。</p>
|
||||
<label>邮箱验证码 *
|
||||
<div class="code-row">
|
||||
<input name="email_code" type="text" required minlength="6" maxlength="6" pattern="[0-9]{6}" inputmode="numeric" autocomplete="one-time-code" title="请输入 6 位数字验证码" placeholder="6 位验证码" />
|
||||
<button type="button" class="btn" id="send-code-btn">获取验证码</button>
|
||||
</div>
|
||||
</label>
|
||||
<div class="form-error" id="register-error" role="alert"></div>
|
||||
<div class="form-hint muted" id="register-hint" role="status"></div>
|
||||
<button class="btn primary block" type="submit" id="register-submit">创建账号</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- 活动墙 -->
|
||||
<main id="feed-view" class="view hidden">
|
||||
<div class="container">
|
||||
<div class="page-head">
|
||||
<div>
|
||||
<h2>活动墙</h2>
|
||||
<p class="muted" id="feed-sub">看看大家都在参加什么</p>
|
||||
</div>
|
||||
<div class="filters">
|
||||
<input id="feed-q" placeholder="搜索活动 / 地点" />
|
||||
<select id="feed-dept"><option value="">全部部门</option></select>
|
||||
<select id="feed-scope">
|
||||
<option value="all">全部可见</option>
|
||||
<option value="mine">我发起的</option>
|
||||
<option value="joined">我参加的</option>
|
||||
<option value="upcoming">未开始的</option>
|
||||
</select>
|
||||
<button class="btn" id="feed-refresh">刷新</button>
|
||||
<button class="btn primary" id="feed-new">+ 记录活动</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="feed-stats" class="stats-row"></div>
|
||||
<div id="feed-list" class="card-list"></div>
|
||||
<div id="feed-empty" class="empty hidden">还没有活动记录,点右上角「+ 记录活动」创建第一条吧。</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<!-- 我的记录 -->
|
||||
<main id="mine-view" class="view hidden">
|
||||
<div class="container">
|
||||
<div class="page-head">
|
||||
<div>
|
||||
<h2>我的记录</h2>
|
||||
<p class="muted">按账号记录你参加过的每一场活动</p>
|
||||
</div>
|
||||
<div class="filters">
|
||||
<input id="mine-q" placeholder="搜索标题 / 地点" />
|
||||
<select id="mine-status">
|
||||
<option value="">全部状态</option>
|
||||
<option value="joined">报名中</option>
|
||||
<option value="attended">已参加</option>
|
||||
<option value="absent">缺席</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div id="mine-stats" class="stats-row"></div>
|
||||
<div id="mine-list" class="card-list"></div>
|
||||
<div id="mine-empty" class="empty hidden">还没有记录。去活动墙报名,或点「+ 记录活动」自己发起一条。</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<!-- 成员 -->
|
||||
<main id="members-view" class="view hidden">
|
||||
<div class="container">
|
||||
<div class="page-head">
|
||||
<div><h2>成员</h2><p class="muted">点开任意成员,查看 TA 的活动记录</p></div>
|
||||
<div class="filters">
|
||||
<select id="members-dept"></select>
|
||||
<input id="members-q" placeholder="搜索账号 / 昵称" />
|
||||
<button id="members-compact" class="btn btn-ghost" type="button">简表</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="members-list" class="member-grid"></div>
|
||||
<table id="members-table" class="hidden">
|
||||
<thead><tr><th>姓名</th><th>部门</th><th>参加数</th></tr></thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
<div id="members-empty" class="empty hidden">没有找到成员。</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<!-- 他人主页 -->
|
||||
<main id="user-view" class="view hidden">
|
||||
<div class="container">
|
||||
<button class="btn ghost" id="user-back">← 返回</button>
|
||||
<div id="user-header" class="profile-header"></div>
|
||||
<div id="user-stats" class="stats-row"></div>
|
||||
<div id="user-list" class="card-list"></div>
|
||||
<div id="user-empty" class="empty hidden">TA 的公开记录还很少。</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<!-- 设置 -->
|
||||
<main id="profile-view" class="view hidden">
|
||||
<div class="container narrow">
|
||||
<div class="page-head"><div><h2>账号设置</h2></div></div>
|
||||
<div class="card form-card">
|
||||
<h3>个人资料</h3>
|
||||
<label>姓名<input id="pf-name" maxlength="32" /></label>
|
||||
<label>所属部门<select id="pf-dept"><option value="">未选择</option></select></label>
|
||||
<label>手机号<input id="pf-phone" maxlength="11" inputmode="numeric" /></label>
|
||||
<label>绑定邮箱<div class="muted" id="pf-email" style="margin-top:4px"></div></label>
|
||||
<button class="btn primary" id="pf-save">保存资料</button>
|
||||
</div>
|
||||
<div class="card form-card">
|
||||
<h3>登录方式</h3>
|
||||
<p class="muted">支持邮箱密码或邮箱验证码登录。忘记密码时可通过邮箱验证码重设;修改密码后,其他设备上的登录状态会退出。</p>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<!-- 活动详情 / 新建弹层 -->
|
||||
<div id="modal-root"></div>
|
||||
|
||||
<div id="toast" class="toast hidden"></div>
|
||||
|
||||
<script src="/js/app.js?v=11"></script>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Vendored
+840
@@ -0,0 +1,840 @@
|
||||
/* WorkLog 活动记录 — 前端逻辑(原生 JS,无构建步骤) */
|
||||
"use strict";
|
||||
|
||||
(() => {
|
||||
const API = "/api";
|
||||
const TOKEN_KEY = "worklog_token";
|
||||
|
||||
// ------------------------------------------------------------- 工具
|
||||
const $ = (sel) => document.querySelector(sel);
|
||||
const $$ = (sel) => Array.from(document.querySelectorAll(sel));
|
||||
|
||||
const esc = (s) =>
|
||||
String(s ?? "").replace(/[&<>"']/g, (c) =>
|
||||
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
|
||||
|
||||
let departments = [];
|
||||
|
||||
const fmtDate = (ms) =>
|
||||
new Date(ms).toLocaleString("zh-CN", {
|
||||
year: "numeric", month: "2-digit", day: "2-digit",
|
||||
hour: "2-digit", minute: "2-digit", hour12: false,
|
||||
});
|
||||
const fmtDay = (ms) =>
|
||||
new Date(ms).toLocaleDateString("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit" });
|
||||
|
||||
const STATUS_LABEL = { joined: "已确认", attended: "已参加", absent: "缺席" };
|
||||
const toLocalInput = (ms) => {
|
||||
const d = new Date(ms - new Date().getTimezoneOffset() * 60000);
|
||||
return d.toISOString().slice(0, 16);
|
||||
};
|
||||
|
||||
let toastTimer = null;
|
||||
const toast = (msg, isErr = false) => {
|
||||
const el = $("#toast");
|
||||
el.textContent = msg;
|
||||
el.classList.toggle("err", isErr);
|
||||
el.classList.remove("hidden");
|
||||
clearTimeout(toastTimer);
|
||||
toastTimer = setTimeout(() => el.classList.add("hidden"), 2600);
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------- API
|
||||
const getToken = () => localStorage.getItem(TOKEN_KEY) || "";
|
||||
const setToken = (t) => (t ? localStorage.setItem(TOKEN_KEY, t) : localStorage.removeItem(TOKEN_KEY));
|
||||
|
||||
function apiErrorMessage(detail, status) {
|
||||
if (typeof detail === "string" && detail) return detail;
|
||||
if (Array.isArray(detail) && detail.length) {
|
||||
const labels = {
|
||||
display_name: "姓名", department: "部门", email: "邮箱", phone: "手机号", email_code: "验证码",
|
||||
title: "活动名称", start_at: "活动时间", location: "地点", description: "活动说明",
|
||||
visibility: "可见范围", capacity: "名额", note: "备注", status: "参加状态",
|
||||
};
|
||||
// 按 Pydantic 错误类型映射成中文;带函数的表示要用错误里的上下文值
|
||||
const typeText = {
|
||||
missing: "请填写",
|
||||
string_too_long: (e) => `最多填写 ${e.ctx?.max_length ?? "?"} 个字符`,
|
||||
string_too_short: (e) => `至少填写 ${e.ctx?.min_length ?? "?"} 个字符`,
|
||||
greater_than: (e) => `需要大于 ${e.ctx?.gt ?? "?"}`,
|
||||
greater_than_equal: (e) => `不能小于 ${e.ctx?.ge ?? "?"}`,
|
||||
less_than_equal: (e) => `不能大于 ${e.ctx?.le ?? "?"}`,
|
||||
int_parsing: "需为数字",
|
||||
int_type: "需为数字",
|
||||
float_type: "需为数字",
|
||||
literal_error: "取值不合法",
|
||||
json_invalid: "提交的数据格式有误",
|
||||
};
|
||||
return detail.map((error) => {
|
||||
const field = labels[error.loc?.at(-1)] || "填写内容";
|
||||
if (error.type === "missing") return `请填写${field}`;
|
||||
const tpl = typeText[error.type];
|
||||
if (typeof tpl === "function") return `${field}${tpl(error)}`;
|
||||
if (tpl) return `${field}${tpl}`;
|
||||
const message = String(error.msg || "格式不正确").replace(/^Value error,\s*/, "");
|
||||
return /[\u4e00-\u9fff]/.test(message) ? message : `${field}格式不正确`;
|
||||
}).join(";");
|
||||
}
|
||||
// 服务器没给具体原因时按状态码给中文兜底
|
||||
const statusText = {
|
||||
400: "请求有误", 403: "没有权限执行此操作", 404: "内容不存在或已被删除",
|
||||
409: "与现有记录冲突,请刷新后重试", 422: "填写内容有误,请检查后重试",
|
||||
429: "操作太频繁,请稍后再试", 500: "服务器内部错误,请稍后重试",
|
||||
502: "上游服务暂时不可用,请稍后重试", 503: "服务暂不可用,请稍后重试",
|
||||
};
|
||||
return statusText[status] || `请求失败(${status})`;
|
||||
}
|
||||
|
||||
async function api(path, { method = "GET", body } = {}) {
|
||||
const headers = { "Content-Type": "application/json" };
|
||||
const token = getToken();
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(API + path, {
|
||||
method, headers,
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
} catch {
|
||||
throw new Error("网络异常,请稍后重试");
|
||||
}
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
if (res.status === 401 && getToken()) {
|
||||
setToken("");
|
||||
showAuth();
|
||||
throw new Error("登录已过期,请重新登录");
|
||||
}
|
||||
throw new Error(apiErrorMessage(data.detail, res.status));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- 状态
|
||||
const state = {
|
||||
me: null,
|
||||
view: "feed",
|
||||
feed: { q: "", department: "", scope: "all", items: [] },
|
||||
mine: { q: "", status: "", items: [], stats: null },
|
||||
members: { q: "", items: [] },
|
||||
user: { id: "", data: null },
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------- 视图切换
|
||||
const VIEWS = ["auth", "feed", "mine", "members", "user", "profile"];
|
||||
function show(view) {
|
||||
state.view = view;
|
||||
VIEWS.forEach((v) => $(`#${v}-view`).classList.toggle("hidden", v !== view));
|
||||
const logged = !!state.me;
|
||||
$("#topbar").classList.toggle("hidden", view === "auth" || !logged);
|
||||
$$("#nav a").forEach((a) => a.classList.toggle("active", a.dataset.view === view));
|
||||
$("#nav").classList.remove("open");
|
||||
if (view === "feed") loadFeed();
|
||||
if (view === "mine") loadMine();
|
||||
if (view === "members") loadMembers();
|
||||
if (view === "profile") fillProfile();
|
||||
}
|
||||
const showAuth = () => show("auth");
|
||||
|
||||
function enterApp(me) {
|
||||
state.me = me;
|
||||
$("#topbar-name").textContent = me.display_name;
|
||||
show("feed");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- 验证码(注册/登录共用)
|
||||
function bindSendCode(btnId, emailSel, hintSel, errSel, purpose) {
|
||||
let cooldownUntil = 0; // 发出成功后的冷却截止时间戳,期间按钮保持禁用
|
||||
$(btnId).addEventListener("click", async () => {
|
||||
const btn = $(btnId);
|
||||
const emailInput = $(emailSel);
|
||||
const email = emailInput.value.trim();
|
||||
if (!emailInput.checkValidity() || !email) {
|
||||
$(errSel).textContent = "请先填写正确的邮箱地址";
|
||||
return;
|
||||
}
|
||||
if (Date.now() < cooldownUntil) return; // 冷却期内忽略连点
|
||||
$(errSel).textContent = "";
|
||||
btn.disabled = true;
|
||||
btn.textContent = "发送中…";
|
||||
try {
|
||||
const data = await api("/auth/send-code", { method: "POST", body: { email, purpose } });
|
||||
if (data.dev_code) {
|
||||
$(hintSel).textContent = `【本机开发模式】验证码:${data.dev_code}`;
|
||||
} else {
|
||||
$(hintSel).textContent = "验证码已发送到你的邮箱,请注意查收";
|
||||
}
|
||||
cooldownUntil = Date.now() + 60_000;
|
||||
let left = 60;
|
||||
const tick = () => {
|
||||
if (Date.now() >= cooldownUntil && left <= 0) { btn.disabled = false; btn.textContent = "获取验证码"; return; }
|
||||
btn.textContent = `${left}s 后重发`;
|
||||
left -= 1;
|
||||
setTimeout(tick, 1000);
|
||||
};
|
||||
tick();
|
||||
} catch (err) {
|
||||
btn.disabled = false;
|
||||
btn.textContent = "获取验证码";
|
||||
$(errSel).textContent = err.message;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- 登录方式
|
||||
let loginMode = "password";
|
||||
function setLoginMode(mode) {
|
||||
loginMode = mode;
|
||||
$("#password-login-fields").classList.toggle("hidden", mode !== "password");
|
||||
$("#code-login-fields").classList.toggle("hidden", mode !== "code");
|
||||
$("#login-form [name=password]").required = mode === "password";
|
||||
$("#login-form [name=email_code]").required = mode === "code";
|
||||
$("#forgot-password-btn").classList.toggle("hidden", mode !== "password");
|
||||
$$('[data-login-mode]').forEach((button) => {
|
||||
const active = button.dataset.loginMode === mode;
|
||||
button.classList.toggle("active", active);
|
||||
button.setAttribute("aria-pressed", String(active));
|
||||
});
|
||||
$("#login-error").textContent = "";
|
||||
$("#login-hint").textContent = "";
|
||||
}
|
||||
|
||||
$$('[data-login-mode]').forEach((button) => button.addEventListener("click", () => setLoginMode(button.dataset.loginMode)));
|
||||
$("#forgot-password-btn").addEventListener("click", () => {
|
||||
$("#reset-form [name=email]").value = $("#login-form [name=email]").value.trim();
|
||||
$("#login-error").textContent = "";
|
||||
$("#reset-error").textContent = "";
|
||||
$("#login-form").classList.add("hidden");
|
||||
$("#reset-form").classList.remove("hidden");
|
||||
});
|
||||
$("#back-to-login-btn").addEventListener("click", () => {
|
||||
const resetEmail = $("#reset-form [name=email]").value.trim();
|
||||
if (resetEmail) $("#login-form [name=email]").value = resetEmail;
|
||||
$("#reset-form").classList.add("hidden");
|
||||
$("#login-form").classList.remove("hidden");
|
||||
setLoginMode("password");
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------- 认证
|
||||
$("#login-form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const f = e.target;
|
||||
const btn = $("#login-submit");
|
||||
$("#login-error").textContent = "";
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const data = await api("/auth/login", {
|
||||
method: "POST",
|
||||
body: loginMode === "password"
|
||||
? { email: f.email.value.trim(), password: f.password.value }
|
||||
: { email: f.email.value.trim(), email_code: f.email_code.value.trim() },
|
||||
});
|
||||
setToken(data.token);
|
||||
enterApp(data.user);
|
||||
} catch (err) { $("#login-error").textContent = err.message; }
|
||||
finally { btn.disabled = false; }
|
||||
});
|
||||
bindSendCode("#login-code-btn", "#login-form [name=email]", "#login-hint", "#login-error", "login");
|
||||
|
||||
$("#reset-form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const f = e.target;
|
||||
const btn = f.querySelector('[type="submit"]');
|
||||
$("#reset-error").textContent = "";
|
||||
if (f.password.value !== f.confirm_password.value) {
|
||||
$("#reset-error").textContent = "两次输入的密码不一致";
|
||||
f.confirm_password.focus();
|
||||
return;
|
||||
}
|
||||
btn.disabled = true;
|
||||
try {
|
||||
await api("/auth/reset-password", {
|
||||
method: "POST",
|
||||
body: {
|
||||
email: f.email.value.trim(), email_code: f.email_code.value.trim(),
|
||||
password: f.password.value, confirm_password: f.confirm_password.value,
|
||||
},
|
||||
});
|
||||
const email = f.email.value.trim();
|
||||
f.reset();
|
||||
f.email.value = email;
|
||||
$("#reset-form").classList.add("hidden");
|
||||
$("#login-form").classList.remove("hidden");
|
||||
$("#login-form [name=email]").value = email;
|
||||
setLoginMode("password");
|
||||
$("#login-hint").textContent = "密码已重设,请使用新密码登录。";
|
||||
} catch (err) { $("#reset-error").textContent = err.message; }
|
||||
finally { btn.disabled = false; }
|
||||
});
|
||||
bindSendCode("#reset-code-btn", "#reset-form [name=email]", "#reset-hint", "#reset-error", "reset");
|
||||
|
||||
$("#register-form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const f = e.target;
|
||||
const btn = $("#register-submit");
|
||||
$("#register-error").textContent = "";
|
||||
if (!f.display_name.value.trim()) {
|
||||
$("#register-error").textContent = "请填写姓名";
|
||||
f.display_name.focus();
|
||||
return;
|
||||
}
|
||||
if (!f.department.value) {
|
||||
$("#register-error").textContent = "请选择所属部门";
|
||||
f.department.focus();
|
||||
return;
|
||||
}
|
||||
if (!/^1[3-9]\d{9}$/.test(f.phone.value.trim())) {
|
||||
$("#register-error").textContent = "请填写正确的 11 位手机号";
|
||||
return;
|
||||
}
|
||||
if (f.password.value !== f.confirm_password.value) {
|
||||
$("#register-error").textContent = "两次输入的密码不一致";
|
||||
f.confirm_password.focus();
|
||||
return;
|
||||
}
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const data = await api("/auth/register", {
|
||||
method: "POST",
|
||||
body: {
|
||||
display_name: f.display_name.value.trim(),
|
||||
department: f.department.value,
|
||||
email: f.email.value.trim(),
|
||||
phone: f.phone.value.trim(),
|
||||
email_code: f.email_code.value.trim(),
|
||||
password: f.password.value,
|
||||
confirm_password: f.confirm_password.value,
|
||||
},
|
||||
});
|
||||
setToken(data.token);
|
||||
enterApp(data.user);
|
||||
toast("欢迎加入 WorkLog 🎉");
|
||||
} catch (err) { $("#register-error").textContent = err.message; }
|
||||
finally { btn.disabled = false; }
|
||||
});
|
||||
bindSendCode("#send-code-btn", "#register-form [name=email]", "#register-hint", "#register-error", "register");
|
||||
|
||||
$$(".tab").forEach((t) =>
|
||||
t.addEventListener("click", () => {
|
||||
$$(".tab").forEach((x) => x.classList.toggle("active", x === t));
|
||||
$("#login-form").classList.toggle("hidden", t.dataset.auth !== "login");
|
||||
$("#reset-form").classList.add("hidden");
|
||||
$("#register-form").classList.toggle("hidden", t.dataset.auth !== "register");
|
||||
})
|
||||
);
|
||||
|
||||
$("#logout-btn").addEventListener("click", async () => {
|
||||
try { await api("/auth/logout", { method: "POST" }); } catch { /* ignore */ }
|
||||
setToken("");
|
||||
state.me = null;
|
||||
showAuth();
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------- 顶栏
|
||||
$("#brand").addEventListener("click", () => show("feed"));
|
||||
$("#new-activity-btn").addEventListener("click", () => openActivityForm());
|
||||
$("#feed-new").addEventListener("click", () => openActivityForm());
|
||||
$("#menu-toggle").addEventListener("click", () => $("#nav").classList.toggle("open"));
|
||||
$$("#nav a").forEach((a) =>
|
||||
a.addEventListener("click", (e) => { e.preventDefault(); show(a.dataset.view); })
|
||||
);
|
||||
|
||||
// ------------------------------------------------------------- 活动卡片
|
||||
const initials = (name) => {
|
||||
const s = String(name || "?").trim();
|
||||
// 中文取第一个字,避免溢出小圆头像
|
||||
return esc(/[\u4e00-\u9fff]/.test(s) ? s.slice(0, 1) : s.slice(0, 2).toUpperCase());
|
||||
};
|
||||
|
||||
function activityCard(a, opts = {}) {
|
||||
const mine = a.my_participation;
|
||||
const isOrganizer = a.organizer_id === state.me.id;
|
||||
const full = a.capacity && a.participant_count >= a.capacity;
|
||||
const statusTag = mine
|
||||
? `<span class="tag ${mine.status}">${STATUS_LABEL[mine.status] || mine.status}</span>`
|
||||
: "";
|
||||
const visTag = a.visibility === "private"
|
||||
? `<span class="tag pri">私密</span>`
|
||||
: `<span class="tag pub">公开</span>`;
|
||||
|
||||
const faces = a.participants.slice(0, 6).map((p) =>
|
||||
`<span class="face" data-user="${p.user_id}" title="${esc(p.display_name)}">${initials(p.display_name)}</span>`
|
||||
).join("");
|
||||
const more = a.participant_count > 6
|
||||
? `<span class="face more">+${a.participant_count - 6}</span>` : "";
|
||||
|
||||
const actions = [];
|
||||
// "我也参加过":点击直接加入
|
||||
if (!mine && a.visibility === "public" && !full) {
|
||||
actions.push(`<button class="btn primary sm" data-act="claim">我也参加过</button>`);
|
||||
}
|
||||
if (mine && !isOrganizer) {
|
||||
actions.push(`<button class="btn sm" data-act="leave">退出</button>`);
|
||||
}
|
||||
if (isOrganizer) {
|
||||
actions.push(`<button class="btn sm" data-act="edit">编辑</button>`);
|
||||
actions.push(`<button class="btn danger sm" data-act="delete">删除</button>`);
|
||||
}
|
||||
|
||||
return `
|
||||
<div class="acard" data-id="${a.id}">
|
||||
<div class="acard-top">
|
||||
<div>
|
||||
<h3 data-act="open">${esc(a.title)}</h3>
|
||||
<div class="acard-meta">
|
||||
${visTag}${statusTag}
|
||||
${full ? `<span class="tag full">已满 ${a.capacity} 人</span>` : ""}
|
||||
<span class="meta-item">📅 ${fmtDate(a.start_at)}</span>
|
||||
${a.location ? `<span class="meta-item">📍 ${esc(a.location)}</span>` : ""}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
${a.description ? `<p class="acard-desc">${esc(a.description).slice(0, 160)}${a.description.length > 160 ? "…" : ""}</p>` : ""}
|
||||
<div class="acard-foot">
|
||||
<div class="faces">
|
||||
${faces}${more}
|
||||
<span class="faces-label">${a.participant_count} 人${opts.showOrganizer && a.organizer ? ` · ${esc(a.organizer.display_name)} 记录` : ""}</span>
|
||||
</div>
|
||||
<div class="acard-actions">${actions.join("")}</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function bindCardEvents(container) {
|
||||
container.querySelectorAll(".acard").forEach((card) => {
|
||||
const id = card.dataset.id;
|
||||
const activity = findActivity(id);
|
||||
card.querySelectorAll("[data-act]").forEach((btn) => {
|
||||
btn.addEventListener("click", async (e) => {
|
||||
e.stopPropagation();
|
||||
const act = btn.dataset.act;
|
||||
if (act === "open") return openDetail(id);
|
||||
if (act === "claim") {
|
||||
try { await api(`/activities/${id}/claim`, { method: "POST" }); toast("已加入这条记录"); refreshCurrent(); }
|
||||
catch (err) { toast(err.message, true); }
|
||||
}
|
||||
if (act === "leave") {
|
||||
if (!confirm("确定退出这条活动吗?")) return;
|
||||
try { await api(`/activities/${id}/leave`, { method: "POST" }); toast("已退出"); refreshCurrent(); }
|
||||
catch (err) { toast(err.message, true); }
|
||||
}
|
||||
if (act === "edit") return openActivityForm(activity);
|
||||
if (act === "delete") {
|
||||
if (!confirm("删除后该活动及所有人的参加记录都会消失,确定?")) return;
|
||||
try { await api(`/activities/${id}`, { method: "DELETE" }); toast("已删除"); refreshCurrent(); }
|
||||
catch (err) { toast(err.message, true); }
|
||||
}
|
||||
});
|
||||
});
|
||||
card.querySelectorAll("[data-user]").forEach((f) =>
|
||||
f.addEventListener("click", (e) => { e.stopPropagation(); openUser(f.dataset.user); })
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function findActivity(id) {
|
||||
return (
|
||||
state.feed.items.find((x) => x.id === id) ||
|
||||
state.mine.items.find((x) => x.id === id) ||
|
||||
(state.user.data && state.user.data.items.find((x) => x.id === id)) ||
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
function refreshCurrent() {
|
||||
if (state.view === "feed") loadFeed();
|
||||
else if (state.view === "mine") loadMine();
|
||||
else if (state.view === "user" && state.user.id) loadUser(state.user.id);
|
||||
else if (state.view === "members") loadMembers();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- 活动墙
|
||||
async function loadFeed() {
|
||||
const p = new URLSearchParams();
|
||||
if (state.feed.q) p.set("q", state.feed.q);
|
||||
if (state.feed.department) p.set("department", state.feed.department);
|
||||
if (state.feed.scope && state.feed.scope !== "all") p.set("scope", state.feed.scope);
|
||||
const data = await api(`/activities?${p}`);
|
||||
state.feed.items = data.items;
|
||||
$("#feed-list").innerHTML = data.items.map((a) => activityCard(a, { showOrganizer: true })).join("");
|
||||
$("#feed-list").classList.toggle("hidden", !data.items.length);
|
||||
$("#feed-empty").classList.toggle("hidden", !!data.items.length);
|
||||
bindCardEvents($("#feed-list"));
|
||||
try {
|
||||
const ov = await api("/stats/overview");
|
||||
$("#feed-stats").innerHTML = `
|
||||
<div class="stat"><b>${ov.members}</b><span>成员</span></div>
|
||||
<div class="stat"><b>${ov.activities}</b><span>公开活动</span></div>
|
||||
<div class="stat"><b>${ov.records}</b><span>参加记录</span></div>
|
||||
<div class="stat"><b>${ov.upcoming}</b><span>未开始</span></div>`;
|
||||
} catch { $("#feed-stats").innerHTML = ""; }
|
||||
}
|
||||
|
||||
$("#feed-q").addEventListener("input", debounce(() => { state.feed.q = $("#feed-q").value.trim(); loadFeed(); }, 350));
|
||||
$("#feed-dept").addEventListener("change", () => { state.feed.department = $("#feed-dept").value; loadFeed(); });
|
||||
$("#feed-scope").addEventListener("change", () => { state.feed.scope = $("#feed-scope").value; loadFeed(); });
|
||||
$("#feed-refresh").addEventListener("click", loadFeed);
|
||||
|
||||
// ------------------------------------------------------------- 我的记录
|
||||
async function loadMine() {
|
||||
const data = await api("/auth/me");
|
||||
state.mine.stats = data.stats;
|
||||
const all = await api(`/activities?scope=joined&limit=200`);
|
||||
state.mine.items = all.items;
|
||||
renderMine();
|
||||
}
|
||||
|
||||
function renderMine() {
|
||||
const { q, status } = { q: $("#mine-q").value.trim(), status: $("#mine-status").value };
|
||||
let items = state.mine.items;
|
||||
if (q) items = items.filter((a) => (a.title + a.location).toLowerCase().includes(q.toLowerCase()));
|
||||
if (status) items = items.filter((a) => a.my_participation && a.my_participation.status === status);
|
||||
|
||||
const s = state.mine.stats || {};
|
||||
$("#mine-stats").innerHTML = `
|
||||
<div class="stat"><b>${s.total ?? 0}</b><span>全部记录</span></div>
|
||||
<div class="stat"><b>${s.attended ?? 0}</b><span>已参加</span></div>
|
||||
<div class="stat"><b>${s.joined ?? 0}</b><span>报名中</span></div>
|
||||
<div class="stat"><b>${s.absent ?? 0}</b><span>缺席</span></div>
|
||||
<div class="stat"><b>${Math.round((s.attendance_rate || 0) * 100)}%</b><span>出席率</span></div>`;
|
||||
|
||||
$("#mine-list").innerHTML = items.map((a) => activityCard(a)).join("");
|
||||
$("#mine-list").classList.toggle("hidden", !items.length);
|
||||
$("#mine-empty").classList.toggle("hidden", !!items.length);
|
||||
bindCardEvents($("#mine-list"));
|
||||
}
|
||||
|
||||
$("#mine-q").addEventListener("input", debounce(renderMine, 200));
|
||||
$("#mine-status").addEventListener("change", renderMine);
|
||||
|
||||
// ------------------------------------------------------------- 成员
|
||||
async function loadMembers() {
|
||||
const q = $("#members-q").value.trim();
|
||||
const dept = $("#members-dept").value;
|
||||
const p = new URLSearchParams();
|
||||
if (q) p.set("q", q);
|
||||
if (dept) p.set("department", dept);
|
||||
const data = await api(`/users?${p}`);
|
||||
state.members.items = data.items;
|
||||
const compact = !$("#members-table").classList.contains("hidden");
|
||||
$("#members-list").innerHTML = data.items.map((u) => `
|
||||
<div class="mcard" data-user="${u.id}">
|
||||
<div class="mcard-top">
|
||||
<div class="avatar">${initials(u.display_name)}</div>
|
||||
<div>
|
||||
<b>${esc(u.display_name)}</b>
|
||||
<div class="muted">${u.department ? esc(u.department) : ""}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mcard-stats">
|
||||
<div><b>${u.total}</b><span>参加</span></div>
|
||||
<div><b>${u.attended}</b><span>已参加</span></div>
|
||||
<div><b>${u.organized}</b><span>发起</span></div>
|
||||
</div>
|
||||
</div>`).join("");
|
||||
$("#members-table tbody").innerHTML = data.items.map((u) => `
|
||||
<tr data-user="${u.id}">
|
||||
<td><b>${esc(u.display_name)}</b></td>
|
||||
<td>${esc(u.department || "—")}</td>
|
||||
<td>${u.total}</td>
|
||||
</tr>`).join("");
|
||||
$("#members-list").classList.toggle("hidden", !data.items.length || compact);
|
||||
$("#members-table").classList.toggle("hidden", !data.items.length || !compact);
|
||||
$("#members-empty").classList.toggle("hidden", !!data.items.length);
|
||||
$$("#members-list .mcard").forEach((c) =>
|
||||
c.addEventListener("click", () => openUser(c.dataset.user))
|
||||
);
|
||||
}
|
||||
|
||||
$("#members-compact").addEventListener("click", () => {
|
||||
const table = $("#members-table");
|
||||
const toCompact = table.classList.contains("hidden");
|
||||
table.classList.toggle("hidden", !toCompact);
|
||||
$("#members-list").classList.toggle("hidden", toCompact || !$("#members-list").innerHTML);
|
||||
$("#members-compact").textContent = toCompact ? "卡片" : "简表";
|
||||
});
|
||||
|
||||
$("#members-q").addEventListener("input", debounce(loadMembers, 300));
|
||||
$("#members-dept").addEventListener("change", loadMembers);
|
||||
|
||||
function fillMembersDept() {
|
||||
$("#members-dept").innerHTML = `<option value="">全部部门</option>` +
|
||||
(departments || []).map((d) => `<option>${esc(d)}</option>`).join("");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- 他人主页
|
||||
async function openUser(userId) {
|
||||
$("#user-view").classList.add("hidden");
|
||||
state.user.id = userId;
|
||||
await loadUser(userId);
|
||||
show("user");
|
||||
}
|
||||
|
||||
async function loadUser(userId) {
|
||||
const data = await api(`/users/${userId}`);
|
||||
state.user.data = data;
|
||||
const u = data.user;
|
||||
$("#user-header").innerHTML = `
|
||||
<div class="avatar">${initials(u.display_name)}</div>
|
||||
<div>
|
||||
<h2>${esc(u.display_name)} ${data.is_self ? '<span class="tag pub">我</span>' : ""}</h2>
|
||||
<p class="muted">${u.department ? `${esc(u.department)} · ` : ""}加入于 ${fmtDay(u.created_at)}</p>
|
||||
</div>`;
|
||||
const s = data.stats;
|
||||
$("#user-stats").innerHTML = `
|
||||
<div class="stat"><b>${s.total}</b><span>可见记录</span></div>
|
||||
<div class="stat"><b>${s.attended}</b><span>已参加</span></div>
|
||||
<div class="stat"><b>${s.organized}</b><span>发起</span></div>
|
||||
<div class="stat"><b>${Math.round((s.attendance_rate || 0) * 100)}%</b><span>出席率</span></div>`;
|
||||
$("#user-list").innerHTML = data.items.map((a) => activityCard(a)).join("");
|
||||
$("#user-list").classList.toggle("hidden", !data.items.length);
|
||||
$("#user-empty").classList.toggle("hidden", !!data.items.length);
|
||||
bindCardEvents($("#user-list"));
|
||||
}
|
||||
|
||||
$("#user-back").addEventListener("click", () => show("members"));
|
||||
|
||||
// ------------------------------------------------------------- 弹层
|
||||
function modal(html) {
|
||||
$("#modal-root").innerHTML = `<div class="modal-mask"><div class="modal">${html}</div></div>`;
|
||||
const mask = $("#modal-root .modal-mask");
|
||||
mask.addEventListener("click", (e) => { if (e.target === mask) closeModal(); });
|
||||
document.addEventListener("keydown", escListener);
|
||||
return mask;
|
||||
}
|
||||
const escListener = (e) => { if (e.key === "Escape") closeModal(); };
|
||||
const closeModal = () => { $("#modal-root").innerHTML = ""; document.removeEventListener("keydown", escListener); };
|
||||
|
||||
// ---- 新建 / 编辑活动
|
||||
function openActivityForm(existing = null) {
|
||||
const isEdit = !!existing;
|
||||
modal(`
|
||||
<h3>${isEdit ? "编辑活动" : "记录一场活动"}</h3>
|
||||
<p class="modal-sub muted">提交后立即生效,所有人可见。</p>
|
||||
<form id="act-form">
|
||||
<label>活动名称 *<input name="title" required maxlength="120" value="${esc(existing?.title || "")}" placeholder="例如:周三晚例会" /></label>
|
||||
<div class="form-row">
|
||||
<label>开始时间 *<input name="start_at" type="datetime-local" required value="${existing ? toLocalInput(existing.start_at) : ""}" /></label>
|
||||
<label>地点<input name="location" maxlength="120" value="${esc(existing?.location || "")}" placeholder="如:3 号会议室" /></label>
|
||||
</div>
|
||||
<label>备注 <span class="hint-inline">(选填)</span><textarea name="description" rows="3" maxlength="2000" placeholder="做什么、带什么、注意事项…">${esc(existing?.description || "")}</textarea></label>
|
||||
<label class="checkline"><input type="checkbox" name="visibility" ${!existing || existing.visibility === "public" ? "checked" : ""} /> 公开活动(所有人可见;不勾选则仅参加者可见)</label>
|
||||
<div class="form-error" id="act-error"></div>
|
||||
<div class="form-actions">
|
||||
<button type="button" class="btn" id="act-cancel">取消</button>
|
||||
<button class="btn primary" type="submit" id="act-submit">${isEdit ? "保存修改" : "提交记录"}</button>
|
||||
</div>
|
||||
</form>`);
|
||||
|
||||
$("#act-cancel").addEventListener("click", closeModal);
|
||||
$("#act-form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const f = e.target;
|
||||
const btn = $("#act-submit");
|
||||
const startMs = f.start_at.value ? new Date(f.start_at.value).getTime() : 0;
|
||||
if (!startMs) { $("#act-error").textContent = "请选择开始时间"; return; }
|
||||
const body = {
|
||||
title: f.title.value.trim(),
|
||||
start_at: startMs,
|
||||
location: f.location.value.trim(),
|
||||
description: f.description.value.trim(),
|
||||
visibility: f.visibility.checked ? "public" : "private",
|
||||
};
|
||||
btn.disabled = true;
|
||||
try {
|
||||
if (isEdit) {
|
||||
await api(`/activities/${existing.id}`, { method: "PATCH", body });
|
||||
toast("已保存");
|
||||
} else {
|
||||
const data = await api("/activities", { method: "POST", body });
|
||||
toast(data.duplicate ? "这条记录刚刚已提交过" : "已记录 ✓");
|
||||
}
|
||||
closeModal();
|
||||
refreshCurrent();
|
||||
} catch (err) {
|
||||
$("#act-error").textContent = err.message;
|
||||
btn.disabled = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---- 活动详情
|
||||
async function openDetail(id) {
|
||||
let data;
|
||||
try { data = await api(`/activities/${id}`); } catch (err) { toast(err.message, true); return; }
|
||||
const a = data.activity;
|
||||
const canEdit = data.can_edit;
|
||||
const mine = a.my_participation;
|
||||
const full = a.capacity && a.participant_count >= a.capacity;
|
||||
|
||||
const rows = a.participants.map((p) => {
|
||||
const isSelfRow = p.user_id === state.me.id;
|
||||
const canManage = canEdit || isSelfRow;
|
||||
const statusSel = canManage
|
||||
? `<select data-role="status" data-uid="${p.user_id}">
|
||||
${["joined", "attended", "absent"].map((s) => `<option value="${s}" ${p.status === s ? "selected" : ""}>${STATUS_LABEL[s]}</option>`).join("")}
|
||||
</select>`
|
||||
: `<span class="tag ${p.status}">${STATUS_LABEL[p.status] || p.status}</span>`;
|
||||
const removeBtn = (canEdit && p.role !== "organizer") || (isSelfRow && p.role !== "organizer")
|
||||
? `<button class="btn ghost sm" data-role="remove" data-uid="${p.user_id}" title="移出">✕</button>`
|
||||
: "";
|
||||
return `<li>
|
||||
<span class="face" data-user="${p.user_id}">${initials(p.display_name)}</span>
|
||||
<span class="grow"><span class="who" data-user="${p.user_id}">${esc(p.display_name)}</span>
|
||||
${p.role === "organizer" ? '<span class="muted">· 记录人</span>' : ""}
|
||||
${p.note ? `<div class="note">${esc(p.note)}</div>` : ""}
|
||||
</span>
|
||||
${statusSel}${removeBtn}
|
||||
</li>`;
|
||||
}).join("");
|
||||
|
||||
const claimBtn = !mine && a.visibility === "public" && !full
|
||||
? `<button class="btn primary" id="d-claim">我也参加过</button>` : "";
|
||||
const leaveBtn = mine && !canEdit
|
||||
? `<button class="btn" id="d-leave">退出</button>` : "";
|
||||
const editBtn = canEdit ? `<button class="btn" id="d-edit">编辑</button>` : "";
|
||||
const delBtn = canEdit ? `<button class="btn danger" id="d-del">删除</button>` : "";
|
||||
const noteRow = mine
|
||||
? `<label>我的备注
|
||||
<input id="d-note" maxlength="500" value="${esc(mine.note || "")}" placeholder="如:带了水和球拍" /></label>`
|
||||
: "";
|
||||
|
||||
modal(`
|
||||
<div class="detail-head">
|
||||
<div>
|
||||
<h3>${esc(a.title)}</h3>
|
||||
<div class="acard-meta">
|
||||
${a.visibility === "private" ? `<span class="tag pri">私密</span>` : `<span class="tag pub">公开</span>`}
|
||||
${mine ? `<span class="tag ${mine.status}">${STATUS_LABEL[mine.status] || mine.status}</span>` : ""}
|
||||
${full ? `<span class="tag full">名额已满</span>` : ""}
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn ghost sm" id="d-close">✕</button>
|
||||
</div>
|
||||
<div class="detail-meta">
|
||||
<div><span class="lbl">时间</span>📅 ${fmtDate(a.start_at)}${a.start_at >= Date.now() ? "(未开始)" : ""}</div>
|
||||
<div><span class="lbl">地点</span>📍 ${a.location ? esc(a.location) : "未填写"}</div>
|
||||
<div><span class="lbl">记录人</span>${esc(a.organizer?.display_name || "未知")}</div>
|
||||
<div><span class="lbl">人数</span>${a.participant_count}${a.capacity ? ` / ${a.capacity}` : ""} · 已参加 ${a.attended_count}</div>
|
||||
</div>
|
||||
${a.description ? `<div class="detail-desc">${esc(a.description)}</div>` : ""}
|
||||
${noteRow}
|
||||
<h4 style="margin:14px 0 2px">一起参加过(${a.participant_count})</h4>
|
||||
<ul class="p-list">${rows || '<li class="muted">还没有其他人的参加记录</li>'}</ul>
|
||||
<div class="form-actions">
|
||||
${claimBtn}${leaveBtn}${editBtn}${delBtn}
|
||||
</div>`);
|
||||
|
||||
$("#d-close").addEventListener("click", closeModal);
|
||||
|
||||
const reload = async () => {
|
||||
closeModal();
|
||||
await openDetail(id);
|
||||
refreshCurrent();
|
||||
};
|
||||
|
||||
const on = (sel, fn) => { const el = $(sel); if (el) el.addEventListener("click", fn); };
|
||||
on("#d-claim", async () => { try { await api(`/activities/${id}/claim`, { method: "POST" }); toast("已加入这条记录"); reload(); } catch (err) { toast(err.message, true); } });
|
||||
on("#d-leave", async () => {
|
||||
if (!confirm("确定退出这条活动吗?")) return;
|
||||
try { await api(`/activities/${id}/leave`, { method: "POST" }); toast("已退出"); reload(); } catch (err) { toast(err.message, true); }
|
||||
});
|
||||
on("#d-edit", () => openActivityForm(a));
|
||||
on("#d-del", async () => {
|
||||
if (!confirm("删除后该活动及所有人的参加记录都会消失,确定?")) return;
|
||||
try { await api(`/activities/${id}`, { method: "DELETE" }); toast("已删除"); closeModal(); refreshCurrent(); } catch (err) { toast(err.message, true); }
|
||||
});
|
||||
|
||||
const noteInput = $("#d-note");
|
||||
if (noteInput) {
|
||||
let timer = null;
|
||||
noteInput.addEventListener("input", () => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(async () => {
|
||||
try {
|
||||
await api(`/activities/${id}/participants/${state.me.id}`, { method: "PATCH", body: { note: noteInput.value } });
|
||||
} catch (err) { toast(err.message, true); }
|
||||
}, 600);
|
||||
});
|
||||
}
|
||||
|
||||
$$("#modal-root [data-role=status]").forEach((sel) =>
|
||||
sel.addEventListener("change", async () => {
|
||||
try {
|
||||
await api(`/activities/${id}/participants/${sel.dataset.uid}`, { method: "PATCH", body: { status: sel.value } });
|
||||
toast("状态已更新");
|
||||
reload();
|
||||
} catch (err) { toast(err.message, true); }
|
||||
})
|
||||
);
|
||||
$$("#modal-root [data-role=remove]").forEach((btn) =>
|
||||
btn.addEventListener("click", async () => {
|
||||
if (!confirm("将该成员移出这条活动?")) return;
|
||||
try { await api(`/activities/${id}/participants/${btn.dataset.uid}`, { method: "DELETE" }); toast("已移出"); reload(); }
|
||||
catch (err) { toast(err.message, true); }
|
||||
})
|
||||
);
|
||||
$$("#modal-root [data-user]").forEach((f) =>
|
||||
f.addEventListener("click", () => { closeModal(); openUser(f.dataset.user); })
|
||||
);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- 设置
|
||||
function fillProfile() {
|
||||
$("#pf-name").value = state.me.display_name;
|
||||
$("#pf-dept").value = state.me.department || "";
|
||||
$("#pf-phone").value = state.me.phone || "";
|
||||
$("#pf-email").textContent = state.me.email || "(未绑定)";
|
||||
}
|
||||
$("#pf-save").addEventListener("click", async () => {
|
||||
const phone = $("#pf-phone").value.trim();
|
||||
if (phone && !/^1[3-9]\d{9}$/.test(phone)) { toast("请填写正确的 11 位手机号", true); return; }
|
||||
try {
|
||||
const data = await api("/auth/me", { method: "PATCH", body: { display_name: $("#pf-name").value.trim(), department: $("#pf-dept").value, phone } });
|
||||
state.me = data.user;
|
||||
$("#topbar-name").textContent = state.me.display_name;
|
||||
toast("资料已保存");
|
||||
} catch (err) { toast(err.message, true); }
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------- 启动
|
||||
function debounce(fn, wait) {
|
||||
let t = null;
|
||||
return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), wait); };
|
||||
}
|
||||
|
||||
function fillFeedDept() {
|
||||
$("#feed-dept").innerHTML = `<option value="">全部部门</option>` +
|
||||
(departments || []).map((d) => `<option>${esc(d)}</option>`).join("");
|
||||
}
|
||||
|
||||
async function boot() {
|
||||
try {
|
||||
const m = await api("/meta");
|
||||
departments = m.departments || [];
|
||||
// 注册页部门下拉:固定五个部门
|
||||
const deptOptions =
|
||||
`<option value="" disabled selected>请选择部门</option>` +
|
||||
departments.map((d) => `<option>${esc(d)}</option>`).join("");
|
||||
$("#register-form [name=department]").innerHTML = deptOptions;
|
||||
// 设置页部门下拉(可留空表示未选择)
|
||||
$("#pf-dept").innerHTML =
|
||||
`<option value="">未选择</option>` +
|
||||
departments.map((d) => `<option>${esc(d)}</option>`).join("");
|
||||
// 活动墙部门筛选:固定五个部门,未登录即可用
|
||||
fillFeedDept();
|
||||
// 成员页部门筛选
|
||||
fillMembersDept();
|
||||
} catch {
|
||||
$("#register-error").textContent = "部门信息加载失败,请刷新页面重试";
|
||||
}
|
||||
|
||||
if (!getToken()) return showAuth();
|
||||
try {
|
||||
const data = await api("/auth/me");
|
||||
enterApp(data.user);
|
||||
} catch { showAuth(); }
|
||||
}
|
||||
|
||||
boot();
|
||||
})();
|
||||
Reference in New Issue
Block a user