Browse Source

修复model生成带有前缀的问题

main
cobb 3 years ago
parent
commit
36fb20bf5f
  1. 6
      main.go
  2. 23
      src/components/HelloWorld.vue

6
main.go

@ -513,8 +513,9 @@ func main() {
r.Use(Cors()) r.Use(Cors())
r.POST("/sql", func(c *gin.Context) { r.POST("/sql", func(c *gin.Context) {
var req struct { var req struct {
Content string `json:"content"`
Camel bool `json:"camel"`
Content string `json:"content"`
Camel bool `json:"camel"`
TablePrefix string `json:"table_prefix"` // 去除表前缀
} }
err := c.BindJSON(&req) err := c.BindJSON(&req)
if err != nil { if err != nil {
@ -525,6 +526,7 @@ func main() {
opts = append(opts, WithGormType()) opts = append(opts, WithGormType())
opts = append(opts, WithJsonTag()) opts = append(opts, WithJsonTag())
opts = append(opts, WithZhTag()) opts = append(opts, WithZhTag())
opts = append(opts, WithTablePrefix(req.TablePrefix))
if req.Camel { if req.Camel {
opts = append(opts, WithCamel()) opts = append(opts, WithCamel())
} }

23
src/components/HelloWorld.vue

@ -107,9 +107,12 @@ function lowerCase(str) {
function rmPrefix(str) { function rmPrefix(str) {
if (str.indexOf("_") > -1) { if (str.indexOf("_") > -1) {
var n = str.indexOf("_"); var n = str.indexOf("_");
return str.slice(n + 1, str.length);
return {
prefix: str.slice(0, n + 1),
suffix: str.slice(n + 1, str.length),
};
} }
return str;
return { prefix: "", suffix: str };
} }
function typeMap(str) { function typeMap(str) {
@ -225,7 +228,7 @@ export default {
this.schemaKey = Date.now(); this.schemaKey = Date.now();
}, },
handleRouteOutput(v) { handleRouteOutput(v) {
const title = rmPrefix(v.title);
const title = rmPrefix(v.title).suffix;
const tableName = toCamelCase(title); const tableName = toCamelCase(title);
const lowTableName = title; const lowTableName = title;
const Output = ` const Output = `
@ -240,7 +243,7 @@ export default {
return Output; return Output;
}, },
handleJsApi(v) { handleJsApi(v) {
const title = rmPrefix(v.title);
const title = rmPrefix(v.title).suffix;
const tableName = toCamelCase(title); const tableName = toCamelCase(title);
const lowTableName = title; const lowTableName = title;
const Output = ` const Output = `
@ -289,7 +292,7 @@ export default {
return Output; return Output;
}, },
handleApiOutput(v) { handleApiOutput(v) {
const title = rmPrefix(v.title);
const title = rmPrefix(v.title).suffix;
const tableName = toCamelCase(title); const tableName = toCamelCase(title);
const apiOutput = ` const apiOutput = `
package api package api
@ -378,7 +381,7 @@ export default {
return apiOutput; return apiOutput;
}, },
handleServiceOutput(v) { handleServiceOutput(v) {
const title = rmPrefix(v.title);
const title = rmPrefix(v.title).suffix;
const tableName = toCamelCase(title); const tableName = toCamelCase(title);
const properties = v.properties; const properties = v.properties;
var primary = ``; var primary = ``;
@ -455,7 +458,7 @@ export default {
`; `;
return serviceOutput; return serviceOutput;
}, },
getMyStruct() {
getMyStruct(tablePrefix) {
var oReq = new XMLHttpRequest(); var oReq = new XMLHttpRequest();
oReq.open("POST", "/sql", false); // oReq.open("POST", "/sql", false); //
oReq.setRequestHeader("Content-type", "application/json"); oReq.setRequestHeader("Content-type", "application/json");
@ -464,17 +467,19 @@ export default {
JSON.stringify({ JSON.stringify({
content: this.sqlinput, content: this.sqlinput,
camel: !this.isUnderScoreCase, camel: !this.isUnderScoreCase,
table_prefix: tablePrefix,
}) })
); //JSON ); //JSON
var result = oReq.responseText; // var result = oReq.responseText; //
return result; return result;
}, },
handleModelOutput(v) { handleModelOutput(v) {
var struct = this.getMyStruct();
const tablePrefix = rmPrefix(v.title).prefix;
var struct = this.getMyStruct(tablePrefix);
console.log("struct", struct); console.log("struct", struct);
const requires = v.required; const requires = v.required;
const properties = v.properties; const properties = v.properties;
const title = rmPrefix(v.title);
const title = rmPrefix(v.title).suffix;
const tableName = toCamelCase(title); const tableName = toCamelCase(title);
const tableNameLower = lowerCase(tableName); const tableNameLower = lowerCase(tableName);
var primary = ``; var primary = ``;

Loading…
Cancel
Save