user.go 5.53 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9
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"
chenweijian's avatar
chenweijian committed
10
	"hilo-user/cv/user_cv"
hujiebin's avatar
hujiebin committed
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 92
	"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
93
					cvUserDetail.Medals, cvUserDetail.MyGroupPowerName, cvUserDetail.Noble.Level)
hujiebin's avatar
hujiebin committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
				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
}
chenweijian's avatar
chenweijian committed
110 111 112

// @Tags 用户
// @Summary 房间内获取用户信息
chenweijian's avatar
chenweijian committed
113
// @Param userExternalId query string true "userExternalId"
chenweijian's avatar
chenweijian committed
114
// @Param groupId query string false "群组id,当传了该id,则返回该用户在该群组的身份"
chenweijian's avatar
chenweijian committed
115
// @Success 200 {object} user_cv.CvUserDetailRoom
chenweijian's avatar
chenweijian committed
116
// @Router /v2/user/detail/room [get]
chenweijian's avatar
chenweijian committed
117 118 119 120 121 122
func GetUserDetailInRoom(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, lang, err := req.GetUserIdLang(c, myContext)
	if err != nil {
		return myContext, err
	}
chenweijian's avatar
chenweijian committed
123
	otherUserId, err := req.ToUserId(myContext, c.Query("userExternalId"))
chenweijian's avatar
chenweijian committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	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,
					cvUserDetail.Medals, 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)
		}
	}

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