micro-account/pkg/application/msg/msg.go
2026-06-01 10:34:16 +08:00

117 lines
2.9 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 msg
import (
"bytes"
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"sort"
"strings"
"time"
"dubbo.apache.org/dubbo-go/v3/common/logger"
)
const (
ClientID = "fenglian"
SECRET = "93a8894116ce0152dd5e145370fc83aa"
)
type SendReport struct {
MsgNo string `json:"msgNo"`
TempNo string `json:"tempNo"`
SigNo string `json:"signNo"`
Content string `json:"content"`
SendTime string `json:"sendTime"`
Mobile string `json:"mobile"`
SendFrom string `json:"sendFrom"`
}
type SendMsgResponse struct {
Code int `json:"code"` // 对应 JSON 中的 "code" 字段(整数)
Msg string `json:"msg"` // 对应 JSON 中的 "msg" 字段(字符串)
MsgNo string `json:"msg_no"` // 对应 JSON 中的 "msg_no" 字段(字符串)
Count int `json:"count"` // 对应 JSON 中的 "count" 字段(整数)
}
// 6位随机验证码
func RandCode() string {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
rndCode := fmt.Sprintf("%06v", rnd.Int31n(1000000))
fmt.Println(rndCode)
return rndCode
}
func ReportMsgWithSendFrom(res SendMsgResponse, telNum string, tempNo uint, sigNo uint, content string, sendFrom string) {
//发送成功,上报发送信息
reqObj := SendReport{
MsgNo: res.MsgNo,
TempNo: fmt.Sprintf("%d", tempNo),
SigNo: fmt.Sprintf("%d", sigNo),
Content: content,
Mobile: telNum,
SendTime: time.Now().Format("2006-01-02 15:04:05"),
SendFrom: sendFrom,
}
jsonData, err := json.Marshal(reqObj)
if err != nil {
return
}
// 构造 POST 请求
non := RandCode()
timeNow := fmt.Sprintf("%d", time.Now().Unix())
sign, _ := makeSignature(SECRET, timeNow, non, ClientID)
url := fmt.Sprintf("https://common.szjixun.cn/api/msg/report?client=%s&nonce=%s&timestamp=%s&sign=%s", ClientID, non, timeNow, sign) // 这里的SECRET需要替换为实际的密钥
fmt.Println("url", url)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonData))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
logger.Fatalf("发送请求失败: %v", err)
return
}
defer resp.Body.Close()
// 检查状态码
if resp.StatusCode != http.StatusOK {
errBody, _ := io.ReadAll(resp.Body)
logger.Fatalf("请求失败,状态码: %d响应: %s", resp.StatusCode, string(errBody))
return
}
// 读取并反序列化响应
respBody, err := io.ReadAll(resp.Body)
fmt.Println(string(respBody))
return
}
func makeSignature(secret string, timestamp string, nonce string, client string) (ret string, err error) {
strs := []string{secret, timestamp, nonce, client}
fmt.Println(timestamp)
fmt.Println(nonce)
sort.Strings(strs)
vv := strings.Join(strs, "")
t := sha1.New()
t.Write([]byte(vv))
return fmt.Sprintf("%x", t.Sum(nil)), nil
}