157 lines
4.3 KiB
Go
157 lines
4.3 KiB
Go
package httpcall
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"finclip-app-manager/infrastructure/config"
|
|
"finclip-app-manager/infrastructure/utils"
|
|
"fmt"
|
|
"github.com/bitly/go-simplejson"
|
|
"github.com/gin-gonic/gin"
|
|
uuid "github.com/satori/go.uuid"
|
|
"gitlab.finogeeks.club/finclip-backend/apm"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type AddOperateLogReq struct {
|
|
Id string `json:"id"`
|
|
CreateTime int64 `json:"createTime"`
|
|
Operator string `json:"operator"`
|
|
IP string `json:"IP"`
|
|
Content string `json:"content"`
|
|
Url string `json:"url"`
|
|
OrganId string `json:"organId"`
|
|
Header interface{} `json:"header"`
|
|
Body interface{} `json:"body"`
|
|
Query interface{} `json:"query"`
|
|
Extra interface{} `json:"extra"`
|
|
}
|
|
|
|
func (client *Client) AddOperateLog(c *gin.Context, organId, content string, extra interface{}, oper string) error {
|
|
traceX := apm.ApmClient().TraceContextFromGin(c)
|
|
url := config.GetConfig().DataCounterHost + "/api/v1/mop/finclip-data-counter/oper/log/add"
|
|
|
|
var data AddOperateLogReq
|
|
data.Id = uuid.NewV4().String()
|
|
data.CreateTime = time.Now().UnixNano() / 1000000
|
|
data.Operator = oper
|
|
if data.Operator == "" {
|
|
account, err := utils.DesEcbDecrypt(c.GetHeader("user-account"))
|
|
if err != nil || account == "" {
|
|
data.Operator = c.GetHeader("user-account")
|
|
} else {
|
|
data.Operator = account
|
|
}
|
|
}
|
|
data.IP = c.Request.Header.Get("X-Real-Ip")
|
|
|
|
if data.IP == "" {
|
|
ip := strings.Split(c.Request.RemoteAddr, ":")
|
|
fmt.Println("ip", ip)
|
|
if len(ip) != 0 {
|
|
data.IP = ip[0]
|
|
}
|
|
}
|
|
data.Content = content
|
|
data.Url = c.Request.RequestURI
|
|
data.OrganId = organId
|
|
data.Header = c.Request.Header
|
|
body, _ := ioutil.ReadAll(c.Request.Body)
|
|
data.Body, _ = simplejson.NewJson(body)
|
|
data.Extra = extra
|
|
fmt.Println("data", data)
|
|
jsonBytes, _ := json.Marshal(data)
|
|
|
|
rsp, err := client.Request(traceX).SetHeaders(client.getNewDefaultReqHeader()).SetBody(jsonBytes).Post(url)
|
|
if err != nil {
|
|
log.Errorf("AddOperateLog err:%s", err.Error())
|
|
return err
|
|
}
|
|
log.Infof("AddOperateLog rsp:%+v", rsp)
|
|
if rsp.StatusCode() != http.StatusOK {
|
|
log.Errorf("AddOperateLog result errcode err:%+v", rsp)
|
|
return errors.New("errcode invalid")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type GenOperateDataV2Req struct {
|
|
OrganId string
|
|
Content string
|
|
Extra interface{}
|
|
Oper string
|
|
AccountId string
|
|
IsDev bool
|
|
}
|
|
|
|
func (client *Client) AddOperateLogV2(c *gin.Context, req GenOperateDataV2Req) error {
|
|
traceX := apm.ApmClient().TraceContextFromGin(c)
|
|
url := config.GetConfig().DataCounterHost + "/api/v1/mop/finclip-data-counter/oper/log/add"
|
|
|
|
var data AddOperateLogReq
|
|
data.Id = uuid.NewV4().String()
|
|
data.CreateTime = time.Now().UnixNano() / 1000000
|
|
if req.AccountId != "" {
|
|
account, err := getAccountById(traceX, req.AccountId, req.IsDev)
|
|
if err == nil {
|
|
data.Operator = account
|
|
}
|
|
}
|
|
if data.Operator == "" {
|
|
data.Operator = req.Oper
|
|
if data.Operator == "" {
|
|
account, err := utils.DesEcbDecrypt(c.GetHeader("user-account"))
|
|
if err != nil || account == "" {
|
|
data.Operator = c.GetHeader("user-account")
|
|
} else {
|
|
data.Operator = account
|
|
}
|
|
//data.Operator = c.GetHeader("user-account")
|
|
}
|
|
}
|
|
data.IP = c.Request.Header.Get("X-Real-Ip")
|
|
if data.IP == "" {
|
|
ip := strings.Split(c.Request.RemoteAddr, ":")
|
|
if len(ip) != 0 {
|
|
data.IP = ip[0]
|
|
}
|
|
}
|
|
data.Content = req.Content
|
|
data.Url = c.Request.RequestURI
|
|
data.OrganId = req.OrganId
|
|
data.Header = c.Request.Header
|
|
body, _ := ioutil.ReadAll(c.Request.Body)
|
|
data.Body, _ = simplejson.NewJson(body)
|
|
data.Extra = req.Extra
|
|
jsonBytes, _ := json.Marshal(data)
|
|
|
|
rsp, err := client.Request(traceX).SetHeaders(client.getNewDefaultReqHeader()).SetBody(jsonBytes).Post(url)
|
|
if err != nil {
|
|
log.Errorf("AddOperateLog err:%s", err.Error())
|
|
return err
|
|
}
|
|
log.Infof("AddOperateLog rsp:%+v", rsp)
|
|
if rsp.StatusCode() != http.StatusOK {
|
|
log.Errorf("AddOperateLog result errcode err:%+v", rsp)
|
|
return errors.New("errcode invalid")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getAccountById(ctx context.Context, id string, isDev bool) (string, error) {
|
|
if isDev {
|
|
accountInfo, err := NewClient().GetAccountInfo(ctx, id)
|
|
if accountInfo != nil {
|
|
return accountInfo.Account, err
|
|
}
|
|
}
|
|
accountInfo, err := NewClient().GetAdminAccountInfo(ctx, id)
|
|
if accountInfo == nil {
|
|
return "", err
|
|
}
|
|
return accountInfo.Account, err
|
|
} |