finclip-app-manager/infrastructure/kafka/operate_log.go

189 lines
5.3 KiB
Go
Raw Normal View History

2023-10-31 14:07:26 +08:00
package kafka
import (
"context"
"encoding/json"
"finclip-app-manager/infrastructure/client/httpcall"
"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"
"io/ioutil"
"strings"
"time"
)
var (
hCaller = httpcall.NewClient()
)
//应用管理日志
const (
BIND_ADD_LOG = "新增合作应用(%s" //新增合作应用
BIND_UP_LOG = "修改合作应用名称(由%s修改为%s" //修改
BIND_STOP_COOP_LOG = "停止合作(%s" //停止合作
BIND_RECOVE_COOP_LOG = "恢复合作(%s" //恢复合作
BIND_ASS_APP_LOG = "合作应用 %s 新增关联小程序" //关联小程序
BIND_CANCEL_ASS_APP_LOG = "合作应用 %s 取消关联小程序" //取消关联
BIND_AUDIT_APPLY_LOG = "合作应用 %s 申请关联小程序" //关联小程序
)
//小程序管理日志
const (
APP_ADD_LOG = "创建小程序(%s-%s" //创建小程序
APP_SUB_REVIEW_LOG = "提交小程序审核(%s-%s" //提交审核
APP_CANCEL_REVIEW_LOG = "撤销小程序审核(%s-%s" //撤销审核
APP_PUB_LOG = "上架小程序(%s-%s开发版本号%d" //上架
APP_DOWN_LOG = "下架小程序(%s-%s开发版本号%d" //下架
APP_EDIT_INFO_LOG = "修改小程序详情信息成功(%s-%s" //基本信息-编辑
APP_EDIT_TEST_DIREC_LOG = "修改小程序测试说明成功(%s-%s" //测试说明-编辑
)
func GenContent(s string, v ...interface{}) string {
return fmt.Sprintf(s, v...)
}
//关联/取消关连小程序
func GenContentAssApp(s string, bindName string, m map[string]string) string {
str := fmt.Sprintf(s, bindName)
mLen := len(m)
i := 1
for k, v := range m {
if i != mLen {
i += 1
str += fmt.Sprintf("%s-%s、", v, k)
} else {
str += fmt.Sprintf("%s-%s", v, k)
}
}
return str
}
type OperateData 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 GenOperateData(ctx context.Context, c *gin.Context, organId, content string, extra interface{}, oper string) error {
if producer == nil {
return nil
}
var data OperateData
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)
return sendMsg(ctx, config.GetConfig().KafkaDataLogTopic, string(jsonBytes))
}
//操作日志
type GenOperateDataV2Req struct {
OrganId string
Content string
Extra interface{}
Oper string
AccountId string
IsDev bool
}
func GenOperateDataV2(ctx context.Context, c *gin.Context, req GenOperateDataV2Req) error {
if producer == nil {
return nil
}
var data OperateData
data.Id = uuid.NewV4().String()
data.CreateTime = time.Now().UnixNano() / 1000000
if req.AccountId != "" {
account, err := getAccountById(ctx, 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 = getAddrIp(c.Request.Header.Get("X-Forwarded-For"))
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)
//return sendMsg("mop_operation_log", string(jsonBytes))
return sendMsg(ctx, config.GetConfig().KafkaDataLogTopic, string(jsonBytes))
}
func getAccountById(ctx context.Context, id string, isDev bool) (string, error) {
if isDev {
accountInfo, err := hCaller.GetAccountInfo(ctx, id)
if accountInfo != nil {
return accountInfo.Account, err
}
}
accountInfo, err := hCaller.GetAdminAccountInfo(ctx, id)
if accountInfo == nil {
return "", err
}
return accountInfo.Account, err
}