瀏覽代碼

Merge branch 'yb/9.3' into debug

hsun 2 年之前
父節點
當前提交
e1a768c13f

+ 13 - 2
controller/collection/collection.go

@@ -64,12 +64,18 @@ func Collect(c *gin.Context) {
 	}
 
 	userInfo := user.GetInfoByClaims(c)
-	if e := collection.AddCollection(int(userInfo.UserID), req.CollectionType, req.PrimaryId, req.ExtendId, req.SourceAgent); e != nil {
+	if userInfo.UserID <= 0 {
+		response.Fail("请登录后操作", c)
+		return
+	}
+
+	collectionId, e := collection.AddCollection(int(userInfo.UserID), req.CollectionType, req.PrimaryId, req.ExtendId, req.SourceAgent)
+	if e != nil {
 		response.FailMsg("操作失败", "加入收藏失败, Err: "+e.Error(), c)
 		return
 	}
 
-	response.Ok("操作成功", c)
+	response.OkData("操作成功", collectionId, c)
 }
 
 // Cancel
@@ -88,6 +94,11 @@ func Cancel(c *gin.Context) {
 	}
 
 	userInfo := user.GetInfoByClaims(c)
+	if userInfo.UserID <= 0 {
+		response.Fail("请登录后操作", c)
+		return
+	}
+
 	if e := collection.CancelCollection(int(userInfo.UserID), req.CollectionId); e != nil {
 		response.FailMsg("操作失败", "取消收藏失败, Err: "+e.Error(), c)
 		return

+ 1 - 1
controller/community/video.go

@@ -34,7 +34,7 @@ func VideoList(c *gin.Context) {
 		req.PageSize = utils.PageSize20
 	}
 	userInfo := user.GetInfoByClaims(c)
-	list, err := community.GetVideoList(req.PageIndex, req.PageSize, req.VideoId, req.VarietyTagId, req.Keywords)
+	list, err := community.GetVideoList(int(userInfo.UserID), req.PageIndex, req.PageSize, req.VideoId, req.VarietyTagId, req.Keywords)
 	if err != nil {
 		response.FailMsg("获取失败", "VideoList ErrMsg:"+err.Error(), c)
 		return

+ 2 - 0
models/response/community.go

@@ -92,6 +92,7 @@ type CommunityVideoItem struct {
 	TeaseTotal          int                                 `json:"tease_total" description:"吐槽数"`
 	CommentTotal        int                                 `json:"comment_total" description:"总共评论数"`
 	CommentList         []*CommunityQuestionCommentListItem `json:"comment_list"`
+	CollectionId        int                                 `description:"收藏ID: 大于0则表示已收藏" json:"collection_id"`
 }
 
 // RoadVideoItem 线上路演
@@ -109,6 +110,7 @@ type RoadVideoItem struct {
 	PublishTime         string `json:"publish_time"`
 	CreateTime          string `json:"create_time"`
 	ModifyTime          string `json:"modify_time"`
+	CollectionId        int    `description:"收藏ID: 大于0则表示已收藏" json:"collection_id"`
 }
 
 type RoadVideoItemResp struct {

+ 8 - 6
models/response/report.go

@@ -12,6 +12,7 @@ type ReportDetail struct {
 	LikeNum           int64                    `description:"点赞总数" json:"like_num"`
 	LikeEnabled       int8                     `description:"是否已点赞: 0-未点赞 1-已点赞" json:"like_enabled"`
 	ReportShowType    int                      `descritpion:"展示形式:1-列表 2-专栏" json:"report_show_type"`
+	CollectionId      int                      `description:"收藏ID: 大于0则表示已收藏" json:"collection_id"`
 }
 
 type ReportChapterListItem struct {
@@ -74,12 +75,13 @@ type ReportChapterItem struct {
 }
 
 type ReportChapterDetail struct {
-	ReportChapterItem      *ReportChapterItem      `json:"report_chapter_item"`
-	PermissionCheck        *PermissionCheckInfo    `json:"permission_check"`
-	ReportChapterMenuList  []*ReportChapterMenu    `json:"report_chapter_menu_list"`
-	AuthOk                 bool                    `json:"auth_ok"`
-	LikeNum                int64  `description:"点赞总数" json:"like_num"`
-	LikeEnabled            int8 `description:"是否已点赞: 0-未点赞 1-已点赞" json:"like_enabled"`
+	ReportChapterItem     *ReportChapterItem   `json:"report_chapter_item"`
+	PermissionCheck       *PermissionCheckInfo `json:"permission_check"`
+	ReportChapterMenuList []*ReportChapterMenu `json:"report_chapter_menu_list"`
+	AuthOk                bool                 `json:"auth_ok"`
+	LikeNum               int64                `description:"点赞总数" json:"like_num"`
+	LikeEnabled           int8                 `description:"是否已点赞: 0-未点赞 1-已点赞" json:"like_enabled"`
+	CollectionId          int                  `description:"收藏ID: 大于0则表示已收藏" json:"collection_id"`
 }
 
 type ReportChapterMenu struct {

+ 16 - 0
models/tables/yb_user_collection/model.go

@@ -38,3 +38,19 @@ func GetItemById(collectionId int) (item *YbUserCollection, err error) {
 		First(&item).Error
 	return
 }
+
+// GetItemByCondition 条件获取收藏
+func GetItemByCondition(condition string, pars []interface{}) (item *YbUserCollection, err error) {
+	err = global.DEFAULT_MYSQL.Model(YbUserCollection{}).
+		Where(condition, pars...).
+		First(&item).Error
+	return
+}
+
+// GetListByCondition 条件获取收藏列表
+func GetListByCondition(condition string, pars []interface{}) (list []*YbUserCollection, err error) {
+	err = global.DEFAULT_MYSQL.Model(YbUserCollection{}).
+		Where(condition, pars...).
+		Scan(&list).Error
+	return
+}

+ 72 - 1
services/collection/collection.go

@@ -22,7 +22,7 @@ const (
 )
 
 // AddCollection 加入收藏
-func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int) (err error) {
+func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int) (collectionId int, err error) {
 	title := ""
 	nowTime := time.Now().Local()
 	publishTime := nowTime
@@ -69,6 +69,17 @@ func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int)
 		return
 	}
 
+	// 是否已收藏
+	isCollect, e := GetUserCollectByItem(userId, collectionType, primaryId, extendId)
+	if e != nil {
+		err = e
+		return
+	}
+	if isCollect > 0 {
+		collectionId = isCollect
+		return
+	}
+
 	item := &yb_user_collection.YbUserCollection{
 		CollectionType: collectionType,
 		UserID:         userId,
@@ -85,6 +96,7 @@ func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int)
 		err = errors.New("新增收藏失败, Err: " + e.Error())
 		return
 	}
+	collectionId = item.CollectionID
 	return
 }
 
@@ -319,3 +331,62 @@ func GetCollectionList(userId, fromType, currPage, pageSize int, keywords string
 
 	return
 }
+
+// GetUserCollectByItem 获取用户是否已收藏
+func GetUserCollectByItem(userId, collectionType, primaryId, extendId int) (collectionId int, err error) {
+	if userId <= 0 || collectionType <= 0 || primaryId <= 0 {
+		return
+	}
+
+	var cond string
+	var pars []interface{}
+	cond += `user_id = ?`
+	pars = append(pars, userId)
+	cond += ` AND collection_type = ?`
+	pars = append(pars, collectionType)
+	cond += ` AND primary_id = ?`
+	pars = append(pars, primaryId)
+	if extendId > 0 {
+		cond += ` AND extend_id = ?`
+		pars = append(pars, extendId)
+	}
+
+	item, e := yb_user_collection.GetItemByCondition(cond, pars)
+	if e != nil && e != utils.ErrNoRow {
+		err = errors.New("获取用户收藏失败, Err: " + e.Error())
+		return
+	}
+	if item != nil && item.CollectionID > 0 {
+		collectionId = item.CollectionID
+	}
+	return
+}
+
+// GetUserCollectByItem 获取用户是否已收藏-列表(视频列表、路演视频列表)
+func GetUserCollectByList(userId, collectionType int, primaryIds []int) (collectMap map[int]int, err error) {
+	collectMap = make(map[int]int, 0)
+	if userId <= 0 || collectionType <= 0 || len(primaryIds) == 0 {
+		return
+	}
+
+	var cond string
+	var pars []interface{}
+	cond += `user_id = ?`
+	pars = append(pars, userId)
+	cond += ` AND collection_type = ?`
+	pars = append(pars, collectionType)
+	cond += ` AND primary_id IN (?)`
+	pars = append(pars, primaryIds)
+
+	list, e := yb_user_collection.GetListByCondition(cond, pars)
+	if e != nil {
+		err = errors.New("获取用户收藏列表失败, Err: " + e.Error())
+		return
+	}
+	for i := range list {
+		if utils.InArrayByInt(primaryIds, list[i].PrimaryID) {
+			collectMap[list[i].PrimaryID] = list[i].CollectionID
+		}
+	}
+	return
+}

+ 39 - 12
services/community/video.go

@@ -11,6 +11,7 @@ import (
 	"hongze/hongze_yb/models/tables/yb_community_video"
 	"hongze/hongze_yb/models/tables/yb_community_video_play_log"
 	"hongze/hongze_yb/models/tables/yb_road_video"
+	"hongze/hongze_yb/services/collection"
 	"hongze/hongze_yb/services/company"
 	"hongze/hongze_yb/services/user"
 	"hongze/hongze_yb/utils"
@@ -20,7 +21,7 @@ import (
 )
 
 // GetVideoList 获取视频列表
-func GetVideoList(pageIndex, pageSize, videoId, varietyTagId int, keywords string) (resp []*response.CommunityVideoItem, err error) {
+func GetVideoList(userId, pageIndex, pageSize, videoId, varietyTagId int, keywords string) (resp []*response.CommunityVideoItem, err error) {
 	condition := make(map[string]interface{})
 	// 分享点进来的直接定位到具体视频
 	if videoId > 0 {
@@ -42,6 +43,7 @@ func GetVideoList(pageIndex, pageSize, videoId, varietyTagId int, keywords strin
 	if len(list) <= 0 {
 		return
 	}
+	videoIds := make([]int, 0)
 	for _, v := range list {
 		item := &response.CommunityVideoItem{
 			CommunityVideoID:    v.CommunityVideoID,
@@ -59,6 +61,17 @@ func GetVideoList(pageIndex, pageSize, videoId, varietyTagId int, keywords strin
 			TencentId:           getSubTencentUrl(v.TencentURL),
 		}
 		resp = append(resp, item)
+		videoIds = append(videoIds, v.CommunityVideoID)
+	}
+
+	// 收藏
+	collectMap, e := collection.GetUserCollectByList(userId, collection.CollectionTypeVideo, videoIds)
+	if e != nil {
+		err = e
+		return
+	}
+	for i := range resp {
+		resp[i].CollectionId = collectMap[resp[i].CommunityVideoID]
 	}
 	return
 }
@@ -72,7 +85,7 @@ func SaveVideoPlayLog(userInfo user.UserInfo, videoId, sourceAgent int, videoTyp
 			err = errors.New("获取视频信息失败, Err: " + e.Error())
 			return
 		}
-	}else{
+	} else {
 		_, e := yb_road_video.GetItemById(videoId)
 		if e != nil {
 			errMsg = "视频不存在或未发布"
@@ -150,7 +163,7 @@ func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, char
 	ParentPermissionNameMap := make(map[string]string)
 	ParentPermissionChildMap := make(map[string]int)
 	for _, v := range validPermissionList {
-		permissionIds += "'"+strconv.Itoa(v.ChartPermissionID) + "'|"
+		permissionIds += "'" + strconv.Itoa(v.ChartPermissionID) + "'|"
 		validPermissionMap["'"+strconv.Itoa(v.ChartPermissionID)+"'"] = struct{}{}
 	}
 	for _, v := range ficcPermissionList {
@@ -163,8 +176,8 @@ func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, char
 		resp.Paging = response.GetPaging(pageIndex, pageSize, 0)
 		return
 	}
-	permissionIds = strings.Trim(permissionIds,"|")
-	condition := `is_deleted = 0 AND publish_state = 1 and chart_permission_ids REGEXP "(`+permissionIds+`)"`
+	permissionIds = strings.Trim(permissionIds, "|")
+	condition := `is_deleted = 0 AND publish_state = 1 and chart_permission_ids REGEXP "(` + permissionIds + `)"`
 	var par []interface{}
 	// 分享点进来的直接定位到具体视频
 	var videoList []*yb_road_video.YbRoadVideo
@@ -177,13 +190,13 @@ func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, char
 				return
 			}
 		}
-		if videoInfo != nil && videoInfo.RoadVideoID > 0{
-			if videoInfo.IsDeleted != 0 || videoInfo.PublishState !=1 {
+		if videoInfo != nil && videoInfo.RoadVideoID > 0 {
+			if videoInfo.IsDeleted != 0 || videoInfo.PublishState != 1 {
 				resp.List = list
 				resp.Paging = response.GetPaging(pageIndex, pageSize, 0)
 				return
 			}
-			permissionIdsSlice := strings.Split(permissionIds,"|")
+			permissionIdsSlice := strings.Split(permissionIds, "|")
 			videoPermissionIdsSlice := strings.Split(videoInfo.ChartPermissionIds, ",")
 			hasPermission := false
 			for _, v1 := range permissionIdsSlice {
@@ -205,11 +218,11 @@ func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, char
 		}
 	} else {
 		if chartPermissionId > 0 {
-			condition += ` and FIND_IN_SET("'`+strconv.Itoa(chartPermissionId)+`'", chart_permission_ids)`
+			condition += ` and FIND_IN_SET("'` + strconv.Itoa(chartPermissionId) + `'", chart_permission_ids)`
 		}
 		if keywords != "" {
 			condition += " and title like ? "
-			par = append(par,"%" + keywords + "%")
+			par = append(par, "%"+keywords+"%")
 		}
 		videoList, total, err = yb_road_video.GetPageListByCondition(condition, par, pageIndex, pageSize)
 		if err != nil {
@@ -245,6 +258,8 @@ func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, char
 		}
 	}
 	var chartPermissionNames string
+
+	videoIds := make([]int, 0)
 	for _, v := range videoList {
 		chartPermissionNames = ""
 		itemParentPermissionNum := make(map[string]int)
@@ -255,7 +270,7 @@ func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, char
 				if p, ok := ParentPermissionNameMap[cid]; ok {
 					if _, ok1 := itemParentPermissionNum[p]; !ok1 {
 						itemParentPermissionNum[p] = 0
-						chartPermissionNames += p +","
+						chartPermissionNames += p + ","
 					}
 				}
 			}
@@ -305,7 +320,19 @@ func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, char
 			ModifyTime:          v.ModifyTime.Format(utils.FormatDateTime),
 		}
 		list = append(list, item)
+		videoIds = append(videoIds, item.RoadVideoID)
 	}
+
+	// 收藏
+	collectMap, e := collection.GetUserCollectByList(int(userInfo.UserID), collection.CollectionTypeRoadVideo, videoIds)
+	if e != nil {
+		err = e
+		return
+	}
+	for i := range list {
+		list[i].CollectionId = collectMap[list[i].RoadVideoID]
+	}
+
 	resp.List = list
 	resp.Paging = response.GetPaging(pageIndex, pageSize, int(total))
 	return
@@ -407,4 +434,4 @@ func HandleCommentByCommunityVideoItemList(questionList []*response.CommunityVid
 	}
 
 	return
-}
+}

+ 9 - 0
services/report/report.go

@@ -21,6 +21,7 @@ import (
 	"hongze/hongze_yb/models/tables/report_chapter_type"
 	"hongze/hongze_yb/models/tables/report_chapter_type_permission"
 	"hongze/hongze_yb/services"
+	"hongze/hongze_yb/services/collection"
 	"hongze/hongze_yb/services/company"
 	elasticService "hongze/hongze_yb/services/elastic"
 	"hongze/hongze_yb/services/user"
@@ -508,6 +509,14 @@ func GetReportDetail(userinfo user.UserInfo, reportId int) (reportDetail respons
 	reportDetail.LikeNum = likeNum
 	reportDetail.LikeEnabled = likeEnabled
 	reportDetail.ReportShowType = int(firstClassify.ShowType)
+
+	// 收藏
+	collectionId, e := collection.GetUserCollectByItem(int(userinfo.UserID), collection.CollectionTypeReport, reportId, 0)
+	if e != nil {
+		err = e
+		return
+	}
+	reportDetail.CollectionId = collectionId
 	return
 }
 

+ 9 - 0
services/report/report_chapter.go

@@ -13,6 +13,7 @@ import (
 	"hongze/hongze_yb/models/tables/rddp/report_chapter"
 	"hongze/hongze_yb/models/tables/report_chapter_type"
 	"hongze/hongze_yb/services"
+	"hongze/hongze_yb/services/collection"
 	"hongze/hongze_yb/services/company"
 	"hongze/hongze_yb/services/user"
 	"hongze/hongze_yb/utils"
@@ -295,6 +296,14 @@ func GetChapterDetail(user user.UserInfo, reportChapterId int) (reportChapterDet
 	reportChapterDetail.AuthOk = authOk
 	reportChapterDetail.LikeNum = likeNum
 	reportChapterDetail.LikeEnabled = likeEnabled
+
+	// 收藏
+	collectionId, e := collection.GetUserCollectByItem(int(user.UserID), collection.CollectionTypeReport, reportChapterItem.ReportId, reportChapterId)
+	if e != nil {
+		err = e
+		return
+	}
+	reportChapterDetail.CollectionId = collectionId
 	return
 }
 

+ 25 - 3
utils/common.go

@@ -963,8 +963,8 @@ func InArray(needle interface{}, hyStack interface{}) bool {
 
 // bit转MB 保留小数
 func Bit2MB(bitSize int64, prec int) (size float64) {
-	mb := float64(bitSize)/float64(1024*1024)
-	size, _ = strconv.ParseFloat(strconv.FormatFloat(mb,'f',prec,64), 64)
+	mb := float64(bitSize) / float64(1024*1024)
+	size, _ = strconv.ParseFloat(strconv.FormatFloat(mb, 'f', prec, 64), 64)
 	return
 }
 
@@ -977,4 +977,26 @@ func SubStr(str string, subLen int) string {
 	}
 	str = string(strRune[:bodyRuneLen])
 	return str
-}
+}
+
+// InArrayByInt php中的in_array(判断Int类型的切片中是否存在该int值)
+func InArrayByInt(idIntList []int, searchId int) (has bool) {
+	for _, id := range idIntList {
+		if id == searchId {
+			has = true
+			return
+		}
+	}
+	return
+}
+
+// InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
+func InArrayByStr(idStrList []string, searchId string) (has bool) {
+	for _, id := range idStrList {
+		if id == searchId {
+			has = true
+			return
+		}
+	}
+	return
+}