qnc-server-old/service/notify.go
2024-09-14 10:48:09 +08:00

128 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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(
`<font color="warning">【全能查】</font>%s。
>接口:<font color="comment">%s</font>
>用户ID:<font color="comment">%d</font>
>订单ID:<font color="comment">%d</font>
>时间:<font color="comment">%s</font>
[点击跳转到退款页面](%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(
`<font color="warning">【全能查】</font>用户投诉
>投诉单号:<font color="comment">%s</font>
>投诉状态:<font color="comment">%s</font>
>投诉内容:<font color="comment">%s</font>
>投诉商户号平台:<font color="comment">%s</font>
>时间:<font color="comment">%s</font>`,
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
}
}