Pārlūkot izejas kodu

Merge branch 'bzq/custom_dw_mini' of eta_server/eta_chart_lib into master

鲍自强 8 mēneši atpakaļ
vecāks
revīzija
7ab8677c9d

+ 2 - 1
.gitignore

@@ -8,4 +8,5 @@
 /eta_chart_lib.exe~
 .Ds_Store
 eta_chart_lib
-*.gz
+*.gz
+.vscode/

+ 124 - 0
controllers/chart.go

@@ -8,6 +8,7 @@ import (
 	"eta/eta_chart_lib/models/data_manage/excel"
 	"eta/eta_chart_lib/services/data"
 	"eta/eta_chart_lib/services/data/cross_variety"
+	dwmini "eta/eta_chart_lib/services/dw_mini"
 	"eta/eta_chart_lib/utils"
 	"fmt"
 	"strings"
@@ -33,6 +34,8 @@ func (this *ChartController) ChartInfoDetail() {
 	}()
 
 	uniqueCode := this.GetString("UniqueCode")
+	token := this.GetString("Token")
+	source, _ := this.GetInt("Source")
 	if uniqueCode == "" {
 		br.Msg = "参数错误"
 		br.ErrMsg = "参数错误,uniqueCode is empty"
@@ -48,6 +51,16 @@ func (this *ChartController) ChartInfoDetail() {
 		br.ErrMsg = "获取配置信息失败, Err: " + e.Error()
 		return
 	}
+	var isCollect bool
+	if source == utils.CHART_SOURCE_DW && token != "" {
+		tmpIsCollect, err := dwmini.GetMyChartIsCollect(token, uniqueCode)
+		if err != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取收藏状态失败,Err:" + err.Error()
+			return
+		}
+		isCollect = tmpIsCollect
+	}
 
 	//判断是否有缓存
 	if utils.Re == nil {
@@ -58,6 +71,9 @@ func (this *ChartController) ChartInfoDetail() {
 					if conf[models.BusinessConfWatermarkChart] == "true" && conf[models.BusinessConfCompanyWatermark] != "" {
 						resp.WaterMark = conf[models.BusinessConfCompanyWatermark]
 					}
+					if isCollect {
+						resp.IsCollect = isCollect
+					}
 					br.Ret = 200
 					br.Success = true
 					br.Msg = "获取成功"
@@ -116,6 +132,9 @@ func (this *ChartController) ChartInfoDetail() {
 		br.ErrMsg = errMsg
 		return
 	}
+	if isCollect {
+		resp.IsCollect = isCollect
+	}
 
 	if conf[models.BusinessConfWatermarkChart] == "true" && conf[models.BusinessConfCompanyWatermark] != "" {
 		resp.WaterMark = conf[models.BusinessConfCompanyWatermark]
@@ -222,6 +241,111 @@ func (this *ChartController) ChartInfoRefresh() {
 	br.Msg = "刷新成功"
 }
 
+// CollectCancel
+// @Title 东吴小程序图表取消收藏接口
+// @Description 东吴小程序图表取消收藏接口
+// @Param	request	body models.ChartDwCollectReq true "type json string"
+// @Success Ret=200 取消收藏成功
+// @router /dw/collectCancel [post]
+func (this *ChartController) CollectCancel() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	var req models.ChartDwCollectReq
+	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,Err:" + err.Error()
+		return
+	}
+	if req.UniqueCode == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,UniqueCode is empty"
+		return
+	}
+	if req.Token == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,Token is empty"
+		return
+	}
+	result, err := dwmini.MyChartCollectCancel(req.Token, req.UniqueCode)
+	if err != nil {
+		br.Msg = "取消收藏失败"
+		br.ErrMsg = "取消收藏失败,Err:" + err.Error()
+		return
+	}
+	if result.Ret != 200 {
+		br.Msg = result.Msg
+		br.ErrMsg = "取消收藏失败,Err:" + result.ErrMsg
+		return
+	}
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "取消收藏成功"
+}
+
+// Collect
+// @Title 东吴小程序图表收藏接口
+// @Description 东吴小程序图表收藏接口
+// @Param   request	body models.ChartDwCollectReq true "type json string"
+// @Success Ret=200 收藏成功
+// @router /dw/collect [post]
+func (this *ChartController) Collect() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	var req models.ChartDwCollectReq
+	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,Err:" + err.Error()
+		return
+	}
+	if req.UniqueCode == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,UniqueCode is empty"
+		return
+	}
+	if req.Token == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,Token is empty"
+		return
+	}
+
+	chartInfo, err := models.GetChartInfoByUniqueCode(req.UniqueCode)
+	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			br.Msg = "该图已被删除,请刷新页面"
+			br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
+			return
+		}
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
+		return
+	}
+
+	result, err := dwmini.MyChartCollect(req.Token, req.UniqueCode, chartInfo.ChartName, chartInfo.ChartImage, chartInfo.ChartInfoId)
+	if err != nil {
+		br.Msg = "收藏失败"
+		br.ErrMsg = "收藏失败,Err:" + err.Error()
+		return
+	}
+	if result.Ret != 200 {
+		br.Msg = result.Msg
+		br.ErrMsg = "收藏失败,Err:" + result.ErrMsg
+		return
+	}
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "收藏成功"
+}
+
 // 获取频度的英文版
 func GetFrequencyEn(frequency string) (frequencyEn string) {
 	switch frequency {

+ 22 - 1
controllers/chart_common.go

@@ -17,6 +17,7 @@ import (
 	future_goodServ "eta/eta_chart_lib/services/data/future_good"
 	"eta/eta_chart_lib/services/data/line_equation"
 	lineFeatureServ "eta/eta_chart_lib/services/data/line_feature"
+	dwmini "eta/eta_chart_lib/services/dw_mini"
 	"eta/eta_chart_lib/utils"
 	"fmt"
 	"strconv"
@@ -29,6 +30,8 @@ import (
 // @Description 根据编码获取图表详情接口
 // @Param   UniqueCode   query   int  true       "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
 // @Param   IsCache   query   bool  true       "是否走缓存,默认false"
+// @Param   Token   query   string  true       "东吴小程序token"
+// @Param   Source   query   int  true       "查询来源 1:东吴"
 // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
 // @router /common/detail [get]
 func (this *ChartController) CommonChartInfoDetailFromUniqueCode() {
@@ -39,6 +42,8 @@ func (this *ChartController) CommonChartInfoDetailFromUniqueCode() {
 	}()
 
 	uniqueCode := this.GetString("UniqueCode")
+	token := this.GetString("Token")
+	source, _ := this.GetInt("Source")
 	if uniqueCode == "" {
 		br.Msg = "参数错误"
 		br.ErrMsg = "参数错误,uniqueCode is empty"
@@ -54,7 +59,16 @@ func (this *ChartController) CommonChartInfoDetailFromUniqueCode() {
 		br.ErrMsg = "获取配置信息失败, Err: " + e.Error()
 		return
 	}
-
+	var isCollect bool
+	if source == utils.CHART_SOURCE_DW && token != "" {
+		tmpIsCollect, err := dwmini.GetMyChartIsCollect(token, uniqueCode)
+		if err != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取收藏状态失败,Err:" + err.Error()
+			return
+		}
+		isCollect = tmpIsCollect
+	}
 	//判断是否有缓存
 	if utils.Re == nil {
 		if utils.Re == nil && utils.Rc.IsExist(key) {
@@ -64,6 +78,9 @@ func (this *ChartController) CommonChartInfoDetailFromUniqueCode() {
 					if conf[models.BusinessConfWatermarkChart] == "true" && conf[models.BusinessConfCompanyWatermark] != "" {
 						resp.WaterMark = conf[models.BusinessConfCompanyWatermark]
 					}
+					if isCollect {
+						resp.IsCollect = isCollect
+					}
 					br.Ret = 200
 					br.Success = true
 					br.Msg = "获取成功"
@@ -123,6 +140,10 @@ func (this *ChartController) CommonChartInfoDetailFromUniqueCode() {
 		return
 	}
 
+	if isCollect {
+		resp.IsCollect = isCollect
+	}
+
 	if conf[models.BusinessConfWatermarkChart] == "true" && conf[models.BusinessConfCompanyWatermark] != "" {
 		resp.WaterMark = conf[models.BusinessConfCompanyWatermark]
 	}

+ 9 - 2
models/chart.go

@@ -5,11 +5,12 @@ import (
 	"eta/eta_chart_lib/models/mgo"
 	"eta/eta_chart_lib/utils"
 	"fmt"
-	"github.com/beego/beego/v2/client/orm"
-	"go.mongodb.org/mongo-driver/bson"
 	"strconv"
 	"time"
 
+	"github.com/beego/beego/v2/client/orm"
+	"go.mongodb.org/mongo-driver/bson"
+
 	"github.com/nosixtools/solarlunar"
 )
 
@@ -281,6 +282,7 @@ type ChartInfoDetailResp struct {
 	CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
 	DataResp             interface{}      `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
 	WaterMark            string           `description:"水印"`
+	IsCollect            bool             `description:"是否收藏"`
 }
 
 // XData 商品价格曲线的的x轴数据
@@ -697,3 +699,8 @@ func (m QuarterDataList) Less(i, j int) bool {
 func (m QuarterDataList) Swap(i, j int) {
 	m[i], m[j] = m[j], m[i]
 }
+
+type ChartDwCollectReq struct {
+	UniqueCode string
+	Token      string
+}

+ 18 - 0
routers/commentsRouter.go

@@ -43,6 +43,24 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_chart_lib/controllers:ChartController"] = append(beego.GlobalControllerRouter["eta/eta_chart_lib/controllers:ChartController"],
+        beego.ControllerComments{
+            Method: "Collect",
+            Router: `/dw/collect`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_chart_lib/controllers:ChartController"] = append(beego.GlobalControllerRouter["eta/eta_chart_lib/controllers:ChartController"],
+        beego.ControllerComments{
+            Method: "CollectCancel",
+            Router: `/dw/collectCancel`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_chart_lib/controllers:ChartController"] = append(beego.GlobalControllerRouter["eta/eta_chart_lib/controllers:ChartController"],
         beego.ControllerComments{
             Method: "FutureGoodChartInfoRefresh",

+ 25 - 0
services/dw_mini/base_mini_lib.go

@@ -0,0 +1,25 @@
+package dwmini
+
+import (
+	"fmt"
+	"io"
+	"net/http"
+	"strings"
+)
+
+func HttpPost(url, postData, token string) ([]byte, error) {
+	body := io.NopCloser(strings.NewReader(postData))
+	client := &http.Client{}
+	req, err := http.NewRequest("POST", url, body)
+	if err != nil {
+		return nil, err
+	}
+	req.Header.Add("Authorization", token)
+	req.Header.Set("Content-Type", "application/json")
+	resp, err := client.Do(req)
+	defer resp.Body.Close()
+	b, err := io.ReadAll(resp.Body)
+	fmt.Println("HttpGet:" + string(b))
+	return b, err
+
+}

+ 89 - 0
services/dw_mini/dw_mini.go

@@ -0,0 +1,89 @@
+package dwmini
+
+import (
+	"encoding/json"
+	"errors"
+	"eta/eta_chart_lib/models"
+	"eta/eta_chart_lib/utils"
+)
+
+func GetMyChartIsCollect(token string, uniqueCode string) (isCollect bool, err error) {
+	url := utils.ETA_MINI_URL + "mychart/isCollect"
+	type MyChartIsCollectReq struct {
+		UniqueCode string
+	}
+	req := MyChartIsCollectReq{UniqueCode: uniqueCode}
+	reqBody, _ := json.Marshal(req)
+	result, err := HttpPost(url, string(reqBody), token)
+	if err != nil {
+		return
+	}
+	type MyChartIsCollect struct {
+		IsCollect bool
+	}
+	type BaseResponse struct {
+		Ret     int
+		Msg     string
+		ErrMsg  string
+		ErrCode string
+		Data    MyChartIsCollect
+	}
+	var resp BaseResponse
+	if err = json.Unmarshal(result, &resp); err != nil {
+		return
+	}
+	if resp.Ret != 200 {
+		err = errors.New(resp.ErrMsg)
+		return
+	}
+	isCollect = resp.Data.IsCollect
+	return
+}
+
+func MyChartCollect(token, uniqueCode, chartName, chartImage string, chartInfoId int) (resp *models.BaseResponse, err error) {
+	url := utils.ETA_MINI_URL + "mychart/collect"
+	type MyChartCollectReq struct {
+		UniqueCode  string
+		ChartName   string
+		ChartImage  string
+		ChartInfoId int
+	}
+	req := MyChartCollectReq{
+		UniqueCode:  uniqueCode,
+		ChartName:   chartName,
+		ChartImage:  chartImage,
+		ChartInfoId: chartInfoId,
+	}
+	reqBody, _ := json.Marshal(req)
+	result, err := HttpPost(url, string(reqBody), token)
+	if err != nil {
+		return
+	}
+	if err = json.Unmarshal(result, &resp); err != nil {
+		return
+	}
+	if resp.Ret != 200 {
+		return
+	}
+	return
+}
+
+func MyChartCollectCancel(token string, uniqueCode string) (resp *models.BaseResponse, err error) {
+	url := utils.ETA_MINI_URL + "mychart/collectCancel"
+	type MyChartCollectCancelReq struct {
+		UniqueCode string
+	}
+	req := MyChartCollectCancelReq{UniqueCode: uniqueCode}
+	reqBody, _ := json.Marshal(req)
+	result, err := HttpPost(url, string(reqBody), token)
+	if err != nil {
+		return
+	}
+	if err = json.Unmarshal(result, &resp); err != nil {
+		return
+	}
+	if resp.Ret != 200 {
+		return
+	}
+	return
+}

+ 6 - 3
utils/config.go

@@ -2,13 +2,14 @@ package utils
 
 import (
 	"fmt"
+	"math"
+	"strconv"
+	"strings"
+
 	beeLogger "github.com/beego/bee/v2/logger"
 	"github.com/beego/beego/v2/server/web"
 	"github.com/qiniu/qmgo"
 	"github.com/shopspring/decimal"
-	"math"
-	"strconv"
-	"strings"
 )
 
 var (
@@ -47,6 +48,7 @@ var (
 	EDB_LIB_URL         string
 	APP_EDB_LIB_NAME_EN string
 	EDB_LIB_Md5_KEY     string
+	ETA_MINI_URL        string
 )
 
 var (
@@ -134,6 +136,7 @@ func init() {
 		EDB_LIB_URL = config["edb_lib_url"]
 		APP_EDB_LIB_NAME_EN = config["app_edb_lib_name_en"]
 		EDB_LIB_Md5_KEY = config["edb_lib_md5_key"]
+		ETA_MINI_URL = config["eta_mini_url"]
 	}
 	//日志配置
 	{

+ 5 - 0
utils/constants.go

@@ -143,6 +143,11 @@ const (
 	CHART_SOURCE_BALANCE_EXCEL                   = 11 // 平衡表图表
 )
 
+// 图表来源
+const (
+	CHART_SOURCE_DW = 1 // 东吴
+)
+
 // ETA表格
 const (
 	EXCEL_DEFAULT         = 1 // 自定义excel