package admin import ( "zhiyuan/pkg/app" "zhiyuan/pkg/utils" "zhiyuan/services/form" "zhiyuan/services/role" "github.com/gin-gonic/gin" ) func RoleInfo(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "role id must be a number", nil) return } auth, err := role.GetInfoByID(id, nil, nil) if err != nil { app.Error(c, err.Error()) return } app.Success(c, auth) } func RoleList(c *gin.Context) { page := app.HandlePageNums(c) where := make(map[string]interface{}) total, err := role.Count(where) if err != nil { app.Error(c, err.Error()) return } type RoleList struct { ID int `json:"id"` Name string `json:"name"` CreatedAt string `json:"created_at"` AuthIds string `json:"auth_ids"` DataAuth int `json:"data_auth"` } var roleList []RoleList _, err = role.GetList(where, []string{"id", "name", "created_at", "auth_ids", "data_auth"}, page, &roleList) if err != nil { app.Error(c, err.Error()) return } for k, v := range roleList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD") roleList[k] = v } data := gin.H{ "list": roleList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func RoleAdd(c *gin.Context) { var form form.RoleAdd if app.Bind(c, &form) != nil { return } id, err := role.Add(form) if err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"id": id}) } func RoleEdit(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "auth id must be a number", nil) return } var form form.RoleAdd if app.Bind(c, &form) != nil { return } err := role.EditByID(form, id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func RoleDel(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "auth id must be a number", nil) return } err := role.DeleteByID(id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) }