This commit is contained in:
duyu 2024-07-26 10:52:53 +08:00
parent c7676ee926
commit 347c44ce01
6 changed files with 168 additions and 3 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -1,6 +1,7 @@
package orders package orders
import ( import (
"strconv"
"time" "time"
common "com.snow.auto_monitor/app/http/controllers" common "com.snow.auto_monitor/app/http/controllers"
@ -83,6 +84,12 @@ func FinishOrder(c *gin.Context) {
return return
} }
err = transServ.AddCallback(strconv.Itoa(int(orders.Id)))
if err != nil {
common.Error(c, 400, "添加回调失败")
return
}
response := &transEnt.FinishOrderResp{ response := &transEnt.FinishOrderResp{
Id: orders.Id, Id: orders.Id,
} }

View File

@ -95,3 +95,17 @@ func VerifySign() gin.HandlerFunc {
} }
} }
} }
func GetMD5Sign(data map[string]interface{}) (string, string) {
merchanId := data["merchantId"].(int64)
merchant, has, err := mermod.GetInstance().GetById(merchanId)
if err != nil {
return "", ""
}
if !has {
return "", ""
}
//验证签名是否正确
hash, rawStr := GenMD5Sign(data, merchant.PrivateKey)
return hash, rawStr
}

View File

@ -2,10 +2,12 @@ package transfersys
import ( import (
"errors" "errors"
"time"
mermod "com.snow.auto_monitor/app/models/merchant" mermod "com.snow.auto_monitor/app/models/merchant"
models "com.snow.auto_monitor/app/models/orders" models "com.snow.auto_monitor/app/models/orders"
promod "com.snow.auto_monitor/app/models/product" promod "com.snow.auto_monitor/app/models/product"
"com.snow.auto_monitor/app/utils/rdbdq"
) )
func GetOrder() (res *models.Orders, err error) { func GetOrder() (res *models.Orders, err error) {
@ -44,3 +46,8 @@ func FinishOrder(orders *models.Orders) (affected int64, err error) {
affected, err = models.GetInstance().SetDealingOrder(orders) affected, err = models.GetInstance().SetDealingOrder(orders)
return return
} }
func AddCallback(order_id string) (err error) {
err = rdbdq.WriteOne("1_"+order_id, time.Now().Add(1*time.Second).Unix())
return
}

View File

@ -0,0 +1,92 @@
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()
}

45
app/utils/rdbdq/rdbdq.go Normal file
View File

@ -0,0 +1,45 @@
package rdbdq
import (
"context"
"fmt"
"time"
goredis "github.com/go-redis/redis/v8"
"github.com/qit-team/snow-core/redis"
)
func WriteOne(value string, executeIime int64) (err error) {
rdb := redis.GetRedis(redis.SingletonMain)
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
err = rdb.ZAdd(ctx, "dealy_queue", &goredis.Z{
Score: float64(executeIime),
Member: value,
}).Err()
return
}
func ReadLatestOne() (res string, err error) {
rdb := redis.GetRedis(redis.SingletonMain)
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
// XReadGroup读取消费者中消息
tasks, err := rdb.ZRangeByScore(ctx, "dealy_queue", &goredis.ZRangeBy{
Min: "-inf",
Max: fmt.Sprintf("%d", time.Now().Unix()),
}).Result()
if err != nil {
return "", err
}
if len(tasks) > 0 {
res := tasks[0]
err = rdb.ZRem(ctx, "dealy_queue", res).Err()
return res, nil
}
return "", nil
}