diff --git a/cv/cp_cv/space.go b/cv/cp_cv/space.go index da85cda67c11c7e7e6a1a635aa25db61bff57d7e..4ab3f4ed8e704c2ccab63736e500ca6dc997c701 100644 --- a/cv/cp_cv/space.go +++ b/cv/cp_cv/space.go @@ -14,6 +14,7 @@ import ( type CvCpInfo struct { UserInfo *user_cv.UserTiny `json:"userInfo"` // 用户信息 CpUserInfo *user_cv.UserTiny `json:"cpUserInfo,omitempty"` // cp用户信息 + CreatedUnix int64 `json:"createdUnix"` // cp创建时间 CpDays int `json:"cpDays"` // cp天数 VisitTimes int64 `json:"visitTimes"` // 空间访问量 ApplyToUnbind bool `json:"applyToUnbind"` // 是否申请撤销cp diff --git a/cv/user_cv/user.go b/cv/user_cv/user.go index 993abad9cb2f8cc7d005606a26b2fd72c44adb03..419988ef44336c65cbecc1161549a0f040c1aa32 100644 --- a/cv/user_cv/user.go +++ b/cv/user_cv/user.go @@ -42,6 +42,20 @@ func UserToTiny(user user_m.User) *UserTiny { } } +func UserTinyToCvTiny(user *user_m.UserTiny) *UserTiny { + return &UserTiny{ + Id: user.ID, + ExternalId: user.ExternalId, + Avatar: user.Avatar, + Nick: user.Nick, + Sex: user.Sex, + Code: user.Code, + Country: user.Country, + CountryIcon: user.CountryIcon, + IsPrettyCode: user.IsPrettyCode, + } +} + //用户基本信息 type CvUserBase struct { //不会有返回值 diff --git a/domain/cache/user_c/user.go b/domain/cache/user_c/user.go index 39321ad954d8e90f770543c085a2cd811182dda7..99a9c5e03a1df6027b57d80387d58bee61798d13 100755 --- a/domain/cache/user_c/user.go +++ b/domain/cache/user_c/user.go @@ -1,8 +1,10 @@ package user_c import ( + "git.hilo.cn/hilo-common/_const/common" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" + "git.hilo.cn/hilo-common/utils" redisV8 "github.com/go-redis/redis/v8" "github.com/jinzhu/copier" "hilo-user/_const/redis_key/user_k" @@ -10,6 +12,7 @@ import ( "hilo-user/domain/model/user_m" "hilo-user/myerr" "hilo-user/myerr/bizerr" + "time" ) // 获取用户简要信息 @@ -113,3 +116,89 @@ func cacheUserTiny(model *domain.Model, user *user_m.User) error { } return nil } + +// 获取用户简要信息 +// param userIds 用户id列表 +func GetUserTinyMap(model *domain.Model, userIds []mysql.ID, isDefAvatar bool) (map[mysql.ID]*user_m.UserTiny, error) { + redisNoIds := make([]mysql.ID, 0) + res := make(map[mysql.ID]*user_m.UserTiny) + for _, id := range userIds { + userTiny := new(user_m.UserTiny) + key := user_k.GetUserTinyKey(id) + err := cache.GetJSON(model, key, userTiny) + if err != nil && err != redisV8.Nil { + return nil, err + } + if err == redisV8.Nil { + redisNoIds = append(redisNoIds, id) + } else if userTiny.ID > 0 { + res[id] = userTiny + } + } + // 从db中读 + users, err := user_m.GetUsers(model, redisNoIds) + if err != nil { + return nil, err + } + for _, u := range users { + userTiny := ToUserTinyBy(u) + err = cacheUserTiny(model, u) + if err != nil { + return nil, err + } + res[userTiny.ID] = userTiny + } + if isDefAvatar { + for _, v := range res { + if len(v.Avatar) <= 0 { + if v.Sex == mysql.MAN { + v.Avatar = utils.MakeFullUrl(common.DefaultAvatarMan) + } else if v.Sex == mysql.WOMAN { + v.Avatar = utils.MakeFullUrl(common.DefaultAvatarWoman) + } + } + } + } + return res, nil +} + +func ToUserTinyBy(user *user_m.User) *user_m.UserTiny { + return &user_m.UserTiny{ + ID: user.ID, + Avatar: IfLogoutStr(IfLogout(user.LogoutTime), "", user.Avatar), + ExternalId: user.ExternalId, + Nick: IfLogoutNick(IfLogout(user.LogoutTime), user.Code, user.Nick), + Sex: user.Sex, + Code: user.Code, + Country: user.Country, + CountryIcon: user.CountryIcon, + IsPrettyCode: user.IsPrettyCode(), + IsLogout: IfLogout(user.LogoutTime), + Birthday: BirthdayToUint64(&user.Birthday), + } +} + +func IfLogout(logoutTime int64) bool { + return logoutTime > 0 && time.Now().Unix() > logoutTime +} + +func BirthdayToUint64(birthday *mysql.Timestamp) *uint64 { + if *birthday == 0 { + return nil + } + return (*uint64)(birthday) +} + +func IfLogoutStr(condition bool, trueVal, falseVal string) string { + if condition { + return trueVal + } + return falseVal +} + +func IfLogoutNick(condition bool, code string, nick string) string { + if condition { + return "Hilo No." + code + } + return nick +} diff --git a/domain/model/user_m/user.go b/domain/model/user_m/user.go index 2e938253c5426233226e6497d0df089ca5c0d759..d1b9f36ec6462baa0fc2fd44546a8ad687351248 100755 --- a/domain/model/user_m/user.go +++ b/domain/model/user_m/user.go @@ -135,3 +135,12 @@ func BirthdayToUint64(birthday *mysql.Timestamp) *uint64 { } return (*uint64)(birthday) } + +//获取用户 +func GetUsers(model *domain.Model, ids []mysql.ID) ([]*User, error) { + res := make([]*User, 0) + if err := model.Db.WithContext(model.Context).Where("id in (?)", ids).Find(&res).Error; err != nil { + return nil, myerr.WrapErr(err) + } + return res, nil +} diff --git a/route/cp_r/cp_relation.go b/route/cp_r/cp_relation.go index 04b92697d8a3f16a3dd8cf4b619338eb555a0514..63200137a279bc1c7083943d8bc732ec44b6697e 100644 --- a/route/cp_r/cp_relation.go +++ b/route/cp_r/cp_relation.go @@ -9,9 +9,11 @@ import ( "git.hilo.cn/hilo-common/myerr/comerr" "git.hilo.cn/hilo-common/sdk/tencentyun" "git.hilo.cn/hilo-common/txop/msg" + "git.hilo.cn/hilo-common/utils" "github.com/gin-gonic/gin" "hilo-user/_const/enum/cp_e" "hilo-user/cv/cp_cv" + "hilo-user/cv/user_cv" "hilo-user/domain/cache/user_c" "hilo-user/domain/model/cp_m" "hilo-user/domain/model/diamond_m" @@ -21,6 +23,7 @@ import ( "hilo-user/req" "hilo-user/resp" "strconv" + "time" ) // @Tags cp关系 @@ -329,13 +332,8 @@ func ReplyCpCancel(c *gin.Context) (*mycontext.MyContext, error) { } type CpDetail struct { - Avatar1 string `json:"avatar1"` - Avatar2 string `json:"avatar2"` - Nick1 string `json:"nick1"` - Nick2 string `json:"nick2"` - Grade int `json:"grade"` // 等级 - GradeName string `json:"gradeName"` // 等级称号 - CreatedTime int64 `json:"createdTime"` // cp创建时间 + CpInfo cp_cv.CvCpInfo `json:"cpInfo"` // cp信息 + CpLevel cp_cv.CvCpLevel `json:"cpLevel"` // cp等级 } // @Tags cp关系 @@ -360,18 +358,30 @@ func CpDetailPage(c *gin.Context) (*mycontext.MyContext, error) { var res *CpDetail if cp.Id > 0 { - res = new(CpDetail) - if cp.UserId1 == user.ID { - res.Avatar1 = user.Avatar - res.Nick1 = user.Nick - user2, err := user_c.GetUserTinyById(model, cp.UserId2) - if err != nil { - return myCtx, err - } - res.Avatar2 = user2.Avatar - res.Nick2 = user2.Nick + userIds := []uint64{cp.UserId1, cp.UserId2} + userMap, err := user_c.GetUserTinyMap(model, userIds, false) + if err != nil { + return myCtx, err + } - res.CreatedTime = cp.CreatedTime.Unix() + res = new(CpDetail) + // 返回值 + level := cp_m.GetCpLevel(model, cp.Id) + res.CpLevel = cp_cv.CvCpLevel{ + Level: level.Level, + Points: cp_e.CpLevelPoints[level.Level] + level.Points, + StartPoints: cp_e.CpLevelPoints[level.Level], + ExpireAtUnix: level.ExpireAt.Unix(), + SettlementDate: level.ExpireAt.Format(utils.DATE_FORMAT), + } + if res.CpLevel.Level != cp_e.CpLevelMax { + res.CpLevel.EndPoints = cp_e.CpLevelPoints[res.CpLevel.Level+1] + } + res.CpInfo = cp_cv.CvCpInfo{ + UserInfo: user_cv.UserTinyToCvTiny(userMap[cp.UserId1]), + CpUserInfo: user_cv.UserTinyToCvTiny(userMap[cp.UserId1]), + CpDays: int(time.Now().Sub(cp.CreatedTime).Hours()/24) + 1, + CreatedUnix: cp.CreatedTime.Unix(), } }