MVP: 按 07 风格重写前端 + Go 后端落地(长文/短文、编辑器、后台管理)
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
<script setup>
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { checkAuth } from './auth'
|
||||
import { adminApi, session } from '../api'
|
||||
import { site } from '../site'
|
||||
|
||||
const router = useRouter()
|
||||
const ready = ref(false)
|
||||
|
||||
onMounted(async () => {
|
||||
if (!(await checkAuth())) {
|
||||
router.replace('/admin/login')
|
||||
return
|
||||
}
|
||||
ready.value = true
|
||||
})
|
||||
|
||||
// 会话过期时(请求返回 401)统一跳回登录页
|
||||
window.addEventListener('one:unauthorized', () => {
|
||||
session.clear()
|
||||
if (router.currentRoute.value.path.startsWith('/admin')) router.replace('/admin/login')
|
||||
})
|
||||
|
||||
async function logout() {
|
||||
try {
|
||||
await adminApi.logout()
|
||||
} catch (e) {
|
||||
/* 忽略 */
|
||||
}
|
||||
session.clear()
|
||||
router.replace('/admin/login')
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="ready" class="admin">
|
||||
<header class="bar">
|
||||
<div class="brand">
|
||||
<RouterLink to="/" class="site">{{ site.site_title || 'ONE' }}</RouterLink>
|
||||
<span class="sep">/</span>
|
||||
<span class="where">后台</span>
|
||||
</div>
|
||||
<nav class="nav">
|
||||
<RouterLink to="/admin" class="link">文章</RouterLink>
|
||||
<RouterLink to="/admin/new" class="link">写新的</RouterLink>
|
||||
<RouterLink to="/admin/tags" class="link">标签</RouterLink>
|
||||
<RouterLink to="/admin/settings" class="link">设置</RouterLink>
|
||||
<RouterLink to="/" class="link">看前台</RouterLink>
|
||||
<button class="link as-btn" @click="logout">退出</button>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<main class="content">
|
||||
<RouterView />
|
||||
</main>
|
||||
</div>
|
||||
<div v-else class="checking">检查登录状态…</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.admin {
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
flex-wrap: wrap;
|
||||
padding: 12px 22px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
background: var(--card);
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 20;
|
||||
}
|
||||
|
||||
.brand {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.site {
|
||||
font-family: var(--serif);
|
||||
font-size: 17px;
|
||||
}
|
||||
|
||||
.sep {
|
||||
color: var(--faint);
|
||||
}
|
||||
|
||||
.where {
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.nav {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.link {
|
||||
padding: 5px 10px;
|
||||
border-radius: 3px;
|
||||
font-size: 14px;
|
||||
color: var(--ink-soft);
|
||||
border: 0;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.link:hover {
|
||||
background: var(--paper-sunken);
|
||||
color: var(--ink);
|
||||
}
|
||||
|
||||
.link.router-link-exact-active {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.as-btn {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.content {
|
||||
max-width: 980px;
|
||||
margin: 0 auto;
|
||||
padding: 26px 22px 80px;
|
||||
}
|
||||
|
||||
.checking {
|
||||
padding: 80px;
|
||||
text-align: center;
|
||||
color: var(--muted);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,605 @@
|
||||
<script setup>
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { adminApi } from '../api'
|
||||
import { md } from '../utils'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
const id = computed(() => (route.params.id ? Number(route.params.id) : 0))
|
||||
const isEdit = computed(() => id.value > 0)
|
||||
|
||||
const loading = ref(true)
|
||||
const saving = ref(false)
|
||||
const savedAt = ref('')
|
||||
const error = ref('')
|
||||
const tab = ref('write') // write | preview
|
||||
const tagInput = ref('')
|
||||
const dirty = ref(false)
|
||||
|
||||
const DRAFT_KEY = 'one.draft.new'
|
||||
|
||||
const form = reactive({
|
||||
kind: 'long',
|
||||
title: '',
|
||||
slug: '',
|
||||
summary: '',
|
||||
content_md: '',
|
||||
tags: [],
|
||||
status: 'draft',
|
||||
published_at: '',
|
||||
reading_minutes: null,
|
||||
override_minutes: false
|
||||
})
|
||||
|
||||
// ---------- 阅读时长 ----------
|
||||
|
||||
function estimateMinutes(text) {
|
||||
if (!text || !text.trim()) return 1
|
||||
const cjk = (text.match(/[㐀-鿿 -〿-]/g) || []).length
|
||||
const latin = (text.match(/[A-Za-z0-9']+/g) || []).length
|
||||
return Math.max(1, Math.floor(cjk / 400) + Math.floor(latin / 220))
|
||||
}
|
||||
|
||||
const autoMinutes = computed(() => estimateMinutes(form.content_md))
|
||||
const finalMinutes = computed(() =>
|
||||
form.override_minutes && form.reading_minutes ? Number(form.reading_minutes) : autoMinutes.value
|
||||
)
|
||||
|
||||
// ---------- 时间 ----------
|
||||
|
||||
function isoToLocal(iso) {
|
||||
if (!iso) return ''
|
||||
const d = new Date(iso)
|
||||
if (Number.isNaN(d.getTime())) return ''
|
||||
const pad = (n) => String(n).padStart(2, '0')
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(
|
||||
d.getMinutes()
|
||||
)}`
|
||||
}
|
||||
|
||||
function localToIso(local) {
|
||||
if (!local) return ''
|
||||
const d = new Date(local)
|
||||
if (Number.isNaN(d.getTime())) return ''
|
||||
return d.toISOString()
|
||||
}
|
||||
|
||||
const publishedLocal = ref('')
|
||||
|
||||
// ---------- 载入 ----------
|
||||
|
||||
onMounted(async () => {
|
||||
if (isEdit.value) {
|
||||
try {
|
||||
const p = await adminApi.post(id.value)
|
||||
form.kind = p.kind || 'long'
|
||||
form.title = p.title || ''
|
||||
form.slug = p.slug || ''
|
||||
form.summary = p.summary || ''
|
||||
form.content_md = p.content_md || ''
|
||||
form.tags = p.tags ? [...p.tags] : []
|
||||
form.status = p.status || 'draft'
|
||||
publishedLocal.value = isoToLocal(p.published_at)
|
||||
if (p.reading_minutes && p.reading_minutes !== estimateMinutes(p.content_md || '')) {
|
||||
form.override_minutes = true
|
||||
form.reading_minutes = p.reading_minutes
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = e.message || '载入失败'
|
||||
}
|
||||
} else {
|
||||
const raw = localStorage.getItem(DRAFT_KEY)
|
||||
if (raw) {
|
||||
try {
|
||||
Object.assign(form, JSON.parse(raw))
|
||||
savedAt.value = '本地草稿已恢复'
|
||||
} catch (e) {
|
||||
localStorage.removeItem(DRAFT_KEY)
|
||||
}
|
||||
}
|
||||
publishedLocal.value = isoToLocal(new Date().toISOString())
|
||||
}
|
||||
loading.value = false
|
||||
// 载入完成后的第一次变更才触发自动保存
|
||||
setTimeout(() => {
|
||||
dirty.value = false
|
||||
watchForm()
|
||||
}, 0)
|
||||
})
|
||||
|
||||
// ---------- 自动保存 ----------
|
||||
|
||||
let timer
|
||||
let stopWatch
|
||||
|
||||
function watchForm() {
|
||||
stopWatch = watch(
|
||||
form,
|
||||
() => {
|
||||
dirty.value = true
|
||||
clearTimeout(timer)
|
||||
timer = setTimeout(autosave, 1500)
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
watch(publishedLocal, () => {
|
||||
dirty.value = true
|
||||
clearTimeout(timer)
|
||||
timer = setTimeout(autosave, 1500)
|
||||
})
|
||||
}
|
||||
|
||||
async function autosave() {
|
||||
if (loading.value) return
|
||||
const payload = buildPayload()
|
||||
if (!payload.content_md.trim() && !payload.title.trim() && !isEdit.value) return
|
||||
saving.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
if (isEdit.value) {
|
||||
const saved = await adminApi.updatePost(id.value, payload)
|
||||
applySaved(saved)
|
||||
} else {
|
||||
localStorage.setItem(DRAFT_KEY, JSON.stringify({ ...form, tags: [...form.tags] }))
|
||||
}
|
||||
dirty.value = false
|
||||
savedAt.value = new Date().toLocaleTimeString('zh-CN', { hour12: false })
|
||||
} catch (e) {
|
||||
error.value = '自动保存失败:' + (e.message || '')
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function applySaved(saved) {
|
||||
form.slug = saved.slug
|
||||
form.status = saved.status
|
||||
publishedLocal.value = isoToLocal(saved.published_at)
|
||||
}
|
||||
|
||||
function buildPayload() {
|
||||
return {
|
||||
kind: form.kind,
|
||||
title: form.title,
|
||||
slug: form.slug,
|
||||
summary: form.summary,
|
||||
content_md: form.content_md,
|
||||
tags: [...form.tags],
|
||||
status: form.status,
|
||||
published_at: localToIso(publishedLocal.value) || new Date().toISOString(),
|
||||
reading_minutes: form.override_minutes && form.reading_minutes ? Number(form.reading_minutes) : null
|
||||
}
|
||||
}
|
||||
|
||||
async function save(status) {
|
||||
if (status) form.status = status
|
||||
if (!isEdit.value && !form.content_md.trim() && !form.title.trim()) {
|
||||
error.value = '先写点什么再保存'
|
||||
return
|
||||
}
|
||||
saving.value = true
|
||||
error.value = ''
|
||||
clearTimeout(timer)
|
||||
try {
|
||||
const payload = buildPayload()
|
||||
if (isEdit.value) {
|
||||
applySaved(await adminApi.updatePost(id.value, payload))
|
||||
} else {
|
||||
const created = await adminApi.createPost(payload)
|
||||
localStorage.removeItem(DRAFT_KEY)
|
||||
dirty.value = false
|
||||
router.replace(`/admin/${created.id}`)
|
||||
return
|
||||
}
|
||||
dirty.value = false
|
||||
savedAt.value = new Date().toLocaleTimeString('zh-CN', { hour12: false })
|
||||
} catch (e) {
|
||||
error.value = e.message || '保存失败'
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function publishNow() {
|
||||
await save('published')
|
||||
}
|
||||
|
||||
async function unpublish() {
|
||||
await save('draft')
|
||||
}
|
||||
|
||||
// ---------- 标签 ----------
|
||||
|
||||
function addTag() {
|
||||
const v = tagInput.value.trim().replace(/[,,]$/, '')
|
||||
if (!v) return
|
||||
if (!form.tags.includes(v)) form.tags.push(v)
|
||||
tagInput.value = ''
|
||||
}
|
||||
|
||||
function onTagInput(e) {
|
||||
if (e.key === 'Enter' || e.key === ',' || e.key === ',') {
|
||||
e.preventDefault()
|
||||
addTag()
|
||||
}
|
||||
}
|
||||
|
||||
function removeTag(t) {
|
||||
form.tags = form.tags.filter((x) => x !== t)
|
||||
}
|
||||
|
||||
// ---------- 预览 ----------
|
||||
|
||||
const preview = computed(() => md.render(form.content_md || ''))
|
||||
|
||||
function onKeydown(e) {
|
||||
// Tab 缩进,而不是跳出输入框
|
||||
if (e.key === 'Tab') {
|
||||
e.preventDefault()
|
||||
const el = e.target
|
||||
const start = el.selectionStart
|
||||
const end = el.selectionEnd
|
||||
form.content_md =
|
||||
form.content_md.slice(0, start) + ' ' + form.content_md.slice(end)
|
||||
setTimeout(() => el.setSelectionRange(start + 2, start + 2))
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section v-if="loading" class="loading">载入中…</section>
|
||||
|
||||
<section v-else class="editor">
|
||||
<header class="head">
|
||||
<div class="left">
|
||||
<h1 class="title">{{ isEdit ? '编辑文章' : '写新的' }}</h1>
|
||||
<span class="state">
|
||||
<span v-if="saving">保存中…</span>
|
||||
<span v-else-if="dirty">有未保存的改动</span>
|
||||
<span v-else-if="savedAt">已保存 {{ savedAt }}</span>
|
||||
<span v-else>还没改过</span>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="right">
|
||||
<button class="btn" @click="form.status === 'published' ? unpublish() : save()">
|
||||
存草稿
|
||||
</button>
|
||||
<button v-if="form.status !== 'published'" class="btn btn-primary" @click="publishNow">
|
||||
发布
|
||||
</button>
|
||||
<button v-else class="btn" @click="unpublish">转为草稿</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<p v-if="error" class="error">{{ error }}</p>
|
||||
|
||||
<div class="grid">
|
||||
<!-- 左:正文 -->
|
||||
<div class="main">
|
||||
<div class="kind-switch">
|
||||
<button class="kind" :class="{ on: form.kind === 'long' }" @click="form.kind = 'long'">
|
||||
长文
|
||||
</button>
|
||||
<button class="kind" :class="{ on: form.kind === 'short' }" @click="form.kind = 'short'">
|
||||
短文
|
||||
</button>
|
||||
<span class="hint">
|
||||
{{ form.kind === 'long' ? '有标题、有结构,适合讲完整一件事' : '一两段话,时间线里不显示标题' }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<input
|
||||
v-model="form.title"
|
||||
class="input title-input"
|
||||
:placeholder="form.kind === 'short' ? '标题(可留空,仅作归档索引)' : '标题'"
|
||||
/>
|
||||
|
||||
<div class="tabs">
|
||||
<button class="tab" :class="{ on: tab === 'write' }" @click="tab = 'write'">编写</button>
|
||||
<button class="tab" :class="{ on: tab === 'preview' }" @click="tab = 'preview'">预览</button>
|
||||
<span class="counter">{{ form.content_md.length }} 字</span>
|
||||
</div>
|
||||
|
||||
<textarea
|
||||
v-show="tab === 'write'"
|
||||
v-model="form.content_md"
|
||||
class="textarea md-input"
|
||||
rows="20"
|
||||
:placeholder="form.kind === 'short' ? '写点什么…' : '# 标题\n\n正文,支持 Markdown。'"
|
||||
@keydown="onKeydown"
|
||||
></textarea>
|
||||
|
||||
<div v-show="tab === 'preview'" class="prose preview" v-html="preview"></div>
|
||||
</div>
|
||||
|
||||
<!-- 右:元信息 -->
|
||||
<aside class="side">
|
||||
<div class="field">
|
||||
<label>类型</label>
|
||||
<p class="value">{{ form.kind === 'short' ? '短文' : '长文' }}</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="slug">Slug(链接)</label>
|
||||
<input id="slug" v-model="form.slug" class="input" placeholder="留空自动生成" />
|
||||
<p v-if="form.slug" class="sub">/post/{{ form.slug }}</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="summary">摘要</label>
|
||||
<textarea
|
||||
id="summary"
|
||||
v-model="form.summary"
|
||||
class="textarea"
|
||||
rows="3"
|
||||
placeholder="长文显示在时间线里的一段话"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>标签</label>
|
||||
<div v-if="form.tags.length" class="taglist">
|
||||
<span v-for="t in form.tags" :key="t" class="tag-chip">
|
||||
{{ t }}
|
||||
<button class="x" @click="removeTag(t)">×</button>
|
||||
</span>
|
||||
</div>
|
||||
<input
|
||||
v-model="tagInput"
|
||||
class="input"
|
||||
placeholder="输入后回车添加"
|
||||
@keydown="onTagInput"
|
||||
@blur="addTag"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="pub">发布时间</label>
|
||||
<input id="pub" v-model="publishedLocal" type="datetime-local" class="input" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="mins">阅读时长(分钟)</label>
|
||||
<div class="mins-row">
|
||||
<input
|
||||
id="mins"
|
||||
v-model="form.reading_minutes"
|
||||
type="number"
|
||||
min="1"
|
||||
class="input"
|
||||
:disabled="!form.override_minutes"
|
||||
:placeholder="String(autoMinutes)"
|
||||
/>
|
||||
<label class="check">
|
||||
<input v-model="form.override_minutes" type="checkbox" />
|
||||
手动指定
|
||||
</label>
|
||||
</div>
|
||||
<p class="sub">自动估算:{{ finalMinutes }} 分钟</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>状态</label>
|
||||
<p class="value">
|
||||
<span class="dot" :class="form.status"></span>
|
||||
{{ form.status === 'published' ? '已发布' : '草稿' }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<button class="btn wide" @click="save()">立即保存</button>
|
||||
<RouterLink to="/admin" class="back">← 返回列表</RouterLink>
|
||||
</aside>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.left {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.state {
|
||||
font-size: 12.5px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.right {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #9a5b45;
|
||||
font-size: 13px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) 280px;
|
||||
gap: 22px;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
@media (max-width: 860px) {
|
||||
.grid {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
.kind-switch {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.kind {
|
||||
padding: 5px 14px;
|
||||
border: 1px solid var(--line);
|
||||
background: var(--card);
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.kind.on {
|
||||
background: var(--accent);
|
||||
border-color: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.hint {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.title-input {
|
||||
font-family: var(--serif);
|
||||
font-size: 19px;
|
||||
padding: 10px 12px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.tabs {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
margin-bottom: -1px;
|
||||
}
|
||||
|
||||
.tab {
|
||||
padding: 7px 14px;
|
||||
border: 1px solid transparent;
|
||||
border-bottom: 0;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
font-size: 13.5px;
|
||||
color: var(--muted);
|
||||
border-radius: 3px 3px 0 0;
|
||||
}
|
||||
|
||||
.tab.on {
|
||||
color: var(--accent);
|
||||
border-color: var(--line);
|
||||
background: var(--card);
|
||||
margin-bottom: -1px;
|
||||
}
|
||||
|
||||
.counter {
|
||||
margin-left: auto;
|
||||
font-size: 12px;
|
||||
color: var(--faint);
|
||||
padding-bottom: 6px;
|
||||
}
|
||||
|
||||
.md-input {
|
||||
min-height: 420px;
|
||||
font-family: var(--mono);
|
||||
font-size: 14px;
|
||||
line-height: 1.8;
|
||||
border-radius: 0 3px 3px 3px;
|
||||
}
|
||||
|
||||
.preview {
|
||||
min-height: 420px;
|
||||
padding: 18px 20px;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 0 3px 3px 3px;
|
||||
}
|
||||
|
||||
.side {
|
||||
border: 1px solid var(--line);
|
||||
background: var(--card);
|
||||
border-radius: 4px;
|
||||
padding: 18px;
|
||||
}
|
||||
|
||||
.value {
|
||||
margin: 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.sub {
|
||||
margin: 4px 0 0;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.taglist {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.x {
|
||||
border: 0;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
color: var(--muted);
|
||||
padding: 0 0 0 4px;
|
||||
}
|
||||
|
||||
.mins-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.check {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
white-space: nowrap;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.dot {
|
||||
display: inline-block;
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 50%;
|
||||
background: var(--faint);
|
||||
margin-right: 6px;
|
||||
vertical-align: 2px;
|
||||
}
|
||||
|
||||
.dot.published {
|
||||
background: var(--accent);
|
||||
}
|
||||
|
||||
.wide {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.back {
|
||||
display: inline-block;
|
||||
margin-top: 14px;
|
||||
font-size: 13px;
|
||||
color: var(--accent);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,117 @@
|
||||
<script setup>
|
||||
import { ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { adminApi, session } from '../api'
|
||||
import { site } from '../site'
|
||||
|
||||
const router = useRouter()
|
||||
const username = ref('admin')
|
||||
const password = ref('')
|
||||
const error = ref('')
|
||||
const busy = ref(false)
|
||||
|
||||
async function submit() {
|
||||
error.value = ''
|
||||
busy.value = true
|
||||
try {
|
||||
const data = await adminApi.login(username.value, password.value)
|
||||
session.user = username.value
|
||||
void data
|
||||
router.push('/admin')
|
||||
} catch (e) {
|
||||
error.value = '用户名或密码不对'
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="page">
|
||||
<form class="card" @submit.prevent="submit">
|
||||
<p class="eyebrow">后台</p>
|
||||
<h1 class="title">登录 {{ site.site_title || 'ONE' }}</h1>
|
||||
|
||||
<div class="field">
|
||||
<label for="u">用户名</label>
|
||||
<input id="u" v-model="username" class="input" autocomplete="username" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="p">密码</label>
|
||||
<input
|
||||
id="p"
|
||||
v-model="password"
|
||||
type="password"
|
||||
class="input"
|
||||
autocomplete="current-password"
|
||||
@keyup.enter="submit"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<p v-if="error" class="error">{{ error }}</p>
|
||||
|
||||
<button class="btn btn-primary" type="submit" :disabled="busy || !password">
|
||||
{{ busy ? '登录中…' : '登录' }}
|
||||
</button>
|
||||
|
||||
<p class="hint">
|
||||
默认账号在服务端环境变量里:<code>ONE_ADMIN_USER</code> / <code>ONE_ADMIN_PASSWORD</code>。
|
||||
</p>
|
||||
|
||||
<RouterLink to="/" class="back">← 回到前台</RouterLink>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.page {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 40px 20px;
|
||||
}
|
||||
|
||||
.card {
|
||||
width: 100%;
|
||||
max-width: 380px;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 4px;
|
||||
padding: 28px;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 22px;
|
||||
margin: 6px 0 22px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #9a5b45;
|
||||
font-size: 13px;
|
||||
margin: -6px 0 14px;
|
||||
}
|
||||
|
||||
.hint {
|
||||
margin-top: 18px;
|
||||
font-size: 12.5px;
|
||||
line-height: 1.8;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.hint code {
|
||||
font-family: var(--mono);
|
||||
font-size: 12px;
|
||||
background: var(--paper-sunken);
|
||||
padding: 1px 4px;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.back {
|
||||
display: inline-block;
|
||||
margin-top: 14px;
|
||||
font-size: 13px;
|
||||
color: var(--accent);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,287 @@
|
||||
<script setup>
|
||||
import { computed, onMounted, ref, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { adminApi } from '../api'
|
||||
import { formatDateShort, relativeDate } from '../utils'
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
const items = ref([])
|
||||
const total = ref(0)
|
||||
const loading = ref(true)
|
||||
const error = ref('')
|
||||
|
||||
const status = ref('')
|
||||
const kind = ref('')
|
||||
const q = ref('')
|
||||
const page = ref(1)
|
||||
const size = 20
|
||||
|
||||
async function load() {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const params = { page: page.value, size }
|
||||
if (status.value) params.status = status.value
|
||||
if (kind.value) params.kind = kind.value
|
||||
if (q.value.trim()) params.q = q.value.trim()
|
||||
const data = await adminApi.posts(params)
|
||||
items.value = data.items || []
|
||||
total.value = data.total || 0
|
||||
} catch (e) {
|
||||
error.value = e.message || '加载失败'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(load)
|
||||
watch([status, kind, page], load)
|
||||
|
||||
let searchTimer
|
||||
function onSearch() {
|
||||
clearTimeout(searchTimer)
|
||||
searchTimer = setTimeout(() => {
|
||||
page.value = 1
|
||||
load()
|
||||
}, 300)
|
||||
}
|
||||
|
||||
const pageCount = computed(() => Math.max(1, Math.ceil(total.value / size)))
|
||||
|
||||
async function remove(id, title) {
|
||||
if (!window.confirm(`删除《${title}》?此操作不可撤销。`)) return
|
||||
try {
|
||||
await adminApi.deletePost(id)
|
||||
await load()
|
||||
} catch (e) {
|
||||
alert(e.message || '删除失败')
|
||||
}
|
||||
}
|
||||
|
||||
function edit(id) {
|
||||
router.push(`/admin/${id}`)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<header class="head">
|
||||
<h1 class="title">文章 <span class="count">{{ total }}</span></h1>
|
||||
<RouterLink to="/admin/new" class="btn btn-primary">写新的</RouterLink>
|
||||
</header>
|
||||
|
||||
<div class="filters">
|
||||
<input v-model="q" class="input search" placeholder="搜索标题 / 摘要 / 正文" @input="onSearch" />
|
||||
<select v-model="status" class="select">
|
||||
<option value="">全部状态</option>
|
||||
<option value="published">已发布</option>
|
||||
<option value="draft">草稿</option>
|
||||
</select>
|
||||
<select v-model="kind" class="select">
|
||||
<option value="">全部类型</option>
|
||||
<option value="long">长文</option>
|
||||
<option value="short">短文</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div v-if="loading" class="loading">载入中…</div>
|
||||
<div v-else-if="error" class="empty">{{ error }}</div>
|
||||
<div v-else-if="!items.length" class="empty">
|
||||
没有匹配的文章。<RouterLink to="/admin/new">写一篇 →</RouterLink>
|
||||
</div>
|
||||
|
||||
<template v-else>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="c-title">标题</th>
|
||||
<th class="c-kind">类型</th>
|
||||
<th class="c-status">状态</th>
|
||||
<th class="c-date">发布时间</th>
|
||||
<th class="c-act"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="p in items" :key="p.id">
|
||||
<td class="c-title">
|
||||
<a href="#" class="name" @click.prevent="edit(p.id)">
|
||||
{{ p.title || '无题' }}
|
||||
</a>
|
||||
<span v-if="p.tags && p.tags.length" class="tags">
|
||||
<span v-for="t in p.tags" :key="t" class="tag-chip">{{ t }}</span>
|
||||
</span>
|
||||
</td>
|
||||
<td class="c-kind">
|
||||
<span class="badge" :class="{ short: p.kind === 'short' }">
|
||||
{{ p.kind === 'short' ? '短文' : '长文' }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="c-status">
|
||||
<span class="dot" :class="p.status"></span>{{ p.status === 'published' ? '已发布' : '草稿' }}
|
||||
</td>
|
||||
<td class="c-date">
|
||||
<span :title="formatDateShort(p.published_at)">{{ relativeDate(p.published_at) }}</span>
|
||||
</td>
|
||||
<td class="c-act">
|
||||
<a href="#" @click.prevent="edit(p.id)">编辑</a>
|
||||
<a v-if="p.status === 'published'" :href="`/post/${p.slug}`" target="_blank">查看</a>
|
||||
<a href="#" class="danger" @click.prevent="remove(p.id, p.title)">删除</a>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<nav v-if="pageCount > 1" class="pager">
|
||||
<button class="btn" :disabled="page <= 1" @click="page = page - 1">← 上一页</button>
|
||||
<span class="page-info">第 {{ page }} / {{ pageCount }} 页</span>
|
||||
<button class="btn" :disabled="page >= pageCount" @click="page = page + 1">下一页 →</button>
|
||||
</nav>
|
||||
</template>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.count {
|
||||
font-family: var(--sans);
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.filters {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
margin-bottom: 16px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.search {
|
||||
flex: 1;
|
||||
min-width: 200px;
|
||||
}
|
||||
|
||||
.select {
|
||||
width: auto;
|
||||
min-width: 120px;
|
||||
}
|
||||
|
||||
.table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 4px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.table th {
|
||||
text-align: left;
|
||||
font-weight: 500;
|
||||
font-size: 12px;
|
||||
letter-spacing: 0.06em;
|
||||
color: var(--muted);
|
||||
padding: 10px 12px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.table td {
|
||||
padding: 12px;
|
||||
border-bottom: 1px solid var(--line-soft);
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
.table tr:last-child td {
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
.table tr:hover td {
|
||||
background: var(--paper-sunken);
|
||||
}
|
||||
|
||||
.name {
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.tags {
|
||||
display: flex;
|
||||
gap: 5px;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 6px;
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 1px 8px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 2px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.badge.short {
|
||||
color: var(--accent);
|
||||
border-color: var(--accent-line);
|
||||
}
|
||||
|
||||
.dot {
|
||||
display: inline-block;
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 50%;
|
||||
background: var(--faint);
|
||||
margin-right: 6px;
|
||||
vertical-align: 2px;
|
||||
}
|
||||
|
||||
.dot.published {
|
||||
background: var(--accent);
|
||||
}
|
||||
|
||||
.c-act {
|
||||
white-space: nowrap;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.c-act a {
|
||||
margin-left: 12px;
|
||||
font-size: 13px;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.c-act a.danger {
|
||||
color: #9a5b45;
|
||||
}
|
||||
|
||||
.pager {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
margin-top: 18px;
|
||||
}
|
||||
|
||||
.page-info {
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.c-kind,
|
||||
.c-date {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,124 @@
|
||||
<script setup>
|
||||
import { onMounted, reactive, ref } from 'vue'
|
||||
import { adminApi } from '../api'
|
||||
import { site } from '../site'
|
||||
|
||||
const form = reactive({
|
||||
site_title: '',
|
||||
site_desc: '',
|
||||
author_name: '',
|
||||
author_bio: '',
|
||||
footer_note: '',
|
||||
icp: '',
|
||||
posts_per_page: 10
|
||||
})
|
||||
|
||||
const loading = ref(true)
|
||||
const saving = ref(false)
|
||||
const error = ref('')
|
||||
const ok = ref('')
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
Object.assign(form, await adminApi.settings())
|
||||
} catch (e) {
|
||||
error.value = e.message || '加载失败'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
})
|
||||
|
||||
async function save() {
|
||||
saving.value = true
|
||||
error.value = ''
|
||||
ok.value = ''
|
||||
try {
|
||||
const saved = await adminApi.saveSettings({ ...form })
|
||||
Object.assign(form, saved)
|
||||
Object.assign(site, saved)
|
||||
ok.value = '已保存'
|
||||
} catch (e) {
|
||||
error.value = e.message || '保存失败'
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<header class="head">
|
||||
<h1 class="title">站点设置</h1>
|
||||
</header>
|
||||
|
||||
<div v-if="loading" class="loading">载入中…</div>
|
||||
|
||||
<form v-else class="form" @submit.prevent="save">
|
||||
<div class="field">
|
||||
<label for="t">站点标题</label>
|
||||
<input id="t" v-model="form.site_title" class="input" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="d">站点简介</label>
|
||||
<input id="d" v-model="form.site_desc" class="input" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="a">作者名</label>
|
||||
<input id="a" v-model="form.author_name" class="input" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="b">作者简介</label>
|
||||
<textarea id="b" v-model="form.author_bio" class="textarea" rows="3"></textarea>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="f">页脚信息</label>
|
||||
<input id="f" v-model="form.footer_note" class="input" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="i">备案号(可留空)</label>
|
||||
<input id="i" v-model="form.icp" class="input" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="p">每页文章数</label>
|
||||
<input id="p" v-model.number="form.posts_per_page" type="number" min="1" max="100" class="input" />
|
||||
</div>
|
||||
|
||||
<p v-if="error" class="error">{{ error }}</p>
|
||||
<p v-if="ok" class="ok">{{ ok }}</p>
|
||||
|
||||
<button class="btn btn-primary" type="submit" :disabled="saving">
|
||||
{{ saving ? '保存中…' : '保存' }}
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.head {
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.form {
|
||||
max-width: 520px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #9a5b45;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.ok {
|
||||
color: var(--accent);
|
||||
font-size: 13px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,180 @@
|
||||
<script setup>
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { adminApi } from '../api'
|
||||
|
||||
const tags = ref([])
|
||||
const loading = ref(true)
|
||||
const error = ref('')
|
||||
const name = ref('')
|
||||
const editing = ref(null)
|
||||
const editName = ref('')
|
||||
|
||||
async function load() {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const data = await adminApi.tags()
|
||||
tags.value = data.tags || []
|
||||
} catch (e) {
|
||||
error.value = e.message || '加载失败'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(load)
|
||||
|
||||
async function create() {
|
||||
if (!name.value.trim()) return
|
||||
try {
|
||||
await adminApi.createTag(name.value.trim())
|
||||
name.value = ''
|
||||
await load()
|
||||
} catch (e) {
|
||||
error.value = e.message || '创建失败'
|
||||
}
|
||||
}
|
||||
|
||||
function startEdit(t) {
|
||||
editing.value = t.id
|
||||
editName.value = t.name
|
||||
}
|
||||
|
||||
async function saveEdit(id) {
|
||||
try {
|
||||
await adminApi.renameTag(id, editName.value)
|
||||
editing.value = null
|
||||
await load()
|
||||
} catch (e) {
|
||||
error.value = e.message || '重命名失败'
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(t) {
|
||||
if (!window.confirm(`删除标签「${t.name}」?文章上的关联会一起移除。`)) return
|
||||
try {
|
||||
await adminApi.deleteTag(t.id)
|
||||
await load()
|
||||
} catch (e) {
|
||||
error.value = e.message || '删除失败'
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<header class="head">
|
||||
<h1 class="title">标签</h1>
|
||||
</header>
|
||||
|
||||
<form class="new" @submit.prevent="create">
|
||||
<input v-model="name" class="input" placeholder="新标签名" />
|
||||
<button class="btn btn-primary" type="submit">添加</button>
|
||||
</form>
|
||||
|
||||
<p v-if="error" class="error">{{ error }}</p>
|
||||
|
||||
<div v-if="loading" class="loading">载入中…</div>
|
||||
<div v-else-if="!tags.length" class="empty">还没有标签。</div>
|
||||
|
||||
<ul v-else class="list">
|
||||
<li v-for="t in tags" :key="t.id" class="row">
|
||||
<template v-if="editing === t.id">
|
||||
<input v-model="editName" class="input" @keyup.enter="saveEdit(t.id)" />
|
||||
<button class="btn" @click="saveEdit(t.id)">保存</button>
|
||||
<button class="btn" @click="editing = null">取消</button>
|
||||
</template>
|
||||
<template v-else>
|
||||
<span class="name">{{ t.name }}</span>
|
||||
<code class="slug">/tag/{{ t.slug }}</code>
|
||||
<span class="count">{{ t.count }} 篇</span>
|
||||
<span class="act">
|
||||
<a href="#" @click.prevent="startEdit(t)">重命名</a>
|
||||
<a href="#" class="danger" @click.prevent="remove(t)">删除</a>
|
||||
</span>
|
||||
</template>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.head {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.new {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin-bottom: 16px;
|
||||
max-width: 420px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #9a5b45;
|
||||
font-size: 13px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 4px;
|
||||
background: var(--card);
|
||||
}
|
||||
|
||||
.row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 10px 14px;
|
||||
border-bottom: 1px solid var(--line-soft);
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.row:last-child {
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
.name {
|
||||
min-width: 90px;
|
||||
}
|
||||
|
||||
.slug {
|
||||
font-family: var(--mono);
|
||||
font-size: 12px;
|
||||
color: var(--faint);
|
||||
}
|
||||
|
||||
.count {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.act {
|
||||
margin-left: auto;
|
||||
display: flex;
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.act a {
|
||||
font-size: 13px;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.act a.danger {
|
||||
color: #9a5b45;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.slug {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,12 @@
|
||||
import { adminApi, session } from '../api'
|
||||
|
||||
// 后台的登录态在服务端(httpOnly cookie);这里只是探测一次是否还有效
|
||||
export async function checkAuth() {
|
||||
try {
|
||||
await adminApi.me()
|
||||
return true
|
||||
} catch (e) {
|
||||
session.clear()
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user