first commit

This commit is contained in:
JaguarJack
2022-12-05 23:01:12 +08:00
commit 0024080c28
322 changed files with 27698 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<template>
<div>
<Schema v-if="active === 1" @next="next" @prev="prev" />
<Structure v-if="active === 2" @next="next" @prev="prev" />
</div>
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue'
import Schema from './steps/schema.vue'
import Structure from './steps/structure.vue'
import { useSchemaStore } from './store'
const schemaStore = useSchemaStore()
const active = ref(1)
const next = () => {
if (active.value++ >= 2) {
active.value = 2
}
}
const prev = () => {
if (active.value-- === 1) {
active.value = 1
}
}
const emit = defineEmits(['close'])
watch(() => schemaStore.getFinished, function (value){
if (value) {
emit('close')
}
})
</script>

View File

@@ -0,0 +1,120 @@
<template>
<div>
<div class="w-full min-h-0 bg-white dark:bg-regal-dark pl-5 pt-5 pr-5 rounded-lg">
<el-form :inline="true">
<el-form-item label="模块名称">
<el-input v-model="query.module" name="module" clearable />
</el-form-item>
<el-form-item label="Schema 名称">
<el-input v-model="query.name" name="name" clearable />
</el-form-item>
<el-form-item>
<el-button type="primary" @click="search()">
<Icon name="magnifying-glass" class="w-4 mr-1 -ml-1" />
搜索
</el-button>
<el-button @click="reset()">
<Icon name="arrow-path" class="w-4 mr-1 -ml-1" />
重置
</el-button>
</el-form-item>
</el-form>
</div>
<div class="pl-2 pr-2 bg-white dark:bg-regal-dark rounded-lg mt-4">
<div class="pt-5 pl-2">
<Add @click="add(null)" />
</div>
<el-table :data="tableData" class="mt-3" v-loading="loading">
<el-table-column prop="module" label="所属模块" />
<el-table-column prop="name" label="schema 名称" />
<el-table-column prop="columns" label="字段">
<template #default="scope">
<el-button size="small" type="success" @click="show(scope.row.id)"><Icon name="eye" class="w-3 mr-1" /> 查看</el-button>
</template>
</el-table-column>
<el-table-column prop="is_soft_delete" label="?软删">
<template #default="scope">
<el-tag v-if="scope.row.is_soft_delete"></el-tag>
<el-tag type="danger" v-else></el-tag>
</template>
</el-table-column>
<el-table-column prop="created_at" label="创建时间" />
<el-table-column label="操作" width="250">
<template #default="scope">
<router-link :to="'/develop/generate/' + scope.row.id">
<el-button type="warning" size="small"><Icon name="wrench-screwdriver" class="w-3 mr-1" /> 生成代码</el-button>
</router-link>
<Destroy @click="destroy(api, scope.row.id)" class="ml-2" />
</template>
</el-table-column>
</el-table>
<div class="pt-2 pb-2 flex justify-end">
<el-pagination
background
layout="total,sizes,prev, pager,next"
:current-page="query.page"
:page-size="query.limit"
@current-change="changePage"
@size-change="changeLimit"
:total="total"
:page-sizes="[10, 20, 30, 50]"
/>
</div>
</div>
<!-- schema 创建 -->
<Dialog v-model="visible" :title="$t('generate.schema.title')" width="650px" destroy-on-close>
<Create @close="close" :primary="id" :api="api" />
</Dialog>
<!-- schema 表结构 -->
<Dialog v-model="showVisible" title="Schema 结构" width="650px" destroy-on-close>
<Show :id="id" :api="api" />
</Dialog>
</div>
</template>
<script lang="ts" setup>
import { computed, onMounted, ref, watch } from 'vue'
import Create from './create.vue'
import Show from './show.vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
const visible = ref<boolean>(false)
const showVisible = ref<boolean>(false)
const id = ref<number>()
const api = 'schema'
const title = ref<string>('')
const { data, query, search, reset, changePage, changeLimit, loading } = useGetList(api)
const { destroy, isDeleted } = useDestroy('确认删除吗? 将会删除数据库的 Schema请提前做好备份一旦删除将无法恢复!')
onMounted(() => search())
const tableData = computed(() => data.value?.data)
const total = computed(() => data.value?.total)
const close = () => {
visible.value = false
reset()
}
const add = () => {
visible.value = true
}
const show = primaryId => {
showVisible.value = true
id.value = primaryId
}
watch(isDeleted, function () {
isDeleted.value = false
reset()
})
</script>

View File

@@ -0,0 +1,38 @@
<template>
<el-table :data="data" class="mt-3" v-loading="loading">
<el-table-column prop="name" label="字段名称" />
<el-table-column prop="type" label="类型" />
<el-table-column prop="nullable" label="nullable">
<template #default="scope">
<el-tag v-if="scope.row.nullable"></el-tag>
<el-tag type="danger" v-else></el-tag>
</template>
</el-table-column>
<el-table-column prop="default" label="默认值">
<template #default="scope"> </template>
</el-table-column>
<el-table-column prop="comment" label="注释" />
</el-table>
</template>
<script lang="ts" setup>
import { useShow } from '/admin/composables/curd/useShow'
import { onMounted } from 'vue'
import { ref } from 'vue'
const props = defineProps({
id: {
type: Number,
required: true,
},
})
const data = ref<Array<object>>()
onMounted(() => {
useShow('schema', props.id).then(r => {
data.value = r.data.columns
})
})
</script>
<style scoped></style>

View File

@@ -0,0 +1,107 @@
<template>
<div class="w-full sm:w-[90%] mx-auto">
<el-form :model="schema" ref="form" label-width="80px">
<el-form-item
:label="$t('generate.code.module.name')"
prop="module"
:rules="[
{
// required: true,
message: $t('generate.code.module.verify'),
},
]"
>
<Select v-model="schema.module" clearable :placeholder="$t('generate.code.module.placeholder')" api="modules" class="w-full" filterable />
</el-form-item>
<el-form-item
:label="$t('generate.schema.name')"
prop="name"
:rules="[
{
required: true,
message: $t('generate.schema.name_verify'),
},
]"
>
<el-input v-model="schema.name" clearable />
</el-form-item>
<el-form-item
:label="$t('generate.schema.engine.name')"
prop="engine"
:rules="[
{
required: true,
message: $t('generate.schema.engine.verify'),
},
]"
>
<el-select class="w-full" v-model="schema.engine" :placeholder="$t('generate.schema.engine.placeholder')" clearable>
<el-option v-for="engine in engines" :key="engine.value" :label="engine.label" :value="engine.value" />
</el-select>
</el-form-item>
<el-form-item :label="$t('generate.schema.default_field.name')">
<el-checkbox v-model="schema.created_at" :label="$t('generate.schema.default_field.created_at')" size="large" />
<el-checkbox v-model="schema.updated_at" :label="$t('generate.schema.default_field.updated_at')" size="large" />
<el-checkbox v-model="schema.creator_id" :label="$t('generate.schema.default_field.creator')" size="large" />
<el-checkbox v-model="schema.deleted_at" :label="$t('generate.schema.default_field.delete_at')" size="large" />
</el-form-item>
<el-form-item
:label="$t('generate.schema.comment.name')"
prop="comment"
:rules="[
{
required: true,
message: $t('generate.schema.comment.verify'),
},
]"
>
<el-input v-model="schema.comment" type="textarea" />
</el-form-item>
</el-form>
<div class="w-full sm:w-96 justify-between mx-auto pl-24 mt-4">
<el-button class="mt-5" @click="$emit('prev')">{{ $t('system.prev') }}</el-button>
<el-button class="mt-5" type="primary" @click="submitCreateTable(form)">{{ $t('system.next') }}</el-button>
</div>
</div>
</template>
<script lang="ts" setup>
import { reactive, computed, ref, unref } from 'vue'
import { useSchemaStore } from '../store'
import type { FormInstance } from 'element-plus'
const schemaStore = useSchemaStore()
schemaStore.start()
const emits = defineEmits(['prev', 'next'])
const schema = reactive(schemaStore.getSchema)
const form = ref<FormInstance>()
const submitCreateTable = (formEl: FormInstance | undefined) => {
if (!formEl) return
formEl.validate(valid => {
if (valid) {
emits('next')
schemaStore.setSchema(unref(schema))
} else {
return false
}
})
}
const engines = computed(() => {
return [
{
value: 'InnoDB',
label: 'InnoDB',
},
{
value: 'MyISAM',
label: 'MyISAM',
},
{
value: 'Memory',
label: 'Memory',
},
]
})
</script>

View File

@@ -0,0 +1,231 @@
<template>
<div>
<el-table :data="structures" class="draggable">
<el-table-column prop="field" :label="$t('generate.schema.structure.field_name.name')" />
<el-table-column prop="type" :label="$t('generate.schema.structure.type.name')">
<template #default="scope">
<el-tag type="success">{{ scope.row.type }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="nullable" :label="$t('generate.schema.structure.nullable')" width="90px">
<template #default="scope">
<el-tag v-if="scope.row.nullable">{{ $t('system.yes') }}</el-tag>
<el-tag v-else type="info">{{ $t('system.no') }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="default" :label="$t('generate.schema.structure.default')" />
<!--<el-table-column prop="comment" label="注释" />-->
<el-table-column prop="id" :label="$t('generate.schema.structure.operate')" width="120px">
<template #default="scope">
<el-button type="primary" :icon="Edit" @click="updateField(scope.row.id)" size="small" />
<el-button type="danger" :icon="Delete" @click="deleteField(scope.row.id)" size="small" />
</template>
</el-table-column>
</el-table>
<div class="flex justify-end mt-4">
<el-button type="success" :icon="Plus" @click="addField">{{ $t('system.add') }}</el-button>
</div>
<div class="w-full sm:w-96 justify-between mx-auto pl-24 mt-2">
<el-button class="mt-5" @click="emits('prev')">{{ $t('system.prev') }}</el-button>
<el-button class="mt-5" type="primary" @click="next">{{ $t('system.confirm') }}</el-button>
</div>
<Dialog v-model="visible" :title="$t('system.add')">
<el-form :model="structure" status-icon label-width="120px" ref="form">
<el-form-item
:label="$t('generate.schema.structure.field_name.name')"
prop="field"
:rules="[
{
required: true,
message: $t('generate.schema.structure.field_name.verify'),
},
]"
>
<el-input v-model="structure.field" />
</el-form-item>
<div class="flex justify-between">
<el-form-item
class="w-full"
:label="$t('generate.schema.structure.type.name')"
prop="type"
:rules="[
{
required: true,
message: $t('generate.schema.structure.type.verify'),
},
]"
>
<el-select v-model="structure.type" :placeholder="$t('generate.schema.structure.type.placeholder')" filterable class="w-full">
<el-option v-for="item in types" :key="item" :label="item" :value="item" />
</el-select>
</el-form-item>
</div>
<el-form-item :label="$t('generate.schema.structure.length')" prop="length">
<el-input-number v-model="structure.length" :min="0" />
</el-form-item>
<div class="flex justify-between">
<el-form-item label="nullable" prop="nullable">
<el-switch v-model="structure.nullable" inline-prompt :active-text="$t('system.yes')" :inactive-text="$t('system.no')" />
</el-form-item>
<el-form-item :label="$t('generate.schema.structure.default')" prop="default" v-if="!structure.nullable">
<el-input v-model="structure.default" />
</el-form-item>
</div>
<el-form-item :label="$t('generate.schema.structure.unique')" prop="unique">
<el-switch v-model="structure.unique" inline-prompt :active-text="$t('system.yes')" :inactive-text="$t('system.no')" />
</el-form-item>
<el-form-item :label="$t('generate.schema.structure.comment')" prop="comment">
<el-input v-model="structure.comment" text />
</el-form-item>
<div class="flex justify-end">
<el-button type="primary" @click="submitStructure(form)">{{ $t('system.confirm') }}</el-button>
</div>
</el-form>
</Dialog>
</div>
</template>
<script lang="ts" setup>
import { computed, onMounted, Ref, ref } from 'vue'
import { useSchemaStore, Structure } from '../store'
import { Delete, Plus, Edit } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import Message from '/admin/support/message'
import http from '/admin/support/http'
import { Code } from '/admin/enum/app'
import Sortable from 'sortablejs'
const schemaStore = useSchemaStore()
const emits = defineEmits(['prev', 'next'])
const visible = ref(false)
const structures = computed(() => {
return schemaStore.getStructures
})
const structure: Ref<Structure> = ref(schemaStore.initStructure())
// structure
const addField = async () => {
await form.value?.clearValidate()
visible.value = true
}
const updateField = (id: number) => {
visible.value = true
schemaStore.getStructures.forEach(s => {
if (s.id === id) {
structure.value = s
}
})
}
onMounted(() => {
const tbody = document.querySelector('.draggable .el-table__body-wrapper tbody')
const structures = schemaStore.getStructures
Sortable.create(tbody, {
draggable: 'tr',
onEnd({ newIndex, oldIndex }) {
const newStructures = []
const s = structures.splice(oldIndex, newIndex - oldIndex)
console.log(s, structures, oldIndex, newIndex)
s.concat(structures).forEach(item => {
newStructures.push(item)
})
schemaStore.setStructures(newStructures)
// console.log(structure)
// structures[newIndex] = structures[oldIndex]
// structures[oldIndex] = temp
},
})
})
const form = ref<FormInstance>()
const submitStructure = (formEl: FormInstance | undefined) => {
if (!formEl) return
formEl.validate(valid => {
if (valid) {
visible.value = !visible.value
schemaStore.addStructure(structure.value)
structure.value = schemaStore.initStructure()
} else {
return false
}
})
}
const deleteField = (id: number) => {
schemaStore.filterStructures(id)
}
const next = () => {
if (schemaStore.getStructures.length < 1) {
Message.error('请先填写表结构数据')
} else {
http.post('schema', schemaStore.$state).then(r => {
if (r.data.code == Code.SUCCESS) {
Message.success('创建成功')
schemaStore.finished()
}
})
}
}
const types: string[] = [
'id',
'smallIncrements',
'mediumIncrements',
'increments',
'smallInteger',
'integer',
'bigIncrements',
'bigInteger',
'mediumInteger',
'unsignedInteger',
'unsignedMediumInteger',
'unsignedSmallInteger',
'unsignedTinyInteger',
'string',
'text',
'binary',
'boolean',
'char',
'dateTimeTz',
'dateTime',
'date',
'decimal',
'double',
'float',
'geometryCollection',
'geometry',
'ipAddress',
'json',
'jsonb',
'lineString',
'longText',
'macAddress',
'mediumText',
'multiLineString',
'multiPoint',
'multiPolygon',
'nullableMorphs',
'nullableTimestamps',
'nullableUuidMorphs',
'point',
'polygon',
'timeTz',
'time',
'timestampTz',
'timestamp',
'timestampsTz',
'timestamps',
'tinyIncrements',
'tinyInteger',
'tinyText',
'unsignedDecimal',
'uuid',
'year',
]
</script>

View File

@@ -0,0 +1,146 @@
import { defineStore } from 'pinia'
/**
* 表信息
*/
export interface Schema {
module: string
name: string
comment: string
engine: string
charset: string
collaction: string
created_at: boolean
creator_id: boolean
updated_at: boolean
deleted_at: boolean
}
/**
* 表结构信息
*/
export interface Structure {
id: number
field: string
length: number
type: string
nullable: boolean
unique: boolean
default: number | string
comment: string
}
/**
* generate
*/
interface CreateSchema {
schema: Schema
structures: Structure[]
is_finished: boolean
}
/**
* useSchemaStore
*/
export const useSchemaStore = defineStore('schemaStore', {
state(): CreateSchema {
return {
// schema
schema: Object.assign({
module: '',
name: '',
comment: '',
engine: 'InnoDB',
charset: 'utf8mb4',
collection: 'utf8mb4_unicode_ci',
created_at: true,
creator_id: true,
updated_at: true,
deleted_at: true,
}),
// structures
structures: [] as Structure[],
// is finished
is_finished: false,
}
},
// store getters
getters: {
getSchema(): Schema {
return this.schema
},
getStructures(): Structure[] {
return this.structures
},
getFinished(): boolean {
return this.is_finished
},
},
// store actions
actions: {
// set schema
setSchema(schema: Schema): void {
this.schema = schema
},
setStructures(structures: Array<Structure>): void {
this.structures = structures
},
// add structure
addStructure(structure: Structure): void {
if (structure.id) {
this.structures = this.structures.filter((s: Structure) => {
if (s.id === structure.id) {
s = structure
}
return s
})
} else {
structure.id = this.structures.length + 1
this.structures.push(structure)
}
},
// filter structures
filterStructures(id: number) {
this.structures = this.structures.filter((s: Structure) => {
return !(s.id === id)
})
},
// init structure
initStructure(): Structure {
return Object.assign({
id: 0,
field: '',
label: '',
type: '',
length: 0,
nullable: false,
unique: false,
default: '',
comment: '',
})
},
/**
* finished
*/
finished(): void {
this.$reset()
this.is_finished = true
},
/**
* unfinished
*/
start(): void {
this.is_finished = false
},
},
})