You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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()
}
}
}