request.go 2.71 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3
package req

import (
hujiebin's avatar
hujiebin committed
4 5 6
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
7 8 9 10 11 12 13
	"github.com/gin-gonic/gin"
	"hilo-user/domain/cache/user_c"
	"hilo-user/domain/model/res_m"
	"hilo-user/myerr/bizerr"
)

func GetUserId(c *gin.Context) (mysql.ID, error) {
hujiebin's avatar
hujiebin committed
14
	if userIdStr, ok := c.Keys[mycontext.USERID]; ok {
chenweijian's avatar
chenweijian committed
15 16 17 18 19 20
		userId := userIdStr.(uint64)
		return userId, nil
	}
	return 0, bizerr.ParaMissing
}

21 22 23 24 25 26 27 28 29 30 31 32 33 34
func ToUserId(myContext *mycontext.MyContext, externalId mysql.Str) (mysql.ID, error) {
	return user_c.ToUserId(domain.CreateModelContext(myContext), externalId)
	//if externalId == "" {
	//	return 0, myerr.NewSysError("externalId 不能为空")
	//}
	//var user user_m.User
	//if err := mysql.Db.Where(&user_m.User{
	//	ExternalId: externalId,
	//}).First(&user).Error; err != nil {
	//	return 0, bizerr.ExternalIdNoExist
	//}
	//return user.ID, nil
}

chenweijian's avatar
chenweijian committed
35 36
// 获取userId和externalId
func GetUserIdAndExtId(c *gin.Context, myContext *mycontext.MyContext) (mysql.ID, string, error) {
hujiebin's avatar
hujiebin committed
37
	if userIdStr, ok := c.Keys[mycontext.USERID]; ok {
chenweijian's avatar
chenweijian committed
38 39
		userId := userIdStr.(uint64)

hujiebin's avatar
hujiebin committed
40
		externalId, ok := c.Get(mycontext.EXTERNAL_ID)
chenweijian's avatar
chenweijian committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
		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) {
hujiebin's avatar
hujiebin committed
56
	if userIdStr, ok := c.Keys[mycontext.USERID]; ok {
chenweijian's avatar
chenweijian committed
57 58
		userId := userIdStr.(uint64)

hujiebin's avatar
hujiebin committed
59 60
		userCode, ok1 := c.Get(mycontext.CODE)
		externalId, ok2 := c.Get(mycontext.EXTERNAL_ID)
chenweijian's avatar
chenweijian committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
		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) {
hujiebin's avatar
hujiebin committed
76
	if userIdStr, ok := c.Keys[mycontext.USERID]; ok {
chenweijian's avatar
chenweijian committed
77 78
		userId := userIdStr.(uint64)

hujiebin's avatar
hujiebin committed
79
		lang, ok := c.Get(mycontext.LANGUAGE)
chenweijian's avatar
chenweijian committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		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
}