finclip-app-manager/infrastructure/client/httpcall/notify_spider.go

177 lines
5.0 KiB
Go

package httpcall
import (
"context"
"encoding/json"
"errors"
"finclip-app-manager/domain/entity"
"finclip-app-manager/infrastructure/config"
"finclip-app-manager/infrastructure/utility"
"fmt"
"net/http"
)
const (
BIND_SDK = "bind"
UNBIND_SDK = "unbind"
STATE_UPDATE = "state"
UP_STATE = 1
DOWN_STATE = 0
)
type SpiderResp struct {
}
type SdkKeyUpdate struct {
AppId string `json:"appId"`
SdkKey string `json:"sdkKey"`
}
type UpdateForbiddenSdkKeys struct {
AddSdkKeys []SdkKeyUpdate `json:"sdkKeys"`
RemoveSdkKeys []SdkKeyUpdate `json:"removeSdkKeys"`
}
type StateUpdate struct {
AppId string `json:"appId"`
State int `json:"state"`
}
type NotifySpiderUpdate struct {
Type string `json:"type"`
SdkKeys []SdkKeyUpdate `json:"sdkKeys"`
State StateUpdate `json:"state"`
}
type AddNotifyReq struct {
OrganId string `json:"organTraceId"`
Tab string `json:"tab"`
Type int `json:"type"`
Content interface{} `json:"content"`
Id string `json:"id"`
}
type AddNotifyResponse struct {
ErrInfo
}
func (c *Client) AddNotify(ctx context.Context, req *AddNotifyReq) error {
url := config.GetConfig().AddNotifyURL
body, _ := json.Marshal(req)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).SetBody(body).Put(url)
if err != nil {
log.Errorf("TouchWechatInfo err:%s", err.Error())
return err
}
result := AddNotifyResponse{}
if err = json.Unmarshal(rsp.Body(), &result); err != nil {
log.Errorf("TouchWechatInfo err:%s", err.Error())
return err
}
log.Infof("TouchWechatInfo rsp:%+v", rsp)
if rsp.StatusCode() != http.StatusOK {
log.Errorf("TouchWechatInfo result errcode err:%+v", rsp)
return errors.New("errcode invalid")
}
return nil
}
func (c *Client) Notify(ctx context.Context, req *NotifySpiderUpdate) (*SpiderResp, error) {
url := config.GetConfig().SpiderHost + config.GetConfig().SpiderNotifyPath
log.Infof("notify spider url:%s", url)
log.Infof("notify spider req:%s", utility.InterfaceToJsonString(req))
spiderResp := &SpiderResp{}
payload, _ := json.Marshal(req)
resp, err := c.Request(ctx).SetResult(&spiderResp).
SetHeader("X-Consumer-Custom-ID", "notify-spider").
SetBody(payload).
Post(url)
if err != nil || resp.StatusCode() != http.StatusOK {
if err != nil {
return nil, err
} else {
return nil, errors.New(fmt.Sprintf("notify spider code:%d", http.StatusOK))
}
}
return spiderResp, nil
}
func (c *Client) UpdateBundleForbiddenInfo(ctx context.Context, req *UpdateForbiddenSdkKeys) (*SpiderResp, error) {
url := config.GetConfig().SpiderHost + config.GetConfig().SpiderUpdateBundleForbiddenURL
log.Infof("notify spider url:%s", url)
log.Infof("notify spider req:%s", utility.InterfaceToJsonString(req))
spiderResp := &SpiderResp{}
payload, _ := json.Marshal(req)
resp, err := c.Request(ctx).SetResult(&spiderResp).
SetHeader("X-Consumer-Custom-ID", "notify-spider").
SetBody(payload).
Post(url)
if err != nil || resp.StatusCode() != http.StatusOK {
if err != nil {
return nil, err
} else {
return nil, errors.New(fmt.Sprintf("notify spider code:%d", http.StatusOK))
}
}
return spiderResp, nil
}
type SpiderPubReq struct {
AppId string `json:"appId"`
AppName string `json:"appName"`
AppDesc string `json:"appDesc"`
OrganName string `json:"organName"`
SdkKeys []string `json:"sdkKeys"`
Resource string `json:"resource"`
AppTag string `json:"appTag"`
AppClass string `json:"appClass"`
}
func (c *Client) PubAppNotifySearchApp(ctx context.Context, req *SpiderPubReq) (*SpiderResp, error) {
spiderResp := &SpiderResp{}
payload, _ := json.Marshal(req)
_, err := c.Request(ctx).SetResult(&spiderResp).
SetHeader("X-Consumer-Custom-ID", "notify-spider").
SetBody(payload).
Post(config.GetConfig().SpiderHost + config.GetConfig().SpiderPubAppNotifyURL)
if err != nil {
return nil, err
}
return spiderResp, nil
}
//预览服务
func (c *Client) PubAppNotifyToMiniprogram(ctx context.Context, req *entity.AppVersion) (*SpiderResp, error) {
spiderResp := &SpiderResp{}
//payload, _ := json.Marshal(req)
//fmt.Println("payload", payload)
fmt.Println("req", utility.InterfaceToJsonString(req))
_, err := c.Request(ctx).SetResult(&spiderResp).
//SetHeader("X-Consumer-Custom-ID", "notify-spider").
SetBody(req).
Post(config.GetConfig().MiniProgramSearchURL)
if err != nil {
fmt.Println("err:", err.Error())
return nil, err
}
return spiderResp, nil
}
func (c *Client) UpdateHistoryAppMsg(ctx context.Context) error {
//traceCtx := apm.ApmClient().TraceContextFromGin(c)
var headers = map[string]string{
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"X-Consumer-Custom-ID": "notify-spider",
"url-call": "internal",
}
url := config.GetConfig().SpiderHost + config.GetConfig().SpiderUpdateAppMsg
rsp, err := c.Request(ctx).SetHeaders(headers).SetResult(&commonConfig).Get(url)
if err != nil || rsp.StatusCode() != 200 {
return errors.New("UpdateHistoryAppMsg err")
}
return nil
}