Files
Artveris/apps/admin/src/views/Install.vue
T
Nixevol e0c4e24b7d feat: dev.py 新增 --stop 并支持安装完成后自动切换到开发模式
- 本地 dev 保持前台模型、不引入 PM2;--stop 复用 kill_project_servers 一键停止
- 无 .env 时监听根 .env 出现作为安装成功信号,自动停安装 API 并以正式模式重启 api/client,避开安装中重启
- 安装 API 在 dev 模式下不再退出、迁移失败透传真实原因;前端提交后统一轮询就绪再进后台
2026-07-09 17:09:15 +08:00

560 lines
18 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="install-page">
<div class="install-card">
<div class="install-head">
<div class="brand">
<h1>{{ form.appName || 'Artveris' }}</h1>
<p>{{ tt('subtitle') }}</p>
</div>
<div class="head-actions">
<lang-switch />
<theme-switch />
</div>
</div>
<n-spin :show="loading">
<n-result
v-if="installed"
status="success"
:title="tt('alreadyInstalled')"
:description="tt('gotoAdmin')"
>
<template #footer>
<n-button type="primary" @click="goAdmin">{{ tt('enterAdmin') }}</n-button>
</template>
</n-result>
<template v-else>
<n-steps :current="step + 1" size="small" class="steps">
<n-step :title="tt('stepBase')" />
<n-step :title="tt('stepDb')" />
<n-step :title="tt('stepStorage')" />
</n-steps>
<!-- 第 1 步:基础配置(含管理员) -->
<section v-show="step === 0" class="box">
<h2>{{ tt('base') }}</h2>
<label><span>{{ tt('appName') }}<i class="req">*</i></span><n-input v-model:value="form.appName" /></label>
<label><span>{{ tt('baseUrl') }}<i class="req">*</i></span><n-input v-model:value="form.baseUrl" placeholder="https://art.example.com" /></label>
<p class="hint">{{ tt('baseUrlHint') }}</p>
<div class="urls">
<div><span>{{ tt('urlAdmin') }}</span>{{ baseTrim }}/admin</div>
<div><span>{{ tt('urlClient') }}</span>{{ baseTrim }}/trace</div>
</div>
<div class="sub">{{ tt('admin') }}</div>
<label><span>{{ tt('adminAccount') }}<i class="req">*</i></span><n-input v-model:value="form.admin.username" /></label>
<label><span>{{ tt('adminPwd') }}<i class="req">*</i></span><n-input v-model:value="form.admin.password" type="password" show-password-on="click" /></label>
<label><span>{{ tt('realName') }}</span><n-input v-model:value="form.admin.realName" /></label>
</section>
<!-- 第 2 步:数据库(MySQL + Redis) -->
<section v-show="step === 1" class="box">
<h2>{{ tt('dbCard') }}</h2>
<div class="sub-row">
<div class="sub">MySQL</div>
<n-button size="tiny" :loading="testingMysql" @click="testMysql">{{ tt('testConnection') }}</n-button>
</div>
<label><span>{{ tt('host') }}<i class="req">*</i></span><n-input v-model:value="form.mysql.host" /></label>
<label><span>{{ tt('port') }}<i class="req">*</i></span><n-input-number v-model:value="form.mysql.port" :min="1" :show-button="false" class="full" /></label>
<label><span>{{ tt('database') }}<i class="req">*</i></span><n-input v-model:value="form.mysql.database" /></label>
<label><span>{{ tt('username') }}<i class="req">*</i></span><n-input v-model:value="form.mysql.username" /></label>
<label><span>{{ tt('password') }}</span><n-input v-model:value="form.mysql.password" type="password" show-password-on="click" /></label>
<p v-if="mysqlTest" class="test-result" :class="mysqlTest.ok ? 'ok' : 'fail'">{{ mysqlTest.msg }}</p>
<div class="sub-row">
<div class="sub">Redis</div>
<n-button size="tiny" :loading="testingRedis" @click="testRedis">{{ tt('testConnection') }}</n-button>
</div>
<label><span>{{ tt('host') }}<i class="req">*</i></span><n-input v-model:value="form.redis.host" /></label>
<label><span>{{ tt('port') }}<i class="req">*</i></span><n-input-number v-model:value="form.redis.port" :min="1" :show-button="false" class="full" /></label>
<label><span>{{ tt('password') }}</span><n-input v-model:value="form.redis.password" type="password" show-password-on="click" /></label>
<p v-if="redisTest" class="test-result" :class="redisTest.ok ? 'ok' : 'fail'">{{ redisTest.msg }}</p>
</section>
<!-- 第 3 步:存储 -->
<section v-show="step === 2" class="box">
<div class="box-head">
<h2>{{ tt('storage') }}</h2>
<n-button size="tiny" :loading="testingStorage" @click="testStorage">{{ tt('testConnection') }}</n-button>
</div>
<label>
<span>{{ tt('driver') }}<i class="req">*</i></span>
<n-select v-model:value="form.storage.driver" :options="driverOptions" class="full" />
</label>
<template v-if="form.storage.driver === 'local'">
<label><span>{{ tt('localDir') }}<i class="req">*</i></span><n-input v-model:value="form.storage.localDir" /></label>
</template>
<template v-else>
<label><span>Endpoint<i class="req">*</i></span><n-input v-model:value="form.storage.ossEndpoint" /></label>
<label><span>Region</span><n-input v-model:value="form.storage.ossRegion" /></label>
<label><span>Bucket<i class="req">*</i></span><n-input v-model:value="form.storage.ossBucket" /></label>
<label><span>AccessKey ID<i class="req">*</i></span><n-input v-model:value="form.storage.ossAccessKeyId" /></label>
<label><span>AccessKey Secret<i class="req">*</i></span><n-input v-model:value="form.storage.ossAccessKeySecret" type="password" show-password-on="click" /></label>
</template>
<label><span>CDN</span><n-input v-model:value="form.storage.cdnBaseUrl" placeholder="https://cdn.example.com" /></label>
<p class="hint">{{ tt('cdnHint') }}</p>
<p v-if="storageTest" class="test-result" :class="storageTest.ok ? 'ok' : 'fail'">{{ storageTest.msg }}</p>
</section>
<ul v-if="stepErrors.length" class="problems">
<li v-for="(p, i) in stepErrors" :key="i">{{ p }}</li>
</ul>
<n-alert v-if="error" type="error" class="mt" :title="error" />
<n-alert v-if="success" type="success" class="mt" :title="success" />
<div class="wizard-foot">
<n-button v-if="step > 0" :disabled="advancing || submitting" @click="step -= 1">{{ tt('prev') }}</n-button>
<div class="spacer" />
<n-button
type="primary"
:disabled="stepErrors.length > 0"
:loading="advancing || submitting"
@click="onNext"
>
{{ step === 2 ? (submitting ? tt('installing') : tt('install')) : tt('next') }}
</n-button>
</div>
</template>
</n-spin>
</div>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import ThemeSwitch from '@/components/ThemeSwitch.vue';
import LangSwitch from '@/components/LangSwitch.vue';
interface InstallForm {
appName: string;
baseUrl: string;
mysql: { host: string; port: number; database: string; username: string; password: string };
redis: { host: string; port: number; password: string };
admin: { username: string; password: string; realName: string };
storage: {
driver: 'local' | 'oss';
localDir: string;
cdnBaseUrl: string;
ossEndpoint: string;
ossRegion: string;
ossBucket: string;
ossAccessKeyId: string;
ossAccessKeySecret: string;
};
}
const { t } = useI18n();
function tt(key: string): string {
return t(`pages.install.${key}`);
}
const driverOptions = computed(() => [
{ label: tt('local'), value: 'local' },
{ label: 'OSS / S3', value: 'oss' },
]);
const form = reactive<InstallForm>({
appName: tt('defaultAppName'),
baseUrl: location.origin,
mysql: { host: '127.0.0.1', port: 3306, database: 'artveris', username: 'artveris', password: '' },
redis: { host: '127.0.0.1', port: 6379, password: '' },
admin: { username: 'admin', password: 'Admin@12345', realName: tt('defaultAdminName') },
storage: {
driver: 'local',
localDir: './storage',
cdnBaseUrl: '',
ossEndpoint: '',
ossRegion: '',
ossBucket: '',
ossAccessKeyId: '',
ossAccessKeySecret: '',
},
});
const step = ref(0);
const loading = ref(true);
const submitting = ref(false);
const advancing = ref(false);
const installed = ref(false);
const error = ref('');
const success = ref('');
const testingMysql = ref(false);
const testingRedis = ref(false);
const testingStorage = ref(false);
const mysqlTest = ref<{ ok: boolean; msg: string } | null>(null);
const redisTest = ref<{ ok: boolean; msg: string } | null>(null);
const storageTest = ref<{ ok: boolean; msg: string } | null>(null);
const baseTrim = computed(() => (form.baseUrl || '').replace(/\/+$/, ''));
// 配置变化后作废旧的测试结果,需重新测试
watch(() => ({ ...form.mysql }), () => (mysqlTest.value = null), { deep: true });
watch(() => ({ ...form.redis }), () => (redisTest.value = null), { deep: true });
watch(() => ({ ...form.storage }), () => (storageTest.value = null), { deep: true });
// 当前步骤的问题清单:非空则禁用「下一步/安装」并逐条展示
const stepErrors = computed<string[]>(() => {
const errs: string[] = [];
if (step.value === 0) {
if (!form.appName.trim()) errs.push(tt('errAppName'));
if (!/^https?:\/\/.+/i.test(baseTrim.value)) errs.push(tt('errBaseUrl'));
if (!form.admin.username.trim()) errs.push(tt('errAdminUser'));
if (!form.admin.password || form.admin.password.length < 6) errs.push(tt('errAdminPwd'));
} else if (step.value === 1) {
if (!form.mysql.host.trim() || !form.mysql.port) errs.push(tt('errMysqlHost'));
if (!/^[A-Za-z0-9_]+$/.test(form.mysql.database || '')) errs.push(tt('errMysqlDb'));
if (!form.mysql.username.trim()) errs.push(tt('errMysqlUser'));
if (!form.redis.host.trim() || !form.redis.port) errs.push(tt('errRedisHost'));
} else {
if (form.storage.driver === 'local') {
if (!form.storage.localDir.trim()) errs.push(tt('errLocalDir'));
} else {
if (!form.storage.ossEndpoint.trim()) errs.push(tt('errOssEndpoint'));
if (!form.storage.ossBucket.trim()) errs.push(tt('errOssBucket'));
if (!form.storage.ossAccessKeyId.trim() || !form.storage.ossAccessKeySecret.trim()) errs.push(tt('errOssKey'));
}
}
return errs;
});
function goAdmin(): void {
location.href = '/admin';
}
interface ApiBody {
code: number;
message?: string;
data?: {
installed?: boolean;
needsDevRestart?: boolean;
message?: string;
restartRequired?: boolean;
defaults?: Record<string, string | number>;
};
}
async function parseApiBody(res: Response): Promise<ApiBody | null> {
const text = await res.text();
if (!text.trim()) return null;
try {
return JSON.parse(text) as ApiBody;
} catch {
throw new Error(tt('invalidResponse'));
}
}
async function fetchInstallStatus(): Promise<{ installed: boolean; needsDevRestart: boolean }> {
try {
const res = await fetch('/api/install/status');
const body = await parseApiBody(res);
return {
installed: Boolean(body?.data?.installed),
needsDevRestart: Boolean(body?.data?.needsDevRestart),
};
} catch {
return { installed: false, needsDevRestart: false };
}
}
async function post(path: string, payload: Record<string, unknown>): Promise<{ ok: boolean; msg: string }> {
try {
const res = await fetch(path, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
const body = await parseApiBody(res);
if (!body) throw new Error(tt('emptyResponse'));
if (!res.ok || body.code !== 0) throw new Error(body.message || tt('testFailed'));
return { ok: true, msg: body.data?.message || tt('testOk') };
} catch (e) {
return { ok: false, msg: (e as Error).message || tt('testFailed') };
}
}
async function testMysql(): Promise<{ ok: boolean; msg: string }> {
testingMysql.value = true;
const result = await post('/api/install/test-connection', {
target: 'mysql',
host: form.mysql.host,
port: form.mysql.port,
database: form.mysql.database,
username: form.mysql.username,
password: form.mysql.password,
});
mysqlTest.value = result;
testingMysql.value = false;
return result;
}
async function testRedis(): Promise<{ ok: boolean; msg: string }> {
testingRedis.value = true;
const result = await post('/api/install/test-connection', {
target: 'redis',
host: form.redis.host,
port: form.redis.port,
password: form.redis.password,
});
redisTest.value = result;
testingRedis.value = false;
return result;
}
async function testStorage(): Promise<{ ok: boolean; msg: string }> {
testingStorage.value = true;
const result = await post('/api/install/test-storage', { ...form.storage });
storageTest.value = result;
testingStorage.value = false;
return result;
}
async function onNext(): Promise<void> {
error.value = '';
if (step.value === 0) {
step.value = 1;
return;
}
if (step.value === 1) {
// 进入下一步前自动测试数据库连接,全部通过才放行
advancing.value = true;
try {
const mysqlOk = mysqlTest.value?.ok ? mysqlTest.value : await testMysql();
const redisOk = redisTest.value?.ok ? redisTest.value : await testRedis();
if (mysqlOk.ok && redisOk.ok) step.value = 2;
} finally {
advancing.value = false;
}
return;
}
await submit();
}
onMounted(async () => {
try {
const res = await fetch('/api/install/status');
const body = await parseApiBody(res);
if (!body) throw new Error(tt('emptyResponse'));
installed.value = Boolean(body.data?.installed);
const d = body.data?.defaults || {};
form.mysql.host = d.mysqlHost || form.mysql.host;
form.mysql.port = d.mysqlPort || form.mysql.port;
form.mysql.database = d.mysqlDatabase || form.mysql.database;
form.mysql.username = d.mysqlUser || form.mysql.username;
form.mysql.password = d.mysqlPassword || form.mysql.password;
form.redis.host = d.redisHost || form.redis.host;
form.redis.port = d.redisPort || form.redis.port;
form.redis.password = d.redisPassword || form.redis.password;
} catch (e) {
error.value = (e as Error).message || tt('loadFailed');
} finally {
loading.value = false;
}
});
async function pollUntilReady(): Promise<boolean> {
for (let i = 0; i < 30; i += 1) {
await new Promise((r) => setTimeout(r, 2000));
const st = await fetchInstallStatus();
if (st.installed && !st.needsDevRestart) return true;
}
return false;
}
async function submit(): Promise<void> {
error.value = '';
success.value = '';
submitting.value = true;
try {
// 安装前先确认存储可用
const storageOk = storageTest.value?.ok ? storageTest.value : await testStorage();
if (!storageOk.ok) return;
const res = await fetch('/api/install', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(form),
});
const body = await parseApiBody(res);
// body 为空 = 安装成功后连接被 API 重启掐断(生产 PM2/Docker 或本地 dev.py 自动切换),视为已提交;
// body 有值且非 0 = 安装校验失败(如 MySQL 连接失败),抛出真实原因。
if (body && (!res.ok || body.code !== 0)) {
throw new Error(body.message || tt('failed'));
}
installed.value = true;
success.value = tt('done');
// 统一轮询等待系统就绪:本地 dev.py 会自动停掉安装 API 并以正式模式重启,
// 生产由 PM2/Docker 按重启策略拉起;就绪后进入后台。
if (await pollUntilReady()) {
goAdmin();
} else {
// 超时兜底:多为 --install-mode 调试模式(写临时 .env,不会自动切换)
success.value = tt('doneLocalDev');
}
} catch (e) {
error.value = (e as Error).message;
} finally {
submitting.value = false;
}
}
</script>
<style scoped>
.install-page {
height: 100vh;
overflow-y: auto;
padding: 24px;
display: flex;
justify-content: center;
align-items: flex-start;
background: var(--soft-bg, #eef2f7);
}
.install-card {
width: 100%;
max-width: 720px;
background: var(--card-bg, #fff);
border: 1px solid var(--border, #d9e0ea);
border-radius: 12px;
padding: 20px 24px 24px;
}
.install-head {
display: flex;
justify-content: space-between;
align-items: center;
gap: 16px;
border-bottom: 1px solid var(--border, #e4e9f1);
padding-bottom: 14px;
margin-bottom: 16px;
}
.head-actions {
display: flex;
align-items: center;
gap: 8px;
}
.brand h1 {
margin: 0;
font-size: 20px;
}
.brand p {
margin: 4px 0 0;
font-size: 13px;
color: var(--n-text-color-3, #6a7687);
}
.steps {
margin-bottom: 16px;
}
.box {
border: 1px solid var(--border, #e1e7f0);
border-radius: 10px;
padding: 14px 16px;
}
.box h2 {
margin: 0 0 12px;
font-size: 15px;
padding-bottom: 8px;
border-bottom: 1px solid var(--border, #e9edf4);
}
.box-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
padding-bottom: 8px;
border-bottom: 1px solid var(--border, #e9edf4);
}
.box-head h2 {
margin: 0;
padding: 0;
border: 0;
}
.sub {
margin: 14px 0 10px;
font-size: 13px;
font-weight: 600;
color: var(--n-text-color-2, #526070);
}
.sub-row {
display: flex;
align-items: center;
justify-content: space-between;
margin: 14px 0 10px;
}
.sub-row .sub {
margin: 0;
}
.urls {
margin: 6px 0 2px;
font-size: 12px;
line-height: 1.7;
color: var(--n-text-color-3, #6a7687);
word-break: break-all;
}
.urls span {
display: inline-block;
min-width: 52px;
color: var(--n-text-color-2, #526070);
}
.box label {
display: grid;
grid-template-columns: 132px 1fr;
align-items: center;
gap: 10px;
margin-bottom: 10px;
}
.box label > span {
font-size: 13px;
color: var(--n-text-color-2, #526070);
}
.req {
color: #d03050;
font-style: normal;
margin-left: 2px;
}
.full {
width: 100%;
}
.hint {
margin: 4px 0 0;
font-size: 12px;
line-height: 1.5;
color: var(--n-text-color-3, #6a7687);
white-space: pre-line;
}
.test-result {
margin: 6px 0 0;
font-size: 12px;
line-height: 1.5;
word-break: break-all;
}
.test-result.ok {
color: #18a058;
}
.test-result.fail {
color: #d03050;
}
.problems {
margin: 10px 0 0;
padding: 8px 12px 8px 28px;
border: 1px solid rgba(208, 48, 80, 0.25);
border-radius: 8px;
background: rgba(208, 48, 80, 0.06);
color: #d03050;
font-size: 12px;
line-height: 1.8;
}
.wizard-foot {
display: flex;
align-items: center;
gap: 12px;
margin-top: 16px;
}
.spacer {
flex: 1;
}
.mt {
margin-top: 14px;
}
</style>