songzhibin97
3 years ago
25 changed files with 559 additions and 37 deletions
-
91server/api/v1/sys_auto_code.go
-
2server/global/global.go
-
6server/go.mod
-
2server/initialize/gorm.go
-
6server/initialize/redis.go
-
8server/model/request/sys_autocode.go
-
2server/model/sys_auto_code.go
-
18server/model/sys_autocode_history.go
-
14server/router/sys_auto_code.go
-
8server/service/jwt_black_list.go
-
4server/service/sys_api.go
-
81server/service/sys_auto_code.go
-
87server/service/sys_autocode_history.go
-
1server/service/sys_initdb.go
-
4server/source/api.go
-
2server/source/authorities_menus.go
-
4server/source/casbin.go
-
2server/source/menu.go
-
4server/utils/file_operations.go
-
41server/utils/injectionCode.go
-
3web/Dockerfile
-
2web/public/index.html
-
32web/src/api/autoCode.js
-
13web/src/view/systemTools/autoCode/index.vue
-
159web/src/view/systemTools/autoCodeAdmin/index.vue
@ -0,0 +1,18 @@ |
|||
package model |
|||
|
|||
import "gin-vue-admin/global" |
|||
|
|||
// 自动迁移代码记录,用于回滚,重放使用
|
|||
|
|||
type SysAutoCodeHistory struct { |
|||
global.GVA_MODEL |
|||
TableName string `json:"tableName"` |
|||
RequestMeta string `gorm:"type:text" json:"requestMeta,omitempty"` // 前端传入的结构化信息
|
|||
AutoCodePath string `gorm:"type:text" json:"autoCodePath,omitempty"` // 其他meta信息 path;path
|
|||
InjectionMeta string `gorm:"type:text" json:"injectionMeta,omitempty"` // 注入的内容 RouterPath@functionName@RouterString;
|
|||
StructName string `json:"structName"` |
|||
StructCNName string `json:"structCNName"` |
|||
ApiIDs string `json:"apiIDs,omitempty"` // api表注册内容
|
|||
Flag int `json:"flag"` // 表示对应状态 0 代表创建, 1 代表回滚 ...
|
|||
|
|||
} |
@ -0,0 +1,87 @@ |
|||
package service |
|||
|
|||
import ( |
|||
"gin-vue-admin/global" |
|||
"gin-vue-admin/model" |
|||
"gin-vue-admin/model/request" |
|||
"gin-vue-admin/utils" |
|||
"strings" |
|||
|
|||
"go.uber.org/zap" |
|||
) |
|||
|
|||
// CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
|
|||
func CreateAutoCodeHistory(meta, structName, structCNName, autoCodePath string, injectionMeta string, tableName string, apiIds string) error { |
|||
return global.GVA_DB.Create(&model.SysAutoCodeHistory{ |
|||
RequestMeta: meta, |
|||
AutoCodePath: autoCodePath, |
|||
InjectionMeta: injectionMeta, |
|||
StructName: structName, |
|||
StructCNName: structCNName, |
|||
TableName: tableName, |
|||
ApiIDs: apiIds, |
|||
}).Error |
|||
} |
|||
|
|||
// RollBack 回滚
|
|||
func RollBack(id uint) error { |
|||
md := model.SysAutoCodeHistory{} |
|||
if err := global.GVA_DB.First(&md, id).Error; err != nil { |
|||
return err |
|||
} |
|||
// 清除API表
|
|||
err := DeleteApiByIds(strings.Split(md.ApiIDs, ";")) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err)) |
|||
} |
|||
// 获取全部表名
|
|||
err, dbNames := GetTables(global.GVA_CONFIG.Mysql.Dbname) |
|||
if err != nil { |
|||
global.GVA_LOG.Error("ClearTag GetTables:", zap.Error(err)) |
|||
} |
|||
// 删除表
|
|||
for _, name := range dbNames { |
|||
if strings.Contains(strings.ToUpper(strings.Replace(name.TableName, "_", "", -1)), strings.ToUpper(md.TableName)) { |
|||
// 删除表
|
|||
if err = DropTable(name.TableName); err != nil { |
|||
global.GVA_LOG.Error("ClearTag DropTable:", zap.Error(err)) |
|||
|
|||
} |
|||
} |
|||
} |
|||
// 删除文件
|
|||
for _, path := range strings.Split(md.AutoCodePath, ";") { |
|||
_ = utils.DeLFile(path) |
|||
} |
|||
// 清除注入
|
|||
for _, v := range strings.Split(md.InjectionMeta, ";") { |
|||
// RouterPath@functionName@RouterString
|
|||
meta := strings.Split(v, "@") |
|||
if len(meta) == 3 { |
|||
_ = utils.AutoClearCode(meta[0], meta[2]) |
|||
} |
|||
} |
|||
md.Flag = 1 |
|||
return global.GVA_DB.Save(&md).Error |
|||
} |
|||
|
|||
func GetMeta(id uint) (string, error) { |
|||
var meta string |
|||
return meta, global.GVA_DB.Model(model.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error |
|||
} |
|||
|
|||
// GetSysHistoryPage 获取系统历史数据
|
|||
func GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) { |
|||
limit := info.PageSize |
|||
offset := info.PageSize * (info.Page - 1) |
|||
db := global.GVA_DB |
|||
var fileLists []model.SysAutoCodeHistory |
|||
err = db.Find(&fileLists).Count(&total).Error |
|||
err = db.Limit(limit).Offset(offset).Order("updated_at desc").Select("id,created_at,updated_at,struct_name,struct_cn_name,flag,table_name").Find(&fileLists).Error |
|||
return err, fileLists, total |
|||
} |
|||
|
|||
// DeletePage 删除历史数据
|
|||
func DeletePage(id uint) error { |
|||
return global.GVA_DB.Delete(model.SysAutoCodeHistory{}, id).Error |
|||
} |
@ -0,0 +1,159 @@ |
|||
<template> |
|||
<div> |
|||
<div class="search-term"> |
|||
<el-form :inline="true" :model="searchInfo" class="demo-form-inline"> |
|||
<el-form-item label="表名"> |
|||
<el-input v-model="searchInfo.tableName" placeholder="表名" /> |
|||
</el-form-item> |
|||
<el-form-item label="结构体名称"> |
|||
<el-input v-model="searchInfo.structName" placeholder="结构体名称" /> |
|||
</el-form-item> |
|||
<el-form-item> |
|||
<el-button size="mini" type="primary" icon="el-icon-plus" @click="goAutoCode(null)">新增</el-button> |
|||
</el-form-item> |
|||
</el-form> |
|||
</div> |
|||
<el-table :data="tableData" border stripe> |
|||
<el-table-column |
|||
type="selection" |
|||
width="55" |
|||
/> |
|||
<el-table-column label="id" width="60" prop="ID" /> |
|||
<el-table-column label="日期" width="180"> |
|||
<template slot-scope="scope">{{ scope.row.CreatedAt|formatDate }}</template> |
|||
</el-table-column> |
|||
<el-table-column label="结构体名" min-width="150" prop="structName" /> |
|||
<el-table-column label="结构体描述" min-width="150" prop="structCNName" /> |
|||
<el-table-column label="表名称" min-width="150" prop="tableName" /> |
|||
<el-table-column label="回滚标记" min-width="150" prop="flag"> |
|||
<template slot-scope="scope"> |
|||
<el-tag |
|||
v-if="scope.row.flag" |
|||
type="danger" |
|||
size="mini" |
|||
effect="dark" |
|||
> |
|||
已回滚 |
|||
</el-tag> |
|||
<el-tag |
|||
v-else |
|||
size="mini" |
|||
type="success" |
|||
effect="dark" |
|||
> |
|||
未回滚 |
|||
</el-tag> |
|||
</template> |
|||
</el-table-column> |
|||
<el-table-column label="操作" min-width="180"> |
|||
<template slot-scope="scope"> |
|||
<div> |
|||
<el-button size="mini" type="primary" :disabled="scope.row.flag === 1" @click="rollback(scope.row)">回滚</el-button> |
|||
<el-button size="mini" type="success" @click="goAutoCode(scope.row)">复用</el-button> |
|||
<el-button size="mini" type="danger" @click="deleteRow(scope.row)">删除</el-button> |
|||
</div> |
|||
</template> |
|||
</el-table-column> |
|||
</el-table> |
|||
<el-pagination |
|||
:current-page="page" |
|||
:page-size="pageSize" |
|||
:page-sizes="[10, 30, 50, 100]" |
|||
:style="{float:'right',padding:'20px'}" |
|||
:total="total" |
|||
layout="total, sizes, prev, pager, next, jumper" |
|||
@current-change="handleCurrentChange" |
|||
@size-change="handleSizeChange" |
|||
/> |
|||
|
|||
</div> |
|||
</template> |
|||
|
|||
<script> |
|||
// 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成 条件搜索时候 请把条件安好后台定制的结构体字段 放到 this.searchInfo 中即可实现条件搜索 |
|||
import { getSysHistory, rollback, delSysHistory } from '@/api/autoCode.js' |
|||
import { formatTimeToStr } from '@/utils/date' |
|||
import infoList from '@/mixins/infoList' |
|||
|
|||
export default { |
|||
name: 'Api', |
|||
filters: { |
|||
formatDate: function(time) { |
|||
if (time !== null && time !== '') { |
|||
var date = new Date(time) |
|||
return formatTimeToStr(date, 'yyyy-MM-dd hh:mm:ss') |
|||
} else { |
|||
return '' |
|||
} |
|||
}, |
|||
formatBoolean: function(bool) { |
|||
if (bool !== null) { |
|||
return bool ? '是' : '否' |
|||
} else { |
|||
return '' |
|||
} |
|||
} |
|||
}, |
|||
mixins: [infoList], |
|||
data() { |
|||
return { |
|||
listApi: getSysHistory |
|||
} |
|||
}, |
|||
created() { |
|||
this.getTableData() |
|||
}, |
|||
methods: { |
|||
async deleteRow(row) { |
|||
this.$confirm('此操作将删除本历史, 是否继续?', '提示', { |
|||
confirmButtonText: '确定', |
|||
cancelButtonText: '取消', |
|||
type: 'warning' |
|||
}).then(async() => { |
|||
const res = await delSysHistory({ id: Number(row.ID) }) |
|||
if (res.code === 0) { |
|||
this.$message.success('删除成功') |
|||
this.getTableData() |
|||
} |
|||
}) |
|||
}, |
|||
async rollback(row) { |
|||
this.$confirm('此操作将删除自动创建的文件和api, 是否继续?', '提示', { |
|||
confirmButtonText: '确定', |
|||
cancelButtonText: '取消', |
|||
type: 'warning' |
|||
}).then(async() => { |
|||
const res = await rollback({ id: Number(row.ID) }) |
|||
if (res.code === 0) { |
|||
this.$message.success('回滚成功') |
|||
this.getTableData() |
|||
} |
|||
}) |
|||
}, |
|||
goAutoCode(row) { |
|||
if (row) { |
|||
this.$router.push({ name: 'autoCodeEdit', params: { |
|||
id: row.ID |
|||
}}) |
|||
} else { |
|||
this.$router.push({ name: 'autoCode' }) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
</script> |
|||
|
|||
<style scoped lang="scss"> |
|||
.button-box { |
|||
padding: 10px 20px; |
|||
.el-button { |
|||
float: right; |
|||
} |
|||
} |
|||
.el-tag--mini { |
|||
margin-left: 5px; |
|||
} |
|||
.warning { |
|||
color: #dc143c; |
|||
} |
|||
</style> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue