diff --git a/controllers/base.go b/controllers/base.go index f376e85..9dccbda 100644 --- a/controllers/base.go +++ b/controllers/base.go @@ -104,7 +104,7 @@ func (c *baseController) UploadJPG(imgPath string, videoUrl string) string { return fmt.Sprintf("%s/%s", domain, ret.Key) } -func (c *baseController) GetUserInfo(uid int) object.UserInfo { +func (c *baseController) GetUserInfo(uid int, token string) object.UserInfo { var ( user models.User videos []*models.Video @@ -114,11 +114,6 @@ func (c *baseController) GetUserInfo(uid int) object.UserInfo { //获取用户信息 c.o.QueryTable(new(models.User)).Filter("id", uid).One(&user) followerCount, _ := c.o.QueryTable(new(models.Follow)).Filter("user_id", uid).Count() - if followerCount > 0 { - isFollow = true - } else { - isFollow = false - } followCount, _ := c.o.QueryTable(new(models.Follow)).Filter("followed_user_id", uid).Count() @@ -126,6 +121,17 @@ func (c *baseController) GetUserInfo(uid int) object.UserInfo { workCount, _ := c.o.QueryTable(new(models.Video)).Filter("author_id", uid).All(&videos) + //判断当前用户是否关注该用户 + if baseId, err := utils.GetUserIdFromToken(token); err == nil { + if exist := c.o.QueryTable(new(models.Follow)).Filter("user_id", user.Id).Filter("followed_user_id", baseId).Exist(); exist { + isFollow = true + } else { + isFollow = false + } + } else { + isFollow = false + } + //获取视频获赞数量 for _, video := range videos { count, _ := c.o.QueryTable(new(models.Favorite)).Filter("video_id", video.Id).Count() diff --git a/controllers/comment.go b/controllers/comment.go index ff1bc4b..ed1a293 100644 --- a/controllers/comment.go +++ b/controllers/comment.go @@ -112,7 +112,7 @@ func (c *CommentController) CommentList() { var commentInfos []object.CommentInfo c.o.QueryTable(new(models.Comment)).Filter("video_id", videoId).OrderBy("-create_time").All(&comments) for _, comm := range comments { - userInfo := c.GetUserInfo(user.Id) + userInfo := c.GetUserInfo(user.Id, tokenString) commentInfo := object.CommentInfo{ Content: comm.Content, CreateDate: comm.CreateTime.Format("2006-01-02 15:04"), diff --git a/controllers/favorite.go b/controllers/favorite.go index dd1dab2..23cbd1b 100644 --- a/controllers/favorite.go +++ b/controllers/favorite.go @@ -130,7 +130,7 @@ func (c *FavoriteController) FavoriteList() { favs []models.Favorite videos []object.VideoInfo ) - userInfo := c.GetUserInfo(userId) + userInfo := c.GetUserInfo(userId, tokenString) c.o.QueryTable(new(models.Favorite)).Filter("user_id", user.Id).All(&favs) for _, fav := range favs { var video models.Video diff --git a/controllers/follow.go b/controllers/follow.go index 6d36ba4..5f2abba 100644 --- a/controllers/follow.go +++ b/controllers/follow.go @@ -104,7 +104,7 @@ func (c *FollowController) ListFollowRelation() { return } userId, _ := c.GetInt("user_id") - followList, err := GetAllFollowByUserId(c, userId) + followList, err := GetAllFollowByUserId(c, userId, token) if err != nil { c.Data["json"] = map[string]interface{}{ @@ -155,7 +155,7 @@ func (c *FollowController) ListFollowerRelation() { // 获取所有粉丝的被关注用户ID var userInfos []object.UserInfo for _, follow := range follows { - userInfo := c.GetUserInfo(follow.FollowedUserId.Id) + userInfo := c.GetUserInfo(follow.FollowedUserId.Id, token) userInfos = append(userInfos, userInfo) } @@ -209,7 +209,7 @@ func (c *FollowController) ListFriendRelation() { for _, follow := range follows { count, _ := c.o.QueryTable(new(models.Follow)).Filter("user_id", follow.FollowedUserId.Id).Filter("followed_user_id", userId).Count() if count == 1 { - userInfo := c.GetUserInfo(follow.FollowedUserId.Id) + userInfo := c.GetUserInfo(follow.FollowedUserId.Id, token) userInfos = append(userInfos, userInfo) } else { continue @@ -292,7 +292,7 @@ func CancelFollow(c *FollowController, fromUseId int, toUseId int) (err error) { } // GetAllFollowByUserId 获取关注列表 -func GetAllFollowByUserId(c *FollowController, userId int) (followList []object.UserInfo, err error) { +func GetAllFollowByUserId(c *FollowController, userId int, token string) (followList []object.UserInfo, err error) { // 查询出被关注者的id集合 var list []orm.ParamsList @@ -302,7 +302,7 @@ func GetAllFollowByUserId(c *FollowController, userId int) (followList []object. id := list[i][0] id_int, _ := strconv.Atoi(id.(string)) - user := c.GetUserInfo(id_int) + user := c.GetUserInfo(id_int, token) if err == nil { followList = append(followList, user) diff --git a/controllers/user.go b/controllers/user.go index c6688e4..f067f10 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -105,7 +105,7 @@ func (c *UserController) Info() { c.ServeJSON() return } - userInfo := c.GetUserInfo(uid) + userInfo := c.GetUserInfo(uid, token) c.Data["json"] = map[string]interface{}{ "status_code": 0, "status_msg": "获取用户信息成功", diff --git a/controllers/video.go b/controllers/video.go index 465991f..ab62006 100644 --- a/controllers/video.go +++ b/controllers/video.go @@ -26,13 +26,7 @@ func (c *VideoController) Feed() { latestTimeStamp := int(time.Now().Unix()) latestTime := time.Unix(int64(latestTimeStamp), 0) //不需要验证token,不登录也能看视频流 - //token := c.GetString("token") - //if token != "" { - // c.o.QueryTable(new(models.Video)).Filter("create_time__lte", latestTime).OrderBy("-create_time").Limit(30, 0).All(&videos) - //} else { - // - // c.o.QueryTable(new(models.Video)).OrderBy("-create_time").Limit(30, 0).All(&videos) - //} + token := c.GetString("token") c.o.QueryTable(new(models.Video)).Filter("create_time__lte", latestTime).OrderBy("-create_time").Limit(30, 0).All(&videos) if len(videos) == 0 { @@ -55,7 +49,7 @@ func (c *VideoController) Feed() { } else { isFavorite = true } - userInfo := c.GetUserInfo(video.AuthorId.Id) + userInfo := c.GetUserInfo(video.AuthorId.Id, token) videoList = append(videoList, &object.VideoInfo{ ID: video.Id, Title: video.Title, @@ -87,10 +81,8 @@ func (c *VideoController) Publish() { c.PublishFail("token验证失败") return } - var user models.User - username, _ := utils.GetUsernameFromToken(token) - c.o.QueryTable(new(models.User)).Filter("username", username).One(&user) + user, _ := utils.GetUserFromToken(token) if url := c.UploadMP4(c.GetFile("data")); url == "" { c.PublishFail("发布失败") return @@ -109,7 +101,7 @@ func (c *VideoController) Publish() { os.Remove(imgPath) video := models.Video{ - AuthorId: &user, + AuthorId: user, PlayUrl: url, Title: title, CoverUrl: coverUrl, @@ -154,7 +146,7 @@ func (c *VideoController) List() { var ( videos []*models.Video videoList []*object.VideoInfo - userInfo = c.GetUserInfo(uid) + userInfo = c.GetUserInfo(uid, token) ) c.o.QueryTable(new(models.Video)).Filter("author_id", uid).All(&videos) for _, video := range videos {