Lee-WineList/api/base.go
2023-04-27 15:56:12 +08:00

45 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"
"strconv"
)
// 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, _ = strconv.Atoi(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()
}
}
}