diff --git a/app/common/model/req.go b/app/common/model/req.go index faa87ff..b4dad03 100644 --- a/app/common/model/req.go +++ b/app/common/model/req.go @@ -12,3 +12,9 @@ type PageReq struct { Ctx context.Context OrderBy string //排序方式 } + +type UpFile struct { + Name string `json:"name"` + Url string `json:"url"` + Uid uint64 `json:"uid"` +} diff --git a/template/vm/go/controller.template b/template/vm/go/controller.template index 6592d29..86f8422 100644 --- a/template/vm/go/controller.template +++ b/template/vm/go/controller.template @@ -10,13 +10,12 @@ package api {{$hasGStr:=false}} -{{$gjsonTag:=false}} {{$libTag:=false}} {{range $index,$column:=.table.Columns}} {{if eq $column.HtmlType "checkbox"}} {{$hasGStr = true}} {{else if eq $column.HtmlType "images" "file" "files"}} -{{$gjsonTag = true}} +{{$libTag = true}} {{$libTag = true}} {{end}} {{end}} @@ -36,9 +35,6 @@ import ( {{if $hasGStr}} "github.com/gogf/gf/text/gstr" {{end}} - {{if $gjsonTag}} - "github.com/gogf/gf/encoding/gjson" - {{end}} ) type {{$structName}} struct { @@ -75,8 +71,9 @@ func (c *{{$structName}}) List(r *ghttp.Request) { func (c *{{$structName}}) Add(r *ghttp.Request) { var req *dao.{{.table.ClassName}}AddReq //获取参数 - if err := r.Parse(&req); err != nil { - c.FailJsonExit(r, err.(gvalid.Error).FirstString()) + err := r.Parse(&req) + if err != nil { + c.FailJsonExit(r, err.(gvalid.Error).FirstString()) } {{range $index,$column:= .table.Columns}} {{if eq $column.ColumnName "created_by"}} @@ -90,19 +87,15 @@ func (c *{{$structName}}) Add(r *ghttp.Request) { req.{{$column.GoField}} = "" } {{else if eq $column.HtmlType "images" "file" "files"}} - up{{$column.GoField}}:=gjson.New(req.{{$column.GoField}}) - for _,obj:=range up{{$column.GoField}}.Array(){ - mp := obj.(g.MapStrAny) - var err error - mp["url"],err = library.GetFilesPath(mp["url"].(string)) + for _,obj:=range req.{{$column.GoField}}{ + obj.Url,err = library.GetFilesPath(obj.Url) if err!=nil{ c.FailJsonExit(r, err.Error()) } } - req.{{$column.GoField}} = up{{$column.GoField}}.MustToJsonString() {{end}} {{end}} - err := service.{{.table.ClassName}}.Add(req,r.GetCtx()) + err = service.{{.table.ClassName}}.Add(req,r.GetCtx()) if err != nil { c.FailJsonExit(r, err.Error()) } @@ -124,7 +117,8 @@ func (c *{{$structName}}) Get(r *ghttp.Request) { func (c *{{$structName}}) Edit(r *ghttp.Request) { var req *dao.{{.table.ClassName}}EditReq //获取参数 - if err := r.Parse(&req); err != nil { + err := r.Parse(&req) + if err != nil { c.FailJsonExit(r, err.(gvalid.Error).FirstString()) } {{range $index,$column:= .table.Columns}} @@ -139,19 +133,15 @@ func (c *{{$structName}}) Edit(r *ghttp.Request) { req.{{$column.GoField}} = "" } {{else if eq $column.HtmlType "images" "file" "files"}} - up{{$column.GoField}}:=gjson.New(req.{{$column.GoField}}) - for _,obj:=range up{{$column.GoField}}.Array(){ - mp := obj.(g.MapStrAny) - var err error - mp["url"],err = library.GetFilesPath(mp["url"].(string)) + for _,obj:=range req.{{$column.GoField}}{ + obj.Url,err = library.GetFilesPath(obj.Url) if err!=nil{ c.FailJsonExit(r, err.Error()) } } - req.{{$column.GoField}} = up{{$column.GoField}}.MustToJsonString() {{end}} {{end}} - err := service.{{.table.ClassName}}.Edit(req,r.GetCtx()) + err = service.{{.table.ClassName}}.Edit(req,r.GetCtx()) if err != nil { c.FailJsonExit(r, err.Error()) } diff --git a/template/vm/go/dao.template b/template/vm/go/dao.template index 3ea026b..63d5e24 100644 --- a/template/vm/go/dao.template +++ b/template/vm/go/dao.template @@ -56,7 +56,7 @@ type {{.table.ClassName}}SearchReq struct { type {{.table.ClassName}}AddReq struct { {{range $index, $column := .table.Columns}} {{if and (eq $column.IsInsert "1") (ne $column.IsPk "1")}} - {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}` + {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else if eq $column.HtmlType "images" "file" "files"}}[]*comModel.UpFile{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}` {{end}} {{if eq $column.ColumnName "created_by"}}CreatedBy uint64 {{end}} {{end}} @@ -68,7 +68,7 @@ type {{.table.ClassName}}EditReq struct { {{.table.PkColumn.GoField}} {{.table.PkColumn.GoType}} `p:"{{.table.PkColumn.HtmlField}}" v:"required#主键ID不能为空"` {{range $index, $column := .table.Columns}} {{if eq $column.IsEdit "1"}} - {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}`{{end}} + {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else if eq $column.HtmlType "images" "file" "files"}}[]*comModel.UpFile{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}`{{end}} {{if eq $column.ColumnName "updated_by"}}UpdatedBy uint64 {{end}} {{end}} } @@ -77,10 +77,27 @@ type {{.table.ClassName}}EditReq struct { {{range $index,$column:= .table.Columns}} {{if and (HasSuffix $column.ColumnName "status") (eq $column.IsList "1") }} -// {{$.table.ClassName}}{{$column.GoField}}Req 设置用户状态参数 +// {{$.table.ClassName}}{{$column.GoField}}Req 设置状态参数 type {{$.table.ClassName}}{{$column.GoField}}Req struct { {{$.table.PkColumn.GoField}} {{$.table.PkColumn.GoType}} `p:"{{$.table.PkColumn.HtmlField}}" v:"required#主键ID不能为空"` {{$column.GoField}} {{$column.GoType}} `p:"{{$column.HtmlField}}" v:"required#{{$column.ColumnComment}}不能为空"` } {{end}} -{{end}} \ No newline at end of file +{{end}} + +// {{.table.ClassName}}ListRes 列表返回结果 +type {{.table.ClassName}}ListRes struct { + {{range $index, $column := .table.Columns}} + {{if eq $column.IsList "1" }} + {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else if eq $column.HtmlType "images" "file" "files"}}[]*comModel.UpFile{{else}}{{$column.GoType}}{{end}} `json:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}` + {{end}} + {{end}} +} + +// {{.table.ClassName}}InfoRes 数据返回结果 +type {{.table.ClassName}}InfoRes struct { + {{range $index, $column := .table.Columns}} + {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else if eq $column.HtmlType "images" "file" "files"}}[]*comModel.UpFile{{else}}{{$column.GoType}}{{end}} `json:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}` + {{end}} +} + diff --git a/template/vm/go/service.template b/template/vm/go/service.template index 6c1ad60..bf491f6 100644 --- a/template/vm/go/service.template +++ b/template/vm/go/service.template @@ -9,6 +9,12 @@ package service +{{$gjson:=false}} +{{range $index, $column := .table.Columns}} +{{if eq $column.HtmlType "images" "file" "files"}} +{{$gjson = true}} +{{end}} +{{end}} import ( "context" @@ -19,6 +25,9 @@ import ( "github.com/gogf/gf/util/gconv" "gfast/library" {{end}} + {{if $gjson}} + "github.com/gogf/gf/encoding/gjson" + {{end}} "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/frame/g" ) @@ -47,44 +56,45 @@ var {{.table.ClassName}} = new({{$structName}}) {{end}} {{end}} -// GetList 获取任务列表 -func (s *{{$structName}}) GetList(req *dao.{{.table.ClassName}}SearchReq) (total, page int, list []*model.{{.table.ClassName}}, err error) { - model := dao.{{.table.ClassName}}.Ctx(req.Ctx) - if req != nil { - {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}} +// GetList 获取列表 +func (s *{{$structName}}) GetList(req *dao.{{.table.ClassName}}SearchReq) (total, page int, list []*dao.{{.table.ClassName}}ListRes, err error) { + m := dao.{{.table.ClassName}}.Ctx(req.Ctx) + if req != nil { + {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}} {{if eq $column.QueryType "LIKE"}} - if req.{{$column.GoField}} != "" { - model = model.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" like ?", "%"+req.{{$column.GoField}}+"%") - } {{end}} + if req.{{$column.GoField}} != "" { + m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" like ?", "%"+req.{{$column.GoField}}+"%") + } {{end}} {{if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}} - if req.{{$column.GoField}} != "" { - model = model.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}}) - } + if req.{{$column.GoField}} != "" { + m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}}) + } {{else if and (eq $column.GoType "Time") (eq $column.ColumnName "created_at")}} - if req.BeginTime != "" { - model = model.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >=", req.BeginTime) - } - if req.EndTime != "" { - model = model.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" <", req.EndTime) - } + if req.BeginTime != "" { + m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >=", req.BeginTime) + } + if req.EndTime != "" { + m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" <", req.EndTime) + } {{else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }} - if req.{{$column.GoField}} != "" { - model = model.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}}) - } + if req.{{$column.GoField}} != "" { + m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}}) + } {{end}} {{end}} {{if and (eq $column.QueryType "BETWEEN") (eq $column.ColumnType "datetime") }} - if req.{{$column.GoField}} != nil { - model = model.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >= ? AND "+dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" < ?", req.{{$column.GoField}}, req.{{$column.GoField}}.Add(gtime.D)) - } + if req.{{$column.GoField}} != nil { + m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >= ? AND "+dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" < ?", req.{{$column.GoField}}, req.{{$column.GoField}}.Add(gtime.D)) + } {{end}} - {{end}}{{end}} - } - total, err = model.Count() - if err != nil { - g.Log().Error(err) - err = gerror.New("获取总行数失败") - return - } + {{end}} + {{end}} + } + total, err = m.Count() + if err != nil { + g.Log().Error(err) + err = gerror.New("获取总行数失败") + return + } {{if ne .table.TplCategory "tree"}} if req.PageNum == 0 { req.PageNum = 1 @@ -97,36 +107,78 @@ func (s *{{$structName}}) GetList(req *dao.{{.table.ClassName}}SearchReq) (total if req.OrderBy!=""{ order = req.OrderBy } - err = model.Page(page, req.PageSize).Order(order).Scan(&list) + var res []*model.{{.table.ClassName}} + err = m.Fields(dao.{{.table.ClassName}}ListRes{}).Page(page, req.PageSize).Order(order).Scan(&res) {{else}} order:= "{{$pk}} asc" if req.OrderBy!=""{ order = req.OrderBy } - err = model.Order(order).Scan(&list) + var res []*model.{{.table.ClassName}} + err = m.Fields(dao.{{.table.ClassName}}ListRes{}).Order(order).Scan(&res) {{end}} if err != nil { g.Log().Error(err) err = gerror.New("获取数据失败") } - return + list = make([]*dao.{{.table.ClassName}}ListRes,len(res)) + for k,v:=range res{ + {{range $index, $column := .table.Columns}} + {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}} + {{$column.HtmlField}}:= ([]*comModel.UpFile)(nil) + err = gjson.DecodeTo(v.{{$column.GoField}},&{{$column.HtmlField}}) + if err!=nil{ + return + } + {{end}} + {{end}} + list[k] = &dao.{{.table.ClassName}}ListRes{ + {{range $index, $column := .table.Columns}} + {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}} + {{$column.GoField}} : {{$column.HtmlField}}, + {{else}} + {{$column.GoField}} : v.{{$column.GoField}}, + {{end}} + {{end}} + } + } + return } // GetInfoById 通过id获取 -func (s *{{$structName}}) GetInfoById(id int64,ctx context.Context) (info *model.{{.table.ClassName}}, err error) { - if id == 0 { - err = gerror.New("参数错误") - return - } - err = dao.{{.table.ClassName}}.Ctx(ctx).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, id).Scan(&info) - if err != nil { - g.Log().Error(err) - } - if info == nil || err != nil { - err = gerror.New("获取信息失败") - } - return +func (s *{{$structName}}) GetInfoById(id int64,ctx context.Context) (info *dao.{{.table.ClassName}}InfoRes, err error) { + if id == 0 { + err = gerror.New("参数错误") + return + } + var data *model.{{.table.ClassName}} + err = dao.{{.table.ClassName}}.Ctx(ctx).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, id).Scan(&data) + if err != nil { + g.Log().Error(err) + } + if data == nil || err != nil { + err = gerror.New("获取信息失败") + } + {{range $index, $column := .table.Columns}} + {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}} + {{$column.HtmlField}}:= ([]*comModel.UpFile)(nil) + err = gjson.DecodeTo(data.{{$column.GoField}},&{{$column.HtmlField}}) + if err!=nil{ + return + } + {{end}} + {{end}} + info = &dao.{{.table.ClassName}}InfoRes{ + {{range $index, $column := .table.Columns}} + {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}} + {{$column.GoField}} : {{$column.HtmlField}}, + {{else}} + {{$column.GoField}} : data.{{$column.GoField}}, + {{end}} + {{end}} + } + return } // Add 添加 diff --git a/template/vm/vue/list-vue.template b/template/vm/vue/list-vue.template index d1a7be8..7209a37 100644 --- a/template/vm/vue/list-vue.template +++ b/template/vm/vue/list-vue.template @@ -636,7 +636,7 @@ export default { //单图地址赋值 this.imageUrl{{$column.GoField}} = data.{{$column.HtmlField}} ? this.getUpFileUrl(process.env.VUE_APP_BASE_API,data.{{$column.HtmlField}}) : '' {{else if eq $column.HtmlType "images" "file" "files"}} - data.{{$column.HtmlField}} =data.{{$column.HtmlField}}?JSON.parse(data.{{$column.HtmlField}}):[] + data.{{$column.HtmlField}} =data.{{$column.HtmlField}} || [] {{end}} {{end}} this.form = data; diff --git a/template/vm/vue/tree-vue.template b/template/vm/vue/tree-vue.template index bf4c5fb..e1a07f9 100644 --- a/template/vm/vue/tree-vue.template +++ b/template/vm/vue/tree-vue.template @@ -665,7 +665,7 @@ export default { //单图地址赋值 this.imageUrl{{$column.GoField}} = data.{{$column.HtmlField}} ? this.getUpFileUrl(process.env.VUE_APP_BASE_API,data.{{$column.HtmlField}}) : '' {{else if eq $column.HtmlType "images" "file" "files"}} - data.{{$column.HtmlField}} =data.{{$column.HtmlField}}?JSON.parse(data.{{$column.HtmlField}}):[] + data.{{$column.HtmlField}} =data.{{$column.HtmlField}} || [] {{end}} {{end}} this.form = data;