diff --git a/app/admin/apis/student/student.go b/app/admin/apis/student/student.go new file mode 100644 index 00000000..92482e54 --- /dev/null +++ b/app/admin/apis/student/student.go @@ -0,0 +1,84 @@ +package student + +import ( + "github.com/gin-gonic/gin" + + "go-admin/app/admin/models" + "go-admin/app/admin/service/dto" + "go-admin/tools" + "go-admin/tools/app" + "go-admin/tools/app/msg" +) + +func GetStudentList(c *gin.Context) { + var data models.Student + var err error + var pageSize = 10 + var pageIndex = 1 + + if size := c.Request.FormValue("pageSize"); size != "" { + pageSize, err = tools.StringToInt(size) + } + if index := c.Request.FormValue("pageIndex"); index != "" { + pageIndex, err = tools.StringToInt(index) + } + + var v dto.StudentSearch + err = c.Bind(&v) + tools.HasError(err, "数据解析失败", 422) + + data.DataScope = tools.GetUserIdStr(c) + result, count, err := data.GetPage(pageSize, pageIndex, v) + tools.HasError(err, "", -1) + + app.PageOK(c, result, count, pageIndex, pageSize, "") +} + +func GetStudent(c *gin.Context) { + var data models.Student + data.Id, _ = tools.StringToInt(c.Param("id")) + result, err := data.Get() + tools.HasError(err, "抱歉未找到相关信息", -1) + + app.OK(c, result, "") +} + +// @Summary 添加Student +// @Description 获取JSON +// @Tags Student +// @Accept application/json +// @Product application/json +// @Param data body models.Student true "data" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": -1, "message": "添加失败"}" +// @Router /api/v1/student [post] +func InsertStudent(c *gin.Context) { + var data models.Student + err := c.ShouldBindJSON(&data) + data.CreateBy = tools.GetUserIdStr(c) + tools.HasError(err, "", 500) + result, err := data.Create() + tools.HasError(err, "", -1) + app.OK(c, result, "") +} + +func UpdateStudent(c *gin.Context) { + var data models.Student + err := c.ShouldBindJSON(&data) + tools.HasError(err, "数据解析失败", -1) + data.UpdateBy = tools.GetUserIdStr(c) + result, err := data.Update(data.Id) + tools.HasError(err, "", -1) + + app.OK(c, result, "") +} + +func DeleteStudent(c *gin.Context) { + var data models.Student + data.UpdateBy = tools.GetUserIdStr(c) + + IDS := tools.IdsStrToIdsIntGroup("id", c) + _, err := data.BatchDelete(IDS) + tools.HasError(err, msg.DeletedFail, 500) + app.OK(c, nil, msg.DeletedSuccess) +} \ No newline at end of file diff --git a/app/admin/apis/tools/gen.go b/app/admin/apis/tools/gen.go index 67ea70fa..6770a0d0 100644 --- a/app/admin/apis/tools/gen.go +++ b/app/admin/apis/tools/gen.go @@ -71,7 +71,7 @@ func GenCode(c *gin.Context) { oldTest := "// {{认证路由自动补充在此处请勿删除}}" newText := "// {{认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1,authMiddleware)" - if !tab.IsAuth { + if tab.IsAuth == 2 { oldTest = "// {{无需认证路由自动补充在此处请勿删除}}" newText = "// {{无需认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1)" routerfile = "template/routernocheckrole.go.template" @@ -142,7 +142,7 @@ func GenCodeV2(c *gin.Context) { table.TableId = id tab, _ := table.Get() - if tab.IsActions { + if tab.IsActions == 1 { ActionsGen(tab) } else { NOActionsGen(tab) @@ -154,7 +154,7 @@ func GenCodeV2(c *gin.Context) { func NOActionsGen(tab tools.SysTables) { routerfile := "template/routercheckrole.go.template" - if !tab.IsAuth { + if tab.IsAuth == 2 { routerfile = "template/routernocheckrole.go.template" } @@ -171,7 +171,7 @@ func NOActionsGen(tab tools.SysTables) { t6, err := template.ParseFiles("template/v2/dto.go.template") tools2.HasError(err, "", -1) - _ = tools2.PathCreate("./app/" + tab.PackageName + "/apis/" + tab.ModuleName + "/") + _ = tools2.PathCreate("./app/" + tab.PackageName + "/apis/" + tab.ModuleName) _ = tools2.PathCreate("./app/" + tab.PackageName + "/models/") _ = tools2.PathCreate("./app/" + tab.PackageName + "/router/") _ = tools2.PathCreate("./app/" + tab.PackageName + "/service/dto/") @@ -202,7 +202,7 @@ func NOActionsGen(tab tools.SysTables) { func ActionsGen(tab tools.SysTables) { routerfile := "template/v2/routercheckrole.go.template" - if !tab.IsAuth { + if tab.IsAuth == 2 { routerfile = "template/v2/routernocheckrole.go.template" } @@ -464,7 +464,7 @@ func GenMenuAndApi(c *gin.Context) { ADelete.MenuName = "" ADelete.Title = "删除" + tab.TableComment ADelete.Icon = "bug" - ADelete.Path = "/api/v1/" + tab.ModuleName + "/:id" + ADelete.Path = "/api/v1/" + tab.ModuleName ADelete.MenuType = "A" ADelete.Action = "DELETE" ADelete.ParentId = Amenu.MenuId diff --git a/app/admin/models/tools/systables.go b/app/admin/models/tools/systables.go index d3cb0401..1adbefc4 100644 --- a/app/admin/models/tools/systables.go +++ b/app/admin/models/tools/systables.go @@ -28,9 +28,9 @@ type SysTables struct { Tree bool `gorm:"size:1;" json:"tree"` Crud bool `gorm:"size:1;" json:"crud"` Remark string `gorm:"size:255;" json:"remark"` - IsDataScope bool `gorm:"size:1;" json:"isDataScope"` - IsActions bool `gorm:"size:1;" json:"isActions"` - IsAuth bool `gorm:"size:1;" json:"isAuth"` + IsDataScope int `gorm:"size:1;" json:"isDataScope"` + IsActions int `gorm:"size:1;" json:"isActions"` + IsAuth int `gorm:"size:1;" json:"isAuth"` IsLogicalDelete string `gorm:"size:1;" json:"isLogicalDelete"` LogicalDelete bool `gorm:"size:1;" json:"logicalDelete"` LogicalDeleteColumn string `gorm:"size:128;" json:"logicalDeleteColumn"` @@ -150,13 +150,13 @@ func (e *SysTables) Create() (SysTables, error) { } func (e *SysTables) Update() (update SysTables, err error) { - if err = orm.Eloquent.Table("sys_tables").First(&update, e.TableId).Error; err != nil { - return - } + //if err = orm.Eloquent.Table("sys_tables").First(&update, e.TableId).Error; err != nil { + // return + //} //参数1:是要修改的数据 //参数2:是修改的数据 - if err = orm.Eloquent.Table("sys_tables").Model(&update).Updates(&e).Error; err != nil { + if err = orm.Eloquent.Table("sys_tables").Where("table_id = ?", e.TableId).Updates(&e).Error; err != nil { return } diff --git a/template/api.go.template b/template/api.go.template index 86ac9766..b588da11 100644 --- a/template/api.go.template +++ b/template/api.go.template @@ -2,9 +2,9 @@ package {{.ModuleName}} import ( "github.com/gin-gonic/gin" - "github.com/gin-gonic/gin/binding" - "go-admin/models" + "go-admin/app/admin/models" + "go-admin/app/admin/service/dto" "go-admin/tools" "go-admin/tools/app" "go-admin/tools/app/msg" diff --git a/template/js.go.template b/template/js.go.template index c4ddf213..fb91a490 100644 --- a/template/js.go.template +++ b/template/js.go.template @@ -37,9 +37,9 @@ data: data } // 删除{{.ClassName}} -export function del{{.ClassName}}({{.PkJsonField}},data) { +export function del{{.ClassName}}(data) { return request({ -url: '/api/v1/{{.ModuleName}}/' + {{.PkJsonField}}, +url: '/api/v1/{{.ModuleName}}', method: 'delete', data: data }) diff --git a/template/model.go.template b/template/model.go.template index 7763c4f3..90389121 100644 --- a/template/model.go.template +++ b/template/model.go.template @@ -3,10 +3,10 @@ package models import ( "strconv" - "go-admin/common/dto" - orm "go-admin/common/global" - "go-admin/common/models" - "go-admin/tools" + "go-admin/common/dto" + orm "go-admin/common/global" + "go-admin/common/models" + "go-admin/tools" {{ range .Columns }} {{- if eq .GoField "CreatedAt" -}} @@ -36,6 +36,24 @@ func ({{.ClassName}}) TableName() string { return "{{.TBName}}" } + +func (e *{{.ClassName}}) Generate() models.ActiveRecord { + o := *e + return &o +} + +func (e *{{.ClassName}}) GetId() interface{} { + return e.{{.PkGoField}} +} + +func (e *{{.ClassName}}) SetCreateBy(createBy uint) { + e.CreateBy = strconv.Itoa(int(createBy)) +} + +func (e *{{.ClassName}}) SetUpdateBy(updateBy uint) { + e.UpdateBy = strconv.Itoa(int(updateBy)) +} + // 创建{{.ClassName}} func (e *{{.ClassName}}) Create() ({{.ClassName}}, error) { var doc {{.ClassName}} @@ -76,7 +94,7 @@ func (e *{{.ClassName}}) Get() ({{.ClassName}}, error) { func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int, v interface{}) ([]{{.ClassName}}, int, error) { var doc []{{.ClassName}} - table := orm.Eloquent.Table(e.TableName()).Scopes(tools.MakeCondition(v)) + table := orm.Eloquent.Table(e.TableName()).Scopes(dto.MakeCondition(v)) // 数据权限控制(如果不需要数据权限请将此处去掉) dataPermission := new(DataPermission) @@ -87,7 +105,7 @@ func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int, v interface{}) ([] } var count int64 - if err := table.Scopes(tools.Paginate(pageSize, pageIndex)).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil { + if err := table.Scopes(dto.Paginate(pageSize, pageIndex)).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil { return nil, 0, err } return doc, int(count), nil diff --git a/template/router.go.template b/template/router.go.template index a93c739f..30f8c733 100644 --- a/template/router.go.template +++ b/template/router.go.template @@ -16,7 +16,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW r.GET("", {{.ModuleName}}.Get{{.ClassName}}List) r.POST("", {{.ModuleName}}.Insert{{.ClassName}}) r.PUT("", {{.ModuleName}}.Update{{.ClassName}}) - r.DELETE("/:{{.PkJsonField}}", {{.ModuleName}}.Delete{{.ClassName}}) + r.DELETE("", {{.ModuleName}}.Delete{{.ClassName}}) } } @@ -28,6 +28,6 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup) { r.GET("/:{{.PkJsonField}}", {{.ModuleName}}.Get{{.ClassName}}) r.POST("", {{.ModuleName}}.Insert{{.ClassName}}) r.PUT("", {{.ModuleName}}.Update{{.ClassName}}) - r.DELETE("/:{{.PkJsonField}}", {{.ModuleName}}.Delete{{.ClassName}}) + r.DELETE("", {{.ModuleName}}.Delete{{.ClassName}}) } } diff --git a/template/routercheckrole.go.template b/template/routercheckrole.go.template index abc83e03..e28b2af5 100644 --- a/template/routercheckrole.go.template +++ b/template/routercheckrole.go.template @@ -3,11 +3,15 @@ package router import ( "github.com/gin-gonic/gin" - "go-admin/middleware" - "go-admin/apis/{{.ModuleName}}" + "go-admin/app/admin/apis/{{.ModuleName}}" + "go-admin/app/admin/middleware" jwt "go-admin/pkg/jwtauth" ) +func init() { + routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router) +} + // 需认证的路由代码 func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) @@ -15,7 +19,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW r.GET("/:{{.PkJsonField}}", {{.ModuleName}}.Get{{.ClassName}}) r.POST("", {{.ModuleName}}.Insert{{.ClassName}}) r.PUT("/:{{.PkJsonField}}", {{.ModuleName}}.Update{{.ClassName}}) - r.DELETE("/:{{.PkJsonField}}", {{.ModuleName}}.Delete{{.ClassName}}) - l.GET("",{{.ModuleName}}.Get{{.ClassName}}List) + r.DELETE("", {{.ModuleName}}.Delete{{.ClassName}}) + r.GET("",{{.ModuleName}}.Get{{.ClassName}}List) } } diff --git a/template/routernocheckrole.go.template b/template/routernocheckrole.go.template index 6f5186f7..076606e0 100644 --- a/template/routernocheckrole.go.template +++ b/template/routernocheckrole.go.template @@ -3,9 +3,13 @@ package router import ( "github.com/gin-gonic/gin" - "go-admin/apis/{{.ModuleName}}" + "go-admin/app/admin/apis/{{.ModuleName}}" ) +func init() { + routerNoCheckRole = append(routerNoCheckRole, register{{.ClassName}}Router) +} + // 无需认证的路由代码 func register{{.ClassName}}Router(v1 *gin.RouterGroup) { r := v1.Group("/{{.ModuleName}}") @@ -13,7 +17,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup) { r.GET("/:{{.PkJsonField}}", {{.ModuleName}}.Get{{.ClassName}}) r.POST("", {{.ModuleName}}.Insert{{.ClassName}}) r.PUT("/:{{.PkJsonField}}", {{.ModuleName}}.Update{{.ClassName}}) - r.DELETE("/:{{.PkJsonField}}", {{.ModuleName}}.Delete{{.ClassName}}) + r.DELETE("", {{.ModuleName}}.Delete{{.ClassName}}) r.GET("",{{.ModuleName}}.Get{{.ClassName}}List) } } diff --git a/template/v2/routercheckrole.go.template b/template/v2/routercheckrole.go.template index cda02ba8..b853e346 100644 --- a/template/v2/routercheckrole.go.template +++ b/template/v2/routercheckrole.go.template @@ -26,6 +26,6 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.{{.ClassName}}ById))) r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control))) r.PUT("/:id", actions.PermissionAction(), actions.UpdateAction(new(dto.{{.ClassName}}Control))) - r.DELETE("/:id", actions.PermissionAction(), actions.DeleteAction(new(dto.{{.ClassName}}ById))) + r.DELETE("", actions.PermissionAction(), actions.DeleteAction(new(dto.{{.ClassName}}ById))) } } diff --git a/template/v2/routernocheckrole.go.template b/template/v2/routernocheckrole.go.template index 741c4df5..39de40c9 100644 --- a/template/v2/routernocheckrole.go.template +++ b/template/v2/routernocheckrole.go.template @@ -9,6 +9,10 @@ import ( "go-admin/common/actions" ) +func init() { + routerNoCheckRole = append(routerNoCheckRole, register{{.ClassName}}Router) +} + // 无需认证的路由代码 func register{{.ClassName}}Router(v1 *gin.RouterGroup) { r := v1.Group("/{{.ModuleName}}") @@ -21,6 +25,6 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup) { r.GET("/:id", actions.ViewAction(new(dto.{{.ClassName}}ById))) r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control))) r.PUT("/:id", actions.UpdateAction(new(dto.{{.ClassName}}Control))) - r.DELETE("/:id", actions.DeleteAction(new(dto.{{.ClassName}}ById))) + r.DELETE("", actions.DeleteAction(new(dto.{{.ClassName}}ById))) } } diff --git a/template/vue.go.template b/template/vue.go.template index a1493e37..adf05059 100644 --- a/template/vue.go.template +++ b/template/vue.go.template @@ -428,11 +428,7 @@ cancelButtonText: '取消', type: 'warning' }).then(function () { - if (Ids.length > 1) { - return del{{.ClassName}}(Ids.shift(), { 'ids': Ids }) - } else { - return del{{.ClassName}}(Ids, { 'ids': [] }) - } + return del{{.ClassName}}( { 'ids': Ids }) }).then(() => { this.getList() this.msgSuccess('删除成功')