Initial role user app

This commit is contained in:
湛兮
2026-06-02 14:46:39 +08:00
commit 003dc60111
62 changed files with 7835 additions and 0 deletions
@@ -0,0 +1,27 @@
"use client";
import { useRouter } from "next/navigation";
import { useEffect, useRef } from "react";
type AnnouncementReadMarkerProps = {
id: string;
read: boolean;
};
export function AnnouncementReadMarker({ id, read }: AnnouncementReadMarkerProps) {
const router = useRouter();
const sentRef = useRef(false);
useEffect(() => {
if (read || sentRef.current) return;
sentRef.current = true;
void fetch(`/api/mobile/announcements/${encodeURIComponent(id)}/read`, { method: "POST" }).then((response) => {
if (response.ok) {
router.refresh();
}
});
}, [id, read, router]);
return null;
}
+33
View File
@@ -0,0 +1,33 @@
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { Bell, CalendarDays, ClipboardList, Home, UserRound } from "lucide-react";
const items = [
{ href: "/dashboard", label: "工作台", icon: Home },
{ href: "/tasks", label: "任务", icon: ClipboardList },
{ href: "/schedule", label: "排班", icon: CalendarDays },
{ href: "/announcements", label: "公告", icon: Bell },
{ href: "/me", label: "我的", icon: UserRound }
] as const;
export function BottomNav() {
const pathname = usePathname();
return (
<nav className="bottom-nav" aria-label="主要导航">
{items.map((item) => {
const Icon = item.icon;
const active = pathname === item.href || pathname.startsWith(`${item.href}/`);
return (
<Link className={`bottom-nav__item${active ? " is-active" : ""}`} href={item.href} key={item.href}>
<Icon aria-hidden size={20} strokeWidth={active ? 2.6 : 2} />
<span>{item.label}</span>
</Link>
);
})}
</nav>
);
}
+16
View File
@@ -0,0 +1,16 @@
import { Inbox } from "lucide-react";
type EmptyStateProps = {
title: string;
description: string;
};
export function EmptyState({ title, description }: EmptyStateProps) {
return (
<section className="empty-state">
<Inbox aria-hidden size={28} />
<h2>{title}</h2>
<p>{description}</p>
</section>
);
}
+35
View File
@@ -0,0 +1,35 @@
import Link from "next/link";
import type { Route } from "next";
import type { CSSProperties } from "react";
type FilterNavOption = {
href: Route;
label: string;
active: boolean;
};
type FilterNavProps = {
label: string;
options: FilterNavOption[];
};
export function FilterNav({ label, options }: FilterNavProps) {
return (
<nav
aria-label={label}
className="segmented"
style={{ "--segment-count": options.length } as CSSProperties}
>
{options.map((option) => (
<Link
aria-current={option.active ? "page" : undefined}
className={option.active ? "is-active" : undefined}
href={option.href}
key={option.label}
>
{option.label}
</Link>
))}
</nav>
);
}
+83
View File
@@ -0,0 +1,83 @@
"use client";
import { useRouter } from "next/navigation";
import { Eye, EyeOff, LockKeyhole, Smartphone } from "lucide-react";
import { FormEvent, useState } from "react";
export function LoginForm() {
const router = useRouter();
const [error, setError] = useState("");
const [pending, setPending] = useState(false);
const [passwordVisible, setPasswordVisible] = useState(false);
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setError("");
setPending(true);
const formData = new FormData(event.currentTarget);
const response = await fetch("/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
username: String(formData.get("username") || "").trim(),
password: String(formData.get("password") || "")
})
});
if (!response.ok) {
const payload = (await response.json().catch(() => null)) as { message?: string } | null;
setError(payload?.message || "登录失败,请检查手机号和密码");
setPending(false);
return;
}
router.replace("/dashboard");
router.refresh();
}
return (
<form className="login-form" onSubmit={handleSubmit}>
<label>
<span>手机号</span>
<div className="input-wrap">
<Smartphone aria-hidden size={18} />
<input
autoComplete="username"
defaultValue="13211111111"
inputMode="tel"
name="username"
placeholder="请输入员工手机号"
required
/>
</div>
</label>
<label>
<span>密码</span>
<div className="input-wrap">
<LockKeyhole aria-hidden size={18} />
<input
autoComplete="current-password"
defaultValue="pw111111"
name="password"
placeholder="请输入登录密码"
required
type={passwordVisible ? "text" : "password"}
/>
<button
aria-label={passwordVisible ? "隐藏密码" : "显示密码"}
className="icon-button"
onClick={() => setPasswordVisible((visible) => !visible)}
type="button"
>
{passwordVisible ? <EyeOff aria-hidden size={18} /> : <Eye aria-hidden size={18} />}
</button>
</div>
</label>
{error ? <p className="form-error">{error}</p> : null}
<button className="primary-button" disabled={pending} type="submit">
{pending ? "登录中..." : "登录"}
</button>
</form>
);
}
+24
View File
@@ -0,0 +1,24 @@
"use client";
import { useRouter } from "next/navigation";
import { LogOut } from "lucide-react";
import { useState } from "react";
export function LogoutButton() {
const router = useRouter();
const [pending, setPending] = useState(false);
async function logout() {
setPending(true);
await fetch("/api/auth/logout", { method: "POST" });
router.replace("/login");
router.refresh();
}
return (
<button className="secondary-button danger" disabled={pending} onClick={logout} type="button">
<LogOut aria-hidden size={18} />
{pending ? "退出中..." : "退出登录"}
</button>
);
}
+15
View File
@@ -0,0 +1,15 @@
type PageHeaderProps = {
eyebrow?: string;
title: string;
description?: string;
};
export function PageHeader({ eyebrow, title, description }: PageHeaderProps) {
return (
<header className="page-header">
{eyebrow ? <p className="eyebrow">{eyebrow}</p> : null}
<h1>{title}</h1>
{description ? <p>{description}</p> : null}
</header>
);
}
+84
View File
@@ -0,0 +1,84 @@
"use client";
import { KeyRound } from "lucide-react";
import { FormEvent, useState } from "react";
import type { ApiEnvelope } from "@/lib/types";
async function readError(response: Response) {
const payload = (await response.json().catch(() => null)) as Partial<ApiEnvelope<unknown>> | null;
return payload?.message || "修改密码失败";
}
export function PasswordChangeForm() {
const [pending, setPending] = useState(false);
const [error, setError] = useState("");
const [message, setMessage] = useState("");
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setError("");
setMessage("");
setPending(true);
const form = event.currentTarget;
const formData = new FormData(form);
const oldPassword = String(formData.get("oldPassword") || "");
const newPassword = String(formData.get("newPassword") || "");
const confirmPassword = String(formData.get("confirmPassword") || "");
if (newPassword !== confirmPassword) {
setError("两次输入的新密码不一致");
setPending(false);
return;
}
const response = await fetch("/api/auth/me/password", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ oldPassword, newPassword })
});
if (!response.ok) {
setError(await readError(response));
setPending(false);
return;
}
form.reset();
setMessage("密码已修改");
setPending(false);
}
return (
<form className="form-stack" onSubmit={handleSubmit}>
<label>
<span>旧密码</span>
<div className="input-wrap">
<KeyRound aria-hidden size={18} />
<input autoComplete="current-password" minLength={8} name="oldPassword" required type="password" />
</div>
</label>
<label>
<span>新密码</span>
<div className="input-wrap">
<KeyRound aria-hidden size={18} />
<input autoComplete="new-password" minLength={8} name="newPassword" required type="password" />
</div>
</label>
<label>
<span>确认新密码</span>
<div className="input-wrap">
<KeyRound aria-hidden size={18} />
<input autoComplete="new-password" minLength={8} name="confirmPassword" required type="password" />
</div>
</label>
{error ? <p className="form-error">{error}</p> : null}
{message ? <p className="form-success">{message}</p> : null}
<button className="primary-button" disabled={pending} type="submit">
<KeyRound aria-hidden size={18} />
{pending ? "修改中..." : "修改密码"}
</button>
</form>
);
}
+8
View File
@@ -0,0 +1,8 @@
type StatusPillProps = {
children: React.ReactNode;
tone?: "default" | "success" | "warning" | "danger";
};
export function StatusPill({ children, tone = "default" }: StatusPillProps) {
return <span className={`status-pill status-pill--${tone}`}>{children}</span>;
}
+145
View File
@@ -0,0 +1,145 @@
"use client";
import { useRouter } from "next/navigation";
import { CheckCircle2, MessageSquarePlus, PlayCircle } from "lucide-react";
import { FormEvent, useState } from "react";
import type { ApiEnvelope, TaskDetail, TaskStatus } from "@/lib/types";
type TaskActionPanelProps = {
taskId: string;
initialStatus: TaskStatus;
};
type TaskAction = "start" | "complete" | "comment";
function normalizeStatus(value: unknown): TaskStatus | null {
switch (value) {
case "PENDING":
case "pending":
return "pending";
case "IN_PROGRESS":
case "in_progress":
return "in_progress";
case "COMPLETED":
case "completed":
return "completed";
case "CANCELLED":
case "cancelled":
return "cancelled";
default:
return null;
}
}
async function readMessage(response: Response) {
const payload = (await response.json().catch(() => null)) as Partial<ApiEnvelope<unknown>> | null;
return payload?.message || "操作失败";
}
export function TaskActionPanel({ taskId, initialStatus }: TaskActionPanelProps) {
const router = useRouter();
const [status, setStatus] = useState(initialStatus);
const [pendingAction, setPendingAction] = useState<TaskAction | null>(null);
const [message, setMessage] = useState("");
const [error, setError] = useState("");
async function runAction(action: Exclude<TaskAction, "comment">) {
setError("");
setMessage("");
setPendingAction(action);
const response = await fetch(`/api/mobile/tasks/${encodeURIComponent(taskId)}/${action}`, { method: "POST" });
if (!response.ok) {
setError(await readMessage(response));
setPendingAction(null);
return;
}
const payload = (await response.json().catch(() => null)) as ApiEnvelope<TaskDetail> | null;
const nextStatus = normalizeStatus(payload?.data?.status);
if (nextStatus) setStatus(nextStatus);
setMessage(action === "start" ? "任务已开始处理" : "任务已完成");
setPendingAction(null);
router.refresh();
}
async function submitComment(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setError("");
setMessage("");
setPendingAction("comment");
const form = event.currentTarget;
const formData = new FormData(form);
const comment = String(formData.get("comment") || "").trim();
if (!comment) {
setError("请填写备注内容");
setPendingAction(null);
return;
}
const response = await fetch(`/api/mobile/tasks/${encodeURIComponent(taskId)}/comment`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ comment })
});
if (!response.ok) {
setError(await readMessage(response));
setPendingAction(null);
return;
}
form.reset();
setMessage("备注已追加");
setPendingAction(null);
router.refresh();
}
const canStart = status === "pending";
const canComplete = status === "pending" || status === "in_progress";
return (
<section className="section-block">
<div className="section-title">
<h2>处理任务</h2>
</div>
<div className="button-row">
<button
className="secondary-button"
disabled={!canStart || pendingAction !== null}
onClick={() => void runAction("start")}
type="button"
>
<PlayCircle aria-hidden size={18} />
{pendingAction === "start" ? "开始中..." : "开始处理"}
</button>
<button
className="primary-button"
disabled={!canComplete || pendingAction !== null}
onClick={() => void runAction("complete")}
type="button"
>
<CheckCircle2 aria-hidden size={18} />
{pendingAction === "complete" ? "完成中..." : "完成任务"}
</button>
</div>
<form className="form-stack" onSubmit={submitComment}>
<label>
<span>处理备注</span>
<textarea className="text-area" maxLength={1000} name="comment" placeholder="填写交接、处理进展或完成说明" />
</label>
<button className="secondary-button" disabled={pendingAction !== null} type="submit">
<MessageSquarePlus aria-hidden size={18} />
{pendingAction === "comment" ? "提交中..." : "追加备注"}
</button>
</form>
{error ? <p className="form-error">{error}</p> : null}
{message ? <p className="form-success">{message}</p> : null}
</section>
);
}