479 lines
17 KiB
Vue
479 lines
17 KiB
Vue
<!--
|
||
FrontConfigForm — 仓库「前端独立部署」配置页(独立标签页,表单项页面)
|
||
对接接口:
|
||
- setting.warehouse.getFrontConfig:加载当前仓库前端部署配置(品牌 + 域名/证书状态)
|
||
- setting.warehouse.saveFrontConfig:保存品牌配置(title/logo/favicon/login_bg/theme)
|
||
- setting.warehouse.saveFrontCert:上传自持域名 HTTPS 证书(domain + pem/key)
|
||
该页从基础设置(WarehouseSettingsForm)独立拆出,作为 basicManagement/newview 的
|
||
viewType='form' 标签页渲染。
|
||
-->
|
||
<template>
|
||
<div v-loading="frontPageLoading" class="front-config-page">
|
||
<div class="settings-header">
|
||
<div class="header-text">
|
||
<span class="title">前端独立部署</span>
|
||
<span class="sub">按仓库自持域名部署并品牌化前端(站点标题 / Logo / favicon / 登录页 / 主题 / HTTPS 证书)</span>
|
||
</div>
|
||
<div class="header-actions">
|
||
<el-button :loading="frontPageLoading" @click="loadFrontConfig">刷新</el-button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="front-config-section">
|
||
<el-divider content-position="left" class="front-config-divider">前端独立部署(品牌配置)</el-divider>
|
||
<el-alert
|
||
v-if="frontCertStatus.level === 'danger' || frontCertStatus.level === 'warning'"
|
||
:type="frontCertStatus.level"
|
||
:title="frontCertStatus.title"
|
||
:description="frontCertStatus.desc"
|
||
show-icon
|
||
:closable="false"
|
||
class="mb-16 front-config-alert"
|
||
/>
|
||
<el-alert
|
||
v-if="frontSyncFailed"
|
||
type="error"
|
||
title="证书部署同步失败"
|
||
:description="frontMeta.last_sync_error || '同步失败,请重新上传证书或稍后重试'"
|
||
show-icon
|
||
:closable="false"
|
||
class="mb-16 front-config-alert"
|
||
/>
|
||
<el-descriptions v-if="frontMeta.domain" :column="4" border size="small" class="front-config-meta">
|
||
<el-descriptions-item label="自持域名">{{ frontMeta.domain || '—' }}</el-descriptions-item>
|
||
<el-descriptions-item label="证书状态">
|
||
<el-tag :type="frontCertTagType" size="small" effect="plain">{{ frontCertStatus.text }}</el-tag>
|
||
</el-descriptions-item>
|
||
<el-descriptions-item label="同步状态">{{ frontSyncStatusText }}</el-descriptions-item>
|
||
<el-descriptions-item label="同步详情">{{ frontMeta.last_synced_at || '—' }} · 尝试 {{ frontMeta.sync_attempts ?? 0 }} 次</el-descriptions-item>
|
||
</el-descriptions>
|
||
<el-form label-width="140px" label-position="right" class="front-config-form">
|
||
<el-form-item label="站点标题">
|
||
<el-input v-model="frontForm.title" maxlength="100" clearable placeholder="站点标题(留空表示清空)" />
|
||
</el-form-item>
|
||
<el-form-item label="Logo 地址">
|
||
<el-input v-model="frontForm.logo_url" maxlength="255" clearable placeholder="Logo 图片 URL(留空清空)" />
|
||
</el-form-item>
|
||
<el-form-item label="favicon 地址">
|
||
<el-input v-model="frontForm.favicon_url" maxlength="255" clearable placeholder="favicon 图标 URL(留空清空)" />
|
||
</el-form-item>
|
||
<el-form-item label="登录页背景图">
|
||
<el-input v-model="frontForm.login_bg_url" maxlength="255" clearable placeholder="登录页背景图 URL(留空清空)" />
|
||
</el-form-item>
|
||
<el-form-item label="主题配置">
|
||
<div class="front-theme-editor">
|
||
<div class="front-theme-default">
|
||
<div class="front-theme-row">
|
||
<span class="front-theme-row__label">网页名称</span>
|
||
<el-input v-model="frontThemeDefault.siteName" class="front-theme-row__value" placeholder="如:XX云仓(留空使用系统默认:天豹云仓)" clearable maxlength="100" />
|
||
</div>
|
||
<div class="front-theme-row">
|
||
<span class="front-theme-row__label">主页面 Logo</span>
|
||
<el-input v-model="frontThemeDefault.mainLogoUrl" class="front-theme-row__value" placeholder="主页面/顶栏 Logo 图片 URL(可留空)" clearable maxlength="255" />
|
||
</div>
|
||
<div class="front-theme-row">
|
||
<span class="front-theme-row__label">登录页 Logo</span>
|
||
<el-input v-model="frontThemeDefault.loginLogoUrl" class="front-theme-row__value" placeholder="登录页 Logo 图片 URL(留空用品牌 Logo)" clearable maxlength="255" />
|
||
</div>
|
||
<div class="front-theme-row">
|
||
<span class="front-theme-row__label">登录页文字</span>
|
||
<el-input v-model="frontThemeDefault.loginText" class="front-theme-row__value" placeholder="登录页标语(留空默认:现代化仓储管理系统 - 助力企业高效运营)" clearable maxlength="200" />
|
||
</div>
|
||
</div>
|
||
<div class="front-config-hint front-theme-hint">以上 4 个为约定主题字段,均可留空:留空不写入主题、使用系统默认值。</div>
|
||
|
||
<el-divider content-position="left" class="front-config-divider">自定义主题键值(映射 CSS 变量 --<key>)</el-divider>
|
||
<div v-for="(row, idx) in frontThemeRows" :key="`front-theme-${idx}`" class="front-theme-row">
|
||
<el-input v-model="row.key" class="front-theme-row__key" placeholder="键(如 primary)" maxlength="60" />
|
||
<el-input v-model="row.value" class="front-theme-row__value" placeholder="值:文本,或 JSON 对象/数组/数字/布尔" />
|
||
<el-button type="danger" plain size="small" @click="removeFrontThemeRow(idx)">删除</el-button>
|
||
</div>
|
||
<div class="front-theme-actions">
|
||
<el-button type="primary" plain size="small" @click="addFrontThemeRow">新增自定义键</el-button>
|
||
</div>
|
||
</div>
|
||
</el-form-item>
|
||
<el-form-item>
|
||
<el-button type="primary" :loading="frontSaving" @click="saveFrontConfig">保存品牌配置</el-button>
|
||
<span class="front-config-hint">品牌字段留空即清空;主题删空全部键后保存即清空主题;保存成功后自动重新加载</span>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<el-divider content-position="left" class="front-config-divider">前端独立部署(自持域名 & 证书)</el-divider>
|
||
<el-form label-width="140px" label-position="right" class="front-config-form">
|
||
<el-form-item label="自持域名">
|
||
<el-input v-model="frontCert.domain" maxlength="100" clearable placeholder="如 self.example.com(全局唯一)" />
|
||
</el-form-item>
|
||
<el-form-item label="证书 PEM">
|
||
<el-input v-model="frontCert.pem" type="textarea" :rows="4" placeholder="-----BEGIN CERTIFICATE-----..." />
|
||
</el-form-item>
|
||
<el-form-item label="私钥 PEM">
|
||
<el-input v-model="frontCert.key" type="textarea" :rows="4" placeholder="-----BEGIN PRIVATE KEY-----..." />
|
||
</el-form-item>
|
||
<el-form-item>
|
||
<el-button type="warning" :loading="frontCertSaving" @click="submitFrontCert">上传证书</el-button>
|
||
<span class="front-config-hint">上传成功后自动写入证书到期时间、重置同步状态并重新加载配置</span>
|
||
</el-form-item>
|
||
</el-form>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, reactive, ref } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import {
|
||
settingWarehouseGetFrontConfig,
|
||
settingWarehouseSaveFrontConfig,
|
||
settingWarehouseSaveFrontCert,
|
||
unwrapSettingWarehousePayload
|
||
} from '@/api/setting.warehouse/settingwarehouse'
|
||
|
||
defineOptions({ name: 'FrontConfigForm' })
|
||
|
||
const okCode = (code: unknown) => code === 1 || code === 200 || code === 0
|
||
|
||
/** getFrontConfig 响应:仓库前端部署元信息(品牌 + 证书/同步内部字段,仅用于本页回显) */
|
||
type FrontConfigMeta = {
|
||
domain?: string
|
||
title?: string
|
||
logo_url?: string
|
||
favicon_url?: string
|
||
login_bg_url?: string
|
||
theme?: Record<string, unknown> | string | null
|
||
cert_path?: string
|
||
key_path?: string
|
||
cert_expire_at?: string | null
|
||
sync_status?: number
|
||
sync_attempts?: number
|
||
last_sync_error?: string
|
||
last_synced_at?: string | null
|
||
[key: string]: unknown
|
||
}
|
||
|
||
// ── 前端独立部署(品牌 & 域名证书):getFrontConfig / saveFrontConfig / saveFrontCert ──
|
||
const frontPageLoading = ref(false)
|
||
const frontSaving = ref(false)
|
||
const frontCertSaving = ref(false)
|
||
const frontMeta = reactive<FrontConfigMeta>({})
|
||
const frontForm = reactive({ title: '', logo_url: '', favicon_url: '', login_bg_url: '' })
|
||
/** 主题约定 4 字段(文案/资源类,允许空;空=不写入主题、使用默认) */
|
||
const FRONT_THEME_FIXED_KEYS = ['siteName', 'mainLogoUrl', 'loginLogoUrl', 'loginText'] as const
|
||
const frontThemeDefault = reactive<Record<string, string>>({
|
||
siteName: '',
|
||
mainLogoUrl: '',
|
||
loginLogoUrl: '',
|
||
loginText: ''
|
||
})
|
||
const frontThemeRows = ref<Array<{ key: string; value: string }>>([])
|
||
const frontCert = reactive({ domain: '', pem: '', key: '' })
|
||
|
||
const frontSyncStatusText = computed(() => {
|
||
const s = Number(frontMeta.sync_status)
|
||
if (s === 0) return '待同步'
|
||
if (s === 1) return '同步成功'
|
||
if (s === 2) return '同步失败'
|
||
return frontMeta.sync_status ?? '—'
|
||
})
|
||
|
||
/** 同步是否失败(用于同步失败告警) */
|
||
const frontSyncFailed = computed(() => Number(frontMeta.sync_status) === 2)
|
||
|
||
function fmtFrontDate(ts: number): string {
|
||
const d = new Date(ts)
|
||
const p = (n: number) => String(n).padStart(2, '0')
|
||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`
|
||
}
|
||
|
||
/** 证书状态:none=未上传,success=正常,warning=30 天内到期,danger=已过期 */
|
||
const frontCertStatus = computed<{
|
||
level: 'success' | 'warning' | 'danger' | 'info' | 'none'
|
||
text: string
|
||
title: string
|
||
desc: string
|
||
}>(() => {
|
||
const raw = String(frontMeta.cert_expire_at ?? '').trim()
|
||
if (!raw) return { level: 'none', text: '未上传', title: '', desc: '' }
|
||
const ts = Date.parse(raw)
|
||
if (Number.isNaN(ts)) return { level: 'info', text: raw, title: '', desc: '' }
|
||
const days = Math.ceil((ts - Date.now()) / 86400000)
|
||
const when = fmtFrontDate(ts)
|
||
if (days < 0) {
|
||
return {
|
||
level: 'danger',
|
||
text: `${when} 已过期`,
|
||
title: 'HTTPS 证书已过期',
|
||
desc: `证书已于 ${when} 过期(${-days} 天),自持域名将无法正常访问,请重新上传证书。`
|
||
}
|
||
}
|
||
if (days <= 30) {
|
||
return {
|
||
level: 'warning',
|
||
text: `${when} 到期(剩 ${days} 天)`,
|
||
title: 'HTTPS 证书即将到期',
|
||
desc: `证书将于 ${when} 到期,剩余 ${days} 天,请及时更新证书。`
|
||
}
|
||
}
|
||
return { level: 'success', text: `有效期至 ${when}`, title: '', desc: '' }
|
||
})
|
||
|
||
/** 证书 tag 类型(none/info 一律用 info 灰) */
|
||
const frontCertTagType = computed<'success' | 'warning' | 'danger' | 'info'>(() => {
|
||
const level = frontCertStatus.value.level
|
||
return level === 'success' || level === 'warning' || level === 'danger' ? level : 'info'
|
||
})
|
||
|
||
function stringifyFrontThemeValue(v: unknown): string {
|
||
if (v === null || v === undefined) return ''
|
||
if (typeof v === 'string') return v
|
||
try {
|
||
return JSON.stringify(v)
|
||
} catch {
|
||
return String(v)
|
||
}
|
||
}
|
||
|
||
function parseFrontThemeValue(text: string): unknown {
|
||
const t = text.trim()
|
||
if (!t) return ''
|
||
try {
|
||
return JSON.parse(t)
|
||
} catch {
|
||
return t
|
||
}
|
||
}
|
||
|
||
function addFrontThemeRow() {
|
||
frontThemeRows.value.push({ key: '', value: '' })
|
||
}
|
||
|
||
function removeFrontThemeRow(index: number) {
|
||
frontThemeRows.value.splice(index, 1)
|
||
}
|
||
|
||
async function loadFrontConfig() {
|
||
frontPageLoading.value = true
|
||
try {
|
||
const res = await settingWarehouseGetFrontConfig()
|
||
if (!res || !okCode(res.code)) {
|
||
ElMessage.error(res?.message || '获取前端部署配置失败')
|
||
return
|
||
}
|
||
const cfg = unwrapSettingWarehousePayload(res as Record<string, unknown>)
|
||
Object.keys(frontMeta).forEach((k) => delete frontMeta[k])
|
||
Object.assign(frontMeta, cfg)
|
||
frontForm.title = String(cfg.title ?? '')
|
||
frontForm.logo_url = String(cfg.logo_url ?? '')
|
||
frontForm.favicon_url = String(cfg.favicon_url ?? '')
|
||
frontForm.login_bg_url = String(cfg.login_bg_url ?? '')
|
||
// theme:与 getFrontConfig 响应参数一致,按对象键逐项展开编辑
|
||
let themeObj: Record<string, unknown> | null = null
|
||
if (cfg.theme && typeof cfg.theme === 'object' && !Array.isArray(cfg.theme)) {
|
||
themeObj = cfg.theme as Record<string, unknown>
|
||
} else if (typeof cfg.theme === 'string' && cfg.theme.trim()) {
|
||
try {
|
||
const parsed = JSON.parse(cfg.theme)
|
||
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) themeObj = parsed as Record<string, unknown>
|
||
} catch {
|
||
// 忽略无法解析的主题字符串
|
||
}
|
||
}
|
||
const src = themeObj
|
||
// 约定 4 字段单独编辑(允许空),其余键进入自定义 CSS 变量区
|
||
FRONT_THEME_FIXED_KEYS.forEach((k) => {
|
||
frontThemeDefault[k] = ''
|
||
})
|
||
frontThemeRows.value = []
|
||
if (src) {
|
||
Object.keys(src).forEach((k) => {
|
||
const value = src[k]
|
||
if (FRONT_THEME_FIXED_KEYS.includes(k as (typeof FRONT_THEME_FIXED_KEYS)[number])) {
|
||
frontThemeDefault[k] = stringifyFrontThemeValue(value)
|
||
} else if (value !== undefined && value !== null && String(value).trim() !== '') {
|
||
frontThemeRows.value.push({ key: k, value: stringifyFrontThemeValue(value) })
|
||
}
|
||
})
|
||
}
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '获取前端部署配置失败')
|
||
} finally {
|
||
frontPageLoading.value = false
|
||
}
|
||
}
|
||
|
||
async function saveFrontConfig() {
|
||
frontSaving.value = true
|
||
try {
|
||
const themeObj: Record<string, unknown> = {}
|
||
// 约定 4 字段允许空:空值不写入主题(前端用默认文案/图片)
|
||
FRONT_THEME_FIXED_KEYS.forEach((k) => {
|
||
const val = String(frontThemeDefault[k] ?? '').trim()
|
||
if (val) themeObj[k] = val
|
||
})
|
||
frontThemeRows.value.forEach((row) => {
|
||
const key = row.key.trim()
|
||
const valueText = row.value.trim()
|
||
if (!key || !valueText) return
|
||
themeObj[key] = parseFrontThemeValue(valueText)
|
||
})
|
||
const res = await settingWarehouseSaveFrontConfig({
|
||
title: frontForm.title.trim(),
|
||
logo_url: frontForm.logo_url.trim(),
|
||
favicon_url: frontForm.favicon_url.trim(),
|
||
login_bg_url: frontForm.login_bg_url.trim(),
|
||
theme: themeObj
|
||
})
|
||
if (!res || !okCode(res.code)) {
|
||
ElMessage.error(res?.message || '保存品牌配置失败')
|
||
return
|
||
}
|
||
ElMessage.success('品牌配置已保存')
|
||
await loadFrontConfig()
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '保存品牌配置失败')
|
||
} finally {
|
||
frontSaving.value = false
|
||
}
|
||
}
|
||
|
||
async function submitFrontCert() {
|
||
const domain = frontCert.domain.trim()
|
||
const pem = frontCert.pem.trim()
|
||
const key = frontCert.key.trim()
|
||
if (!domain || !pem || !key) {
|
||
ElMessage.warning('域名、证书 PEM、私钥 PEM 均必填')
|
||
return
|
||
}
|
||
frontCertSaving.value = true
|
||
try {
|
||
const res = await settingWarehouseSaveFrontCert({ domain, pem, key })
|
||
if (!res || !okCode(res.code)) {
|
||
ElMessage.error(res?.message || '证书上传失败')
|
||
return
|
||
}
|
||
ElMessage.success('证书已上传并生效')
|
||
frontCert.domain = ''
|
||
frontCert.pem = ''
|
||
frontCert.key = ''
|
||
await loadFrontConfig()
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '证书上传失败')
|
||
} finally {
|
||
frontCertSaving.value = false
|
||
}
|
||
}
|
||
|
||
loadFrontConfig()
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.front-config-page {
|
||
padding: 12px 16px 24px;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.settings-header {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12px;
|
||
flex-wrap: wrap;
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.header-text {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 4px;
|
||
}
|
||
|
||
.title {
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
.sub {
|
||
font-size: 13px;
|
||
color: #909399;
|
||
}
|
||
|
||
.header-actions {
|
||
display: flex;
|
||
gap: 8px;
|
||
}
|
||
|
||
.mb-16 {
|
||
margin-bottom: 16px;
|
||
}
|
||
|
||
.front-config-section {
|
||
margin-top: 8px;
|
||
}
|
||
|
||
.front-config-divider {
|
||
margin: 12px 0;
|
||
}
|
||
|
||
.front-config-meta {
|
||
margin-bottom: 12px;
|
||
max-width: 720px;
|
||
}
|
||
|
||
.front-config-alert {
|
||
margin: 0 0 12px;
|
||
max-width: 720px;
|
||
}
|
||
|
||
.front-config-form {
|
||
max-width: 720px;
|
||
}
|
||
|
||
.front-config-hint {
|
||
font-size: 12px;
|
||
color: #909399;
|
||
margin-left: 8px;
|
||
}
|
||
|
||
.front-theme-editor {
|
||
width: 100%;
|
||
max-width: 560px;
|
||
}
|
||
|
||
.front-theme-row {
|
||
display: flex;
|
||
gap: 8px;
|
||
align-items: center;
|
||
margin-bottom: 8px;
|
||
|
||
&__key {
|
||
flex: 0 0 180px;
|
||
}
|
||
|
||
&__label {
|
||
flex: 0 0 180px;
|
||
font-size: 13px;
|
||
color: #606266;
|
||
text-align: right;
|
||
padding-right: 4px;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
&__value {
|
||
flex: 1;
|
||
min-width: 200px;
|
||
}
|
||
}
|
||
|
||
.front-theme-actions {
|
||
margin-bottom: 4px;
|
||
}
|
||
|
||
.front-theme-hint {
|
||
margin-left: 0;
|
||
margin-top: 6px;
|
||
}
|
||
</style>
|