417 lines
13 KiB
Vue
417 lines
13 KiB
Vue
<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 />
|
|
<n-button v-if="!installed" type="primary" size="large" :loading="submitting" @click="submit">
|
|
{{ submitting ? tt('installing') : tt('start') }}
|
|
</n-button>
|
|
</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>
|
|
<div class="grid">
|
|
<section 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://example.com" /></label>
|
|
<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>
|
|
|
|
<section 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>
|
|
|
|
<section 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>
|
|
</div>
|
|
|
|
<n-alert v-if="error" type="error" class="mt" :title="error" />
|
|
<n-alert v-if="success" type="success" class="mt" :title="success" />
|
|
</template>
|
|
</n-spin>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, onMounted, reactive, ref } 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 baseTrim = computed(() => (form.baseUrl || '').replace(/\/+$/, ''));
|
|
|
|
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: '', realName: tt('defaultAdminName') },
|
|
storage: {
|
|
driver: 'local',
|
|
localDir: './storage',
|
|
cdnBaseUrl: '',
|
|
ossEndpoint: '',
|
|
ossRegion: '',
|
|
ossBucket: '',
|
|
ossAccessKeyId: '',
|
|
ossAccessKeySecret: '',
|
|
},
|
|
});
|
|
|
|
const loading = ref(true);
|
|
const submitting = 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);
|
|
|
|
function goAdmin(): void {
|
|
location.href = '/admin';
|
|
}
|
|
|
|
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 res.json();
|
|
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<void> {
|
|
testingMysql.value = true;
|
|
mysqlTest.value = null;
|
|
mysqlTest.value = 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,
|
|
});
|
|
testingMysql.value = false;
|
|
}
|
|
|
|
async function testRedis(): Promise<void> {
|
|
testingRedis.value = true;
|
|
redisTest.value = null;
|
|
redisTest.value = await post('/api/install/test-connection', {
|
|
target: 'redis',
|
|
host: form.redis.host,
|
|
port: form.redis.port,
|
|
password: form.redis.password,
|
|
});
|
|
testingRedis.value = false;
|
|
}
|
|
|
|
async function testStorage(): Promise<void> {
|
|
testingStorage.value = true;
|
|
storageTest.value = null;
|
|
storageTest.value = await post('/api/install/test-storage', { ...form.storage });
|
|
testingStorage.value = false;
|
|
}
|
|
|
|
onMounted(async () => {
|
|
try {
|
|
const res = await fetch('/api/install/status');
|
|
const body = await res.json();
|
|
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<void> {
|
|
for (let i = 0; i < 30; i += 1) {
|
|
await new Promise((r) => setTimeout(r, 2000));
|
|
try {
|
|
const res = await fetch('/api/install/status');
|
|
const body = await res.json();
|
|
if (body.data?.installed) break;
|
|
} catch {
|
|
// 服务重启期间请求失败属正常
|
|
}
|
|
}
|
|
goAdmin();
|
|
}
|
|
|
|
async function submit(): Promise<void> {
|
|
error.value = '';
|
|
success.value = '';
|
|
submitting.value = true;
|
|
try {
|
|
const res = await fetch('/api/install', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(form),
|
|
});
|
|
const body = await res.json();
|
|
if (!res.ok || body.code !== 0) throw new Error(body.message || tt('failed'));
|
|
success.value = tt('done');
|
|
await pollUntilReady();
|
|
} 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: 1180px;
|
|
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);
|
|
}
|
|
.grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
|
gap: 14px;
|
|
}
|
|
.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);
|
|
}
|
|
.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;
|
|
}
|
|
.mt {
|
|
margin-top: 14px;
|
|
}
|
|
</style>
|