Browse Source

新增收藏接口

rdluck 4 years ago
parent
commit
9b2c32926f
5 changed files with 228 additions and 1 deletions
  1. 73 1
      controllers/article.go
  2. 69 0
      controllers/user.go
  3. 65 0
      models/article_collect.go
  4. 1 0
      models/db.go
  5. 20 0
      models/user.go

+ 73 - 1
controllers/article.go

@@ -1,6 +1,10 @@
 package controllers
 
-import "hongze/hongze_cygx/models"
+import (
+	"encoding/json"
+	"hongze/hongze_cygx/models"
+	"time"
+)
 
 type ArticleController struct {
 	BaseAuthController
@@ -41,3 +45,71 @@ func (this *ArticleController) Detail() {
 	br.Msg = "获取成功"
 	br.Data = detail
 }
+
+// @Title 收藏
+// @Description 收藏
+// @Param	request	body models.ArticleCollectReq true "type json string"
+// @Success 200 {object} models.FontsCollectResp
+// @router /collect [post]
+func (this *ArticleController) FunnyCollect() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	user := this.User
+	if user == nil {
+		br.Msg = "请重新登录"
+		br.Ret = 408
+		return
+	}
+	uid := user.UserId
+	var req models.ArticleCollectReq
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	count, err := models.GetArticleCollectCount(uid, req.ArticleId)
+	if err != nil {
+		br.Msg = "获取数据失败!"
+		br.ErrMsg = "获取数据失败,Err:" + err.Error()
+		return
+	}
+	resp := new(models.ArticleCollectResp)
+	if count <= 0 {
+		item := new(models.CygxArticleCollect)
+		item.CreateTime = time.Now()
+		item.ArticleId = req.ArticleId
+		item.UserId = uid
+		item.CreateTime = time.Now()
+		_, err = models.AddCygxArticleCollect(item)
+		if err != nil {
+			br.Msg = "收藏失败"
+			br.ErrMsg = "收藏失败,Err:" + err.Error()
+			return
+		}
+		br.Msg = "收藏成功"
+		resp.Status = 1
+	} else {
+		err = models.RemoveArticleCollect(uid, req.ArticleId)
+		if err != nil {
+			br.Msg = "取消收藏失败"
+			br.ErrMsg = "取消收藏失败,Err:" + err.Error()
+			return
+		}
+		br.Msg = "已取消收藏"
+		resp.Status = 2
+	}
+	collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
+	if err != nil {
+		br.Msg = "获取数据失败"
+		br.ErrMsg = "获取数据失败,Err:" + err.Error()
+		return
+	}
+	resp.CollectCount=collectTotal
+	br.Ret = 200
+	br.Success = true
+	br.Data = resp
+}

+ 69 - 0
controllers/user.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/utils"
+	"rdluck_tools/paging"
 	"strconv"
 	"strings"
 	"time"
@@ -269,3 +270,71 @@ func (this *UserController) CheckLogin() {
 //	br.Success = true
 //	br.Data = resp
 //}
+
+// @Title 获取我的收藏
+// @Description 获取我的收藏列表
+// @Param   PageSize    query   int true       "PageSize"
+// @Param   CurrentIndex    query   int true       "CurrentIndex"
+// @Success 200 {object} models.ArticleCollectListResp
+// @router /collect/list [get]
+func (this *UserController) CollectList() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	userId := this.User.UserId
+	var pageSize, currentIndex, startSize int
+	pageSize, _ = this.GetInt("PageSize")
+	currentIndex, _ = this.GetInt("CurrentIndex")
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+	startSize = utils.StartIndex(currentIndex, pageSize)
+
+	total, err := models.GetArticleUserCollectCount(userId)
+	if err != nil {
+		br.Msg = "获取数据失败"
+		br.ErrMsg = "获取数据失败,Err:" + err.Error()
+		return
+	}
+	list, err := models.GetArticleUserCollectList(startSize, pageSize, userId)
+	if err != nil {
+		br.Msg = "获取数据失败"
+		br.ErrMsg = "获取数据失败,Err:" + err.Error()
+		return
+	}
+	//listLen := len(list)
+	//for i := 0; i < listLen; i++ {
+	//	item := list[i]
+	//	uf, err := cache.GetUserFunnyCache(userId, item.FunnyId)
+	//	if err != nil && err.Error()!=utils.ErrNoRow() {
+	//		br.Msg = "获取信息失败"
+	//		br.ErrMsg = "判断是否收藏失败,Err:" + err.Error()
+	//		return
+	//	}
+	//	if uf!=nil {
+	//		if uf.IsCollect > 0 {
+	//			list[i].IsCollect=true
+	//		}
+	//		if uf.IsComment > 0 {
+	//			list[i].IsComment=true
+	//		}
+	//		if uf.IsLike > 0 {
+	//			list[i].IsLike=true
+	//		}
+	//	}
+	//	list[i].CreateTimeStamp=list[i].CreateTime.Unix()
+	//}
+	page := paging.GetPaging(currentIndex, pageSize, total)
+	resp := new(models.ArticleCollectListResp)
+	resp.List = list
+	resp.Paging = page
+	br.Msg = "获取成功!"
+	br.Ret = 200
+	br.Success = true
+	br.Data = resp
+}

+ 65 - 0
models/article_collect.go

@@ -0,0 +1,65 @@
+package models
+
+import (
+	"rdluck_tools/orm"
+	"time"
+)
+
+type CygxArticleCollect struct {
+	Id         int `orm:"column(id);pk"`
+	ArticleId  int
+	UserId     int
+	CreateTime time.Time
+}
+
+//添加收藏信息
+func AddCygxArticleCollect(item *CygxArticleCollect) (lastId int64, err error) {
+	o := orm.NewOrm()
+	lastId, err = o.Insert(item)
+	return
+}
+
+
+type ArticleCollectReq struct {
+	ArticleId   int `description:"报告id"`
+}
+
+type ArticleCollectResp struct {
+	Status       int `description:"1:收藏,2:取消收藏"`
+	CollectCount int `description:"收藏总数"`
+}
+
+func RemoveArticleCollect(userId, articleId int) (err error) {
+	o := orm.NewOrm()
+	sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND article_id=? `
+	_, err = o.Raw(sql, userId, articleId).Exec()
+	return
+}
+
+func GetArticleCollectUsersCount(articleId int) (count int, err error) {
+	sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE article_id=? `
+	err = orm.NewOrm().Raw(sql, articleId).QueryRow(&count)
+	return
+}
+
+func GetArticleCollectCount(userId, articleId int) (count int, err error) {
+	sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND article_id=? `
+	err = orm.NewOrm().Raw(sql, userId, articleId).QueryRow(&count)
+	return
+}
+
+type ArticleCollectList struct {
+	Id         int `orm:"column(id);pk"`
+	ArticleId  int
+	UserId     int
+	CreateTime time.Time
+	Title           string `description:"标题"`
+	TitleEn         string `description:"英文标题 "`
+	UpdateFrequency string `description:"更新周期"`
+	CreateDate      string `description:"创建时间"`
+	PublishDate     string `description:"发布时间"`
+	Body            string `description:"内容"`
+	Abstract        string `description:"摘要"`
+	CategoryName    string `description:"一级分类"`
+	SubCategoryName string `description:"二级分类"`
+}

+ 1 - 0
models/db.go

@@ -31,5 +31,6 @@ func init() {
 		new(CygxSession),
 		new(WxUserLog),
 		new(WxUserCode),
+		new(CygxArticleCollect),
 	)
 }

+ 20 - 0
models/user.go

@@ -3,6 +3,7 @@ package models
 import (
 	"hongze/hongze_cygx/utils"
 	"rdluck_tools/orm"
+	"rdluck_tools/paging"
 	"time"
 )
 
@@ -143,3 +144,22 @@ type CheckStatusResp struct {
 	IsAuth         bool   `description:"true:需要授权,false:不需要授权"`
 	PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
 }
+
+func GetArticleUserCollectCount(userId int) (count int, err error) {
+	sql := `SELECT COUNT(1) AS count FROM cygx_article_collect AS a WHERE a.user_id=? `
+	err = orm.NewOrm().Raw(sql,userId).QueryRow(&count)
+	return
+}
+
+func GetArticleUserCollectList(startSize, pageSize, userId int) (items []*ArticleCollectList, err error) {
+	sql := `SELECT a.* FROM cygx_article_collect AS a
+			WHERE a.user_id=? 
+           ORDER BY a.create_time DESC LIMIT ?,? `
+	_, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+type ArticleCollectListResp struct {
+	List   []*ArticleCollectList
+	Paging *paging.PagingItem
+}