| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package util
- import "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\";\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<ResponseData<" + table.GreatHump + ">>;" +
- "\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<ResponseData<BaseListVo<" + table.GreatHump + ">[]>>;" +
- "\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<ResponseData<" + table.GreatHump + "[]>>;" +
- "\n}" +
- "\n" +
- "\nexport function SaveBase" + table.GreatHump + "(" + table.GreatHump + ": " + table.GreatHump + ") {" +
- "\n return api.PostDataByPath(\"/back/base/" + table.LittleHump + "\", {article: " + table.GreatHump + "}, true) as Promise<ResponseData<" + table.GreatHump + ">>;" +
- "\n}" +
- "\n" +
- "\nexport function UpdateBase" + table.GreatHump + "(" + table.GreatHump + ": " + table.GreatHump + ", id: number | string) {" +
- "\n return api.GetDataByPath(\"/back/base/" + table.LittleHump + "\", {" + table.LittleHump + ": " + table.GreatHump + ", id: id}, true) as Promise<ResponseData<" + table.GreatHump + ">>;" +
- "\n}" +
- "\n" +
- "\nexport function DeleteBase" + table.GreatHump + "ById(id: number | string) {" +
- "\n return api.GetDataByPath(\"/back/base/" + table.LittleHump + "?id=\" + id, {}, true) as Promise<ResponseData<any>>;" +
- "\n}\n\n"
- }
- url := "D:\\project\\study\\kkc\\kkc-shop\\src\\api\\back\\BaseBackApi.ts"
- WriteFile(url, vueFileContent+apiList+objectList)
- }
|