MysqlCreateVue.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "char":
  10. return "string"
  11. case "int":
  12. return "number"
  13. case "decimal":
  14. return "number"
  15. case "datetime":
  16. return "string"
  17. case "longblob":
  18. return "string"
  19. default:
  20. panic("未定义的类型:" + str)
  21. }
  22. }
  23. func MysqlToVueApi() {
  24. vueFileContent := "import api, {ResponseData} from \"../api.ts\";" +
  25. "\nimport {BaseListVo} from \"../detail/DetailApi.ts\";\n\n"
  26. var objectList = ""
  27. var apiList = ""
  28. //生成结构体
  29. for i := range MTables {
  30. table := MTables[i]
  31. //添加名字
  32. objectList += "export class " + table.GreatHump + "{"
  33. ct := "\n\tstatic Create(){\n return new " + table.GreatHump + "("
  34. for j := range table.MColumns {
  35. column := table.MColumns[j]
  36. objectList += "\n\t" + column.LittleHump + ": " + column.VueTypeName
  37. ct += "null,"
  38. //switch column.VueTypeName {
  39. //case "number":
  40. // ct += "0,"
  41. // break
  42. //case "string":
  43. // ct += "\"\","
  44. //}
  45. }
  46. objectList += ct + ");\n }"
  47. objectList += "\n\n\tconstructor("
  48. for j := range table.MColumns {
  49. column := table.MColumns[j]
  50. objectList += column.LittleHump + ": " + column.VueTypeName + ", "
  51. }
  52. objectList += ") {"
  53. for j := range table.MColumns {
  54. column := table.MColumns[j]
  55. objectList += "\n\t\tthis." + column.LittleHump + " = " + column.LittleHump + ";"
  56. }
  57. objectList += "\n\t}\n}\n\n"
  58. //生成api
  59. apiList += "export function GetBase" + table.GreatHump + "ById(id: number | string) {" +
  60. "\n return api.GetDataByPath(\"/back/base/" + table.LittleHump + "?id=\" + id, {}, true) as Promise<ResponseData<" + table.GreatHump + ">>;" +
  61. "\n}" +
  62. "\n" +
  63. //"\nexport function GetBase" + table.GreatHump + "ListBy" + table.GreatHump + "(" + table.GreatHump + ": " + table.GreatHump + ", pageNum = 1, pageSize = 10) {" +
  64. "\nexport function GetBase" + table.GreatHump + "ListBy" + table.GreatHump + "(param: " + table.GreatHump + ", pageNum = 1, pageSize = 10) {" +
  65. "\n return api.PostDataByPath(\"/back/base/" + table.LittleHump + "/list\", {param: param,pageNum: pageNum,pageSize: pageSize}, true) as Promise<ResponseData<BaseListVo<" + table.GreatHump + ">>>;" +
  66. "\n}" +
  67. "\n" +
  68. "\nexport function GetBase" + table.GreatHump + "ListByIds(ids: number[] | string[]) {" +
  69. "\n return api.PostDataByPath(\"/back/base/" + table.LittleHump + "/in\", {" +
  70. "\n ids: ids" +
  71. "\n }, true) as Promise<ResponseData<" + table.GreatHump + "[]>>;" +
  72. "\n}" +
  73. "\n" +
  74. "\nexport function SaveBase" + table.GreatHump + "(" + table.GreatHump + ": " + table.GreatHump + ") {" +
  75. "\n return api.PostDataByPath(\"/back/base/" + table.LittleHump + "\", {article: " + table.GreatHump + "}, true) as Promise<ResponseData<" + table.GreatHump + ">>;" +
  76. "\n}" +
  77. "\n" +
  78. "\nexport function UpdateBase" + table.GreatHump + "(" + table.GreatHump + ": " + table.GreatHump + ", id: number | string) {" +
  79. "\n return api.GetDataByPath(\"/back/base/" + table.LittleHump + "\", {" + table.LittleHump + ": " + table.GreatHump + ", id: id}, true) as Promise<ResponseData<" + table.GreatHump + ">>;" +
  80. "\n}" +
  81. "\n" +
  82. "\nexport function DeleteBase" + table.GreatHump + "ById(id: number | string) {" +
  83. "\n return api.GetDataByPath(\"/back/base/" + table.LittleHump + "?id=\" + id, {}, true) as Promise<ResponseData<any>>;" +
  84. "\n}\n\n"
  85. }
  86. //url := "D:\\project\\study\\kkc\\kkc-shop\\src\\api\\back\\BaseBackApi.ts"
  87. WriteFile(GetVueApiUrl(), vueFileContent+apiList+objectList)
  88. }
  89. // MysqlToVueView 生成vue的view表单
  90. func MysqlToVueView() {
  91. }