user.go 3.52 KB
Newer Older
hujiebin's avatar
hujiebin 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 84 85 86 87 88 89 90 91
package user_r

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/resource/redisCli"
	"github.com/gin-gonic/gin"
	"hilo-user/_const/redis_key/user_k"
	"hilo-user/domain/model/group_m"
	"hilo-user/domain/model/tim_m"
	"hilo-user/domain/service/user_s"
	"hilo-user/req"
	"hilo-user/resp"
	"time"
)

// @Tags 用户
// @Summary 获取用户详细信息
// @Param token header string true "token"
// @Param timestamp header string true "时间戳"
// @Param nonce header string true "随机数字"
// @Param signature header string true "sha1加密结果"
// @Param deviceType header string true "系统类型 ios android"
// @Param deviceVersion header string true "系统版本"
// @Success 200 {object} user_cv.CvUserDetail
// @Router /v1/user/detail [get]
func UserDetail(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, lang, err := req.GetUserIdLang(c, myContext)
	if err != nil {
		return myContext, err
	}

	cvUserDetail, err := user_s.NewUserService(myContext).GetUserDetail(userId, userId, lang)
	if err != nil {
		return myContext, err
	}
	resp.ResponseOk(c, cvUserDetail)
	return myContext, nil
}

// @Tags 用户
// @Summary 获取用户详细信息
// @Param token header string true "token"
// @Param timestamp header string true "时间戳"
// @Param nonce header string true "随机数字"
// @Param signature header string true "sha1加密结果"
// @Param deviceType header string true "系统类型 ios android"
// @Param deviceVersion header string true "系统版本"
// @Param userExternalId path string true "userExternalId"
// @Param groupId query string false "群组id,当传了该id,则返回该用户在该群组的身份"
// @Success 200 {object} user_cv.CvUserDetail
// @Router /v1/user/detail/{userExternalId} [get]
func UserDetailByExternalId(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, lang, err := req.GetUserIdLang(c, myContext)
	if err != nil {
		return myContext, err
	}
	otherUserId, err := req.ToUserId(myContext, mysql.Str(c.Param("userExternalId")))
	if err != nil {
		return nil, err
	}
	model := domain.CreateModelContext(myContext)

	imGroupId := c.Query("groupId")
	if imGroupId != "" {
		imGroupId, err = group_m.ToImGroupId(model, imGroupId)
		if err != nil {
			return myContext, err
		}
	}

	cvUserDetail, err := user_s.NewUserService(myContext).GetUserDetail(otherUserId, userId, lang)
	if err != nil {
		return myContext, err
	}
	if imGroupId != "" {
		cvUserDetail.GroupRole, err = group_m.GetGroupRoleById(model, imGroupId, otherUserId)
		if err != nil {
			return myContext, err
		}
	}

	if cvUserDetail != nil {
		// 检查是否需要同步
		if n, err := redisCli.GetRedis().Exists(model, user_k.GetKeySyncTimHilo(userId)).Result(); err == nil {
			if n == 0 {
				// FIXME:转异步执行
				err = tim_m.FlushHiloInfo(*cvUserDetail.ExternalId, cvUserDetail.IsVip, cvUserDetail.IsPrettyCode,
iamhujiebin's avatar
iamhujiebin committed
92
					cvUserDetail.Medals, cvUserDetail.MyGroupPowerName, cvUserDetail.Noble.Level)
hujiebin's avatar
hujiebin committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
				if err == nil {
					redisCli.GetRedis().Set(model, user_k.GetKeySyncTimHilo(userId), "1", time.Minute)
				} else {
					model.Log.Info("UserBaseByExternalId, FlushHiloInfo failed: ", err)
				}
			} else {
				model.Log.Info("UserDetailByExternalId, no need to sync yet: ", userId)
			}
		} else {
			model.Log.Info("UserDetailByExternalId, check KeySyncTimHilo failed", err)
		}
	}

	resp.ResponseOk(c, cvUserDetail)
	return myContext, nil
}