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.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, }) }