refactor(admin): 重构评论管理界面样式和功能
feat(login): 添加API地址配置功能并支持本地存储 fix(api): 移除CWD_CONFIG_KV相关配置改用数据库存储 fix(cors): 修改跨域配置允许所有来源 perf(email): 添加邮件发送日志记录和调试信息 perf(comments): 优化评论提交和邮件通知逻辑 docs(backend): 更新后端配置文档移除CWD_CONFIG_KV相关说明 chore: 更新端口号和示例配置
This commit is contained in:
@@ -1,8 +1,19 @@
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL.replace(/\/+$/, '');
|
||||
const rawEnvApiBaseUrl = (import.meta.env.VITE_API_BASE_URL || '').trim();
|
||||
|
||||
function getApiBaseUrl(): string {
|
||||
const stored = (localStorage.getItem('cwd_admin_api_base_url') || '').trim();
|
||||
const source = stored || rawEnvApiBaseUrl;
|
||||
const apiBaseUrl = source.replace(/\/+$/, '');
|
||||
if (!apiBaseUrl) {
|
||||
throw new Error('未配置 API 地址,请在登录页填写后重试');
|
||||
}
|
||||
return apiBaseUrl;
|
||||
}
|
||||
|
||||
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
|
||||
async function request<T>(method: HttpMethod, path: string, body?: unknown): Promise<T> {
|
||||
const apiBaseUrl = getApiBaseUrl();
|
||||
const token = localStorage.getItem('cwd_admin_token');
|
||||
const headers: HeadersInit = {};
|
||||
if (body !== undefined) {
|
||||
@@ -11,7 +22,7 @@ async function request<T>(method: HttpMethod, path: string, body?: unknown): Pro
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
const res = await fetch(`${API_BASE_URL}${path}`, {
|
||||
const res = await fetch(`${apiBaseUrl}${path}`, {
|
||||
method,
|
||||
headers,
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined
|
||||
@@ -44,4 +55,3 @@ export function put<T>(path: string, body?: unknown): Promise<T> {
|
||||
export function del<T>(path: string): Promise<T> {
|
||||
return request<T>('DELETE', path);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,254 +1,421 @@
|
||||
<template>
|
||||
<div class="page">
|
||||
<h2 class="page-title">评论管理</h2>
|
||||
<div class="toolbar">
|
||||
<div class="toolbar-left">
|
||||
<select v-model="statusFilter" class="toolbar-select">
|
||||
<option value="">全部状态</option>
|
||||
<option value="approved">已通过</option>
|
||||
<option value="pending">待审核</option>
|
||||
<option value="rejected">已拒绝</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="toolbar-right">
|
||||
<button class="toolbar-button" @click="loadComments">刷新</button>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="loading" class="page-hint">加载中...</div>
|
||||
<div v-else-if="error" class="page-error">{{ error }}</div>
|
||||
<div v-else>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>文章</th>
|
||||
<th>作者</th>
|
||||
<th>邮箱</th>
|
||||
<th>内容</th>
|
||||
<th>状态</th>
|
||||
<th>时间</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="item in filteredComments" :key="item.id">
|
||||
<td>{{ item.id }}</td>
|
||||
<td>{{ item.postSlug }}</td>
|
||||
<td>{{ item.author }}</td>
|
||||
<td>{{ item.email }}</td>
|
||||
<td class="table-content">{{ item.contentText }}</td>
|
||||
<td>{{ item.status }}</td>
|
||||
<td>{{ formatDate(item.pubDate) }}</td>
|
||||
<td>
|
||||
<button class="table-button" @click="changeStatus(item, 'approved')" :disabled="item.status === 'approved'">
|
||||
通过
|
||||
</button>
|
||||
<button class="table-button" @click="changeStatus(item, 'pending')" :disabled="item.status === 'pending'">
|
||||
待审
|
||||
</button>
|
||||
<button class="table-button" @click="changeStatus(item, 'rejected')" :disabled="item.status === 'rejected'">
|
||||
拒绝
|
||||
</button>
|
||||
<button class="table-button table-button-danger" @click="removeComment(item)">删除</button>
|
||||
</td>
|
||||
</tr>
|
||||
<tr v-if="filteredComments.length === 0">
|
||||
<td colspan="8" class="page-hint">暂无数据</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div v-if="pagination.total > 1" class="pagination">
|
||||
<button class="pagination-button" :disabled="pagination.page <= 1" @click="goPage(pagination.page - 1)">上一页</button>
|
||||
<span class="pagination-info">{{ pagination.page }} / {{ pagination.total }}</span>
|
||||
<button class="pagination-button" :disabled="pagination.page >= pagination.total" @click="goPage(pagination.page + 1)">
|
||||
下一页
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="page">
|
||||
<h2 class="page-title">评论管理</h2>
|
||||
<div class="toolbar">
|
||||
<div class="toolbar-left">
|
||||
<select v-model="statusFilter" class="toolbar-select">
|
||||
<option value="">全部状态</option>
|
||||
<option value="approved">已通过</option>
|
||||
<option value="pending">待审核</option>
|
||||
<option value="rejected">已拒绝</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="toolbar-right">
|
||||
<button class="toolbar-button" @click="loadComments">刷新</button>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="loading" class="page-hint">加载中...</div>
|
||||
<div v-else-if="error" class="page-error">{{ error }}</div>
|
||||
<div v-else>
|
||||
<div class="comment-list">
|
||||
<div
|
||||
v-for="item in filteredComments"
|
||||
:key="item.id"
|
||||
class="comment-card"
|
||||
>
|
||||
<div class="comment-card-header">
|
||||
<div class="comment-author">
|
||||
<div class="comment-author-name">
|
||||
{{ item.author }}
|
||||
</div>
|
||||
<div class="comment-author-email">
|
||||
{{ item.email }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="comment-meta">
|
||||
<span class="comment-path">{{ item.postSlug }}</span>
|
||||
<span class="comment-time">{{ formatDate(item.pubDate) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="comment-content">
|
||||
{{ item.contentText }}
|
||||
</div>
|
||||
<div class="comment-footer">
|
||||
<div class="comment-info">
|
||||
<span class="comment-id">#{{ item.id }}</span>
|
||||
<span
|
||||
class="comment-status"
|
||||
:class="`comment-status-${item.status}`"
|
||||
>
|
||||
{{ formatStatus(item.status) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="comment-actions">
|
||||
<button
|
||||
class="comment-action"
|
||||
@click="changeStatus(item, 'approved')"
|
||||
:disabled="item.status === 'approved'"
|
||||
>
|
||||
通过
|
||||
</button>
|
||||
<button
|
||||
class="comment-action"
|
||||
@click="changeStatus(item, 'pending')"
|
||||
:disabled="item.status === 'pending'"
|
||||
>
|
||||
待审
|
||||
</button>
|
||||
<button
|
||||
class="comment-action"
|
||||
@click="changeStatus(item, 'rejected')"
|
||||
:disabled="item.status === 'rejected'"
|
||||
>
|
||||
拒绝
|
||||
</button>
|
||||
<button
|
||||
class="comment-action comment-action-danger"
|
||||
@click="removeComment(item)"
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
v-if="filteredComments.length === 0"
|
||||
class="page-hint comment-empty"
|
||||
>
|
||||
暂无数据
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="pagination.total > 1" class="pagination">
|
||||
<button
|
||||
class="pagination-button"
|
||||
:disabled="pagination.page <= 1"
|
||||
@click="goPage(pagination.page - 1)"
|
||||
>
|
||||
上一页
|
||||
</button>
|
||||
<span class="pagination-info"
|
||||
>{{ pagination.page }} / {{ pagination.total }}</span
|
||||
>
|
||||
<button
|
||||
class="pagination-button"
|
||||
:disabled="pagination.page >= pagination.total"
|
||||
@click="goPage(pagination.page + 1)"
|
||||
>
|
||||
下一页
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref, computed } from 'vue';
|
||||
import { CommentItem, CommentListResponse, fetchComments, deleteComment, updateCommentStatus } from '../api/admin';
|
||||
import { onMounted, ref, computed } from "vue";
|
||||
import {
|
||||
CommentItem,
|
||||
CommentListResponse,
|
||||
fetchComments,
|
||||
deleteComment,
|
||||
updateCommentStatus,
|
||||
} from "../api/admin";
|
||||
|
||||
const comments = ref<CommentItem[]>([]);
|
||||
const pagination = ref<{ page: number; total: number }>({ page: 1, total: 1 });
|
||||
const loading = ref(false);
|
||||
const error = ref('');
|
||||
const statusFilter = ref('');
|
||||
const error = ref("");
|
||||
const statusFilter = ref("");
|
||||
|
||||
const filteredComments = computed(() => {
|
||||
if (!statusFilter.value) {
|
||||
return comments.value;
|
||||
}
|
||||
return comments.value.filter((item) => item.status === statusFilter.value);
|
||||
if (!statusFilter.value) {
|
||||
return comments.value;
|
||||
}
|
||||
return comments.value.filter((item) => item.status === statusFilter.value);
|
||||
});
|
||||
|
||||
function formatDate(value: string) {
|
||||
const d = new Date(value);
|
||||
if (Number.isNaN(d.getTime())) {
|
||||
return value;
|
||||
}
|
||||
return d.toLocaleString();
|
||||
const d = new Date(value);
|
||||
if (Number.isNaN(d.getTime())) {
|
||||
return value;
|
||||
}
|
||||
return d.toLocaleString();
|
||||
}
|
||||
|
||||
async function loadComments(page = 1) {
|
||||
loading.value = true;
|
||||
error.value = '';
|
||||
try {
|
||||
const res = await fetchComments(page);
|
||||
comments.value = res.data;
|
||||
pagination.value = { page: res.pagination.page, total: res.pagination.total };
|
||||
} catch (e: any) {
|
||||
error.value = e.message || '加载失败';
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
function formatStatus(status: string) {
|
||||
if (status === "approved") {
|
||||
return "已通过";
|
||||
}
|
||||
if (status === "pending") {
|
||||
return "待审核";
|
||||
}
|
||||
if (status === "rejected") {
|
||||
return "已拒绝";
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
async function loadComments(page?: number) {
|
||||
const targetPage = typeof page === "number" ? page : 1;
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
try {
|
||||
const res = await fetchComments(targetPage);
|
||||
comments.value = res.data;
|
||||
pagination.value = { page: res.pagination.page, total: res.pagination.total };
|
||||
} catch (e: any) {
|
||||
error.value = e.message || "加载失败";
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function goPage(page: number) {
|
||||
if (page < 1 || page > pagination.value.total) {
|
||||
return;
|
||||
}
|
||||
await loadComments(page);
|
||||
if (page < 1 || page > pagination.value.total) {
|
||||
return;
|
||||
}
|
||||
await loadComments(page);
|
||||
}
|
||||
|
||||
async function changeStatus(item: CommentItem, status: string) {
|
||||
try {
|
||||
await updateCommentStatus(item.id, status);
|
||||
item.status = status;
|
||||
} catch (e: any) {
|
||||
error.value = e.message || '更新状态失败';
|
||||
}
|
||||
try {
|
||||
await updateCommentStatus(item.id, status);
|
||||
item.status = status;
|
||||
} catch (e: any) {
|
||||
error.value = e.message || "更新状态失败";
|
||||
}
|
||||
}
|
||||
|
||||
async function removeComment(item: CommentItem) {
|
||||
if (!window.confirm(`确认删除评论 ${item.id} 吗`)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await deleteComment(item.id);
|
||||
comments.value = comments.value.filter((c) => c.id !== item.id);
|
||||
} catch (e: any) {
|
||||
error.value = e.message || '删除失败';
|
||||
}
|
||||
if (!window.confirm(`确认删除评论 ${item.id} 吗`)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await deleteComment(item.id);
|
||||
comments.value = comments.value.filter((c) => c.id !== item.id);
|
||||
} catch (e: any) {
|
||||
error.value = e.message || "删除失败";
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadComments();
|
||||
loadComments();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
color: #24292f;
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
color: #24292f;
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 8px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.toolbar-left {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.toolbar-right {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.toolbar-select {
|
||||
padding: 4px 8px;
|
||||
font-size: 13px;
|
||||
padding: 4px 8px;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.toolbar-button {
|
||||
padding: 6px 10px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #d0d7de;
|
||||
background-color: #f6f8fa;
|
||||
cursor: pointer;
|
||||
font-size: 13px;
|
||||
padding: 6px 10px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #d0d7de;
|
||||
background-color: #f6f8fa;
|
||||
cursor: pointer;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.page-hint {
|
||||
font-size: 14px;
|
||||
color: #57606a;
|
||||
font-size: 14px;
|
||||
color: #57606a;
|
||||
}
|
||||
|
||||
.page-error {
|
||||
font-size: 14px;
|
||||
color: #d1242f;
|
||||
font-size: 14px;
|
||||
color: #d1242f;
|
||||
}
|
||||
|
||||
.table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
background-color: #ffffff;
|
||||
.comment-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.table th,
|
||||
.table td {
|
||||
border: 1px solid #d0d7de;
|
||||
padding: 6px 8px;
|
||||
font-size: 13px;
|
||||
text-align: left;
|
||||
vertical-align: top;
|
||||
.comment-card {
|
||||
background-color: #ffffff;
|
||||
border-radius: 6px;
|
||||
border: 1px solid #d0d7de;
|
||||
padding: 10px 12px;
|
||||
box-shadow: 0 1px 0 rgba(27, 31, 36, 0.04);
|
||||
}
|
||||
|
||||
.table-content {
|
||||
max-width: 260px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
.comment-card-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
gap: 12px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.table-button {
|
||||
margin-right: 4px;
|
||||
padding: 4px 6px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #d0d7de;
|
||||
background-color: #f6f8fa;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
.comment-author {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.table-button-danger {
|
||||
border-color: #d1242f;
|
||||
color: #d1242f;
|
||||
.comment-author-name {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #24292f;
|
||||
}
|
||||
|
||||
.comment-author-email {
|
||||
font-size: 12px;
|
||||
color: #57606a;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.comment-meta {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
gap: 4px;
|
||||
font-size: 12px;
|
||||
color: #57606a;
|
||||
}
|
||||
|
||||
.comment-path {
|
||||
max-width: 220px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.comment-time {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.comment-content {
|
||||
font-size: 14px;
|
||||
line-height: 1.6;
|
||||
color: #24292f;
|
||||
margin-bottom: 8px;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.comment-footer {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.comment-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 12px;
|
||||
color: #57606a;
|
||||
}
|
||||
|
||||
.comment-id {
|
||||
padding: 2px 6px;
|
||||
border-radius: 999px;
|
||||
background-color: #f6f8fa;
|
||||
}
|
||||
|
||||
.comment-status {
|
||||
padding: 2px 8px;
|
||||
border-radius: 999px;
|
||||
background-color: #f6f8fa;
|
||||
}
|
||||
|
||||
.comment-status-approved {
|
||||
color: #1a7f37;
|
||||
background-color: #e7f5eb;
|
||||
}
|
||||
|
||||
.comment-status-pending {
|
||||
color: #9a6700;
|
||||
background-color: #fff8c5;
|
||||
}
|
||||
|
||||
.comment-status-rejected {
|
||||
color: #d1242f;
|
||||
background-color: #ffebe9;
|
||||
}
|
||||
|
||||
.comment-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.comment-action {
|
||||
padding: 4px 8px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #d0d7de;
|
||||
background-color: #f6f8fa;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.comment-action-danger {
|
||||
border-color: #d1242f;
|
||||
color: #d1242f;
|
||||
}
|
||||
|
||||
.comment-action:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.comment-empty {
|
||||
margin-top: 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.pagination {
|
||||
margin-top: 8px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-top: 8px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.pagination-button {
|
||||
padding: 4px 8px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #d0d7de;
|
||||
background-color: #f6f8fa;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
padding: 4px 8px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #d0d7de;
|
||||
background-color: #f6f8fa;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.pagination-info {
|
||||
font-size: 13px;
|
||||
font-size: 13px;
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
@@ -1,127 +1,156 @@
|
||||
<template>
|
||||
<div class="login-page">
|
||||
<div class="login-card">
|
||||
<h1 class="login-title">CWD 评论后台</h1>
|
||||
<form class="login-form" @submit.prevent="handleSubmit">
|
||||
<div class="form-item">
|
||||
<label class="form-label">管理员账号</label>
|
||||
<input v-model="name" class="form-input" type="text" autocomplete="username" />
|
||||
</div>
|
||||
<div class="form-item">
|
||||
<label class="form-label">密码</label>
|
||||
<input v-model="password" class="form-input" type="password" autocomplete="current-password" />
|
||||
</div>
|
||||
<div v-if="error" class="form-error">{{ error }}</div>
|
||||
<button class="form-button" type="submit" :disabled="submitting">
|
||||
<span v-if="submitting">登录中...</span>
|
||||
<span v-else>登录</span>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="login-page">
|
||||
<div class="login-card">
|
||||
<h1 class="login-title">CWD 评论后台</h1>
|
||||
<form class="login-form" @submit.prevent="handleSubmit">
|
||||
<div class="form-item">
|
||||
<label class="form-label">API 地址</label>
|
||||
<input v-model="apiBaseUrl" class="form-input" type="text" />
|
||||
</div>
|
||||
<div class="form-item">
|
||||
<label class="form-label">管理员账号</label>
|
||||
<input v-model="name" class="form-input" type="text" autocomplete="username" />
|
||||
</div>
|
||||
<div class="form-item">
|
||||
<label class="form-label">密码</label>
|
||||
<input
|
||||
v-model="password"
|
||||
class="form-input"
|
||||
type="password"
|
||||
autocomplete="current-password"
|
||||
/>
|
||||
</div>
|
||||
<div v-if="error" class="form-error">{{ error }}</div>
|
||||
<button class="form-button" type="submit" :disabled="submitting">
|
||||
<span v-if="submitting">登录中...</span>
|
||||
<span v-else>登录</span>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
import { loginAdmin } from '../api/admin';
|
||||
import { onMounted, ref } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { loginAdmin } from "../api/admin";
|
||||
|
||||
const router = useRouter();
|
||||
const name = ref('');
|
||||
const password = ref('');
|
||||
const defaultAdminName = (import.meta.env.VITE_ADMIN_NAME || "").trim();
|
||||
const defaultAdminPassword = (import.meta.env.VITE_ADMIN_PASSWORD || "").trim();
|
||||
const name = ref(defaultAdminName);
|
||||
const password = ref(defaultAdminPassword);
|
||||
const submitting = ref(false);
|
||||
const error = ref('');
|
||||
const error = ref("");
|
||||
const apiBaseUrl = ref("");
|
||||
|
||||
const rawEnvApiBaseUrl = (import.meta.env.VITE_API_BASE_URL || "").trim();
|
||||
const defaultApiBaseUrl = rawEnvApiBaseUrl.replace(/\/+$/, "");
|
||||
|
||||
onMounted(() => {
|
||||
const stored = (localStorage.getItem("cwd_admin_api_base_url") || "").trim();
|
||||
if (stored) {
|
||||
apiBaseUrl.value = stored;
|
||||
return;
|
||||
}
|
||||
apiBaseUrl.value = defaultApiBaseUrl;
|
||||
});
|
||||
|
||||
async function handleSubmit() {
|
||||
if (!name.value || !password.value) {
|
||||
error.value = '请输入账号和密码';
|
||||
return;
|
||||
}
|
||||
error.value = '';
|
||||
submitting.value = true;
|
||||
try {
|
||||
await loginAdmin(name.value, password.value);
|
||||
router.push({ name: 'comments' });
|
||||
} catch (e: any) {
|
||||
error.value = e.message || '登录失败';
|
||||
} finally {
|
||||
submitting.value = false;
|
||||
}
|
||||
const normalizedApiBaseUrl = apiBaseUrl.value.trim().replace(/\/+$/, "");
|
||||
if (!normalizedApiBaseUrl) {
|
||||
error.value = "请输入 API 地址";
|
||||
return;
|
||||
}
|
||||
if (!name.value || !password.value) {
|
||||
error.value = "请输入账号和密码";
|
||||
return;
|
||||
}
|
||||
error.value = "";
|
||||
submitting.value = true;
|
||||
try {
|
||||
localStorage.setItem("cwd_admin_api_base_url", normalizedApiBaseUrl);
|
||||
await loginAdmin(name.value, password.value);
|
||||
router.push({ name: "comments" });
|
||||
} catch (e: any) {
|
||||
error.value = e.message || "登录失败";
|
||||
} finally {
|
||||
submitting.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.login-page {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.login-card {
|
||||
background-color: #ffffff;
|
||||
padding: 32px 40px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
|
||||
width: 360px;
|
||||
background-color: #ffffff;
|
||||
padding: 32px 40px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
|
||||
width: 360px;
|
||||
}
|
||||
|
||||
.login-title {
|
||||
margin: 0 0 24px;
|
||||
font-size: 22px;
|
||||
text-align: center;
|
||||
color: #333333;
|
||||
margin: 0 0 24px;
|
||||
font-size: 22px;
|
||||
text-align: center;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
.login-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.form-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
font-size: 14px;
|
||||
color: #555555;
|
||||
font-size: 14px;
|
||||
color: #555555;
|
||||
}
|
||||
|
||||
.form-input {
|
||||
padding: 8px 10px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #d0d7de;
|
||||
font-size: 14px;
|
||||
outline: none;
|
||||
padding: 8px 10px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #d0d7de;
|
||||
font-size: 14px;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.form-input:focus {
|
||||
border-color: #0969da;
|
||||
box-shadow: 0 0 0 1px rgba(9, 105, 218, 0.2);
|
||||
border-color: #0969da;
|
||||
box-shadow: 0 0 0 1px rgba(9, 105, 218, 0.2);
|
||||
}
|
||||
|
||||
.form-error {
|
||||
font-size: 13px;
|
||||
color: #d1242f;
|
||||
font-size: 13px;
|
||||
color: #d1242f;
|
||||
}
|
||||
|
||||
.form-button {
|
||||
margin-top: 8px;
|
||||
padding: 10px 0;
|
||||
border-radius: 4px;
|
||||
border: none;
|
||||
background-color: #0969da;
|
||||
color: #ffffff;
|
||||
font-size: 15px;
|
||||
cursor: pointer;
|
||||
margin-top: 8px;
|
||||
padding: 10px 0;
|
||||
border-radius: 4px;
|
||||
border: none;
|
||||
background-color: #0969da;
|
||||
color: #ffffff;
|
||||
font-size: 15px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.form-button:disabled {
|
||||
opacity: 0.7;
|
||||
cursor: default;
|
||||
opacity: 0.7;
|
||||
cursor: default;
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user