Skip to content

Commit e58c15d

Browse files
committed
提交模板,
1 parent 35740d7 commit e58c15d

File tree

9 files changed

+361
-15
lines changed

9 files changed

+361
-15
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
/test/
33
/resource/autocode/
44
/resource/font/
5-
/resource/temp/
65
/resource/conf.yaml
76
.idea/

main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ func main() {
4949
logger.Log.WithFields(logrus.Fields{"data": ""}).Info("项目配置")
5050
})
5151
fileItem := fyne.NewMenuItem("模板文件", func() {
52-
53-
fmt.Println("模板文件")
52+
fmt.Println()
53+
pwd, _ := os.Getwd()
54+
u, _ := url.Parse(pwd + "/resource/temp/")
55+
_ = a.OpenURL(u)
5456
})
5557

5658
helpMenu := fyne.NewMenu("帮助",

page/autocode.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"code/gen/util/conf"
88
"code/gen/util/logger"
99
"errors"
10-
"fmt"
1110
"fyne.io/fyne/v2"
1211
"fyne.io/fyne/v2/container"
1312
"fyne.io/fyne/v2/dialog"
@@ -64,7 +63,6 @@ func AutoScreen(win fyne.Window) fyne.CanvasObject {
6463
})
6564
tableList.OnSelected = func(id widget.TableCellID) {
6665
if len(tableListData) > id.Row {
67-
fmt.Println(len(tableListData), id.Row)
6866
tableListData[id.Row].Checked = !tableListData[id.Row].Checked
6967
tableList.Unselect(id)
7068
tableList.Refresh()

resource/temp/api.go.tpl

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package api
2+
3+
import (
4+
"strconv"
5+
"github.com/gin-gonic/gin"
6+
"{{.ModName}}/util/global"
7+
"{{.ModName}}/util/result"
8+
"{{.ModName}}/{{.Abbr}}/model"
9+
"{{.ModName}}/{{.Abbr}}/service"
10+
)
11+
12+
// @Tags {{.SupStructName}}
13+
// @Summary 创建{{.SupStructName}}
14+
// @accept application/json
15+
// @Produce application/json
16+
// @Param data body model.{{.SupStructName}} true "创建{{.SupStructName}}"
17+
// @Success 200 {string} string "{"code":200,"data":{},"msg":"操作成功"}"
18+
// @Router /{{.RouterName}}/create{{.SupStructName}} [post]
19+
func Create{{.SupStructName}}(c *gin.Context) {
20+
21+
var req model.{{.SupStructName}}
22+
23+
if err := c.ShouldBindJSON(&req);err != nil {
24+
c.JSON(200, result.ReturnFailMsg("获取参数失败"))
25+
} else {
26+
res := service.Create{{.SupStructName}}(req)
27+
c.JSON(200, res)
28+
}
29+
}
30+
31+
// @Tags {{.SupStructName}}
32+
// @Summary 删除{{.SupStructName}}
33+
// @accept application/json
34+
// @Produce application/json
35+
// @Param data body model.{{.SupStructName}} true "删除{{.SupStructName}}"
36+
// @Success 200 {string} string "{"code":200,"data":{},"msg":"删除成功"}"
37+
// @Router /{{.RouterName}}/delete{{.SupStructName}} [delete]
38+
func Delete{{.SupStructName}}(c *gin.Context) {
39+
40+
var req model.{{.SupStructName}}
41+
42+
if err := c.ShouldBindJSON(&req);err != nil {
43+
c.JSON(200, result.ReturnFailMsg("获取参数失败"))
44+
} else {
45+
res := service.Delete{{.SupStructName}}(req)
46+
c.JSON(200, res)
47+
}
48+
49+
}
50+
51+
// @Tags {{.SupStructName}}
52+
// @Summary 批量删除{{.SupStructName}}
53+
// @accept application/json
54+
// @Produce application/json
55+
// @Param data body global.IdsReq true "批量删除{{.SupStructName}}"
56+
// @Success 200 {string} string "{"code":200,"data":{},"msg":"删除成功"}"
57+
// @Router /{{.RouterName}}/delete{{.SupStructName}}ByIds [delete]
58+
func Delete{{.SupStructName}}ByIds(c *gin.Context) {
59+
60+
var req global.IdsReq
61+
62+
if err := c.ShouldBindJSON(&req);err != nil {
63+
c.JSON(200, result.ReturnFailMsg("获取参数失败"))
64+
} else {
65+
res := service.Delete{{.SupStructName}}ByIds(req.Ids)
66+
c.JSON(200, res)
67+
}
68+
}
69+
70+
// @Tags {{.SupStructName}}
71+
// @Summary 更新{{.SupStructName}}
72+
// @accept application/json
73+
// @Produce application/json
74+
// @Param data body model.{{.SupStructName}} true "更新{{.SupStructName}}"
75+
// @Success 200 {string} string "{"code":200,"data":{},"msg":"更新成功"}"
76+
// @Router /{{.RouterName}}/update{{.SupStructName}} [put]
77+
func Update{{.SupStructName}}(c *gin.Context) {
78+
var req model.{{.SupStructName}}
79+
80+
if err := c.ShouldBindJSON(&req);err != nil {
81+
c.JSON(200, result.ReturnFailMsg("获取参数失败"))
82+
} else {
83+
res := service.Update{{.SupStructName}}(req)
84+
c.JSON(200, res)
85+
}
86+
}
87+
88+
// @Tags {{.SupStructName}}
89+
// @Summary 用id查询{{.SupStructName}}
90+
// @accept application/json
91+
// @Produce application/json
92+
// @Param id path integer true "ID"
93+
// @Success 200 {string} string "{"code":200,"data":{},"msg":"查询成功"}"
94+
// @Router /{{.RouterName}}/get/{id} [get]
95+
func Get{{.SupStructName}}(c *gin.Context) {
96+
97+
idstr:=c.Param("id") //查询路径Path参数
98+
id, err := strconv.ParseInt(idstr, 10, 64)
99+
if err!=nil{
100+
c.JSON(200, result.ReturnFailMsg("获取参数失败"))
101+
}else{
102+
res := service.Get{{.SupStructName}}(id)
103+
c.JSON(200, res)
104+
}
105+
106+
}
107+
108+
// @Tags {{.SupStructName}}
109+
// @Summary 分页获取{{.SupStructName}}列表
110+
// @accept application/json
111+
// @Produce application/json
112+
// @Param data body model.{{.SupStructName}}Req true "分页获取{{.SupStructName}}列表"
113+
// @Success 200 {string} string "{"code":200,"data":{},"msg":"获取成功"}"
114+
// @Router /{{.RouterName}}/get{{.SupStructName}}List [post]
115+
func GetPageLimit{{.SupStructName}}(c *gin.Context) {
116+
117+
var req model.{{.SupStructName}}Req
118+
119+
if err := c.ShouldBindJSON(&req);err != nil {
120+
c.JSON(200, result.ReturnFailMsg("获取参数失败"))
121+
} else {
122+
res := service.GetPageLimit{{.SupStructName}}(req)
123+
c.JSON(200, res)
124+
}
125+
}

resource/temp/mapper.go.tpl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package mapper
2+
3+
import (
4+
"{{.ModName}}/util/global"
5+
"{{.ModName}}/{{.Abbr}}/model"
6+
)
7+
//创建
8+
func Create{{.SupStructName}}(data model.{{.SupStructName}}) (err error) {
9+
err = global.DB.Create(&data).Error
10+
return
11+
}
12+
//根据ID删除
13+
func Delete{{.SupStructName}}(data model.{{.SupStructName}}) (err error) {
14+
err = global.DB.Delete(&data).Error
15+
return
16+
}
17+
//根据ID批量删除
18+
func Delete{{.SupStructName}}ByIds(ids []int64) (err error) {
19+
err = global.DB.Delete(&model.{{.SupStructName}}{},"id in ?",ids).Error
20+
return
21+
}
22+
23+
//根据id 更新 ,排除零值
24+
func Update{{.SupStructName}}(data model.{{.SupStructName}}) (err error) {
25+
err = global.DB.Updates(&data).Error
26+
return
27+
}
28+
29+
//根据id 更新, Save 会保存所有的字段,即使字段是零值 没有记录则insert
30+
func Save{{.SupStructName}}(data model.{{.SupStructName}}) (err error) {
31+
err = global.DB.Save(&data).Error
32+
return
33+
}
34+
35+
//根据id获取model
36+
func Get{{.SupStructName}}(id int64) (err error, data model.{{.SupStructName}}) {
37+
err = global.DB.Where("id = ?", id).First(&data).Error
38+
return
39+
}
40+
41+
//获取所有的model
42+
func GetAll{{.SupStructName}}() (err error, list []model.{{.SupStructName}}) {
43+
err = global.DB.Find(&list).Error
44+
return
45+
}
46+
47+
//按条件分页查询 limit offset ,参数用指针&, 数据会自动填充到req对象
48+
func GetPageLimit{{.SupStructName}}(data *model.{{.SupStructName}}Req) (err error) {
49+
50+
var list []model.{{.SupStructName}}
51+
if err = global.DB.Model(&data.{{.SupStructName}}).Where(&data.{{.SupStructName}}).Count(&data.Total).Error; err != nil {
52+
return
53+
}
54+
if err = global.DB.Where(&data.{{.SupStructName}}).Limit(int(data.PageSize)).Offset(int(data.Offset())).Find(&list).Error; err != nil {
55+
return
56+
}
57+
data.Data=list
58+
return
59+
}

resource/temp/model.go.tpl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
package model
3+
4+
import "time"
5+
import "{{.ModName}}/util/page"
6+
7+
// Model {{.TableName}} {{.TableComment}}
8+
type {{.SupStructName}} struct {
9+
{{- range .Fields}}
10+
{{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" gorm:"default:default:(-)"` //{{.ColumeComment}}{{ end }}
11+
}
12+
13+
{{ if .TableName }}
14+
func ({{.SupStructName}}) TableName() string {
15+
return "{{.TableName}}"
16+
}
17+
{{ end }}
18+
19+
type {{.SupStructName}}Req struct {
20+
page.PageData
21+
{{.SupStructName}}
22+
}

resource/temp/router.go.tpl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package router
2+
3+
import (
4+
"{{.ModName}}/util/global"
5+
"{{.ModName}}/auth/service"
6+
"{{.ModName}}/{{.Abbr}}/api"
7+
)
8+
9+
func init() {
10+
// 路由权限相关
11+
r := global.Router
12+
v2 := r.Group("{{.RouterName}}")
13+
{
14+
// v2.POST("", api.XX) 不走权限
15+
auth := v2.Group("")
16+
auth.Use(service.AuthRequired())
17+
{
18+
// 需要权限
19+
auth.POST("create{{.SupStructName}}", api.Create{{.SupStructName}})
20+
auth.DELETE("Delete{{.SupStructName}}", api.Delete{{.SupStructName}})
21+
auth.DELETE("Delete{{.SupStructName}}ByIds", api.Delete{{.SupStructName}}ByIds)
22+
auth.PUT("Update{{.SupStructName}}", api.Update{{.SupStructName}})
23+
auth.GET("get/:id", api.Get{{.SupStructName}})
24+
auth.POST("GetPageLimit{{.SupStructName}}", api.GetPageLimit{{.SupStructName}})
25+
26+
}
27+
}
28+
}

resource/temp/service.go.tpl

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
package service
3+
4+
import (
5+
"errors"
6+
"github.com/sirupsen/logrus"
7+
"gorm.io/gorm"
8+
9+
"{{.ModName}}/{{.Abbr}}/mapper"
10+
"{{.ModName}}/{{.Abbr}}/model"
11+
12+
"{{.ModName}}/util/logger"
13+
"{{.ModName}}/util/result"
14+
)
15+
16+
//创建
17+
func Create{{.SupStructName}}(data model.{{.SupStructName}}) *result.Response {
18+
19+
if err := mapper.Create{{.SupStructName}}(data);err!=nil{
20+
logger.Log.WithFields(logrus.Fields{"err": err, "data": data}).Error("保存异常-{{.SupStructName}}")
21+
return result.ReturnFailMsg("保存失败")
22+
}
23+
return result.ReturnSucNil()
24+
}
25+
26+
//根据ID删除
27+
func Delete{{.SupStructName}}(data model.{{.SupStructName}}) *result.Response {
28+
29+
if data.ID == 0{
30+
return result.ReturnFailMsg("参数获取失败")
31+
}
32+
33+
if err := mapper.Delete{{.SupStructName}}(data);err!=nil{
34+
logger.Log.WithFields(logrus.Fields{"err": err, "data": data}).Error("删除异常-{{.SupStructName}}")
35+
return result.ReturnFailMsg("删除失败")
36+
}
37+
38+
return result.ReturnSucNil()
39+
}
40+
41+
//根据ID批量删除
42+
func Delete{{.SupStructName}}ByIds(ids []int64) *result.Response {
43+
if len(ids) == 0{
44+
return result.ReturnFailMsg("参数获取失败")
45+
}
46+
47+
if err := mapper.Delete{{.SupStructName}}ByIds(ids);err!=nil{
48+
logger.Log.WithFields(logrus.Fields{"err": err, "data": ids}).Error("批量删除异常-{{.SupStructName}}")
49+
return result.ReturnFailMsg("批量删除失败")
50+
}
51+
52+
return result.ReturnSucNil()
53+
}
54+
55+
//根据id 更新 ,排除零值
56+
func Update{{.SupStructName}}(data model.{{.SupStructName}}) *result.Response {
57+
if data.ID == 0{
58+
return result.ReturnFailMsg("参数获取失败")
59+
}
60+
61+
if err := mapper.Update{{.SupStructName}}(data);err!=nil{
62+
logger.Log.WithFields(logrus.Fields{"err": err, "data": data}).Error("Update异常-{{.SupStructName}}")
63+
return result.ReturnFailMsg("更新失败")
64+
}
65+
66+
return result.ReturnSucNil()
67+
}
68+
69+
//根据id 更新, Save 会保存所有的字段,即使字段是零值 没有记录则insert
70+
func Save{{.SupStructName}}(data model.{{.SupStructName}}) *result.Response {
71+
if data.ID == 0{
72+
return result.ReturnFailMsg("参数获取失败")
73+
}
74+
75+
if err := mapper.Save{{.SupStructName}}(data);err!=nil{
76+
logger.Log.WithFields(logrus.Fields{"err": err, "data": data}).Error("save异常-{{.SupStructName}}")
77+
return result.ReturnFailMsg("更新失败")
78+
}
79+
80+
return result.ReturnSucNil()
81+
}
82+
83+
//根据id获取model
84+
func Get{{.SupStructName}}(id int64) *result.Response {
85+
86+
if id == 0{
87+
return result.ReturnFailMsg("参数获取失败")
88+
}
89+
90+
if err,data := mapper.Get{{.SupStructName}}(id);err!=nil && !errors.Is(err, gorm.ErrRecordNotFound){
91+
logger.Log.WithFields(logrus.Fields{"err": err, "data": id}).Error("Get异常-{{.SupStructName}}")
92+
return result.ReturnFailMsg("获取数据失败")
93+
}else{
94+
return result.ReturnSuc(data)
95+
}
96+
97+
}
98+
99+
//获取所有的model
100+
func GetAll{{.SupStructName}}() *result.Response {
101+
102+
if err,data := mapper.GetAll{{.SupStructName}}();err!=nil {
103+
logger.Log.WithFields(logrus.Fields{"err": err}).Error("GetAll异常-{{.SupStructName}}")
104+
return result.ReturnFailMsg("获取数据失败")
105+
}else{
106+
return result.ReturnSuc(data)
107+
}
108+
}
109+
110+
//按条件分页查询 limit offset ,参数用指针&, 数据会自动填充到req对象
111+
func GetPageLimit{{.SupStructName}}(data model.{{.SupStructName}}Req) *result.Response {
112+
113+
if err := mapper.GetPageLimit{{.SupStructName}}(&data);err!=nil {
114+
logger.Log.WithFields(logrus.Fields{"err": err,"data":data}).Error("GetPageLimit异常-{{.SupStructName}}")
115+
return result.ReturnFailMsg("获取数据失败")
116+
}else{
117+
return result.ReturnSuc(data.PageData)
118+
}
119+
120+
}

0 commit comments

Comments
 (0)