45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
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()
|
||
}
|
||
}
|
||
}
|