提交 caead325 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

增加验证规则复用

上级 6561ef87
......@@ -146,13 +146,9 @@ func GetExaCustomerList(c *gin.Context) {
waitUse := claims.(*request.CustomClaims)
var pageInfo request.PageInfo
_ = c.ShouldBindQuery(&pageInfo)
CustomerVerify := utils.Rules{
"Page": {utils.NotEmpty()},
"PageSize": {utils.NotEmpty()},
}
CustomerVerifyErr := utils.Verify(pageInfo, CustomerVerify)
if CustomerVerifyErr != nil {
response.FailWithMessage(CustomerVerifyErr.Error(), c)
PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
if PageVerifyErr != nil {
response.FailWithMessage(PageVerifyErr.Error(), c)
return
}
err, customerList, total := service.GetCustomerInfoList(waitUse.AuthorityId, pageInfo)
......
......@@ -23,13 +23,13 @@ func CreateApi(c *gin.Context) {
var api model.SysApi
_ = c.ShouldBindJSON(&api)
ApiVerify := utils.Rules{
"Path": {utils.NotEmpty()},
"Description": {utils.NotEmpty()},
"ApiGroup": {utils.NotEmpty()},
"Path": {utils.NotEmpty()},
"Description": {utils.NotEmpty()},
"ApiGroup": {utils.NotEmpty()},
"Method": {utils.NotEmpty()},
}
ApiVerifyErr := utils.Verify(api, ApiVerify)
if ApiVerifyErr!=nil {
if ApiVerifyErr != nil {
response.FailWithMessage(ApiVerifyErr.Error(), c)
return
}
......@@ -53,10 +53,10 @@ func DeleteApi(c *gin.Context) {
var a model.SysApi
_ = c.ShouldBindJSON(&a)
ApiVerify := utils.Rules{
"ID": {utils.NotEmpty()},
"ID": {utils.NotEmpty()},
}
ApiVerifyErr := utils.Verify(a.Model, ApiVerify)
if ApiVerifyErr!=nil {
if ApiVerifyErr != nil {
response.FailWithMessage(ApiVerifyErr.Error(), c)
return
}
......@@ -82,13 +82,9 @@ func GetApiList(c *gin.Context) {
// 此结构体仅本方法使用
var sp request.SearchApiParams
_ = c.ShouldBindJSON(&sp)
ApiVerify := utils.Rules{
"Page": {utils.NotEmpty()},
"PageSize": {utils.NotEmpty()},
}
ApiVerifyErr := utils.Verify(sp.PageInfo, ApiVerify)
if ApiVerifyErr!=nil {
response.FailWithMessage(ApiVerifyErr.Error(), c)
PageVerifyErr := utils.Verify(sp.PageInfo, utils.CustomizeMap["PageVerify"])
if PageVerifyErr != nil {
response.FailWithMessage(PageVerifyErr.Error(), c)
return
}
err, list, total := service.GetAPIInfoList(sp.SysApi, sp.PageInfo, sp.OrderKey, sp.Desc)
......@@ -119,7 +115,7 @@ func GetApiById(c *gin.Context) {
"Id": {utils.NotEmpty()},
}
ApiVerifyErr := utils.Verify(idInfo, ApiVerify)
if ApiVerifyErr!=nil {
if ApiVerifyErr != nil {
response.FailWithMessage(ApiVerifyErr.Error(), c)
return
}
......@@ -143,13 +139,13 @@ func UpdateApi(c *gin.Context) {
var api model.SysApi
_ = c.ShouldBindJSON(&api)
ApiVerify := utils.Rules{
"Path": {utils.NotEmpty()},
"Description": {utils.NotEmpty()},
"ApiGroup": {utils.NotEmpty()},
"Path": {utils.NotEmpty()},
"Description": {utils.NotEmpty()},
"ApiGroup": {utils.NotEmpty()},
"Method": {utils.NotEmpty()},
}
ApiVerifyErr := utils.Verify(api, ApiVerify)
if ApiVerifyErr!=nil {
if ApiVerifyErr != nil {
response.FailWithMessage(ApiVerifyErr.Error(), c)
return
}
......
......@@ -23,12 +23,12 @@ func CreateAuthority(c *gin.Context) {
var auth model.SysAuthority
_ = c.ShouldBindJSON(&auth)
AuthorityVerify := utils.Rules{
"AuthorityId": {utils.NotEmpty()},
"AuthorityName": {utils.NotEmpty()},
"AuthorityId": {utils.NotEmpty()},
"AuthorityName": {utils.NotEmpty()},
"ParentId": {utils.NotEmpty()},
}
AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
if AuthorityVerifyErr!=nil {
if AuthorityVerifyErr != nil {
response.FailWithMessage(AuthorityVerifyErr.Error(), c)
return
}
......@@ -52,20 +52,20 @@ func CopyAuthority(c *gin.Context) {
var copyInfo resp.SysAuthorityCopyResponse
_ = c.ShouldBindJSON(&copyInfo)
OldAuthorityVerify := utils.Rules{
"OldAuthorityId": {utils.NotEmpty()},
"OldAuthorityId": {utils.NotEmpty()},
}
OldAuthorityVerifyErr := utils.Verify(copyInfo, OldAuthorityVerify)
if OldAuthorityVerifyErr!=nil {
if OldAuthorityVerifyErr != nil {
response.FailWithMessage(OldAuthorityVerifyErr.Error(), c)
return
}
AuthorityVerify := utils.Rules{
"AuthorityId": {utils.NotEmpty()},
"AuthorityName": {utils.NotEmpty()},
"AuthorityId": {utils.NotEmpty()},
"AuthorityName": {utils.NotEmpty()},
"ParentId": {utils.NotEmpty()},
}
AuthorityVerifyErr := utils.Verify(copyInfo.Authority, AuthorityVerify)
if AuthorityVerifyErr!=nil {
if AuthorityVerifyErr != nil {
response.FailWithMessage(AuthorityVerifyErr.Error(), c)
return
}
......@@ -89,10 +89,10 @@ func DeleteAuthority(c *gin.Context) {
var a model.SysAuthority
_ = c.ShouldBindJSON(&a)
AuthorityVerify := utils.Rules{
"AuthorityId": {utils.NotEmpty()},
"AuthorityId": {utils.NotEmpty()},
}
AuthorityVerifyErr := utils.Verify(a, AuthorityVerify)
if AuthorityVerifyErr!=nil {
if AuthorityVerifyErr != nil {
response.FailWithMessage(AuthorityVerifyErr.Error(), c)
return
}
......@@ -117,12 +117,12 @@ func UpdateAuthority(c *gin.Context) {
var auth model.SysAuthority
_ = c.ShouldBindJSON(&auth)
AuthorityVerify := utils.Rules{
"AuthorityId": {utils.NotEmpty()},
"AuthorityName": {utils.NotEmpty()},
"AuthorityId": {utils.NotEmpty()},
"AuthorityName": {utils.NotEmpty()},
"ParentId": {utils.NotEmpty()},
}
AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
if AuthorityVerifyErr!=nil {
if AuthorityVerifyErr != nil {
response.FailWithMessage(AuthorityVerifyErr.Error(), c)
return
}
......@@ -145,13 +145,9 @@ func UpdateAuthority(c *gin.Context) {
func GetAuthorityList(c *gin.Context) {
var pageInfo request.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
AuthorityVerify := utils.Rules{
"Page": {utils.NotEmpty()},
"PageSize": {utils.NotEmpty()},
}
AuthorityVerifyErr := utils.Verify(pageInfo, AuthorityVerify)
if AuthorityVerifyErr!=nil {
response.FailWithMessage(AuthorityVerifyErr.Error(), c)
PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
if PageVerifyErr != nil {
response.FailWithMessage(PageVerifyErr.Error(), c)
return
}
err, list, total := service.GetAuthorityInfoList(pageInfo)
......@@ -179,10 +175,10 @@ func SetDataAuthority(c *gin.Context) {
var auth model.SysAuthority
_ = c.ShouldBindJSON(&auth)
AuthorityVerify := utils.Rules{
"AuthorityId": {utils.NotEmpty()},
"AuthorityId": {utils.NotEmpty()},
}
AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
if AuthorityVerifyErr!=nil {
if AuthorityVerifyErr != nil {
response.FailWithMessage(AuthorityVerifyErr.Error(), c)
return
}
......
......@@ -40,12 +40,8 @@ func GetMenu(c *gin.Context) {
func GetMenuList(c *gin.Context) {
var pageInfo request.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
PageVerify := utils.Rules{
"Page": {utils.NotEmpty()},
"PageSize": {utils.NotEmpty()},
}
PageVerifyErr := utils.Verify(pageInfo, PageVerify)
if PageVerifyErr!=nil{
PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
if PageVerifyErr != nil {
response.FailWithMessage(PageVerifyErr.Error(), c)
return
}
......@@ -78,7 +74,7 @@ func AddBaseMenu(c *gin.Context) {
"ParentId": {utils.NotEmpty()},
"Name": {utils.NotEmpty()},
"Component": {utils.NotEmpty()},
"Sort": {utils.Ge("0"),"ge=0"},
"Sort": {utils.Ge("0"), "ge=0"},
}
MenuVerifyErr := utils.Verify(menu, MenuVerify)
if MenuVerifyErr != nil {
......@@ -129,7 +125,7 @@ func AddMenuAuthority(c *gin.Context) {
var addMenuAuthorityInfo request.AddMenuAuthorityInfo
_ = c.ShouldBindJSON(&addMenuAuthorityInfo)
MenuVerify := utils.Rules{
"AuthorityId": {"notEmpty"},
"AuthorityId": {"notEmpty"},
}
MenuVerifyErr := utils.Verify(addMenuAuthorityInfo, MenuVerify)
if MenuVerifyErr != nil {
......@@ -156,7 +152,7 @@ func GetMenuAuthority(c *gin.Context) {
var authorityIdInfo request.AuthorityIdInfo
_ = c.ShouldBindJSON(&authorityIdInfo)
MenuVerify := utils.Rules{
"AuthorityId": {"notEmpty"},
"AuthorityId": {"notEmpty"},
}
MenuVerifyErr := utils.Verify(authorityIdInfo, MenuVerify)
if MenuVerifyErr != nil {
......@@ -183,7 +179,7 @@ func DeleteBaseMenu(c *gin.Context) {
var idInfo request.GetById
_ = c.ShouldBindJSON(&idInfo)
MenuVerify := utils.Rules{
"Id": {"notEmpty"},
"Id": {"notEmpty"},
}
MenuVerifyErr := utils.Verify(idInfo, MenuVerify)
if MenuVerifyErr != nil {
......@@ -215,7 +211,7 @@ func UpdateBaseMenu(c *gin.Context) {
"ParentId": {utils.NotEmpty()},
"Name": {utils.NotEmpty()},
"Component": {utils.NotEmpty()},
"Sort": {utils.Ge("0"),"ge=0"},
"Sort": {utils.Ge("0"), "ge=0"},
}
MenuVerifyErr := utils.Verify(menu, MenuVerify)
if MenuVerifyErr != nil {
......@@ -250,7 +246,7 @@ func GetBaseMenuById(c *gin.Context) {
var idInfo request.GetById
_ = c.ShouldBindJSON(&idInfo)
MenuVerify := utils.Rules{
"Id": {"notEmpty"},
"Id": {"notEmpty"},
}
MenuVerifyErr := utils.Verify(idInfo, MenuVerify)
if MenuVerifyErr != nil {
......
......@@ -28,13 +28,13 @@ func Register(c *gin.Context) {
var R request.RegisterStruct
_ = c.ShouldBindJSON(&R)
UserVerify := utils.Rules{
"Username": {utils.NotEmpty()},
"NickName": {utils.NotEmpty()},
"Password": {utils.NotEmpty()},
"AuthorityId": {utils.NotEmpty()},
"Username": {utils.NotEmpty()},
"NickName": {utils.NotEmpty()},
"Password": {utils.NotEmpty()},
"AuthorityId": {utils.NotEmpty()},
}
UserVerifyErr := utils.Verify(R, UserVerify)
if UserVerifyErr!=nil {
if UserVerifyErr != nil {
response.FailWithMessage(UserVerifyErr.Error(), c)
return
}
......@@ -57,13 +57,13 @@ func Login(c *gin.Context) {
var L request.RegisterAndLoginStruct
_ = c.ShouldBindJSON(&L)
UserVerify := utils.Rules{
"CaptchaId": {utils.NotEmpty()},
"Captcha": {utils.NotEmpty()},
"Username": {utils.NotEmpty()},
"Password": {utils.NotEmpty()},
"CaptchaId": {utils.NotEmpty()},
"Captcha": {utils.NotEmpty()},
"Username": {utils.NotEmpty()},
"Password": {utils.NotEmpty()},
}
UserVerifyErr := utils.Verify(L, UserVerify)
if UserVerifyErr!=nil {
if UserVerifyErr != nil {
response.FailWithMessage(UserVerifyErr.Error(), c)
return
}
......@@ -157,12 +157,12 @@ func ChangePassword(c *gin.Context) {
var params request.ChangePasswordStruct
_ = c.ShouldBindJSON(&params)
UserVerify := utils.Rules{
"Username": {utils.NotEmpty()},
"Password": {utils.NotEmpty()},
"NewPassword": {utils.NotEmpty()},
"Username": {utils.NotEmpty()},
"Password": {utils.NotEmpty()},
"NewPassword": {utils.NotEmpty()},
}
UserVerifyErr := utils.Verify(params, UserVerify)
if UserVerifyErr!=nil {
if UserVerifyErr != nil {
response.FailWithMessage(UserVerifyErr.Error(), c)
return
}
......@@ -225,13 +225,9 @@ func UploadHeaderImg(c *gin.Context) {
func GetUserList(c *gin.Context) {
var pageInfo request.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
UserVerify := utils.Rules{
"Page": {utils.NotEmpty()},
"PageSize": {utils.NotEmpty()},
}
UserVerifyErr := utils.Verify(pageInfo, UserVerify)
if UserVerifyErr!=nil {
response.FailWithMessage(UserVerifyErr.Error(), c)
PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
if PageVerifyErr != nil {
response.FailWithMessage(PageVerifyErr.Error(), c)
return
}
err, list, total := service.GetUserInfoList(pageInfo)
......@@ -259,11 +255,11 @@ func SetUserAuthority(c *gin.Context) {
var sua request.SetUserAuth
_ = c.ShouldBindJSON(&sua)
UserVerify := utils.Rules{
"UUID": {utils.NotEmpty()},
"UUID": {utils.NotEmpty()},
"AuthorityId": {utils.NotEmpty()},
}
UserVerifyErr := utils.Verify(sua, UserVerify)
if UserVerifyErr!=nil {
if UserVerifyErr != nil {
response.FailWithMessage(UserVerifyErr.Error(), c)
return
}
......@@ -290,7 +286,7 @@ func DeleteUser(c *gin.Context) {
"Id": {utils.NotEmpty()},
}
UserVerifyErr := utils.Verify(reqId, UserVerify)
if UserVerifyErr!=nil {
if UserVerifyErr != nil {
response.FailWithMessage(UserVerifyErr.Error(), c)
return
}
......
......@@ -14,7 +14,6 @@ import (
func Routers() *gin.Engine {
var Router = gin.Default()
//Router.Use(middleware.LoadTls()) // 打开就能玩https了
global.GVA_LOG.Debug("use middleware logger")
// 跨域
......
package initialize
import "gin-vue-admin/utils"
func init() {
_ = utils.RegisterRule("PageVerify",
utils.Rules{
"Page": {utils.NotEmpty()},
"PageSize": {utils.NotEmpty()},
},
)
}
// 空值校验工具 仅用于检验空字符串 其余类型请勿使用
package utils
import (
"errors"
"fmt"
"reflect"
)
func HasGap(input interface{}) error {
getType := reflect.TypeOf(input)
getValue := reflect.ValueOf(input)
// 获取方法字段
for i := 0; i < getType.NumField(); i++ {
field := getType.Field(i)
value := getValue.Field(i).Interface()
switch value.(type) {
case string:
if value == "" {
fmt.Printf("%s为空", field.Name)
return errors.New(fmt.Sprintf("%s为空", field.Name))
}
default:
if value == nil {
fmt.Printf("%s为空", field.Name)
return errors.New(fmt.Sprintf("%s为空", field.Name))
}
}
}
// 获取方法
// 1. 先获取interface的reflect.Type,然后通过.NumMethod进行遍历
//for i := 0; i < getType.NumMethod(); i++ {
// m := getType.Method(i)
// fmt.Printf("%s: %v\n", m.Name, m.Type)
//}
return nil
}
......@@ -9,6 +9,20 @@ import (
type Rules map[string][]string
type RulesMap map[string]Rules
var CustomizeMap = make(map[string]Rules)
// 注册自定义规则方案建议在路由初始化层即注册
func RegisterRule(key string, rule Rules) (err error) {
if CustomizeMap[key] != nil {
return errors.New(key + "已注册,无法重复注册")
} else {
CustomizeMap[key] = rule
return nil
}
}
// 非空 不能为其对应类型的0值
func NotEmpty() string {
return "notEmpty"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册