2
This commit is contained in:
@@ -183,8 +183,8 @@ const handleChangePassword = () => {
|
||||
}
|
||||
|
||||
const refreshCurrentPage = () => {
|
||||
// 刷新当前路由页面,不刷新整个浏览器
|
||||
router.replace(router.currentRoute.value.fullPath)
|
||||
// 强制刷新当前路由页面
|
||||
router.go(0)
|
||||
}
|
||||
|
||||
interface MenuItem {
|
||||
|
||||
@@ -1,204 +1,405 @@
|
||||
<template>
|
||||
<div class="admin-about">
|
||||
<div class="page-header">
|
||||
<h1 class="page-title">关于页面管理</h1>
|
||||
<button @click="$router.push('/admin/about/create')" class="btn btn-primary">
|
||||
<span class="icon">+</span> 新增资料
|
||||
</button>
|
||||
</div>
|
||||
<div class="about-form-container">
|
||||
<h1 class="page-title">关于页面管理</h1>
|
||||
|
||||
<div class="table-container">
|
||||
<table class="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>姓名</th>
|
||||
<th>头衔/简介</th>
|
||||
<th>主要展示</th>
|
||||
<th>最后更新</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="profile in profiles" :key="profile.id">
|
||||
<td>
|
||||
<div class="flex items-center gap-3">
|
||||
<img :src="profile.avatar" alt="avatar" class="w-8 h-8 rounded-full object-cover" v-if="profile.avatar">
|
||||
<span>{{ profile.name }}</span>
|
||||
<div class="form-container">
|
||||
<form @submit.prevent="handleSubmit" class="about-form">
|
||||
<!-- Basic Info Section -->
|
||||
<h3 class="section-title">基本信息</h3>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div class="form-group">
|
||||
<label for="name">姓名</label>
|
||||
<input
|
||||
type="text"
|
||||
id="name"
|
||||
v-model="form.name"
|
||||
placeholder="请输入姓名"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="avatar">头像</label>
|
||||
<ImageUpload
|
||||
v-model="form.avatar"
|
||||
:category-id="1"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div class="form-group">
|
||||
<label for="location">所在地</label>
|
||||
<input
|
||||
type="text"
|
||||
id="location"
|
||||
v-model="form.location"
|
||||
placeholder="例如:中国 · 杭州"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="email">邮箱</label>
|
||||
<input
|
||||
type="email"
|
||||
id="email"
|
||||
v-model="form.email"
|
||||
placeholder="example@domain.com"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div class="form-group">
|
||||
<label for="wechat">微信</label>
|
||||
<input
|
||||
type="text"
|
||||
id="wechat"
|
||||
v-model="form.wechat"
|
||||
placeholder="微信号"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group flex items-end pb-3">
|
||||
<label class="checkbox-container">
|
||||
<input type="checkbox" v-model="form.isPrimary">
|
||||
<span class="checkmark"></span>
|
||||
<span class="label-text">设为主页展示资料</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="bio">个人简介 (支持 HTML)</label>
|
||||
<textarea
|
||||
id="bio"
|
||||
v-model="form.bio"
|
||||
rows="4"
|
||||
placeholder="请输入个人简介..."
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<!-- Tech Stack Section -->
|
||||
<h3 class="section-title mt-8">技术栈</h3>
|
||||
<div class="form-group">
|
||||
<label for="techStack">技术标签 (用英文逗号分隔)</label>
|
||||
<input
|
||||
type="text"
|
||||
id="techStack"
|
||||
v-model="techStackInput"
|
||||
placeholder="Vue 3, React, TypeScript, Golang..."
|
||||
/>
|
||||
<div class="tech-tags mt-2 flex flex-wrap gap-2" v-if="techStackPreview.length">
|
||||
<span v-for="(tag, idx) in techStackPreview" :key="idx" class="badge">{{ tag }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Experience Section -->
|
||||
<h3 class="section-title mt-8 flex justify-between items-center">
|
||||
<span>工作经历</span>
|
||||
<button type="button" @click="addExperience" class="btn btn-sm btn-secondary">+ 添加经历</button>
|
||||
</h3>
|
||||
|
||||
<div class="experiences-list space-y-4">
|
||||
<div v-for="(exp, index) in form.experiences" :key="index" class="experience-item p-4 border border-white/10 rounded-lg bg-white/5 relative">
|
||||
<button type="button" @click="removeExperience(index)" class="absolute top-2 right-2 text-red-500 hover:text-red-400">×</button>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||
<div class="form-group mb-0">
|
||||
<label class="text-xs text-white/60 mb-1 block">年份/时间段</label>
|
||||
<input type="text" v-model="exp.year" placeholder="2023 - 至今" class="text-sm">
|
||||
</div>
|
||||
</td>
|
||||
<td class="bio-cell">{{ truncate(profile.bio, 50) }}</td>
|
||||
<td>
|
||||
<span v-if="profile.isPrimary" class="badge badge-success">是</span>
|
||||
<span v-else class="badge badge-secondary">否</span>
|
||||
</td>
|
||||
<td>{{ formatDate(profile.updatedAt) }}</td>
|
||||
<td class="actions">
|
||||
<button @click="$router.push(`/admin/about/edit/${profile.id}`)" class="btn btn-sm btn-secondary">
|
||||
编辑
|
||||
</button>
|
||||
<button @click="handleDelete(profile.id)" class="btn btn-sm btn-danger">
|
||||
删除
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
<tr v-if="profiles.length === 0">
|
||||
<td colspan="5" class="text-center py-4 text-gray-400">暂无数据</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="form-group mb-0">
|
||||
<label class="text-xs text-white/60 mb-1 block">职位/角色</label>
|
||||
<input type="text" v-model="exp.role" placeholder="高级前端工程师" class="text-sm">
|
||||
</div>
|
||||
<div class="form-group mb-0">
|
||||
<label class="text-xs text-white/60 mb-1 block">公司/组织</label>
|
||||
<input type="text" v-model="exp.company" placeholder="某某科技有限公司" class="text-sm">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="form.experiences.length === 0" class="text-center text-white/40 py-4">
|
||||
暂无工作经历,请点击右上角添加
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Submit Buttons -->
|
||||
<div class="form-actions mt-8">
|
||||
<button type="submit" class="submit-btn" :disabled="isSubmitting">
|
||||
{{ isSubmitting ? '提交中...' : (currentProfileId ? '更新资料' : '创建资料') }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { getAdminAboutProfiles, deleteAboutProfile, AboutProfile } from '../../services/api'
|
||||
import { ref, reactive, computed, onMounted } from 'vue'
|
||||
import { useToast } from '../../composables/useToast'
|
||||
import { createAboutProfile, updateAboutProfile, getAdminAboutProfiles, AboutProfile, Experience } from '../../services/api'
|
||||
import ImageUpload from '../../components/admin/ImageUpload.vue'
|
||||
|
||||
const profiles = ref<AboutProfile[]>([])
|
||||
const toast = useToast()
|
||||
const isSubmitting = ref(false)
|
||||
const currentProfileId = ref<number | null>(null)
|
||||
const loading = ref(true)
|
||||
|
||||
const fetchProfiles = async () => {
|
||||
const form = reactive({
|
||||
name: '',
|
||||
avatar: '',
|
||||
location: '',
|
||||
bio: '',
|
||||
email: '',
|
||||
wechat: '',
|
||||
isPrimary: false,
|
||||
experiences: [] as Experience[]
|
||||
})
|
||||
|
||||
const techStackInput = ref('')
|
||||
|
||||
const techStackPreview = computed(() => {
|
||||
return techStackInput.value.split(',').map(t => t.trim()).filter(t => t)
|
||||
})
|
||||
|
||||
const addExperience = () => {
|
||||
form.experiences.push({ year: '', role: '', company: '' })
|
||||
}
|
||||
|
||||
const removeExperience = (index: number) => {
|
||||
form.experiences.splice(index, 1)
|
||||
}
|
||||
|
||||
const loadProfile = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
profiles.value = await getAdminAboutProfiles()
|
||||
} catch (error) {
|
||||
console.error('Error fetching profiles:', error)
|
||||
toast.error('获取资料列表失败')
|
||||
}
|
||||
}
|
||||
|
||||
const handleDelete = async (id: number) => {
|
||||
if (confirm('确定要删除这条资料吗?')) {
|
||||
try {
|
||||
await deleteAboutProfile(id)
|
||||
toast.success('删除成功')
|
||||
fetchProfiles()
|
||||
} catch (error) {
|
||||
console.error('Error deleting profile:', error)
|
||||
toast.error('删除失败')
|
||||
const profiles = await getAdminAboutProfiles()
|
||||
// 获取第一条数据(因为只有一条数据)
|
||||
const profile = profiles.length > 0 ? profiles[0] : null
|
||||
|
||||
if (profile) {
|
||||
currentProfileId.value = profile.id
|
||||
form.name = profile.name || ''
|
||||
form.avatar = profile.avatar || ''
|
||||
form.location = profile.location || ''
|
||||
form.bio = profile.bio || ''
|
||||
form.email = profile.email || ''
|
||||
form.wechat = profile.wechat || ''
|
||||
form.isPrimary = profile.isPrimary || false
|
||||
form.experiences = profile.experiences || []
|
||||
techStackInput.value = (profile.techStack || []).join(', ')
|
||||
} else {
|
||||
// 没有数据,显示空表单
|
||||
currentProfileId.value = null
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading profile:', error)
|
||||
toast.error('加载资料失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const truncate = (text: string, length: number) => {
|
||||
if (!text) return ''
|
||||
// Strip HTML tags
|
||||
const plainText = text.replace(/<[^>]+>/g, '')
|
||||
return plainText.length > length ? plainText.substring(0, length) + '...' : plainText
|
||||
}
|
||||
const handleSubmit = async () => {
|
||||
isSubmitting.value = true
|
||||
try {
|
||||
const payload = {
|
||||
...form,
|
||||
techStack: techStackPreview.value
|
||||
}
|
||||
|
||||
const formatDate = (dateStr: string) => {
|
||||
if (!dateStr) return '-'
|
||||
return new Date(dateStr).toLocaleDateString()
|
||||
if (currentProfileId.value) {
|
||||
await updateAboutProfile(currentProfileId.value, payload)
|
||||
toast.success('资料更新成功')
|
||||
} else {
|
||||
const result = await createAboutProfile(payload)
|
||||
currentProfileId.value = result.id
|
||||
toast.success('资料创建成功')
|
||||
}
|
||||
// 重新加载数据
|
||||
await loadProfile()
|
||||
} catch (error: any) {
|
||||
console.error('Error submitting form:', error)
|
||||
toast.error(error.message || '操作失败')
|
||||
} finally {
|
||||
isSubmitting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
fetchProfiles()
|
||||
loadProfile()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.admin-about {
|
||||
max-width: 1200px;
|
||||
.about-form-container {
|
||||
width: 100%;
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.page-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
font-size: 1.5rem;
|
||||
font-size: 1.75rem;
|
||||
font-weight: 600;
|
||||
color: #d4b383;
|
||||
margin-bottom: 1.5rem;
|
||||
font-family: 'Inter', sans-serif;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.table-container {
|
||||
.form-container {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
backdrop-filter: blur(10px);
|
||||
-webkit-backdrop-filter: blur(10px);
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
overflow: hidden;
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.admin-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.admin-table th,
|
||||
.admin-table td {
|
||||
padding: 0.75rem 1rem;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
font-family: 'Inter', sans-serif;
|
||||
}
|
||||
|
||||
.admin-table th {
|
||||
background: rgba(255, 255, 255, 0.08);
|
||||
.section-title {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
color: #d4b383;
|
||||
white-space: nowrap;
|
||||
margin-bottom: 1.25rem;
|
||||
padding-bottom: 0.5rem;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.admin-table tr:hover {
|
||||
background: rgba(212, 179, 131, 0.05);
|
||||
.form-group {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.bio-cell {
|
||||
max-width: 300px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 0.375rem;
|
||||
.form-group label {
|
||||
display: block;
|
||||
font-size: 0.95rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
border: 1px solid transparent;
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
margin-bottom: 0.5rem;
|
||||
font-family: 'Inter', sans-serif;
|
||||
}
|
||||
|
||||
.btn-sm {
|
||||
padding: 0.25rem 0.5rem;
|
||||
font-size: 0.875rem;
|
||||
.form-group input,
|
||||
.form-group textarea {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 0.5rem;
|
||||
font-size: 0.95rem;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: white;
|
||||
font-family: 'Inter', sans-serif;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
.form-group input:focus,
|
||||
.form-group textarea:focus {
|
||||
outline: none;
|
||||
border-color: #d4b383;
|
||||
box-shadow: 0 0 0 3px rgba(212, 179, 131, 0.2);
|
||||
}
|
||||
|
||||
.badge {
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 9999px;
|
||||
font-size: 0.875rem;
|
||||
color: rgba(255, 255, 255, 0.9);
|
||||
}
|
||||
|
||||
.checkbox-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
position: relative;
|
||||
padding-left: 30px;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
}
|
||||
|
||||
.checkbox-container input {
|
||||
position: absolute;
|
||||
opacity: 0;
|
||||
cursor: pointer;
|
||||
height: 0;
|
||||
width: 0;
|
||||
}
|
||||
|
||||
.checkmark {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
height: 20px;
|
||||
width: 20px;
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.checkbox-container:hover input ~ .checkmark {
|
||||
background-color: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.checkbox-container input:checked ~ .checkmark {
|
||||
background-color: #d4b383;
|
||||
color: #050505;
|
||||
border-color: #d4b383;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
.checkmark:after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.checkbox-container input:checked ~ .checkmark:after {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.checkbox-container .checkmark:after {
|
||||
left: 7px;
|
||||
top: 3px;
|
||||
width: 5px;
|
||||
height: 10px;
|
||||
border: solid #050505;
|
||||
border-width: 0 2px 2px 0;
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
|
||||
.form-actions {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.submit-btn {
|
||||
padding: 0.75rem 1.5rem;
|
||||
background-color: #d4b383;
|
||||
color: #050505;
|
||||
border: 1px solid #d4b383;
|
||||
border-radius: 0.5rem;
|
||||
cursor: pointer;
|
||||
font-weight: 500;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.submit-btn:hover:not(:disabled) {
|
||||
background-color: transparent;
|
||||
color: #d4b383;
|
||||
box-shadow: 0 0 15px rgba(212, 179, 131, 0.3);
|
||||
}
|
||||
|
||||
.submit-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
border-color: rgba(255, 255, 255, 0.2);
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 0.375rem;
|
||||
font-size: 0.875rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
@@ -206,40 +407,4 @@ onMounted(() => {
|
||||
border-color: #d4b383;
|
||||
color: #d4b383;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background-color: rgba(239, 68, 68, 0.2);
|
||||
color: #ef4444;
|
||||
border-color: rgba(239, 68, 68, 0.3);
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
background-color: rgba(239, 68, 68, 0.3);
|
||||
border-color: #ef4444;
|
||||
box-shadow: 0 0 15px rgba(239, 68, 68, 0.3);
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 0.25em 0.6em;
|
||||
font-size: 75%;
|
||||
font-weight: 700;
|
||||
line-height: 1;
|
||||
text-align: center;
|
||||
white-space: nowrap;
|
||||
vertical-align: baseline;
|
||||
border-radius: 0.25rem;
|
||||
}
|
||||
|
||||
.badge-success {
|
||||
background-color: rgba(16, 185, 129, 0.2);
|
||||
color: #34d399;
|
||||
border: 1px solid rgba(16, 185, 129, 0.3);
|
||||
}
|
||||
|
||||
.badge-secondary {
|
||||
background-color: rgba(107, 114, 128, 0.2);
|
||||
color: #9ca3af;
|
||||
border: 1px solid rgba(107, 114, 128, 0.3);
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
@@ -68,8 +68,6 @@ const routes = [
|
||||
|
||||
// 关于页面管理
|
||||
{ path: 'about', name: 'admin-about', component: () => import('./pages/admin/About.vue') },
|
||||
{ path: 'about/create', name: 'admin-about-create', component: () => import('./pages/admin/AboutForm.vue') },
|
||||
{ path: 'about/edit/:id', name: 'admin-about-edit', component: () => import('./pages/admin/AboutForm.vue') },
|
||||
|
||||
// 合作咨询管理
|
||||
{ path: 'inquiries', name: 'admin-inquiries', component: () => import('./pages/admin/Inquiries.vue') },
|
||||
|
||||
@@ -51,23 +51,30 @@ type UVTrendData struct {
|
||||
|
||||
// GetDailyUV 获取UV趋势
|
||||
func GetDailyUV(startDate, endDate string) ([]UVTrendData, error) {
|
||||
query := config.DB.Model(&models.UserAccessLog{}).
|
||||
Select("FROM_UNIXTIME(access_time, '%Y-%m-%d') as date, COUNT(DISTINCT user_ip) as count")
|
||||
|
||||
// 确定日期范围
|
||||
var startTime, endTime time.Time
|
||||
if startDate != "" {
|
||||
startUnix := parseDateToUnix(startDate, false)
|
||||
query = query.Where("access_time >= ?", startUnix)
|
||||
startTime = time.Unix(startUnix, 0)
|
||||
} else {
|
||||
// 默认最近7天
|
||||
startUnix := time.Now().AddDate(0, 0, -6).Unix()
|
||||
query = query.Where("access_time >= ?", startUnix)
|
||||
startTime = time.Now().AddDate(0, 0, -6)
|
||||
startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local)
|
||||
}
|
||||
|
||||
if endDate != "" {
|
||||
endUnix := parseDateToUnix(endDate, true)
|
||||
query = query.Where("access_time <= ?", endUnix)
|
||||
endTime = time.Unix(endUnix, 0)
|
||||
} else {
|
||||
endTime = time.Now()
|
||||
endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 23, 59, 59, 0, time.Local)
|
||||
}
|
||||
|
||||
// 查询数据库
|
||||
query := config.DB.Model(&models.UserAccessLog{}).
|
||||
Select("FROM_UNIXTIME(access_time, '%Y-%m-%d') as date, COUNT(DISTINCT user_ip) as count").
|
||||
Where("access_time >= ?", startTime.Unix()).
|
||||
Where("access_time <= ?", endTime.Unix())
|
||||
|
||||
var results []UVTrendData
|
||||
err := query.Group("date").
|
||||
Order("date ASC").
|
||||
@@ -77,13 +84,36 @@ func GetDailyUV(startDate, endDate string) ([]UVTrendData, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 暂未实现真实的同比环比计算逻辑,设为0
|
||||
for i := range results {
|
||||
results[i].YoY = 0
|
||||
results[i].MoM = 0
|
||||
// 创建日期到数据的映射
|
||||
resultMap := make(map[string]UVTrendData)
|
||||
for _, r := range results {
|
||||
resultMap[r.Date] = r
|
||||
}
|
||||
|
||||
return results, nil
|
||||
// 生成完整日期列表
|
||||
var fullResults []UVTrendData
|
||||
current := time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local)
|
||||
endDay := time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local)
|
||||
|
||||
for !current.After(endDay) {
|
||||
dateStr := current.Format("2006-01-02")
|
||||
if data, exists := resultMap[dateStr]; exists {
|
||||
data.YoY = 0
|
||||
data.MoM = 0
|
||||
fullResults = append(fullResults, data)
|
||||
} else {
|
||||
// 填充0值
|
||||
fullResults = append(fullResults, UVTrendData{
|
||||
Date: dateStr,
|
||||
Count: 0,
|
||||
YoY: 0,
|
||||
MoM: 0,
|
||||
})
|
||||
}
|
||||
current = current.AddDate(0, 0, 1)
|
||||
}
|
||||
|
||||
return fullResults, nil
|
||||
}
|
||||
|
||||
// GetUserRegions 获取用户地域分布
|
||||
|
||||
@@ -352,23 +352,31 @@ func GetTopPosts(limit int) ([]models.Post, error) {
|
||||
|
||||
// GetNewPostsTrend 获取新增文章趋势
|
||||
func GetNewPostsTrend(startDate, endDate string) ([]TrendData, error) {
|
||||
query := config.DB.Model(&models.Post{}).
|
||||
Select("FROM_UNIXTIME(created_at, '%Y-%m-%d') as date, COUNT(*) as count").
|
||||
Where("deleted_at = ?", 0)
|
||||
|
||||
// 确定日期范围
|
||||
var startTime, endTime time.Time
|
||||
if startDate != "" {
|
||||
startUnix := parseDateToUnix(startDate, false)
|
||||
query = query.Where("created_at >= ?", startUnix)
|
||||
startTime = time.Unix(startUnix, 0)
|
||||
} else {
|
||||
startUnix := time.Now().AddDate(0, 0, -6).Unix()
|
||||
query = query.Where("created_at >= ?", startUnix)
|
||||
startTime = time.Now().AddDate(0, 0, -6)
|
||||
startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local)
|
||||
}
|
||||
|
||||
if endDate != "" {
|
||||
endUnix := parseDateToUnix(endDate, true)
|
||||
query = query.Where("created_at <= ?", endUnix)
|
||||
endTime = time.Unix(endUnix, 0)
|
||||
} else {
|
||||
endTime = time.Now()
|
||||
endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 23, 59, 59, 0, time.Local)
|
||||
}
|
||||
|
||||
// 查询数据库
|
||||
query := config.DB.Model(&models.Post{}).
|
||||
Select("FROM_UNIXTIME(created_at, '%Y-%m-%d') as date, COUNT(*) as count").
|
||||
Where("deleted_at = ?", 0).
|
||||
Where("created_at >= ?", startTime.Unix()).
|
||||
Where("created_at <= ?", endTime.Unix())
|
||||
|
||||
var results []TrendData
|
||||
err := query.Group("date").
|
||||
Order("date ASC").
|
||||
@@ -378,13 +386,36 @@ func GetNewPostsTrend(startDate, endDate string) ([]TrendData, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set YoY and MoM to 0
|
||||
for i := range results {
|
||||
results[i].YoY = 0
|
||||
results[i].MoM = 0
|
||||
// 创建日期到数据的映射
|
||||
resultMap := make(map[string]TrendData)
|
||||
for _, r := range results {
|
||||
resultMap[r.Date] = r
|
||||
}
|
||||
|
||||
return results, nil
|
||||
// 生成完整日期列表
|
||||
var fullResults []TrendData
|
||||
current := time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local)
|
||||
endDay := time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local)
|
||||
|
||||
for !current.After(endDay) {
|
||||
dateStr := current.Format("2006-01-02")
|
||||
if data, exists := resultMap[dateStr]; exists {
|
||||
data.YoY = 0
|
||||
data.MoM = 0
|
||||
fullResults = append(fullResults, data)
|
||||
} else {
|
||||
// 填充0值
|
||||
fullResults = append(fullResults, TrendData{
|
||||
Date: dateStr,
|
||||
Count: 0,
|
||||
YoY: 0,
|
||||
MoM: 0,
|
||||
})
|
||||
}
|
||||
current = current.AddDate(0, 0, 1)
|
||||
}
|
||||
|
||||
return fullResults, nil
|
||||
}
|
||||
|
||||
// GetPostHistory 获取文章修改历史
|
||||
|
||||
Reference in New Issue
Block a user