MysqlCreateVue.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package util
  2. import "strings"
  3. // 修改mysql数据库对应的go类型
  4. func formatSqlToVueType(str string) string {
  5. str = strings.ToLower(str)
  6. switch str {
  7. case "varchar":
  8. return "string"
  9. case "int":
  10. return "number"
  11. case "decimal":
  12. return "number"
  13. case "datetime":
  14. return "string"
  15. case "longblob":
  16. return "string"
  17. default:
  18. panic("未定义的类型:" + str)
  19. }
  20. }
  21. func MysqlToVueApi() {
  22. vueFileContent := "import api, {ResponseData} from \"../api.ts\";\n\n"
  23. var objectList = ""
  24. var apiList = ""
  25. //生成结构体
  26. for i := range MTables {
  27. table := MTables[i]
  28. //添加名字
  29. objectList += "export class " + table.AaBbName + "{"
  30. for j := range table.MColumns {
  31. column := table.MColumns[j]
  32. objectList += "\n\t" + column.aaBbName + ": " + column.VueTypeName
  33. }
  34. objectList += "\n\n\tconstructor("
  35. for j := range table.MColumns {
  36. column := table.MColumns[j]
  37. objectList += column.aaBbName + ": " + column.VueTypeName + ", "
  38. }
  39. objectList += ") {"
  40. for j := range table.MColumns {
  41. column := table.MColumns[j]
  42. objectList += "\n\t\tthis." + column.aaBbName + " = " + column.aaBbName + ";"
  43. }
  44. objectList += "\n\t}\n}\n\n"
  45. //生成api
  46. apiList += "export function GetBase" + table.AaBbName + "ById(id: number | string) {" +
  47. "\n return api.GetDataByPath(\"/back/base/" + table.aaBbName + "?id=\" + id, {}, true) as Promise<ResponseData<" + table.AaBbName + ">>;" +
  48. "\n}" +
  49. "\n" +
  50. "\nexport function GetBase" + table.AaBbName + "ListBy" + table.AaBbName + "(" + table.AaBbName + ": " + table.AaBbName + ") {" +
  51. "\n return api.PostDataByPath(\"/back/base/" + table.aaBbName + "/list\", {" + table.aaBbName + ": " + table.AaBbName + "}, true) as Promise<ResponseData<BaseListVo<" + table.AaBbName + ">[]>>;" +
  52. "\n}" +
  53. "\n" +
  54. "\nexport function GetBase" + table.AaBbName + "ListByIds(ids: number[] | string[]) {" +
  55. "\n return api.PostDataByPath(\"/back/base/" + table.aaBbName + "/in\", {" +
  56. "\n ids: ids" +
  57. "\n }, true) as Promise<ResponseData<" + table.AaBbName + "[]>>;" +
  58. "\n}" +
  59. "\n" +
  60. "\nexport function SaveBase" + table.AaBbName + "(" + table.AaBbName + ": " + table.AaBbName + ") {" +
  61. "\n return api.PostDataByPath(\"/back/base/" + table.aaBbName + "\", {article: " + table.AaBbName + "}, true) as Promise<ResponseData<" + table.AaBbName + ">>;" +
  62. "\n}" +
  63. "\n" +
  64. "\nexport function UpdateBase" + table.AaBbName + "(" + table.AaBbName + ": " + table.AaBbName + ", id: number | string) {" +
  65. "\n return api.GetDataByPath(\"/back/base/" + table.aaBbName + "\", {" + table.aaBbName + ": " + table.AaBbName + ", id: id}, true) as Promise<ResponseData<" + table.AaBbName + ">>;" +
  66. "\n}" +
  67. "\n" +
  68. "\nexport function DeleteBase" + table.AaBbName + "ById(id: number | string) {" +
  69. "\n return api.GetDataByPath(\"/back/base/" + table.aaBbName + "?id=\" + id, {}, true) as Promise<ResponseData<any>>;" +
  70. "\n}\n\n"
  71. }
  72. url := "D:\\project\\study\\kkc\\kkc-shop\\src\\api\\back\\BaseBackApi.ts"
  73. WriteFile(url, vueFileContent+apiList+objectList)
  74. }