整合前端仓库并保留提交历史

This commit is contained in:
2026-08-31 11:52:41 +08:00
172 changed files with 120216 additions and 1 deletions
@@ -0,0 +1,400 @@
<template>
<transition name="ad-fade">
<div v-if="!isHidden && displayedAds.length > 0" class="advertisement-wrapper" :class="[`ad-${type}`, position && type === 'float' ? `ad-position-${position}` : '']">
<div
v-for="ad in displayedAds"
:key="ad.id"
class="ad-item"
>
<!-- 简单模式 -->
<component
v-if="type === 'simple'"
:is="SimpleModeRaw"
:ad="ad"
@click="handleClick"
/>
<!-- 悬浮模式 -->
<component
v-else-if="type === 'float'"
:is="FloatModeRaw"
:ad="ad"
:closable="closable"
@click="handleClick"
@close="handleClose"
/>
<!-- 自定义扩展模式 -->
<component
v-else-if="customModeComponent"
:is="customModeComponent"
:ad="ad"
v-bind="customModeProps"
@click="handleClick"
@close="handleClose"
/>
<!-- 默认模式(向后兼容) -->
<div
v-else
class="ad-card"
@click="handleClick(ad)"
>
<div class="ad-image-wrapper">
<img
:src="ad.image"
:alt="ad.title"
class="ad-image"
loading="lazy"
/>
<div class="ad-gradient"></div>
<div v-if="showTitle" class="ad-content">
<h3 class="ad-title">{{ ad.title }}</h3>
<p v-if="ad.description" class="ad-description">{{ ad.description }}</p>
<span class="ad-badge">广告</span>
</div>
</div>
<button
v-if="closable"
class="ad-close-btn"
@click.stop="handleClose(ad)"
title="关闭"
>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6L6 18M6 6l12 12"/>
</svg>
</button>
</div>
</div>
</div>
</transition>
</template>
<script setup lang="ts">
import { ref, onMounted, computed, markRaw, shallowRef } from 'vue'
import type { Component } from 'vue'
import { getAdvertisements } from '~/api/site'
import type { AdvertisementItem } from '~/api/site/types'
import SimpleMode from './modes/simple.vue'
import FloatMode from './modes/float.vue'
import { getModeComponent, registerMode } from './modes'
interface Props {
type?: string // 简化类型定义,支持字符串类型
offset?: number
limit?: number
showTitle?: boolean
closable?: boolean
showTodayBtn?: boolean
autoCloseDelay?: number
position?: 'top' | 'center' | 'bottom'
customModeProps?: Record<string, any>
}
const props = withDefaults(defineProps<Props>(), {
type: 'simple',
offset: 0,
limit: 0,
showTitle: false,
closable: false,
showTodayBtn: false,
autoCloseDelay: 0,
position: 'center',
customModeProps: () => ({})
})
const emit = defineEmits<{
click: [ad: AdvertisementItem]
close: [ad: AdvertisementItem]
}>()
// 将组件标记为原始组件,避免响应式代理
const SimpleModeRaw = markRaw(SimpleMode)
const FloatModeRaw = markRaw(FloatMode)
// 使用这些标记为原始的组件
const ads = ref<AdvertisementItem[]>([])
const isHidden = ref(false)
const customModeComponent = shallowRef<Component | null>(null)
const COMPONENT_STORAGE_KEY = `ad_component_${props.type}_${props.offset}`
// 动态获取自定义模式组件
onMounted(() => {
if (!['simple', 'float'].includes(props.type!)) {
const component = getModeComponent(props.type!)
if (component) {
customModeComponent.value = markRaw(component)
}
}
})
const checkTodayClosed = () => {
const today = new Date().toDateString()
const stored = localStorage.getItem(`ad_today_${COMPONENT_STORAGE_KEY}`)
return stored === today
}
onMounted(async () => {
if (checkTodayClosed()) {
isHidden.value = true
return
}
try {
ads.value = await getAdvertisements()
} catch (error) {
console.error('获取广告失败:', error)
}
})
const displayedAds = computed(() => {
const now = Date.now() / 1000
const validAds = ads.value.filter(
ad =>
ad.enabled === 1 &&
ad.start_time <= now &&
ad.end_time >= now
)
const slicedAds = validAds.slice(props.offset)
const result = props.limit > 0 ? slicedAds.slice(0, props.limit) : slicedAds
return result
})
const handleClick = (ad: AdvertisementItem) => {
emit('click', ad)
if (ad.link) {
window.open(ad.link, '_blank')
}
}
const handleClose = (ad: AdvertisementItem) => {
isHidden.value = true
emit('close', ad)
}
// 导出注册方法供外部使用
defineExpose({
registerMode
})
</script>
<style scoped lang="scss">
// 基础广告卡片
.advertisement-wrapper {
width: 100%;
&.ad-simple {
display: block;
}
&.ad-float {
display: block;
}
}
.ad-item {
display: block;
}
// 悬浮广告定位
.ad-float {
position: fixed;
right: 24px;
top: 50%;
transform: translateY(-50%);
z-index: 100;
width: 200px;
.ad-item {
width: 100%;
}
}
// 右上位置
.ad-float.ad-position-top {
top: 24px;
transform: none;
}
// 右中位置(默认)
.ad-float.ad-position-center {
top: 50%;
transform: translateY(-50%);
}
// 右下位置
.ad-float.ad-position-bottom {
top: auto;
bottom: 24px;
transform: none;
}
// 默认卡片样式(向后兼容)
.ad-card {
position: relative;
cursor: pointer;
overflow: hidden;
border-radius: 0;
background: transparent;
box-shadow:
0 4px 6px rgba(0, 0, 0, 0.07),
0 10px 20px rgba(0, 0, 0, 0.08);
transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
&:hover {
transform: none;
box-shadow:
0 8px 12px rgba(102, 126, 234, 0.2),
0 20px 40px rgba(0, 0, 0, 0.15);
.ad-close-btn {
opacity: 1;
transform: rotate(90deg);
}
}
.ad-image-wrapper {
position: relative;
width: 100%;
overflow: hidden;
border-radius: 0;
}
.ad-image {
width: 100%;
height: auto;
display: block;
object-fit: cover;
}
.ad-gradient {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 60%;
background: linear-gradient(
to top,
rgba(0, 0, 0, 0.8) 0%,
rgba(0, 0, 0, 0.4) 50%,
transparent 100%
);
pointer-events: none;
}
.ad-content {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 24px;
z-index: 1;
}
.ad-title {
color: #fff;
font-size: 18px;
font-weight: 600;
margin: 0;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
line-height: 1.4;
}
.ad-description {
color: rgba(255, 255, 255, 0.8);
font-size: 14px;
margin: 0 0 12px;
line-height: 1.5;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
.ad-badge {
display: none;
}
.ad-close-btn {
position: absolute;
top: 12px;
right: 12px;
width: 36px;
height: 36px;
padding: 0;
background: rgba(255, 255, 255, 0.95);
border: none;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
color: #666;
opacity: 0;
transform: rotate(0deg);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
backdrop-filter: blur(10px);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
svg {
width: 14px;
height: 14px;
}
&:hover {
background: #fff;
color: #333;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
}
}
// 动画
.ad-fade-enter-active,
.ad-fade-leave-active {
transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
.ad-fade-enter-from,
.ad-fade-leave-to {
opacity: 0;
transform: translateY(20px);
}
// 响应式
@media (max-width: 768px) {
.ad-float {
right: 12px;
width: 160px;
.ad-card {
.ad-title {
font-size: 16px;
}
.ad-description {
font-size: 13px;
}
}
}
.ad-card {
&:hover {
transform: none;
box-shadow: 0 8px 12px rgba(102, 126, 234, 0.2);
}
.ad-close-btn {
opacity: 1;
transform: translateY(0);
}
.ad-title {
font-size: 16px;
}
.ad-description {
font-size: 13px;
}
}
}
</style>
@@ -0,0 +1,137 @@
<template>
<div class="ad-float-card" @click="handleClick">
<div class="ad-image-wrapper">
<img :src="ad.image" :alt="ad.title" class="ad-image" />
</div>
<div class="ad-content">
<h3 class="ad-title">{{ ad.title }}</h3>
<p v-if="ad.description" class="ad-description">{{ ad.description }}</p>
<span class="ad-badge">广告</span>
</div>
<button
v-if="closable"
class="ad-close-btn"
@click.stop="handleClose"
title="关闭"
>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6L6 18M6 6l12 12"/>
</svg>
</button>
</div>
</template>
<script setup lang="ts">
import type { AdvertisementItem } from '~/api/site/types'
interface Props {
ad: AdvertisementItem
closable?: boolean
}
const props = withDefaults(defineProps<Props>(), {
closable: false
})
const emit = defineEmits<{
click: [ad: AdvertisementItem]
close: [ad: AdvertisementItem]
}>()
const handleClick = () => {
emit('click', props.ad)
}
const handleClose = () => {
emit('close', props.ad)
}
</script>
<style scoped lang="scss">
.ad-float-card {
position: relative;
width: 100%;
background: #fff;
border-radius: 3px;
overflow: hidden;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
cursor: pointer;
.ad-image-wrapper {
width: 100%;
border-radius: 1px 1px 0 0;
}
.ad-image {
width: 100%;
height: 120px;
object-fit: contain;
display: block;
}
.ad-content {
padding: 12px;
}
.ad-title {
color: #1a1a1a;
font-size: 14px;
font-weight: 600;
line-height: 1.4;
margin: 0 0 8px;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
.ad-description {
color: #666;
font-size: 12px;
line-height: 1.5;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
margin: 0 0 10px;
}
.ad-badge {
display: inline-block;
padding: 3px 6px;
background: #f5f5f5;
color: #999;
font-size: 11px;
font-weight: 500;
border-radius: 3px;
margin-top: 8px;
line-height: 1;
}
.ad-close-btn {
position: absolute;
bottom: 6px;
right: 6px;
width: 24px;
height: 24px;
background: transparent;
border-radius: 0;
display: flex;
align-items: center;
justify-content: center;
color: rgba(0, 0, 0, 0.6);
opacity: 1;
transition: all 0.2s ease;
svg {
width: 12px;
height: 12px;
}
&:hover {
opacity: 1;
color: rgba(0, 0, 0, 1);
}
}
}
</style>
@@ -0,0 +1,26 @@
import type { Component } from 'vue'
// 内置模式
import SimpleMode from './simple.vue'
import FloatMode from './float.vue'
// 动态加载自定义模式
const customModes: Record<string, Component> = {
simple: SimpleMode,
float: FloatMode
}
// 注册自定义模式
export const registerMode = (name: string, component: Component) => {
customModes[name] = component
}
// 获取模式组件
export const getModeComponent = (modeName: string): Component | undefined => {
return customModes[modeName]
}
// 获取所有可用模式
export const getAvailableModes = (): string[] => {
return Object.keys(customModes)
}
@@ -0,0 +1,63 @@
<template>
<div class="ad-simple-layout" @click="handleClick">
<img :src="ad.image" :alt="ad.title" class="ad-simple-image" />
<!-- <span class="ad-badge">广告</span> -->
</div>
</template>
<script setup lang="ts">
import type { AdvertisementItem } from '~/api/site/types'
interface Props {
ad: AdvertisementItem
}
const props = defineProps<Props>()
const emit = defineEmits<{
click: [ad: AdvertisementItem]
}>()
const handleClick = () => {
emit('click', props.ad)
}
</script>
<style scoped lang="scss">
.ad-simple-layout {
position: relative;
width: 100%;
height: 50px;
margin: 4px 0;
overflow: hidden;
background: transparent;
border-radius: 1px;
cursor: pointer;
transition: box-shadow 0.2s ease;
&:hover {
background-color: rgb(218, 225, 225);
}
.ad-simple-image {
width: 100%;
height: 50px;
object-fit: contain;
display: block;
}
.ad-badge {
position: absolute;
top: 6px;
right: 6px;
padding: 2px 6px;
background: rgba(0, 0, 0, 0.5);
color: #fff;
font-size: 10px;
font-weight: 500;
border-radius: 2px;
line-height: 1;
z-index: 1;
}
}
</style>