package service
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"qnc-server/config"
"qnc-server/utils"
"strconv"
"time"
)
// const webhookURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3b71e94f-8ca6-459b-b678-1db890e8170f"
type Notify struct {
}
type MarkdownMessage struct {
MsgType string `json:"msgtype"`
Markdown Markdown `json:"markdown"`
}
type Markdown struct {
Content string `json:"content"`
}
func (n *Notify) SendNotification(message string, interfa string, userID uint, orderID uint) {
enID, err := utils.IDEncrypt(strconv.Itoa(int(orderID)), "njbh287yfbuyh18suygbhd98")
if err != nil {
log.Println("IDEncrypt error:", err)
return
}
url := fmt.Sprintf("%s/api/pay/refund_details/%s", config.ConfigData.Server.Domain, enID)
currentTime := time.Now().Format("2006年1月2日 15:04:05")
content := fmt.Sprintf(
`【全能查】%s。
>接口:%s
>用户ID:%d
>订单ID:%d
>时间:%s
[点击跳转到退款页面](%s)`,
message, interfa, userID, orderID, currentTime, url,
)
var webhookURL = fmt.Sprintf("%s?key=%s", config.ConfigData.Robot.Addr, config.ConfigData.Robot.Webhook)
markdownMessage := MarkdownMessage{
MsgType: "markdown",
Markdown: Markdown{
Content: content,
},
}
jsonData, err := json.Marshal(markdownMessage)
if err != nil {
log.Printf("机器人通知 Marshal 错误:%s", err.Error())
return
}
req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(jsonData))
if err != nil {
log.Printf("机器人通知 NewRequest 错误:%s", err.Error())
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("机器人通知 请求 错误:%s", err.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("机器人通知 响应 错误:%s", resp.Status)
return
}
}
func (n *Notify) SendComplaintNotification(message string, complaintType string, platform string, complaintID string) {
currentTime := time.Now().Format("2006年1月2日 15:04:05")
content := fmt.Sprintf(
`【全能查】用户投诉
>投诉单号:%s
>投诉状态:%s
>投诉内容:%s
>投诉商户号平台:%s
>时间:%s`,
complaintID, complaintType, message, platform, currentTime,
)
var webhookURL = fmt.Sprintf("%s?key=%s", config.ConfigData.Robot.Addr, config.ConfigData.Robot.Webhook)
markdownMessage := MarkdownMessage{
MsgType: "markdown",
Markdown: Markdown{
Content: content,
},
}
jsonData, err := json.Marshal(markdownMessage)
if err != nil {
log.Printf("机器人通知 Marshal 错误:%s", err.Error())
return
}
req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(jsonData))
if err != nil {
log.Printf("机器人通知 NewRequest 错误:%s", err.Error())
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("机器人通知 请求 错误:%s", err.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("机器人通知 响应 错误:%s", resp.Status)
return
}
}