初始化提交
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/responses"
|
||||
"hospital-admin-api/consts"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Auth Auth认证
|
||||
func Auth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 获取角色id
|
||||
roleId := c.GetInt64("RoleId")
|
||||
if roleId == 0 {
|
||||
responses.Fail(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户id
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId == 0 {
|
||||
responses.Fail(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户数据
|
||||
AdminUserDao := dao.AdminUser{}
|
||||
adminUser, err := AdminUserDao.GetAdminUserFirstById(userId)
|
||||
if err != nil || adminUser.UserId == 0 {
|
||||
responses.FailWithMessage("用户数据错误", c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
if adminUser.Status == 2 {
|
||||
responses.FailWithMessage("用户审核中", c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
if adminUser.Status == 3 {
|
||||
responses.FailWithMessage("用户已删除或禁用", c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取角色数据
|
||||
AdminRoleDao := dao.AdminRole{}
|
||||
adminRole, err := AdminRoleDao.GetAdminRoleFirstById(roleId)
|
||||
if err != nil || adminRole.RoleId == 0 {
|
||||
responses.FailWithMessage("用户数据错误", c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 超级管理员不验证权限
|
||||
if adminRole.IsAdmin == 1 {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取角色菜单
|
||||
AdminRoleMenuDao := dao.AdminRoleMenu{}
|
||||
adminRoleMenu, _ := AdminRoleMenuDao.GetAdminMenuListByRoleId(roleId)
|
||||
if adminRoleMenu == nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"message": "暂无权限",
|
||||
"code": consts.CLIENT_HTTP_UNAUTHORIZED,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
var apiPermissions = make(map[string]bool)
|
||||
|
||||
// 获取菜单对应api
|
||||
AdminMenuApiDao := dao.AdminMenuApi{}
|
||||
for _, v := range adminRoleMenu {
|
||||
AdminMenuApi, _ := AdminMenuApiDao.GetAdminMenuApiListByMenuID(v.MenuID)
|
||||
if AdminMenuApi == nil {
|
||||
// 菜单无需权限
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 将API权限存储在apiPermissions中
|
||||
for _, api := range AdminMenuApi {
|
||||
apiPermissions[api.API.APIPath+api.API.APIMethod] = true
|
||||
}
|
||||
}
|
||||
|
||||
path := ""
|
||||
// 找到最后一个数字的索引
|
||||
lastSlashIndex := strings.LastIndex(c.Request.RequestURI, "/")
|
||||
if lastSlashIndex != -1 {
|
||||
// 替换最后一个数字部分为 :id
|
||||
path = c.Request.RequestURI[:lastSlashIndex] + "/:id" + c.Request.Method
|
||||
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "请求路径错误",
|
||||
"code": consts.SERVER_ERROR,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 在apiPermissions中查找对应的API权限
|
||||
hasPermission := apiPermissions[path]
|
||||
if !hasPermission {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"message": "暂无权限",
|
||||
"code": consts.CLIENT_HTTP_UNAUTHORIZED,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
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()
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,28 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Cors
|
||||
// @Description: 跨域中间件
|
||||
// @return gin.HandlerFunc
|
||||
func Cors() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
method := c.Request.Method
|
||||
origin := c.Request.Header.Get("Origin")
|
||||
if origin != "" {
|
||||
c.Header("Access-Control-Allow-Origin", origin)
|
||||
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
||||
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
|
||||
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
|
||||
c.Header("Access-Control-Allow-Credentials", "false")
|
||||
c.Set("content-type", "application/json")
|
||||
}
|
||||
if method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"hospital-admin-api/api/responses"
|
||||
"hospital-admin-api/consts"
|
||||
"hospital-admin-api/global"
|
||||
"hospital-admin-api/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Jwt jwt认证
|
||||
func Jwt() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
authorization := c.Request.Header.Get("Authorization")
|
||||
if authorization == "" || !strings.HasPrefix(authorization, "Bearer ") {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": "请求未授权",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 去除Bearer
|
||||
authorization = authorization[7:] // 截取字符
|
||||
|
||||
// 检测是否存在黑名单
|
||||
res, _ := global.Redis.Get(c, "jwt_black_"+authorization).Result()
|
||||
if res != "" {
|
||||
responses.Fail(c)
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 解析jwt
|
||||
t, err := utils.ParseJwt(authorization)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误/过期",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("UserId", t.UserId) // 用户id
|
||||
c.Set("RoleId", t.RoleId) // 角色id
|
||||
c.Set("DeptId", t.DeptId) // 部门id
|
||||
c.Set("PostId", t.PostId) // 岗位id
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
"hospital-admin-api/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Logrus 日志中间件
|
||||
func Logrus() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
// 开始时间
|
||||
startTime := time.Now()
|
||||
|
||||
// 处理请求
|
||||
c.Next()
|
||||
|
||||
// 结束时间
|
||||
endTime := time.Now()
|
||||
|
||||
// 执行时间
|
||||
latencyTime := fmt.Sprintf("%6v", endTime.Sub(startTime))
|
||||
|
||||
// 请求方式
|
||||
reqMethod := c.Request.Method
|
||||
|
||||
// 请求路由
|
||||
reqUri := c.Request.RequestURI
|
||||
|
||||
// 状态码
|
||||
statusCode := c.Writer.Status()
|
||||
|
||||
// 请求IP
|
||||
clientIP := c.ClientIP()
|
||||
|
||||
// 日志格式
|
||||
global.Logger.WithFields(logrus.Fields{
|
||||
"http_status": statusCode,
|
||||
"total_time": latencyTime,
|
||||
"ip": clientIP,
|
||||
"method": reqMethod,
|
||||
"uri": reqUri,
|
||||
}).Info("access")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user