From 6868eebc621fc6235cd10af7de36d108d5d6a201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E7=90=A6?= Date: Tue, 23 Jun 2026 13:17:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .mimocode/plans/1782182188576-happy-harbor.md | 120 +++ AGENTS.md | 31 + index.html | 935 +++++++++++++++--- main.go | 502 +++++++++- pwd.json | 4 + 6 files changed, 1411 insertions(+), 182 deletions(-) create mode 100644 .mimocode/plans/1782182188576-happy-harbor.md create mode 100644 AGENTS.md create mode 100644 pwd.json diff --git a/.gitignore b/.gitignore index a6900b8..72adc2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.opencode/ /signature-server +/uploads/ diff --git a/.mimocode/plans/1782182188576-happy-harbor.md b/.mimocode/plans/1782182188576-happy-harbor.md new file mode 100644 index 0000000..f705f7a --- /dev/null +++ b/.mimocode/plans/1782182188576-happy-harbor.md @@ -0,0 +1,120 @@ +# 计划:签名列表 + 登录 + 下载功能 + +## 背景 + +当前系统:单文件 Go 服务器 (`main.go`),内嵌 `index.html` 前端。签名文件保存在 `uploads/<姓名>/<姓名>+<日期>.png`。没有认证、没有列表、没有下载。 + +**目标**:添加登录验证功能,在登录后可以查看签名列表并下载,同时保持原有签名功能不变。 + +--- + +## 架构决策 + +**保持单文件方式**:所有改动都在 `main.go`(后端路由)和 `index.html`(前端单页应用)中完成。运行时只需额外创建 `pwd.json` 文件。 + +**认证方式**:简单的 Token 认证 —— 后端登录成功后生成 Token,前端存入 `localStorage`,请求受保护路由时携带 Token。无需复杂的 Session 框架。 + +**文件存储不变**:签名文件仍保存在 `uploads/<姓名>/<姓名>+<日期>.png`,列表接口扫描该目录。 + +--- + +## 具体改动 + +### 1. 后端 (`main.go`) + +#### 新增路由: +| 路由 | 方法 | 说明 | +|------|------|------| +| `/login` | POST | 根据 `pwd.json` 验证账号密码,返回 `{success, token}` | +| `/api/signatures` | GET | 列出所有签名文件(需要有效 Token) | +| `/download` | GET | 下载签名图片(需要有效 Token,`?path=` 参数) | +| `/api/check-auth` | GET | 验证 Token 是否有效,返回 `{authenticated}` | + +#### 实现细节: + +**`pwd.json` 加载**: +- 从可执行文件目录读取:`filepath.Join(filepath.Dir(os.Args[0]), "pwd.json")` +- 如果找不到则从当前工作目录读取 +- 格式:`{"account": "liqi", "password": "qiqi991012"}` +- 启动时加载一次到内存中 + +**Token 系统**: +- 登录成功后生成 Token = base64(账号 + 时间戳) +- 用 `map[string]string` 存储有效 Token(Token → 账号),带过期时间 +- Token 24 小时后过期 +- 内存存储即可,无需持久化 + +**`/api/signatures` 处理函数**: +- 遍历 `uploads/` 目录,收集所有 `.png` 文件 +- 返回 JSON 数组:`[{name, path, date, size}]` +- 按修改时间倒序排列 +- 需要 `Authorization: Bearer ` 请求头 + +**`/download` 处理函数**: +- 接受 `?path=uploads/<姓名>/<文件名>.png` 查询参数 +- 验证路径在 `uploads/` 目录内(防止路径穿越攻击) +- 流式返回文件,设置 `Content-Disposition: attachment` 头 +- 需要有效 Token + +### 2. 前端 (`index.html`) + +#### 核心逻辑变更: +**默认进入签名页面(无需登录)**,只有点击"签名列表"时才检查登录状态。 + +#### 新增区域(SPA 风格,无页面刷新): + +**登录视图** (`#loginView`): +- 居中卡片,包含账号密码输入框 +- "登录"按钮 +- 登录失败显示错误提示 +- 登录成功后自动跳转到签名列表 + +**签名列表视图** (`#listView`): +- 卡片列表显示所有签名 +- 列:签署人、日期、文件大小、下载按钮 +- "返回签名"按钮回到签名页面 +- 空状态显示"暂无签名记录" + +**导航栏**: +- "签名列表"按钮始终可见 +- 点击时检查登录状态,未登录则跳转登录页 +- 登录后显示"注销"按钮 + +#### 状态管理: +- `currentView`:'sign' | 'login' | 'list' +- `authToken`:存储在 `localStorage` +- 通过显示/隐藏区域切换视图 + +### 3. `pwd.json`(运行时文件) + +```json +{ + "account": "liqi", + "password": "qiqi991012" +} +``` + +放在可执行文件旁边(或 `go run` 时的工作目录)。 + +--- + +## 修改文件清单 + +| 文件 | 操作 | 范围 | +|------|------|------| +| `main.go` | 编辑 | 添加路由、认证逻辑、文件扫描 | +| `index.html` | 编辑 | 添加登录视图、列表视图、导航、认证状态 | +| `pwd.json` | 新建 | 账号密码(运行时) | + +--- + +## 验证步骤 + +1. **启动服务器**:`go run main.go` +2. **测试默认进入**:打开 `http://localhost:10081`,直接进入签名页面(无需登录) +3. **测试签名**:签名并提交,验证保存到 `uploads/` +4. **测试列表入口**:点击"签名列表"按钮,验证跳转到登录页面 +5. **测试登录**:输入账号密码,登录后跳转到签名列表 +6. **测试下载**:点击下载按钮,验证 PNG 文件正确下载 +7. **测试返回**:点击"返回签名",验证回到签名页面 +8. **测试注销**:点击"注销",再次点击"签名列表"需重新登录 diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..27e17c3 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,31 @@ +# AGENTS.md + +## Project Overview + +Single-file Go HTTP server for electronic signature capture and archiving. Frontend HTML is embedded into the Go binary via `//go:embed`. + +## Build & Run + +```bash +# Local development +go run main.go +# Server starts on port 10081 + +# Cross-compile (Windows/Linux) +build.bat +# Outputs to dist/ with timestamped filenames +``` + +## Architecture + +- `main.go` — Entire backend. Embeds `index.html`, serves it at `/`, handles `POST /upload` to save base64 PNGs to `uploads//+.png` +- `index.html` — Standalone frontend (Tailwind CDN, Phosphor Icons, canvas-based signature) +- `uploads/` — Runtime signature storage (gitignored) + +## Key Constraints + +- No `go.mod` present — initialize one if adding dependencies +- No tests exist +- Frontend uses CDN resources (Tailwind, Phosphor Icons) — no local build step +- Upload handler validates name non-empty, strips base64 prefix, saves as PNG +- Port is hardcoded to `:10081` diff --git a/index.html b/index.html index d7abf39..391c035 100644 --- a/index.html +++ b/index.html @@ -5,16 +5,18 @@ 电子签名归档系统 + - +