com.snow.auto_monitor/app/services/transfersys/transfersys_callback.go

93 lines
2.6 KiB
Go
Raw Normal View History

2024-07-26 10:52:53 +08:00
package transfersys
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"com.snow.auto_monitor/app/http/middlewares"
orderServ "com.snow.auto_monitor/app/services/orders"
"com.snow.auto_monitor/app/utils/rdbdq"
"github.com/qit-team/snow-core/log/logger"
)
func DoCallBack() {
fmt.Println("开始执行周期任务DoCallBack")
// 创建一个新的Ticker每3秒钟触发一次
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop() // 在函数结束时停止Ticker
for range ticker.C {
for i := 0; i < 10; i++ {
res, err := rdbdq.ReadLatestOne()
if err != nil {
logger.Error(context.TODO(), "middlewares", err)
continue
}
if res != "" {
// fmt.Println("获取到的数据:", res)
wait_time, err := strconv.Atoi(strings.Split(res, "_")[0])
if err != nil {
logger.Error(context.TODO(), "middlewares", err)
continue
}
order_id, err := strconv.Atoi(strings.Split(res, "_")[1])
if err != nil {
logger.Error(context.TODO(), "middlewares", err)
continue
}
order, err := orderServ.GetById(int64(order_id))
if err != nil {
logger.Error(context.TODO(), "middlewares", err)
continue
}
// fmt.Println(order)
contentType := "application/x-www-form-urlencoded"
status := "01"
if order.Status != 1 {
status = "03"
}
datamap := map[string]interface{}{
"merchantId": order.MerchantId,
"outTradeNo": order.OutTradeNo,
"status": status,
"rechargeAccount": order.RechargeAccount,
}
sign, _ := middlewares.GetMD5Sign(datamap)
data := fmt.Sprintf("merchantId=%d&outTradeNo=%s&status=%s&rechargeAccount=%s&sign=%s", order.MerchantId, order.OutTradeNo, status, order.RechargeAccount, sign)
// fmt.Println(data)
notifyUrl := "http://openapi.1688sup.com/notify/JingRongZhuanDan"
resp, err := http.Post(notifyUrl, contentType, strings.NewReader(data))
if err != nil {
logger.Error(context.TODO(), "middlewares", err)
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Error(context.TODO(), "middlewares", err)
return
}
if string(b) != "success" && wait_time < 1024 {
wait_time = wait_time * 4
err = rdbdq.WriteOne(strconv.FormatInt(int64(wait_time), 10)+"_"+strconv.FormatInt(int64(order_id), 10),
time.Now().Add(time.Duration(wait_time)*time.Second).Unix())
if err != nil {
logger.Error(context.TODO(), "middlewares", string(b))
logger.Error(context.TODO(), "middlewares", err)
return
}
}
}
}
}
}
func init() {
go DoCallBack()
}