user.go 3.5 KB
Newer Older
1 2 3 4 5
package user_r

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
hujiebin's avatar
hujiebin committed
6 7
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/resource/redisCli"
8
	"github.com/gin-gonic/gin"
hujiebin's avatar
hujiebin committed
9 10 11 12
	"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"
13 14
	"hilo-user/req"
	"hilo-user/resp"
hujiebin's avatar
hujiebin committed
15
	"time"
16 17 18 19 20 21 22 23 24 25 26 27 28 29
)

// @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)
hujiebin's avatar
hujiebin committed
30
	userId, lang, err := req.GetUserIdLang(c, myContext)
31 32 33 34
	if err != nil {
		return myContext, err
	}

hujiebin's avatar
hujiebin committed
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
	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
	}
65 66
	model := domain.CreateModelContext(myContext)

hujiebin's avatar
hujiebin committed
67 68 69 70 71 72 73 74 75
	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)
76 77 78
	if err != nil {
		return myContext, err
	}
hujiebin's avatar
hujiebin committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	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,
					nil, cvUserDetail.MyGroupPowerName, cvUserDetail.Noble.Level)
				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)
		}
	}

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