From d9949bd8ad1e4bf7bcb6d24fd7c9ad35d1a51460 Mon Sep 17 00:00:00 2001 From: anghunk Date: Thu, 15 Jan 2026 15:02:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E7=AE=A1=E7=90=86=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 61 +++-- src/views/admin.ts | 607 ++++++++++++++++++++++-------------------- src/views/login.ts | 211 ++++++++------- src/views/settings.ts | 130 +++++++++ 4 files changed, 599 insertions(+), 410 deletions(-) create mode 100644 src/views/settings.ts diff --git a/src/index.ts b/src/index.ts index fcd3133..c28fb10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,44 +1,49 @@ -import { Hono } from 'hono' -import { cors } from 'hono/cors' -import { Bindings } from './bindings' -import { LoginView } from './views/login' -import { AdminView } from './views/admin' -import { customCors } from './utils/cors' -import { adminAuth } from './utils/auth' +import { Hono } from 'hono'; +import { cors } from 'hono/cors'; +import { Bindings } from './bindings'; +import { loginView } from './views/login'; +import { AdminView } from './views/admin'; +import { SettingsView } from './views/settings'; +import { customCors } from './utils/cors'; +import { adminAuth } from './utils/auth'; -import { getComments } from './api/public/getComments' -import { postComment } from './api/public/postComment' -import { adminLogin } from './api/admin/login' -import { deleteComment } from './api/admin/deleteComment' -import { listComments } from './api/admin/listComments' -import { updateStatus } from './api/admin/updateStatus' +import { getComments } from './api/public/getComments'; +import { postComment } from './api/public/postComment'; +import { adminLogin } from './api/admin/login'; +import { deleteComment } from './api/admin/deleteComment'; +import { listComments } from './api/admin/listComments'; +import { updateStatus } from './api/admin/updateStatus'; -const app = new Hono<{ Bindings: Bindings }>() +const app = new Hono<{ Bindings: Bindings }>(); // 跨域 app.use('/api/*', async (c, next) => { - const corsMiddleware = customCors(c.env.ALLOW_ORIGIN) - return corsMiddleware(c, next) -}) + const corsMiddleware = customCors(c.env.ALLOW_ORIGIN); + return corsMiddleware(c, next); +}); app.use('/admin/*', async (c, next) => { - const corsMiddleware = customCors(c.env.ALLOW_ORIGIN) - return corsMiddleware(c, next) -}) + const corsMiddleware = customCors(c.env.ALLOW_ORIGIN); + return corsMiddleware(c, next); +}); // 页面路由 -app.get('/', (c) => c.redirect('/login')) -app.get('/login', (c) => c.html(LoginView)) -app.get('/admin', (c) => c.html(AdminView)) +app.get('/', (c) => c.redirect('/login')); +app.get('/login', (c) => { + const isDev = new URL(c.req.url).hostname === 'localhost'; + return c.html(loginView(isDev, c.env.ADMIN_NAME, c.env.ADMIN_PASSWORD)); +}); +app.get('/admin', (c) => c.html(AdminView)); +app.get('/admin/settings', (c) => c.html(SettingsView)); // API -app.get('/api/comments', getComments) -app.post('/api/comments', postComment) +app.get('/api/comments', getComments); +app.post('/api/comments', postComment); -app.post('/admin/login', adminLogin) -app.use('/admin/*', adminAuth) +app.post('/admin/login', adminLogin); +app.use('/admin/*', adminAuth); app.delete('/admin/comments/delete', deleteComment); app.get('/admin/comments/list', listComments); app.put('/admin/comments/status', updateStatus); -export default app \ No newline at end of file +export default app; diff --git a/src/views/admin.ts b/src/views/admin.ts index 38a268a..e734c26 100644 --- a/src/views/admin.ts +++ b/src/views/admin.ts @@ -1,314 +1,339 @@ -import { html } from "hono/html"; +import { html } from 'hono/html'; export const AdminView = html` - - - - - - CWD 评论后台管理系统 - - - - - - + + + + + + CWD 评论后台管理系统 + + + + + + +
+ + +
+ + {{ toast.message }} +
+
-
- - -
- - {{ toast.message }} -
-
+ + - - -
-
-
-

设置

- -
-
- - -

设置后,文章链接将自动拼接此前缀

-
-
- -
-
-
-
+ +
+
+

评论管理

+ +
+
+
+
- - - - -
-
-
-
- -
-
- - - - - - - - - - - - - - - - - + + + +
信息内容状态操作
暂无评论数据
-
-
-
- - () -
- {{ comment.url }} -
IP:
-
{{ formatDate(comment.pubDate) }}
-
-
-
-
- - {{ config.blogDomain + comment.postSlug }} - -
- +
+ + + + + + + + + + + + + + + + + - - - -
信息内容状态操作
暂无评论数据
+
+
+
+ + () +
+ {{ comment.url }} +
IP:
+
{{ formatDate(comment.pubDate) }}
+
+
+
+
+ + {{ config.blogDomain + comment.postSlug }} + +
+ - {{ comment.status }} - - - - - -
-
+ }" + > + {{ comment.status }} +
+
+ + + +
+
- -
- -
-
-
-
+ +
+ +
+
+ + - - - + return { + ...toRefs(state), + fetchComments, + updateStatus, + confirmDelete, + changePage, + logout, + formatDate, + }; + }, + }).mount('#app'); + + + `; diff --git a/src/views/login.ts b/src/views/login.ts index d63d336..569577e 100644 --- a/src/views/login.ts +++ b/src/views/login.ts @@ -1,100 +1,129 @@ -import { html } from "hono/html"; +import { html } from 'hono/html'; -export const LoginView = html` - - - - - - 登录 - CWD 评论后台 - - - - - - +export const loginView = (isDev: boolean, adminName?: string, adminPassword?: string) => html` + + + + + + 登录 - CWD 评论后台 + + + + + + +
+
+

管理员登录

+
+
+ + +
+
+ + +
+
+ + +
+ +
+

{{ error }}

+
+
-
-
-

管理员登录

-
-
- - -
-
- - -
-
- - -
- -
-

{{ error }}

-
-
+ - - + return { ...toRefs(state), handleLogin }; + }, + }).mount('#app'); + + + `; diff --git a/src/views/settings.ts b/src/views/settings.ts new file mode 100644 index 0000000..390f5b0 --- /dev/null +++ b/src/views/settings.ts @@ -0,0 +1,130 @@ +import { html } from 'hono/html'; + +export const SettingsView = html` + + + + + + 设置 - CWD 评论后台 + + + + + + +
+ + +
+ + {{ toast.message }} +
+
+ + + + + +
+

设置

+
+
+ + +

设置后,文章链接将自动拼接此前缀

+
+
+
+ +
+
+
+ + + + +`;