| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package util
- import "strings"
- // 修改mysql数据库对应的go类型
- func formatSqlToVueType(str string) string {
- str = strings.ToLower(str)
- switch str {
- case "varchar":
- 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.AaBbName + "{"
- for j := range table.MColumns {
- column := table.MColumns[j]
- objectList += "\n\t" + column.aaBbName + ": " + column.VueTypeName
- }
- objectList += "\n\n\tconstructor("
- for j := range table.MColumns {
- column := table.MColumns[j]
- objectList += column.aaBbName + ": " + column.VueTypeName + ", "
- }
- objectList += ") {"
- for j := range table.MColumns {
- column := table.MColumns[j]
- objectList += "\n\t\tthis." + column.aaBbName + " = " + column.aaBbName + ";"
- }
- objectList += "\n\t}\n}\n\n"
- //生成api
- apiList += "export function GetBase" + table.AaBbName + "ById(id: number | string) {" +
- "\n return api.GetDataByPath(\"/back/base/" + table.aaBbName + "?id=\" + id, {}, true) as Promise<ResponseData<" + table.AaBbName + ">>;" +
- "\n}" +
- "\n" +
- "\nexport function GetBase" + table.AaBbName + "ListBy" + table.AaBbName + "(" + table.AaBbName + ": " + table.AaBbName + ") {" +
- "\n return api.PostDataByPath(\"/back/base/" + table.aaBbName + "/list\", {" + table.aaBbName + ": " + table.AaBbName + "}, true) as Promise<ResponseData<BaseListVo<" + table.AaBbName + ">[]>>;" +
- "\n}" +
- "\n" +
- "\nexport function GetBase" + table.AaBbName + "ListByIds(ids: number[] | string[]) {" +
- "\n return api.PostDataByPath(\"/back/base/" + table.aaBbName + "/in\", {" +
- "\n ids: ids" +
- "\n }, true) as Promise<ResponseData<" + table.AaBbName + "[]>>;" +
- "\n}" +
- "\n" +
- "\nexport function SaveBase" + table.AaBbName + "(" + table.AaBbName + ": " + table.AaBbName + ") {" +
- "\n return api.PostDataByPath(\"/back/base/" + table.aaBbName + "\", {article: " + table.AaBbName + "}, true) as Promise<ResponseData<" + table.AaBbName + ">>;" +
- "\n}" +
- "\n" +
- "\nexport function UpdateBase" + table.AaBbName + "(" + table.AaBbName + ": " + table.AaBbName + ", id: number | string) {" +
- "\n return api.GetDataByPath(\"/back/base/" + table.aaBbName + "\", {" + table.aaBbName + ": " + table.AaBbName + ", id: id}, true) as Promise<ResponseData<" + table.AaBbName + ">>;" +
- "\n}" +
- "\n" +
- "\nexport function DeleteBase" + table.AaBbName + "ById(id: number | string) {" +
- "\n return api.GetDataByPath(\"/back/base/" + table.aaBbName + "?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)
- }
|