request.go 2.22 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
package req

import (
	"github.com/gin-gonic/gin"
	"hilo-user/_const"
	"hilo-user/domain"
	"hilo-user/domain/cache/user_c"
	"hilo-user/domain/model/res_m"
	"hilo-user/mycontext"
	"hilo-user/myerr/bizerr"
	"hilo-user/resource/mysql"
)

func GetUserId(c *gin.Context) (mysql.ID, error) {
	if userIdStr, ok := c.Keys[_const.USERID]; ok {
		userId := userIdStr.(uint64)
		return userId, nil
	}
	return 0, bizerr.ParaMissing
}

// 获取userId和externalId
func GetUserIdAndExtId(c *gin.Context, myContext *mycontext.MyContext) (mysql.ID, string, error) {
	if userIdStr, ok := c.Keys[_const.USERID]; ok {
		userId := userIdStr.(uint64)

		externalId, ok := c.Get(_const.EXTERNAL_ID)
		if ok {
			return userId, externalId.(string), nil
		} else {
			user, err := user_c.GetUserTinyById(domain.CreateModelContext(myContext), userId)
			if err != nil {
				return 0, "", bizerr.ExternalIdNoExist
			}
			return userId, user.ExternalId, nil
		}
	}
	return 0, "", bizerr.ParaMissing
}

// 获取userId和ExtId和code
func GetUserIdExtIdCode(c *gin.Context, myContext *mycontext.MyContext) (mysql.ID, string, string, error) {
	if userIdStr, ok := c.Keys[_const.USERID]; ok {
		userId := userIdStr.(uint64)

		userCode, ok1 := c.Get(_const.CODE)
		externalId, ok2 := c.Get(_const.EXTERNAL_ID)
		if ok1 && ok2 {
			return userId, externalId.(string), userCode.(string), nil
		} else {
			user, err := user_c.GetUserTinyById(domain.CreateModelContext(myContext), userId)
			if err != nil {
				return 0, "", "", bizerr.ExternalIdNoExist
			}
			return userId, user.ExternalId, user.Code, nil
		}
	}
	return 0, "", "", bizerr.ParaMissing
}

// 获取userId和LANGUAGE
func GetUserIdLang(c *gin.Context, myContext *mycontext.MyContext) (mysql.ID, string, error) {
	if userIdStr, ok := c.Keys[_const.USERID]; ok {
		userId := userIdStr.(uint64)

		lang, ok := c.Get(_const.LANGUAGE)
		if ok {
			return userId, lang.(string), nil
		} else {
			model := domain.CreateModelContext(myContext)
			user, err := user_c.GetUserTinyById(model, userId)
			if err != nil {
				return 0, "", bizerr.ExternalIdNoExist
			}
			lang, err := res_m.GetLangeByCountry(model.Db, user.Country)
			if err != nil {
				return 0, "", err
			}
			return userId, lang, nil
		}
	}
	return 0, "", bizerr.ParaMissing
}