80 lines
1.6 KiB
Go
80 lines
1.6 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"vote-admin-video-api/api/dao"
|
|
"vote-admin-video-api/api/responses"
|
|
"vote-admin-video-api/consts"
|
|
)
|
|
|
|
// Auth Auth认证
|
|
func Auth() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// 获取用户id
|
|
adminUserId := c.GetInt64("AdminUserId")
|
|
if adminUserId == 0 {
|
|
responses.Fail(c)
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
// 获取用户数据
|
|
adminUserDao := dao.AdminUserDao{}
|
|
adminUser, err := adminUserDao.GetAdminUserById(adminUserId)
|
|
if err != nil || adminUser == nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"message": "用户数据错误",
|
|
"code": consts.UserStatusError,
|
|
"data": "",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if adminUser.Status == 2 {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"message": "用户审核中",
|
|
"code": consts.UserStatusError,
|
|
"data": "",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if adminUser.Status == 3 {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"message": "用户状态异常",
|
|
"code": consts.UserStatusError,
|
|
"data": "",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if adminUser.IsDisabled == 1 {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"message": "用户已被禁用",
|
|
"code": consts.UserStatusError,
|
|
"data": "",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if adminUser.IsDeleted == 1 {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"message": "用户状态异常",
|
|
"code": consts.UserStatusError,
|
|
"data": "",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
c.Set("AdminUserId", adminUserId) // 用户id
|
|
|
|
c.Next()
|
|
}
|
|
}
|