Lee-WineList/api/base.go

45 lines
1.1 KiB
Go
Raw Normal View History

2023-04-24 17:19:41 +08:00
package api
import (
"Lee-WineList/client"
"Lee-WineList/core"
"Lee-WineList/model/entity"
"git.echol.cn/loser/logger/log"
"github.com/gin-gonic/gin"
"net/http"
2023-04-27 15:56:12 +08:00
"strconv"
2023-04-24 17:19:41 +08:00
)
// 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
}
2023-04-27 15:56:12 +08:00
u.Id, _ = strconv.Atoi(userId)
2023-04-24 17:19:41 +08:00
// 查询
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()
}
}
}