1
This commit is contained in:
+53
-55
@@ -112,6 +112,54 @@ func Auth() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
path := c.Request.URL.Path
|
||||
|
||||
// 匹配路由为/:id的接口
|
||||
reg := regexp.MustCompile("/(\\d+)$")
|
||||
|
||||
match := reg.MatchString(c.Request.RequestURI)
|
||||
if match {
|
||||
// 找到最后一个数字的索引
|
||||
lastSlashIndex := strings.LastIndex(c.Request.RequestURI, "/")
|
||||
if lastSlashIndex != -1 {
|
||||
|
||||
// 替换最后一个数字部分为 :id
|
||||
path = path[:lastSlashIndex] + "/:id"
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "请求路径错误",
|
||||
"code": consts.SERVER_ERROR,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 检测接口是否需要验证权限
|
||||
adminApiDao := dao.AdminApiDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["api_path"] = path
|
||||
maps["api_method"] = c.Request.Method
|
||||
adminApis, err := adminApiDao.GetAdminApiList(maps)
|
||||
if len(adminApis) == 0 || err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "请求路径错误",
|
||||
"code": consts.SERVER_ERROR,
|
||||
"data": "",
|
||||
})
|
||||
}
|
||||
|
||||
// 接口无需验证权限
|
||||
if adminApis[0].IsAuth == 0 {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
path = path + c.Request.Method
|
||||
|
||||
// 获取角色菜单id
|
||||
AdminRoleMenuDao := dao.AdminRoleMenuDao{}
|
||||
adminRoleMenu, _ := AdminRoleMenuDao.GetAdminRoleMenuListByRoleId(roleId)
|
||||
@@ -130,46 +178,18 @@ func Auth() gin.HandlerFunc {
|
||||
// 获取菜单对应api
|
||||
adminMenuApiDao := dao.AdminMenuApiDao{}
|
||||
for _, v := range adminRoleMenu {
|
||||
AdminMenuApi, _ := adminMenuApiDao.GetAdminMenuApiListWithAPIByMenuID(v.MenuID)
|
||||
if AdminMenuApi == nil {
|
||||
// 菜单无需权限
|
||||
c.Next()
|
||||
return
|
||||
adminMenuApi, _ := adminMenuApiDao.GetAdminMenuApiListWithAPIByMenuID(v.MenuID)
|
||||
if adminMenuApi == nil {
|
||||
// 菜单无绑定接口
|
||||
continue
|
||||
}
|
||||
|
||||
// 将API权限存储在apiPermissions中
|
||||
for _, api := range AdminMenuApi {
|
||||
for _, api := range adminMenuApi {
|
||||
apiPermissions[api.API.APIPath+api.API.APIMethod] = true
|
||||
}
|
||||
}
|
||||
|
||||
path := c.Request.URL.Path
|
||||
|
||||
// 编译正则表达式
|
||||
reg := regexp.MustCompile("/(\\d+)$")
|
||||
|
||||
// 进行匹配
|
||||
match := reg.MatchString(c.Request.RequestURI)
|
||||
if match {
|
||||
// 找到最后一个数字的索引
|
||||
lastSlashIndex := strings.LastIndex(c.Request.RequestURI, "/")
|
||||
if lastSlashIndex != -1 {
|
||||
// 替换最后一个数字部分为 :id
|
||||
path = path[:lastSlashIndex] + "/:id" + c.Request.Method
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "请求路径错误",
|
||||
"code": consts.SERVER_ERROR,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
path = path + c.Request.Method
|
||||
}
|
||||
|
||||
// 在apiPermissions中查找对应的API权限
|
||||
hasPermission := apiPermissions[path]
|
||||
if !hasPermission {
|
||||
@@ -186,25 +206,3 @@ func Auth() gin.HandlerFunc {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// Auth 权限
|
||||
// func Auth() gin.HandlerFunc {
|
||||
// return func(c *gin.Context) {
|
||||
// fmt.Println(123)
|
||||
|
||||
//
|
||||
// // result, err := dao.AdminRole.GetAdminRoleById(roleId)
|
||||
// // fmt.Println(result)
|
||||
// // if err != nil {
|
||||
// // responses.FailWithMessage("用户数据错误", c)
|
||||
// // c.Abort()
|
||||
// // return
|
||||
// // }
|
||||
// // responses.OkWithData(&result, c)
|
||||
// // c.Abort()
|
||||
//
|
||||
// // 获取请求路径
|
||||
// // url := c.Request.RequestURI
|
||||
// c.Next()
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user