xingzai 2 éve
szülő
commit
df121e79eb
3 módosított fájl, 188 hozzáadás és 4 törlés
  1. 68 3
      controllers/chart.go
  2. 28 1
      models/chart.go
  3. 92 0
      services/chart.go

+ 68 - 3
controllers/chart.go

@@ -2,10 +2,13 @@ package controllers
 
 import (
 	"encoding/json"
+	"fmt"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/services"
 	"hongze/hongze_cygx/utils"
+	"strconv"
+	"strings"
 	"time"
 )
 
@@ -58,6 +61,7 @@ func (this *ChartController) Collection() {
 		return
 	}
 	mobile := user.Mobile
+	uid := user.UserId
 	resp := new(models.HomeChartListItem)
 	if mobile == "" {
 		br.Ret = 200
@@ -71,21 +75,60 @@ func (this *ChartController) Collection() {
 	}
 	pageSize, _ := this.GetInt("PageSize")
 	currentIndex, _ := this.GetInt("CurrentIndex")
+	var startSize int
 	if pageSize <= 0 {
 		pageSize = utils.PageSize20
 	}
 	if currentIndex <= 0 {
 		currentIndex = 1
-		pageSize = 0
 	}
-	list, err, total := services.GetChartCollectionByApi(mobile, pageSize, (currentIndex-1)*pageSize)
+	startSize = utils.StartIndex(currentIndex, pageSize)
+	//list, err, total := services.GetChartCollectionByApi(mobile, pageSize, (currentIndex-1)*pageSize)
+	//if err != nil {
+	//	br.Msg = "获取图表分类失败!"
+	//	br.ErrMsg = "获取图表分类失败"
+	//	return
+	//}
+	list, err, _ := services.GetChartCollectionByApi(mobile, 9999, 0)
 	if err != nil {
 		br.Msg = "获取图表分类失败!"
 		br.ErrMsg = "获取图表分类失败"
 		return
 	}
+	var chartIds string
+	var condition string
+	var pars []interface{}
+
+	if len(list) > 0 {
+		for _, v := range list {
+			chartIds += strconv.Itoa(v.ChartId) + ","
+		}
+	}
+	chartIds = strings.TrimRight(chartIds, ",")
+	if chartIds == "" {
+		chartIds = "0"
+	}
+	condition = ` AND a.chart_id IN (` + chartIds + `) `
+	total, err := models.GetChartCount(condition, pars)
+	if err != nil {
+		br.Msg = "获取信息失败"
+		br.Msg = "获取帖子总数失败,Err:" + err.Error()
+		return
+	}
+	startSize = paging.StartIndex(currentIndex, pageSize)
+	listCollection, err := models.GetChartListCollection(chartIds, uid, startSize, pageSize)
+	if err != nil {
+		br.Msg = "获取信息失败"
+		br.Msg = "获取帖子总数失败,GetChartListCollection Err:" + err.Error()
+		return
+	}
+	for k, v := range listCollection {
+		if v.NumTop > 0 {
+			listCollection[k].IsTop = true
+		}
+	}
 	page := paging.GetPaging(currentIndex, pageSize, total)
-	resp.List = list
+	resp.List = listCollection
 	resp.Paging = page
 	br.Ret = 200
 	br.Success = true
@@ -126,6 +169,13 @@ func (this *ChartController) Detail() {
 		br.ErrMsg = "获取信息失败,Err:" + err.Error()
 		return
 	}
+	GetIsCollectionChart, err := services.GetIsCollectionChart(mobile, chartId)
+	if err != nil {
+		br.Msg = "获取信息失败"
+		br.ErrMsg = "获取三方关注信息失败,Err:" + err.Error()
+		return
+	}
+	detail.IsCollection = GetIsCollectionChart
 	if detail.CollectionNum > 0 {
 		detail.IsCollection = true
 	}
@@ -153,6 +203,7 @@ func (this *ChartController) ChartCollect() {
 		return
 	}
 	uid := user.UserId
+	mobile := user.Mobile
 	var req models.ChartCollectReq
 	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
 	if err != nil {
@@ -195,6 +246,20 @@ func (this *ChartController) ChartCollect() {
 		br.Msg = "已取消收藏"
 		resp.Status = 2
 	}
+
+	GetIsCollectionChart, err := services.GetIsCollectionChart(mobile, req.ChartId)
+	if err != nil {
+		br.Msg = "获取信息失败"
+		br.ErrMsg = "获取三方关注信息失败,Err:" + err.Error()
+		return
+	}
+	fmt.Println("GetIsCollectionChart", GetIsCollectionChart)
+	//如果存在就取消收藏、反之添加收藏
+	if GetIsCollectionChart {
+		go services.DeleteCollectionChart(mobile, req.ChartId)
+	} else {
+		go services.AddCollectionChart(mobile, req.ChartId)
+	}
 	br.Ret = 200
 	br.Success = true
 	br.Data = resp

+ 28 - 1
models/chart.go

@@ -1,6 +1,7 @@
 package models
 
 import (
+	"fmt"
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"time"
@@ -28,7 +29,7 @@ type Pagination struct {
 
 type ChartInfo struct {
 	ChartInfo *ChartResultApidate `json:"chart_info"`
-	ChartId   int                 `json:"id"`
+	ChartId   int                 `json:"chart_id"`
 }
 
 type ChartResultApidate struct {
@@ -143,6 +144,7 @@ type HomeChartListResp struct {
 	HttpUrl    string `orm:"column(iframe)";description:"文章链接跳转地址"`
 	IsNeedJump bool   `description:"是否需要跳转链接地址"`
 	IsTop      bool   `description:"是否置顶"`
+	NumTop     int    `description:"置顶数量"`
 	Source     int    `description:"来源  1:文章, 2:图表"`
 }
 
@@ -168,6 +170,30 @@ func GetChartList(condition string, pars []interface{}, startSize, pageSize int)
 	return
 }
 
+//获取图表列表
+func GetChartListCollection(chartIds string, userId, startSize, pageSize int) (items []*HomeChartListResp, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT a.*,
+			t.create_time AS t_create_time,
+			c.create_time AS c_create_time,
+			( SELECT COUNT(*) FROM cygx_chart_top AS t WHERE t.chart_id = a.chart_id AND t.user_id = ? ) AS num_top,
+			( SELECT COUNT(*) FROM cygx_chart_collect AS c WHERE c.chart_id = a.chart_id AND c.user_id = ? ) AS num_c 
+		FROM
+			cygx_chart AS a
+			LEFT JOIN cygx_chart_top AS t ON t.chart_id = a.chart_id
+			LEFT JOIN cygx_chart_collect AS c ON c.chart_id = a.chart_id 
+		WHERE
+			a.publish_status = 1 
+			AND a.chart_id IN (` + chartIds + `) 
+		GROUP BY
+			a.chart_id 
+		ORDER BY
+			t_create_time DESC,
+			c_create_time DESC LIMIT ?,? `
+	_, err = o.Raw(sql, userId, userId, startSize, pageSize).QueryRows(&items)
+	return
+}
+
 func GetChartListConfig() (items []*HomeChartListResp, err error) {
 	o := orm.NewOrm()
 	sql := ` SELECT * FROM cygx_chart AS a WHERE a.publish_status=1    AND  ctag_id IN (7,8,9,10) GROUP BY ctag_id  ORDER BY create_date  DESC LIMIT 4 `
@@ -184,6 +210,7 @@ func GetChartCount(condition string, pars []interface{}) (count int, err error)
 	if condition != "" {
 		sql += condition
 	}
+	fmt.Println(sql)
 	err = o.Raw(sql, pars).QueryRow(&count)
 	return
 }

+ 92 - 0
services/chart.go

@@ -209,6 +209,7 @@ func GetChartCollectionByApi(mobile string, take, skip int) (items []*models.Hom
 		fmt.Println(err)
 		return
 	}
+	fmt.Println(string(body))
 	var chartResult models.ChartFavoritesResultApi
 	err = json.Unmarshal(body, &chartResult)
 	if err != nil {
@@ -232,3 +233,94 @@ func GetChartCollectionByApi(mobile string, take, skip int) (items []*models.Hom
 	total = chartResult.Pagination.Total
 	return
 }
+
+//判断策略平台是否已经添加过收藏
+func GetIsCollectionChart(mobile string, chartId int) (isCollection bool, err error) {
+	//获取所有的收藏列表,进行比对看看是否收藏,调用三方接口详情没有是否收藏的字段
+	list, err, _ := GetChartCollectionByApi(mobile, 9999, 0)
+	if err != nil {
+		return
+	}
+	fmt.Println(list)
+	fmt.Println(mobile)
+	for _, v := range list {
+		fmt.Println(v.ChartId)
+		if v.ChartId == chartId {
+			isCollection = true
+		}
+	}
+	fmt.Println(isCollection)
+	return
+}
+
+//添加收藏
+func AddCollectionChart(mobile string, chartId int) (err error) {
+	authorization, err := GetUserTokenByMobile(mobile)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	authorization = "bearer " + authorization
+	url := "https://vmp.hzinsights.com/v2api/charts/favorites"
+	method := "POST"
+	payload := strings.NewReader(`{
+    "chart_id":5120
+}`)
+	client := &nhttp.Client{}
+	req, err := nhttp.NewRequest(method, url, payload)
+	fmt.Println("添加")
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	req.Header.Add("Authorization", authorization)
+	req.Header.Add("Content-Type", "application/json")
+	req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
+	res, err := client.Do(req)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	defer res.Body.Close()
+
+	body, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	fmt.Println(string(body))
+	return
+}
+
+//移除收藏
+func DeleteCollectionChart(mobile string, chartId int) (err error) {
+	authorization, err := GetUserTokenByMobile(mobile)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	authorization = "bearer " + authorization
+	url := "https://vmp.hzinsights.com/v2api/charts/favorites/" + strconv.Itoa(chartId)
+	method := "DELETE"
+	client := &nhttp.Client{}
+	req, err := nhttp.NewRequest(method, url, nil)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	req.Header.Add("Authorization", authorization)
+	req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
+	res, err := client.Do(req)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	defer res.Body.Close()
+	body, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	fmt.Println(string(body))
+	return
+}