123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package instance
- import (
- "encoding/json"
- "errors"
- "eta/eta_chart_lib/facade"
- "eta/eta_chart_lib/models"
- "eta/eta_chart_lib/utils"
- "fmt"
- )
- const (
- bookMarkUrl = "user/bookMark"
- unBookMarkUrl = "user/unBookMark"
- checkBookMarkUrl = "user/checkBookMark"
- target = "ht"
- )
- type HTCollectionReq struct {
- UniqueCode string `json:"uniqueCode"`
- }
- type HTMiniFacade struct {
- facade.BaseMiniFacade
- }
- type HTResponse struct {
- Ret int
- Msg string
- Data interface{} `json:"data"`
- ErrMsg string
- ErrCode int
- Success bool
- }
- type HTRequest struct {
- facade.BaseRequest
- ChartInfoId int
- ChartName string
- ChartImage string
- SourceType string
- SourceId int
- }
- func dealChartInfo(baseReq facade.BaseRequest) (request HTRequest, err error) {
- chartInfo, err := models.GetChartInfoByUniqueCode(baseReq.UniqueCode)
- if err != nil {
- utils.FileLog.Error("收藏失败,获取图表信息失败:%v", err.Error())
- return
- }
- request = HTRequest{
- BaseRequest: baseReq,
- ChartImage: chartInfo.ChartImage,
- ChartInfoId: chartInfo.ChartInfoId,
- ChartName: chartInfo.ChartName,
- SourceType: "chart",
- SourceId: chartInfo.ChartInfoId,
- }
- return
- }
- func parseResult(resp string) (response HTResponse, err error) {
- err = json.Unmarshal([]byte(resp), &response)
- if err != nil {
- utils.FileLog.Error("收藏失败,解析应答失败:%v,应答结果:%s", err.Error(), resp)
- }
- if !response.Success {
- utils.FileLog.Error("收藏失败,应答结果:%d[%s]", response.ErrCode, response.ErrMsg)
- err = errors.New(response.Msg + response.ErrMsg)
- }
- return
- }
- func (ht *HTMiniFacade) Collect(req facade.BaseRequest) (err error) {
- param, err := dealChartInfo(req)
- if err != nil {
- utils.FileLog.Error("收藏失败,获取图表信息失败:%v", err.Error())
- return err
- }
- url, err := generateUrl(bookMarkUrl)
- if err != nil {
- return
- }
- resp, err := ht.Post(url, param, req.Auth)
- if err != nil {
- utils.FileLog.Error("收藏失败,err:%v,resp:%v", err, resp)
- return
- }
- _, err = parseResult(resp)
- return
- }
- func (ht *HTMiniFacade) UnCollect(req facade.BaseRequest) (err error) {
- param, err := dealChartInfo(req)
- if err != nil {
- utils.FileLog.Error("收藏失败,获取图表信息失败:%v", err.Error())
- return err
- }
- url, err := generateUrl(unBookMarkUrl)
- if err != nil {
- return
- }
- resp, err := ht.Post(url, param, req.Auth)
- if err != nil {
- utils.FileLog.Error("取消收藏失败,err:%v,resp:%v", err, resp)
- return
- }
- _, err = parseResult(resp)
- return
- }
- func generateUrl(path string) (url string, err error) {
- configPath := utils.GetMiniUrl(target)
- if configPath == "" {
- utils.FileLog.Error("获取mini接口地址失败", err)
- err = errors.New("获取mini接口地址失败")
- return
- }
- url = fmt.Sprintf("%s%s", configPath, path)
- return
- }
- func (ht *HTMiniFacade) IsCollect(req facade.BaseRequest) bool {
- param, err := dealChartInfo(req)
- if err != nil {
- utils.FileLog.Error("获取是否收藏失败,获取图表信息失败:%v", err.Error())
- return false
- }
- url, err := generateUrl(checkBookMarkUrl)
- if err != nil {
- return false
- }
- resp, err := ht.Post(url, param, req.Auth)
- if err != nil {
- utils.FileLog.Error("获取是否收藏失败,err:%v,resp:%v", err, resp)
- return false
- }
- response, err := parseResult(resp)
- if err != nil {
- utils.FileLog.Error("获取是否收藏失败,解析应答失败:%v,应答结果:%s", err, resp)
- return false
- }
- var resMap = response.Data.(map[string]interface{})
- bookMarked := resMap["isBookMarked"]
- if bookMarked == nil {
- return false
- }
- return bookMarked.(bool)
- }
- func init() {
- facade.RegisterMiniFacade(target, &HTMiniFacade{
- BaseMiniFacade: facade.FacadeClient,
- })
- }
|