70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
|
package api
|
|||
|
|
|||
|
import (
|
|||
|
"Lee-WineList/client"
|
|||
|
"Lee-WineList/common/constant"
|
|||
|
"Lee-WineList/core"
|
|||
|
"Lee-WineList/model/entity"
|
|||
|
"git.echol.cn/loser/logger/log"
|
|||
|
"github.com/gin-gonic/gin"
|
|||
|
"net/http"
|
|||
|
)
|
|||
|
|
|||
|
// GetAdminUser 获取登录的管理员信息
|
|||
|
func GetAdminUser(ctx *gin.Context, u *entity.AdminUser) {
|
|||
|
userId := ctx.Request.Header.Get("userId")
|
|||
|
if userId == "" {
|
|||
|
ctx.Abort()
|
|||
|
core.R(ctx).FailWithMessageAndCode("未授权操作", http.StatusUnauthorized)
|
|||
|
return
|
|||
|
}
|
|||
|
u.Id = userId
|
|||
|
// 查询用户信息
|
|||
|
err := client.MySQL.Where("id = ?", u.Id).First(&u).Error
|
|||
|
if err != nil {
|
|||
|
log.Errorf("获取用户信息失败:%s", err.Error())
|
|||
|
core.R(ctx).FailWithMessageAndCode("用户状态异常", http.StatusBadRequest)
|
|||
|
ctx.Abort()
|
|||
|
return
|
|||
|
}
|
|||
|
// 校验
|
|||
|
if u.Status != constant.UserStatusActive {
|
|||
|
core.R(ctx).FailWithMessageAndCode("用户已被禁用", http.StatusBadRequest)
|
|||
|
ctx.Abort()
|
|||
|
return
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// GetUser 获取登录的普通用户信息,dontResponse表示只获取用户信息,不论对错, dontCheck表示不检查微信绑定
|
|||
|
func GetUser(ctx *gin.Context, u *entity.User, dontResponse, dontCheck bool) {
|
|||
|
userId := ctx.Request.Header.Get("userId")
|
|||
|
if userId == "" {
|
|||
|
if !dontResponse {
|
|||
|
ctx.Abort()
|
|||
|
core.R(ctx).FailWithMessageAndCode("未授权操作", http.StatusUnauthorized)
|
|||
|
}
|
|||
|
return
|
|||
|
}
|
|||
|
u.Id = userId
|
|||
|
|
|||
|
// 查询
|
|||
|
err := client.MySQL.Take(&u).Error
|
|||
|
if err != nil {
|
|||
|
log.Errorf("获取用户信息失败:%s", err.Error())
|
|||
|
ctx.Abort()
|
|||
|
if !dontResponse {
|
|||
|
core.R(ctx).FailWithMessageAndCode("用户状态异常", http.StatusBadRequest)
|
|||
|
}
|
|||
|
return
|
|||
|
}
|
|||
|
// 需要跳过微信绑定检验
|
|||
|
if !dontCheck {
|
|||
|
// 检查微信绑定
|
|||
|
if u.WechatOpenId == nil || *u.WechatOpenId == "" {
|
|||
|
log.Errorf("%v 未绑定微信", u.Nickname)
|
|||
|
core.R(ctx).FailWithMessageAndCode("请先绑定微信", http.StatusForbidden)
|
|||
|
ctx.Abort()
|
|||
|
}
|
|||
|
}
|
|||
|
}
|