d67849a311
- 内容迁入数据库:articles/works 表(中英双语 + 内容块), server/utils/seed.ts 灌入现有 6 动态 + 6 作品,站点初始内容不变 - 前台内容 API:/api/news、/api/work(列表/详情,仅已发布), 页面/首页组件改为 useFetch 拉取并按 locale 选 zh/en 字段 - 后台管理 /admin(admin 中间件守卫): - Dashboard 统计、动态/作品增删改(块编辑器、图片上传 /api/admin/upload) - 用户管理(角色切换/删除)、评论审核、联系/订阅管理(导出 CSV) - 预置管理员:NUXT_ADMIN_INITIAL_PASSWORD 首次初始化创建 admin 账号 - 图片自托管:上传存 PVC /data/uploads,/uploads/[...] 路由服务 - sitemap 改查 DB;i18n 移除内容键(305 键对齐);删除 app/data 冗余模块 - 角色以数据库为准(后台可切换);admin 中间件 + 客户端会话恢复插件
87 lines
2.8 KiB
Vue
87 lines
2.8 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 后台图片字段:URL 输入 + 本地图片上传(调 /api/admin/upload)+ 预览。
|
||
* v-model 绑定 URL 字符串。
|
||
*/
|
||
import { ref } from 'vue'
|
||
|
||
withDefaults(defineProps<{ modelValue?: string | null; label?: string }>(), {
|
||
modelValue: '',
|
||
label: '图片',
|
||
})
|
||
|
||
const emit = defineEmits<{ 'update:modelValue': [value: string] }>()
|
||
|
||
const fileInput = ref<HTMLInputElement | null>(null)
|
||
const uploading = ref(false)
|
||
const error = ref('')
|
||
|
||
const choose = () => fileInput.value?.click()
|
||
|
||
const onFileChange = async (e: Event) => {
|
||
const input = e.target as HTMLInputElement
|
||
const file = input.files?.[0]
|
||
input.value = ''
|
||
if (!file) return
|
||
uploading.value = true
|
||
error.value = ''
|
||
try {
|
||
const fd = new FormData()
|
||
fd.append('file', file)
|
||
const res = await $fetch<{ ok: boolean; url: string }>('/api/admin/upload', {
|
||
method: 'POST',
|
||
body: fd,
|
||
})
|
||
emit('update:modelValue', res.url)
|
||
} catch (err: any) {
|
||
error.value = err?.data?.message || '上传失败'
|
||
} finally {
|
||
uploading.value = false
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<div class="flex items-center justify-between mb-2">
|
||
<label class="text-sm font-semibold text-gray-800">{{ label }}</label>
|
||
<button
|
||
v-if="modelValue"
|
||
type="button"
|
||
@click="emit('update:modelValue', '')"
|
||
class="text-xs text-gray-400 hover:text-red-600 flex items-center gap-1 transition-colors"
|
||
>
|
||
<Icon name="ph:x-circle" class="w-3.5 h-3.5" /> 清除
|
||
</button>
|
||
</div>
|
||
<div class="flex items-center gap-3">
|
||
<input
|
||
:value="modelValue"
|
||
type="text"
|
||
placeholder="图片 URL(本地 /uploads/... 或 https://...)"
|
||
@input="emit('update:modelValue', ($event.target as HTMLInputElement).value)"
|
||
class="flex-1 min-w-0 px-3 py-2 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5"
|
||
/>
|
||
<button
|
||
type="button"
|
||
:disabled="uploading"
|
||
@click="choose"
|
||
class="flex items-center gap-1.5 px-3 py-2 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors disabled:opacity-60 flex-shrink-0"
|
||
>
|
||
<Icon v-if="uploading" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
|
||
<Icon v-else name="ph:upload-simple" class="w-4 h-4" />
|
||
{{ uploading ? '上传中…' : '上传图片' }}
|
||
</button>
|
||
<input ref="fileInput" type="file" accept="image/*" class="hidden" @change="onFileChange" />
|
||
</div>
|
||
<div v-if="modelValue" class="mt-3">
|
||
<img
|
||
:src="modelValue"
|
||
alt="图片预览"
|
||
class="h-32 w-auto max-w-full rounded-lg border border-gray-100 object-cover"
|
||
/>
|
||
</div>
|
||
<p v-if="error" class="mt-1 text-xs text-red-600">{{ error }}</p>
|
||
</div>
|
||
</template>
|