Files
kami_itunes_tools/main.go
danial 6424dcfe8b feat(queue): 添加苹果卡充值功能并优化消费流程
- 在 consumption.go 中添加了苹果卡充值的 API 请求
- 在 main.go 中实现了 SendAppleCard 函数,用于发送苹果卡信息
- 优化了消费流程,增加了时间延迟和错误处理
- 引入了新的库和工具,提高了代码的健壮性和可读性
2025-03-26 23:32:50 +08:00

207 lines
4.9 KiB
Go
Raw Permalink 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 main
import (
"bufio"
"bytes"
"demo/models"
"demo/queue"
"demo/utils"
"encoding/json"
"fmt"
"io"
"log/slog"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
)
// 读取配置文件
func readConfig() (*models.Host, string) {
file, err := os.Open("config.txt")
if err != nil {
fmt.Println("读取配置文件出错", err)
return nil, ""
}
defer func(file *os.File) {
_ = file.Close()
}(file)
var lines []string
//读取第一行,默认是配置文件
reader := bufio.NewReader(file)
for {
line, err2 := reader.ReadString(byte('\n'))
if err2 == io.EOF {
lines = append(lines, line)
break
}
lines = append(lines, line[:len(line)-1])
}
for i, line := range lines {
fmt.Printf("%d: %s\n", i+1, line)
}
fmt.Print("请输入IP序号")
var i int
_, _ = fmt.Scanln(&i)
s := strings.Split(strings.TrimSpace(lines[i-1]), " ")
return &models.Host{
GateWayHost: s[0],
SubmitHost: s[1],
}, ""
}
func Queue(host *models.Host, count int) {
queue.Production(host.GateWayHost, models.Order{
Count: count,
Amount: 100,
})
queue.Consumption(host.SubmitHost, models.Order{
Count: count,
Amount: 100,
})
}
func QueueWithOrder(host *models.Host, count int) {
totalAmount := 0
totalAllowedAmount := []int{100}
for i := range count {
time.Sleep(time.Second * 5)
o := models.Order{
Count: count,
Amount: totalAllowedAmount[rand.Intn(len(totalAllowedAmount))],
}
totalAmount += o.Amount
o.AmountAfter = totalAmount
fmt.Println("正在发送订单:", i+1)
queue.ProductionOne(host.GateWayHost, o)
fmt.Println("正在处理订单:", i+1)
queue.ConsumptionOne(host.SubmitHost, o, false, false)
}
fmt.Println("总充值,消费", totalAmount)
}
func QueueWithRandomStatus(host *models.Host, count int) {
totalAmount := 0
for range count {
o := models.Order{
Count: count,
Amount: rand.Intn(100) + 100,
}
totalAmount += o.Amount
o.AmountAfter = totalAmount
queue.ProductionOne(host.GateWayHost, o)
m := queue.ConsumptionOne(host.SubmitHost, o, true, false)
switch m.Status {
case "20":
totalAmount -= 0
default:
totalAmount -= o.Amount
}
fmt.Println("充值金额", totalAmount)
}
}
func QueueWithRandomDiff(host *models.Host, count int) {
totalAmount := 0
totalFactAmount := 0
for range count {
o := models.Order{
Count: count,
Amount: rand.Intn(9900) + 100,
}
totalAmount += o.Amount
totalFactAmount += o.Amount
o.AmountAfter = totalAmount
queue.ProductionOne(host.GateWayHost, o)
m := queue.ConsumptionOne(host.SubmitHost, o, false, true)
switch m.Status {
case "20":
totalFactAmount -= o.Amount
totalFactAmount += m.Amount
default:
totalAmount -= o.Amount
}
}
fmt.Println("充值金额:", totalAmount, "实际充值金额:", totalFactAmount)
}
// SendAppleCard 发送苹果卡
func SendAppleCard() {
params := map[string]string{
"callbackUrl": "http://kami_gateway:12309/appleCard/notify",
"attach": "6666ccd7ae7b9424435b2a90a4b2d38cca6",
"faceValue": "200",
"cardNo": "",
"cardPass": "X5K4Z534VRV4N4NR",
"merchantId": "P1911913300694929409",
"timestamp": strconv.FormatInt(time.Now().Unix(), 10),
}
params["sign"] = utils.TmpEncrypt(params["attach"] + params["cardPass"] + params["timestamp"])
marshal, err := json.Marshal(params)
if err != nil {
slog.Error("内部错误请稍后再试试01")
return
//return false, "内部错误请稍后再试试01"
}
client := http.Client{}
res, err := client.Post("http://118.195.133.33:12310/api/cardInfo/appleCard/submit", "application/json", bytes.NewReader(marshal))
if err != nil || res == nil {
slog.Error("内部错误请稍后再试试02")
return
}
resp, err := io.ReadAll(res.Body)
if err != nil {
slog.Error("内部错误请稍后再试试03")
return
}
// 打印res
slog.Info("res", "返回信息", string(resp))
}
func main() {
//SendAppleCard()
host, _ := readConfig()
if host == nil {
fmt.Println("请配置host")
return
}
//选择项,选择手动处理还是自动化处理
fmt.Println("选择项 消费1.手动处理 2.消费 3.拉单 4.拉单并消费(无序) 5.拉单并消费(有序) 6.拉单并消费(存在错误率) 7.拉单并消费(存在错误金额)")
var choice int
fmt.Print("请输入选择:")
_, _ = fmt.Scanln(&choice)
//首先填写批量手动处理订单个数
fmt.Print("请输入批量手动处理订单个数:")
var count int
_, _ = fmt.Scanln(&count)
switch choice {
case 1:
queue.Manual(host.SubmitHost, count)
case 2:
queue.Consumption(host.SubmitHost, models.Order{
Count: count,
Amount: 100,
})
case 3:
queue.Production(host.GateWayHost, models.Order{
Count: count,
Amount: 100,
})
case 4:
Queue(host, count)
case 5:
QueueWithOrder(host, count)
case 6:
QueueWithRandomStatus(host, count)
case 7:
QueueWithRandomDiff(host, count)
default:
fmt.Println("输入错误")
}
fmt.Println("退出成功")
}