package api

import (
	"git.echol.cn/loser/logger/log"
	"github.com/gin-gonic/gin"
	"miniapp/global"
	"miniapp/model/app"
	"miniapp/model/common/response"
	"strconv"
)

func GetUser(ctx *gin.Context, u *app.User, dontResponse, dontCheck bool) {
	userId := ctx.Request.Header.Get("userId")
	if userId == "" {
		if !dontResponse {
			ctx.Abort()
			response.FailWithMessage("未授权操作", ctx)
		}
		return
	}
	id, _ := strconv.Atoi(userId)
	u.ID = uint(id)

	// 查询
	err := global.GVA_DB.Take(&u).Error
	if err != nil {
		log.Errorf("获取用户信息失败：%s", err.Error())
		ctx.Abort()
		if !dontResponse {
			response.FailWithMessage("用户状态异常", ctx)
		}
		return
	}
	// 需要跳过微信绑定检验
	if !dontCheck {
		// 检查微信绑定
		if u.WechatOpenId == "" {
			log.Errorf("%v 未绑定微信", u.Nickname)
			response.FailWithMessage("请先绑定微信", ctx)
			ctx.Abort()
		}
	}
}
