Browse Source

修改生成步骤

main
cobb 4 years ago
parent
commit
c47a0a858a
  1. 173
      src/components/HelloWorld.vue
  2. 89
      src/components/preview/previewCodeDialg.vue

173
src/components/HelloWorld.vue

@ -22,6 +22,7 @@
</s-json-schema-editor> </s-json-schema-editor>
</el-col> </el-col>
</el-row> </el-row>
<el-button type="primary" @click="renderer()">生成代码</el-button>
<div style="margin: 20px 0"></div> <div style="margin: 20px 0"></div>
<el-row> <el-row>
<vue-json-editor <vue-json-editor
@ -36,24 +37,34 @@
<br /> <br />
</el-row> </el-row>
<div style="margin: 20px 0"></div> <div style="margin: 20px 0"></div>
<el-row>
<el-input
type="textarea"
:rows="16"
:autosize="{ minRows: 8, maxRows: 16 }"
:key="schemaKey"
v-model="output"
<el-dialog
title="生成代码"
:visible.sync="dialogVisible"
:before-close="handleClose()"
width="80%"
> >
</el-input>
</el-row>
<el-button type="primary" @click="selectText()">复制当前页</el-button>
<el-tabs v-model="activeName">
<el-tab-pane
v-for="(item, key) in output"
:key="key"
:label="key"
:name="key"
>
<div :id="key" class="tab-info" style="height:50vh;background: #fff;padding: 0 20px;overflow-y: scroll;" />
</el-tab-pane>
</el-tabs>
</el-dialog>
</div> </div>
</template> </template>
<script> <script>
import vueJsonEditor from "vue-json-editor"; import vueJsonEditor from "vue-json-editor";
import { Parser } from "sql-ddl-to-json-schema"; import { Parser } from "sql-ddl-to-json-schema";
import { marked } from "marked";
import hljs from "highlight.js";
// import "highlight.js/styles/atelier-plateau-light.css";
// model
function toCamelCase(str) { function toCamelCase(str) {
var regExp = /[-_]\w/gi; var regExp = /[-_]\w/gi;
return titleCase( return titleCase(
@ -88,6 +99,8 @@ export default {
data() { data() {
return { return {
schemaKey: "19878878", schemaKey: "19878878",
activeName: "",
dialogVisible: false,
schema: { schema: {
type: "object", type: "object",
title: "title", title: "title",
@ -102,7 +115,7 @@ export default {
\`goods_id\` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品id', \`goods_id\` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品id',
PRIMARY KEY (\`goods_id\`) USING BTREE PRIMARY KEY (\`goods_id\`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '商品表' ROW_FORMAT = Dynamic;`, ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '商品表' ROW_FORMAT = Dynamic;`,
output: "",
output: {},
}; };
}, },
created() {}, created() {},
@ -111,6 +124,53 @@ export default {
this.schemaKey = Date.now(); this.schemaKey = Date.now();
}, },
methods: { methods: {
handleClose() {
console.log("close");
},
renderer() {
this.dialogVisible = true;
marked.setOptions({
renderer: new marked.Renderer(),
highlight: function (code) {
return hljs.highlightAuto(code).value;
},
pedantic: false,
gfm: true,
tables: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false,
});
for (const key in this.output) {
if (this.activeName === "") {
this.activeName = key;
}
document.getElementById(key).innerHTML = marked(this.output[key]);
}
},
selectText() {
const element = document.getElementById(this.activeName);
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
} else {
alert("none");
}
document.execCommand("copy");
this.$message({
message: "复制成功",
type: "success",
});
},
onJsonChange(value) { onJsonChange(value) {
this.onJsonSave(value); this.onJsonSave(value);
}, },
@ -135,10 +195,84 @@ export default {
}, },
onSchemaChange(v) { onSchemaChange(v) {
this.schema = JSON.stringify(v, null, 2); this.schema = JSON.stringify(v, null, 2);
this.handleObjectOutput(v);
this.output["model"] = this.handleModelOutput(v);
this.output["api"] = this.handleApiOutput(v);
this.output["service"] = this.handleServiceOutput(v);
// this.renderer();
this.schemaKey = Date.now(); this.schemaKey = Date.now();
}, },
handleApiOutput(v) {
const tableName = toCamelCase(v.title);
const apiOutput = `
package api
import (
"Carp/internal/app/ginplus"
"Carp/internal/app/model"
"Carp/internal/app/schema"
"Carp/internal/app/service"
"Carp/pkg/errors"
"Carp/pkg/utils"
"github.com/gin-gonic/gin"
"github.com/google/wire"
"github.com/tealeg/xlsx"
)
var ${tableName}ApiSet = wire.NewSet(wire.Struct(new(${tableName}Api), "*"))
type ${tableName}Api struct {
${tableName}Service *service.${tableName}Service
}
func (l *${tableName}Api) Create${tableName}(c *gin.Context) {
req := &model.${tableName}{}
if err := ginplus.ParseJSON(c, req); err != nil {
ginplus.ResError(c, err)
return
}
err := l.${tableName}Service.Create${tableName}(c.Request.Context(), req)
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResOK(c)
}
func (l *${tableName}Api) Get${tableName}One(c *gin.Context) {
req := &schema.IDResult{}
if err := ginplus.ParseJSON(c, req); err != nil {
ginplus.ResError(c, err)
return
}
resp, err := l.${tableName}Service.Get${tableName}One(c.Request.Context(), req.ID)
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResSuccess(c, resp)
}
func (l *${tableName}Api) List${tableName}(c *gin.Context) {
req := &model.${tableName}ReqParams{}
if err := ginplus.ParseJSON(c, req); err != nil {
ginplus.ResError(c, err)
return
}
resp, err := l.${tableName}Service.Get${tableName}List(c.Request.Context(), req)
if err != nil {
ginplus.ResError(c, err)
return
}
if req.Export {
file := resp.(*xlsx.File)
ginplus.ResExcel(c, file, "导出信息列表")
return
}
ginplus.ResSuccess(c, resp)
}
`;
return apiOutput;
},
handleServiceOutput(v) { handleServiceOutput(v) {
const properties = v.properties; const properties = v.properties;
const tableName = toCamelCase(v.title); const tableName = toCamelCase(v.title);
@ -191,7 +325,7 @@ export default {
return pagination, nil return pagination, nil
} }
func (a *${tableName}Service) Get${tableName}Detail(ctx context.Context, ${primaryLower} int64) (interface{}, error) {
func (a *${tableName}Service) Get${tableName}One(ctx context.Context, ${primaryLower} int64) (interface{}, error) {
var o model.${tableName} var o model.${tableName}
err := a.${tableName}.Q().${primary}(${primaryLower}).One(ctx, &o) err := a.${tableName}.Q().${primary}(${primaryLower}).One(ctx, &o)
if err != nil { if err != nil {
@ -200,7 +334,7 @@ export default {
return o, nil return o, nil
} }
func (a *${tableName}Service) Del${tableName}One(ctx context.Context, ${primaryLower} ...int64) error {
func (a *${tableName}Service) Del${tableName}(ctx context.Context, ${primaryLower} ...int64) error {
var l []model.${tableName} var l []model.${tableName}
_, err := a.${tableName}.Q().${primary}In(${primaryLower}...).List(ctx, &l) _, err := a.${tableName}.Q().${primary}In(${primaryLower}...).List(ctx, &l)
if err != nil { if err != nil {
@ -224,9 +358,9 @@ export default {
} }
`; `;
return serviceOutput
return serviceOutput;
}, },
handleObjectOutput(v) {
handleModelOutput(v) {
const requires = v.required; const requires = v.required;
const properties = v.properties; const properties = v.properties;
const tableName = toCamelCase(v.title); const tableName = toCamelCase(v.title);
@ -308,7 +442,7 @@ export default {
// List // List
func (obj *${tableNameLower}Q) List(ctx context.Context, value interface{}) (int64, error) { func (obj *${tableNameLower}Q) List(ctx context.Context, value interface{}) (int64, error) {
var cnt int64 var cnt int64
err := obj.Query(ctx).Order("update_time desc").Find(value).Offset(-1).Count(&cnt).Error
err := obj.Query(ctx).Order("${primaryLower} desc").Find(value).Offset(-1).Count(&cnt).Error
return cnt, err return cnt, err
} }
@ -528,7 +662,7 @@ export default {
} }
} }
}); });
this.output =
const modelOutput =
enumdefine + enumdefine +
enumUpdate + enumUpdate +
paramsheader + paramsheader +
@ -539,6 +673,7 @@ export default {
filterHeader + filterHeader +
filtercenter + filtercenter +
filtertail; filtertail;
return modelOutput;
}, },
}, },
}; };

89
src/components/preview/previewCodeDialg.vue

@ -1,89 +0,0 @@
<template>
<div class="previewCode">
<el-tabs v-model="activeName">
<el-tab-pane
v-for="(item, key) in previewCode"
:key="key"
:label="key"
:name="key"
>
<div :id="key" class="tab-info" />
</el-tab-pane>
</el-tabs>
</div>
</template>
<script>
import marked from "marked";
import hljs from "highlight.js";
import "highlight.js/styles/atelier-plateau-light.css";
export default {
name: "PreviewCode",
props: {
previewCode: {
type: Object,
default: {},
},
},
data() {
return {
activeName: "",
};
},
mounted() {
marked.setOptions({
renderer: new marked.Renderer(),
highlight: function (code) {
return hljs.highlightAuto(code).value;
},
pedantic: false,
gfm: true,
tables: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false,
});
for (const key in this.previewCode) {
if (this.activeName === "") {
this.activeName = key;
}
document.getElementById(key).innerHTML = marked(props.previewCode[key]);
}
},
methods: {
selectText() {
const element = document.getElementById(this.activeName);
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
} else {
alert("none");
}
},
copy() {
this.$emit("OnCopy", this.activeName);
},
},
};
</script>
<style lang="scss">
.previewCode {
.tab-info {
height: 50vh;
background: #fff;
padding: 0 20px;
overflow-y: scroll;
}
}
</style>
Loading…
Cancel
Save