MVP: 按 07 风格重写前端 + Go 后端落地(长文/短文、编辑器、后台管理)

This commit is contained in:
Sakurasan
2026-09-20 20:56:03 +08:00
parent edee708802
commit 4d8f2de3a4
96 changed files with 6005 additions and 2602 deletions
+73
View File
@@ -0,0 +1,73 @@
const base = ''
async function request(path, { method = 'GET', body, auth = false } = {}) {
const headers = { 'Content-Type': 'application/json' }
const res = await fetch(base + path, {
method,
headers,
credentials: 'include',
body: body === undefined ? undefined : JSON.stringify(body)
})
if (res.status === 401) {
window.dispatchEvent(new CustomEvent('one:unauthorized'))
const err = new Error('未登录或登录已过期')
err.status = 401
throw err
}
if (!res.ok) {
let msg = `请求失败(${res.status})`
try {
const data = await res.json()
if (data && data.error) msg = data.error
} catch (_) {
/* 非 JSON 响应 */
}
const err = new Error(msg)
err.status = res.status
throw err
}
if (res.status === 204) return null
return res.json()
}
export const publicApi = {
site: () => request('/api/site'),
posts: (params = {}) => request('/api/posts?' + new URLSearchParams(params)),
post: (slug) => request('/api/posts/' + encodeURIComponent(slug)),
archive: () => request('/api/archive'),
tags: () => request('/api/tags')
}
export const adminApi = {
login: (username, password) =>
request('/api/admin/login', { method: 'POST', body: { username, password } }),
logout: () => request('/api/admin/logout', { method: 'POST' }),
me: () => request('/api/admin/me'),
posts: (params = {}) => request('/api/admin/posts?' + new URLSearchParams(params)),
post: (id) => request('/api/admin/posts/' + id),
createPost: (body) => request('/api/admin/posts', { method: 'POST', body }),
updatePost: (id, body) => request('/api/admin/posts/' + id, { method: 'PUT', body }),
deletePost: (id) => request('/api/admin/posts/' + id, { method: 'DELETE' }),
tags: () => request('/api/admin/tags'),
createTag: (name) => request('/api/admin/tags', { method: 'POST', body: { name } }),
renameTag: (id, name) => request('/api/admin/tags/' + id, { method: 'PUT', body: { name } }),
deleteTag: (id) => request('/api/admin/tags/' + id, { method: 'DELETE' }),
settings: () => request('/api/admin/settings'),
saveSettings: (body) => request('/api/admin/settings', { method: 'PUT', body })
}
// 后台登录态:cookie 由服务端下发(httpOnly),这里只存一份展示用的用户名
const USER_KEY = 'one.admin.user'
export const session = {
get user() {
return localStorage.getItem(USER_KEY) || ''
},
set user(v) {
if (v) localStorage.setItem(USER_KEY, v)
else localStorage.removeItem(USER_KEY)
},
clear() {
localStorage.removeItem(USER_KEY)
}
}