diff --git a/go.mod b/go.mod index b62af3d..b7902d6 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.14 require ( github.com/23233/simple_valid v1.0.4 + github.com/23233/sv v1.0.8 github.com/casbin/casbin/v2 v2.2.2 github.com/casbin/xorm-adapter v0.0.0-20191120030838-267478260350 github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect diff --git a/simple_admin.go b/simple_admin.go index 58a3b5a..1668a4a 100644 --- a/simple_admin.go +++ b/simple_admin.go @@ -2,11 +2,10 @@ package simple_admin import ( "fmt" - "github.com/23233/simple_valid" + "github.com/23233/sv" "github.com/casbin/casbin/v2" "github.com/imdario/mergo" "github.com/kataras/iris/v12" - "github.com/kataras/iris/v12/context" "github.com/pkg/errors" "log" "reflect" @@ -17,7 +16,7 @@ import ( var ( NowSpAdmin *SpAdmin - SvKey = "sv" + SvKey = sv.GlobalContextKey DefaultPrefix = "/SP_PREFIX" ) @@ -28,7 +27,6 @@ type SpAdmin struct { defaultMethods map[string]string // 默认权限方法 defaultRole map[string]string // 默认角色 sitePolicy map[string]string - sv simple_valid.ReqValid prefix string } @@ -75,13 +73,6 @@ func New(c Config) (*SpAdmin, error) { // 生成表名列表 modelTables := c.generateTables() - // 验证器初始化 - sv := simple_valid.New(SvKey, func(err error, ctx context.Context) { - ctx.StatusCode(iris.StatusBadRequest) - _, _ = ctx.JSON(iris.Map{"detail": err.Error()}) - return - }) - NowSpAdmin = &SpAdmin{ config: c, casbinEnforcer: enforcer, @@ -101,7 +92,6 @@ func New(c Config) (*SpAdmin, error) { "login_site": "login_site", "user_manage": c.getUserModelTableName(), }, - sv: sv, prefix: DefaultPrefix, } // 进行视图注册绑定 @@ -127,15 +117,15 @@ func (lib *SpAdmin) Router(router iris.Party) { // 首页 router.Get("/", Index) // 登录 - router.Post("/login", lib.sv.Run(new(UserLoginReq)), Login) + router.Post("/login", sv.Run(new(UserLoginReq)), Login) // 注册 - router.Post("/reg", lib.sv.Run(new(UserLoginReq)), Reg) + router.Post("/reg", sv.Run(new(UserLoginReq)), Reg) router.Get("/config", Configuration) c := router.Party("/v", CustomJwt.Serve, TokenToUserUidMiddleware) // 获取当前用户 c.Get("/get_current_user", GetCurrentUser) // 变更用户密码 - c.Post("/change_password", lib.sv.Run(new(UserChangePasswordReq)), ChangeUserPassword) + c.Post("/change_password", sv.Run(new(UserChangePasswordReq)), ChangeUserPassword) // 获取所有表 c.Get("/get_routers", GetRouters) // 获取单表列信息 @@ -150,12 +140,12 @@ func (lib *SpAdmin) Router(router iris.Party) { // 修改 c.Put("/{routerName:string}/{id:uint64}", PolicyValidMiddleware, EditRouterData) // 删除 delete模式在某些匹配时候有问题 - c.Post("/{routerName:string}/delete", PolicyValidMiddleware, lib.sv.Run(new(DeleteReq)), RemoveRouterData) + c.Post("/{routerName:string}/delete", PolicyValidMiddleware, sv.Run(new(DeleteReq)), RemoveRouterData) // 权限相关 - c.Post("/change_user_role", PolicyRequireAdminMiddleware, lib.sv.Run(new(UserChangeRolesReq)), ChangeUserRoles) + c.Post("/change_user_role", PolicyRequireAdminMiddleware, sv.Run(new(UserChangeRolesReq)), ChangeUserRoles) // 进行自定义action绑定 for _, action := range lib.config.CustomAction { - c.Handle(action.Methods, action.Path, lib.sv.Run(action.Valid), action.Func) + c.Handle(action.Methods, action.Path, sv.Run(action.Valid), action.Func) } }