package util import ( "sort" "strings" ) // 修改mysql数据库对应的go类型 func formatSqlToVueType(str string) string { str = strings.ToLower(str) switch str { case "varchar": return "string" case "char": return "string" case "int": return "number" case "decimal": return "number" case "datetime": return "string" case "longblob": return "string" default: panic("未定义的类型:" + str) } } func MysqlToVueApi() { vueFileContent := "import api, {ResponseData} from \"../api.ts\";" + "\nimport {BaseListVo} from \"../detail/DetailApi.ts\";\n\n" var objectList = "" var apiList = "" //生成结构体 for i := range MTables { table := MTables[i] //添加名字 objectList += "export class " + table.GreatHump + "{" ct := "\n\tstatic Create(){\n return new " + table.GreatHump + "(" for j := range table.MColumns { column := table.MColumns[j] objectList += "\n\t" + column.LittleHump + ": " + column.VueTypeName ct += "null," //switch column.VueTypeName { //case "number": // ct += "0," // break //case "string": // ct += "\"\"," //} } objectList += ct + ");\n }" objectList += "\n\n\tconstructor(" for j := range table.MColumns { column := table.MColumns[j] objectList += column.LittleHump + ": " + column.VueTypeName + ", " } objectList += ") {" for j := range table.MColumns { column := table.MColumns[j] objectList += "\n\t\tthis." + column.LittleHump + " = " + column.LittleHump + ";" } objectList += "\n\t}\n}\n\n" //生成api apiList += "export function GetBase" + table.GreatHump + "ById(id: number | string) {" + "\n return api.GetDataByPath(\"/back/base/" + table.LittleHump + "?id=\" + id, {}, true) as Promise>;" + "\n}" + "\n" + //"\nexport function GetBase" + table.GreatHump + "ListBy" + table.GreatHump + "(" + table.GreatHump + ": " + table.GreatHump + ", pageNum = 1, pageSize = 10) {" + "\nexport function GetBase" + table.GreatHump + "ListBy" + table.GreatHump + "(param: " + table.GreatHump + ", pageNum = 1, pageSize = 10) {" + "\n return api.PostDataByPath(\"/back/base/" + table.LittleHump + "/list\", {param: param,pageNum: pageNum,pageSize: pageSize}, true) as Promise>>;" + "\n}" + "\n" + "\nexport function GetBase" + table.GreatHump + "ListByIds(ids: number[] | string[]) {" + "\n return api.PostDataByPath(\"/back/base/" + table.LittleHump + "/in\", {" + "\n ids: ids" + "\n }, true) as Promise>;" + "\n}" + "\n" + "\nexport function SaveBase" + table.GreatHump + "(" + table.GreatHump + ": " + table.GreatHump + ") {" + "\n return api.PostDataByPath(\"/back/base/" + table.LittleHump + "\", {" + table.LittleHump + ": " + table.GreatHump + "}, true) as Promise>;" + "\n}" + "\n" + "\nexport function UpdateBase" + table.GreatHump + "(" + table.GreatHump + ": " + table.GreatHump + ", id: number | string) {" + "\n return api.PutDataByPath(\"/back/base/" + table.LittleHump + "\", {" + table.LittleHump + ": " + table.GreatHump + ", id: id}, true) as Promise>;" + "\n}" + "\n" + "\nexport function DeleteBase" + table.GreatHump + "ById(id: number | string) {" + "\n return api.DeleteDataByPath(\"/back/base/" + table.LittleHump + "?id=\" + id, {}, true) as Promise>;" + "\n}\n\n" } //url := "D:\\project\\study\\kkc\\kkc-shop\\src\\api\\back\\BaseBackApi.ts" WriteFile(GetVueApiUrl(), vueFileContent+apiList+objectList) } // MysqlToVueView 生成vue的view表单 // 查询类型,搁置 // 输入框 input // 文本域 textarea // 下拉框 Select // 复选框 Checkbox // 单选框 Radio // 时间选择 Time // 图片上传 image // 文件上传 File // 富文本 Editor func CreateVue() { //sql表格名 for _, gTable := range GTables { //跳过 一些表 if gTable.GenTable.Name == "DictData" || gTable.GenTable.Name == "DictType" || gTable.GenTable.Name == "GenTableColumn" || gTable.GenTable.Name == "GenTable" { continue } view := MysqlToVueView(gTable, gTable.GenTableColumn) WriteFile(ProjectGoCreateUrl+"file\\"+gTable.GenTable.Name+"View.vue", view) } } func MysqlToVueView(table GTable, columns []GenTableColumn) string { vueContent := "" + "\n" + // =============================js代码部分 "\n" } return vueContent } // 输入框 input // 文本域 textarea // 下拉框 Select // 复选框 Checkbox // 单选框 Radio // 时间选择 Time // 图片上传 Image // 文件上传 File // 富文本 Editor func elTableColumn(name, value, typeName string, dictType string) string { if typeName == "input" || typeName == "textarea" || typeName == "select" || typeName == "radio" || typeName == "checkbox" || typeName == "time" || typeName == "text" { if dictType == "" { return "\n " } else { return "\n " + "\n " + "\n " } } if typeName == "image" { return "\n " + "\n " + "\n " } return "\n " } func elDialog(name, value, typeName, dictType string) string { typeName = strings.ToLower(typeName) if typeName == "input" { return "\n " + "\n " + "\n " } else if typeName == "number" { return "\n " + "\n " + "\n " } else if typeName == "textarea" { return "\n " + "\n " + "\n " } else if typeName == "select" { return "\n " + "\n " + "\n " + "\n " + "\n " } else if typeName == "radio" { if dictType == "" { return "\n " + " " + "\n " + "\n {{ item[1] }}" + "\n " + "\n " + "\n " } else { return "\n " + "\n " + "\n " + "\n {{item.dictLabel}}" + "\n " + "\n " + "\n " } } else if typeName == "checkbox" { return "\n " + "\n " + "\n " + "\n " + "\n " } else if typeName == "time" { return "\n " + " " + "\n " } else if typeName == "image" { return "\n " + " " + "\n " } else if typeName == "file" { } else if typeName == "editor" { return "\n " + " " + "\n " } else if typeName == "text" { return "\n " + " {{dialogForm." + value + "}}" + "\n " } //默认的就显示文本 return "\n " + " {{dialogForm." + value + "}}" + "\n " }