Files
yg-point-mall/src/views/tenant/initialize.vue

539 lines
13 KiB
Vue
Raw Normal View History

2023-06-13 17:21:00 +08:00
<template>
<div class="app-container">
2023-06-27 16:42:55 +08:00
<el-steps
2023-06-30 17:15:25 +08:00
:active="activeStep"
:process-status="
2023-06-27 16:42:55 +08:00
currentStepStatus === 0
? 'error'
: currentStepStatus === 1
? 'process'
: 'success'
"
2023-06-30 17:15:25 +08:00
align-center
class="init-steps"
finish-status="success"
2023-06-27 16:42:55 +08:00
>
2023-06-30 17:15:25 +08:00
<el-step title="开始" />
<el-step title="检查" />
<el-step title="步骤2" />
<el-step title="步骤3" />
2023-06-13 17:21:00 +08:00
</el-steps>
2023-06-29 16:32:52 +08:00
<!-- 错误提示 -->
<el-alert
2023-06-30 17:15:25 +08:00
v-if="currentStepStatus === 0"
:closable="false"
:type="
2023-06-29 16:32:52 +08:00
currentStepStatus === 0
? 'error'
: currentStepStatus === 1
? 'info'
: 'success'
"
2023-06-30 17:15:25 +08:00
show-icon
>{{
2023-06-30 16:00:28 +08:00
activeStep === 1
2023-06-30 17:15:25 +08:00
? "错误码: " +
2023-06-29 16:32:52 +08:00
tenantInitCheckResult.result +
", 错误信息: " +
tenantInitCheckResult.msg
2023-06-30 17:15:25 +08:00
: activeStep === 2
? "错误码: " +
createTablesResult.result +
", 错误信息: " +
createTablesResult.msg
: activeStep === 3
? "错误码: " +
initDataResult.result +
", 错误信息: " +
initDataResult.msg
: ""
2023-06-29 16:32:52 +08:00
}}
</el-alert>
<!-- 数据库配置信息 -->
<el-row
2023-06-30 17:15:25 +08:00
v-if="Object.keys(tenantInitCheckResult).length"
style="margin: 36px 36px 0"
2023-06-29 16:32:52 +08:00
>
<el-col :span="12">
<div class="data-card init-check-base-data">
<div class="card-title">
<span>基础数据</span>
</div>
<div class="card-content">
<div class="card-content-item">
<span class="card-content-item-title">租户名称:</span>
<span class="card-content-item-value">{{
2023-06-30 17:15:25 +08:00
tenantInitCheckResult.tenantName
}}</span>
2023-06-29 16:32:52 +08:00
</div>
<!--模式-->
<div class="card-content-item">
<span class="card-content-item-title">模式:</span>
<span class="card-content-item-value">{{
2023-06-30 17:15:25 +08:00
tenantModeDict.find(
(item) => item.value === tenantInitCheckResult.mode
)?.label
}}</span>
2023-06-29 16:32:52 +08:00
</div>
</div>
</div>
</el-col>
<el-col :span="12">
<div
2023-06-30 17:15:25 +08:00
v-if="tenantInitCheckResult.datasource"
class="data-card init-check-base-data"
style="margin-left: 24px"
2023-06-29 16:32:52 +08:00
>
<div class="card-title">
<span>数据源数据</span>
</div>
<div class="card-content">
<div class="card-content-item">
<span class="card-content-item-title">数据库IP:</span>
<span class="card-content-item-value">{{
2023-06-30 17:15:25 +08:00
tenantInitCheckResult.datasource.ip
}}</span>
2023-06-29 16:32:52 +08:00
</div>
<!-- 端口-->
<div class="card-content-item">
<span class="card-content-item-title">端口:</span>
<span class="card-content-item-value">{{
2023-06-30 17:15:25 +08:00
tenantInitCheckResult.datasource.port
}}</span>
2023-06-29 16:32:52 +08:00
</div>
<!--模式-->
<div class="card-content-item">
<span class="card-content-item-title">类型:</span>
<span class="card-content-item-value">{{
2023-06-30 17:15:25 +08:00
tenantInitCheckResult.datasource.type
}}</span>
2023-06-29 16:32:52 +08:00
</div>
<!-- 数据库名称-->
<div class="card-content-item">
<span class="card-content-item-title">数据库名称:</span>
<span class="card-content-item-value">{{
2023-06-30 17:15:25 +08:00
tenantInitCheckResult.datasource.dbName
}}</span>
2023-06-29 16:32:52 +08:00
</div>
<!-- 用户名-->
<div class="card-content-item">
<span class="card-content-item-title">用户名:</span>
<span class="card-content-item-value">{{
2023-06-30 17:15:25 +08:00
tenantInitCheckResult.datasource.username
}}</span>
2023-06-29 16:32:52 +08:00
</div>
<!-- 密码-->
<div class="card-content-item">
<span class="card-content-item-title">密码:</span>
<span class="card-content-item-value">{{
2023-06-30 17:15:25 +08:00
tenantInitCheckResult.datasource.password
}}</span>
2023-06-29 16:32:52 +08:00
</div>
</div>
</div>
</el-col>
</el-row>
<!-- spinner -->
2023-06-29 14:57:23 +08:00
<el-row v-if="loading" justify="center">
<el-col :span="1.5">
<div class="btn-loading">
<el-icon size="80">
2023-06-30 17:15:25 +08:00
<refresh />
2023-06-29 14:57:23 +08:00
</el-icon>
</div>
</el-col>
</el-row>
2023-06-29 16:32:52 +08:00
2023-06-29 14:57:23 +08:00
<div v-else class="steps-content">
2023-06-27 16:42:55 +08:00
<div v-if="activeStep === 0" class="start-step">
<el-row justify="center">
<el-col :span="1.5">
2023-06-29 16:32:52 +08:00
<div class="circle-button" @click="initTenant">开始</div>
2023-06-27 16:42:55 +08:00
</el-col>
</el-row>
</div>
<div v-else-if="activeStep === 1">
2023-06-29 16:32:52 +08:00
<el-row justify="center">
2023-06-27 16:42:55 +08:00
<el-col :span="1.5">
<div
2023-06-30 17:15:25 +08:00
v-if="currentStepStatus === 0"
class="circle-button error"
@click="router.push({ path: '/tenant/tenant' })"
2023-06-27 16:42:55 +08:00
>
关闭
</div>
<div
2023-06-30 17:15:25 +08:00
v-else-if="currentStepStatus === 2"
class="circle-button"
@click="initTenant"
2023-06-27 16:42:55 +08:00
>
2023-06-27 17:33:06 +08:00
创建库
2023-06-27 16:42:55 +08:00
</div>
</el-col>
</el-row>
</div>
<template v-else-if="activeStep === 2">
2023-06-29 16:32:52 +08:00
<el-row justify="center">
2023-06-27 16:42:55 +08:00
<el-col :span="1.5">
2023-06-29 11:19:07 +08:00
<div
2023-06-30 17:15:25 +08:00
v-if="currentStepStatus === 0"
class="circle-button error"
@click="router.push({ path: '/tenant/tenant' })"
2023-06-29 11:19:07 +08:00
>
关闭
</div>
2023-06-29 14:57:23 +08:00
<div
2023-06-30 17:15:25 +08:00
v-else-if="currentStepStatus === 2"
class="circle-button"
@click="initTenant"
2023-06-29 14:57:23 +08:00
>
<span>导入数据</span>
</div>
2023-06-27 16:42:55 +08:00
</el-col>
</el-row>
</template>
<template v-else-if="activeStep === 3">
2023-06-29 16:32:52 +08:00
<el-row justify="center">
2023-06-27 16:42:55 +08:00
<el-col :span="1.5">
2023-06-29 11:19:07 +08:00
<div
2023-06-30 17:15:25 +08:00
v-if="currentStepStatus === 0"
class="circle-button error"
@click="router.push({ path: '/tenant/tenant' })"
2023-06-29 11:19:07 +08:00
>
关闭
</div>
2023-06-29 14:57:23 +08:00
<div
2023-06-30 17:15:25 +08:00
v-else-if="currentStepStatus === 2"
class="circle-button finish"
@click="router.push({ path: '/tenant/tenant' })"
2023-06-29 14:57:23 +08:00
>
完成
2023-06-29 11:19:07 +08:00
</div>
2023-06-27 16:42:55 +08:00
</el-col>
</el-row>
</template>
</div>
2023-06-13 17:21:00 +08:00
</div>
</template>
2023-06-27 16:42:55 +08:00
<script setup>
2023-06-30 17:15:25 +08:00
import { onMounted, reactive, ref, toRefs, watchEffect } from "vue";
import { check, createTables, initData } from "@/api/tenant/init";
import { useRoute, useRouter } from "vue-router";
import { tenantModeDict } from "@/constant/dict";
import { Refresh } from "@element-plus/icons-vue";
2023-06-13 17:21:00 +08:00
2023-06-27 16:42:55 +08:00
const route = useRoute();
const router = useRouter();
const activeStep = ref(0);
const currentStepStatus = ref(1); // 0: error 1: process 2: finish
const tenantId = ref(undefined);
2023-06-29 14:57:23 +08:00
const loading = ref(false);
2023-06-27 16:42:55 +08:00
if (route.query.id) {
tenantId.value = route.query.id;
} else {
2023-06-30 17:15:25 +08:00
router.push({ path: "/tenant/tenant" });
2023-06-27 16:42:55 +08:00
}
const data = reactive({
tenantInitCheckResult: {},
2023-06-27 17:33:06 +08:00
createTablesResult: {},
2023-06-29 11:19:07 +08:00
initDataResult: {},
2023-06-27 16:42:55 +08:00
});
2023-06-30 17:15:25 +08:00
const { tenantInitCheckResult, createTablesResult, initDataResult } =
toRefs(data);
2023-06-29 16:32:52 +08:00
const initTenant = async () => {
loading.value = true;
const promiseList = [tenantInitCheck, createDatabase, importData];
for (const promise of promiseList.slice(activeStep.value)) {
try {
await promise();
} catch (e) {
loading.value = false;
break;
}
}
loading.value = false;
};
2023-06-27 16:42:55 +08:00
const tenantInitCheck = () => {
activeStep.value = 1;
currentStepStatus.value = 1;
2023-06-29 16:32:52 +08:00
return check(tenantId.value)
2023-06-30 17:15:25 +08:00
.catch(() => {
currentStepStatus.value = 2;
activeStep.value = 0;
throw new Error("租户初始化检查失败");
})
.then((resp) => {
tenantInitCheckResult.value = resp;
if (resp.result === 200) {
2023-06-29 14:57:23 +08:00
currentStepStatus.value = 2;
2023-06-30 17:15:25 +08:00
} else {
currentStepStatus.value = 0;
2023-06-30 15:40:14 +08:00
throw new Error("租户初始化检查失败");
2023-06-30 17:15:25 +08:00
}
});
2023-06-27 16:42:55 +08:00
};
2023-06-27 17:33:06 +08:00
2023-06-29 11:19:07 +08:00
// 第二步: 创建数据库
2023-06-27 17:33:06 +08:00
const createDatabase = () => {
2023-06-29 16:32:52 +08:00
// loading.value = true;
2023-06-27 17:33:06 +08:00
activeStep.value = 2;
currentStepStatus.value = 1;
2023-06-29 16:32:52 +08:00
return createTables(tenantId.value)
2023-06-30 17:15:25 +08:00
.catch(() => {
currentStepStatus.value = 2;
activeStep.value = 1;
throw new Error("创建数据库失败");
})
.then((resp) => {
createTablesResult.value = resp;
if (resp.result === 200) {
2023-06-29 14:57:23 +08:00
currentStepStatus.value = 2;
2023-06-30 17:15:25 +08:00
} else {
currentStepStatus.value = 0;
2023-06-30 15:40:14 +08:00
throw new Error("创建数据库失败");
2023-06-30 17:15:25 +08:00
}
});
2023-06-29 11:19:07 +08:00
};
// 第三步,导入数据
const importData = () => {
2023-06-29 16:32:52 +08:00
// loading.value = true;
2023-06-29 11:19:07 +08:00
activeStep.value = 3;
currentStepStatus.value = 1;
2023-06-29 16:32:52 +08:00
return initData(tenantId.value)
2023-06-30 17:15:25 +08:00
.catch(() => {
currentStepStatus.value = 2;
activeStep.value = 2;
throw new Error("导入数据失败");
})
.then((resp) => {
initDataResult.value = resp;
if (resp.result === 200) {
2023-06-29 14:57:23 +08:00
currentStepStatus.value = 2;
2023-06-30 17:15:25 +08:00
} else {
currentStepStatus.value = 0;
2023-06-30 15:40:14 +08:00
throw new Error("导入数据失败");
2023-06-30 17:15:25 +08:00
}
});
2023-06-27 17:33:06 +08:00
};
2023-06-27 16:42:55 +08:00
onMounted(() => {
watchEffect(() => {
2023-06-29 14:13:09 +08:00
let progressLineWidth =
2023-06-30 17:15:25 +08:00
(activeStep.value - 1) * 100 +
Math.abs(currentStepStatus.value - 1) * 30 +
70;
2023-06-29 14:57:23 +08:00
progressLineWidth = progressLineWidth < 100 ? 0 : progressLineWidth;
2023-06-27 16:42:55 +08:00
let progressLineColor;
if (currentStepStatus.value === 0) {
2023-06-27 17:33:06 +08:00
const percent = ((activeStep.value - 1) / activeStep.value) * 100;
2023-06-27 16:42:55 +08:00
progressLineColor = `linear-gradient(to right, #67c23a ${percent}%, #f56c6c ${percent}%)`;
// 进度条颜色
} else {
progressLineColor = `linear-gradient(to right, #67c23a ${100}%, #f56c6c ${100}%)`;
}
document
2023-06-30 17:15:25 +08:00
.querySelector(".app-container")
.style.setProperty("--progress-line-color", progressLineColor);
2023-06-27 16:42:55 +08:00
document
2023-06-30 17:15:25 +08:00
.querySelector(".app-container")
.style.setProperty("--progress-line-width", progressLineWidth + "%");
2023-06-27 16:42:55 +08:00
});
});
</script>
<style lang="scss" scoped>
:deep(.init-steps) {
margin-top: 120px;
.el-step__icon {
z-index: 9999;
}
.el-step:first-child {
.el-step__head.is-success {
.el-step__line::before {
content: "";
display: block;
position: absolute;
left: 12px;
top: 0;
height: 2px;
width: var(--progress-line-width);
z-index: 99;
background: var(--progress-line-color);
2023-06-29 14:13:09 +08:00
transition: all 1s linear;
2023-06-27 16:42:55 +08:00
}
.el-step__line::after {
content: "";
display: block;
position: absolute;
left: calc(var(--progress-line-width) - 7px);
top: -6px;
width: 14px;
height: 14px;
2023-06-29 14:13:09 +08:00
transition: all 1s linear;
2023-06-27 16:42:55 +08:00
border-radius: 50%;
z-index: 99;
background: var(--progress-line-color);
}
}
}
}
.steps-content {
margin-top: 80px;
padding: 0 60px;
.start-step {
}
.check-step {
display: flex;
}
}
2023-06-29 14:57:23 +08:00
.btn-loading {
margin-top: 80px;
width: 100px;
height: 100px;
border-radius: 50%;
background-color: #409eff;
display: flex;
justify-content: center;
align-items: center;
.el-icon {
animation: spin 1s linear infinite;
color: white;
}
}
2023-06-27 16:42:55 +08:00
.circle-button {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: #409eff;
color: #fff;
text-align: center;
line-height: 100px;
cursor: pointer;
box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.2);
user-select: none;
&:hover {
background-color: #66b1ff;
}
// 按钮按下时
&:active {
background-color: #3a8ee6;
box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.2) inset;
}
&.error {
background-color: #f56c6c;
&:hover {
background-color: #f78989;
}
&:active {
background-color: #f56c6c;
box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.2) inset;
}
}
&.finish {
background-color: #67c23a;
&:hover {
background-color: #85ce61;
}
&:active {
background-color: #67c23a;
box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.2) inset;
}
}
}
.data-card {
width: 100%;
height: 100%;
background-color: #fff;
border-radius: 4px;
border: 1px solid #ebeef5;
min-height: 120px;
position: relative;
padding: 16px;
.card-title {
position: absolute;
top: -10px;
left: 10px;
background-color: #fff;
//禁止选中
user-select: none;
// 水平方向padding 10px
padding: 0 5px;
font-size: 12px;
// 浅灰色字体
color: #606266;
border-radius: 12px;
}
.card-content {
display: flex;
flex-direction: column;
justify-content: start;
height: 100%;
.card-content-item {
display: flex;
justify-content: space-between;
&:not(:last-child) {
margin-bottom: 10px;
}
}
.card-content-item-title {
font-size: 14px;
font-weight: 500;
color: #606266;
}
.card-content-item-value {
font-size: 14px;
color: #909399;
}
}
}
2023-06-29 14:57:23 +08:00
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
2023-06-29 16:32:52 +08:00
:deep(.el-step__head.is-success) {
transition: all 1s linear;
}
:deep(.el-step__title) {
font-size: 20px;
}
2023-06-27 16:42:55 +08:00
</style>