mirror of
https://git.oceanpay.cc/danial/kami_scripts.git
synced 2025-12-18 21:12:33 +00:00
- 新增配置项 QueryURL,支持自定义查询地址 - 修改QueryCardInput结构体,调整字段名更准确 - 使用resty替代beego httplib,提升请求稳定性和重试能力 - 调整请求URL为kami-spider-monorepo服务地址 - 优化错误重试逻辑,针对验证码识别失败进行重试 - 调整响应解析和错误处理逻辑 - 更新order_service调用,传递新配置和请求参数 - 升级多个依赖包版本,提升模块稳定性和安全性 - 修正配置文件config.yaml中字段及格式,使配置更合理
104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/go-resty/resty/v2"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type HeePayClient struct {
|
|
}
|
|
|
|
func NewHeePayClient() *HeePayClient {
|
|
return &HeePayClient{}
|
|
}
|
|
|
|
type QueryCardInput struct {
|
|
QueryURL string `json:"query_url"`
|
|
OrderId string `json:"order_id"`
|
|
CardNumber string `json:"card_number"`
|
|
CardPassword string `json:"card_password"`
|
|
}
|
|
|
|
type QueryCardOutput struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data struct {
|
|
CardNumber string `json:"card_number"`
|
|
CardPassword string `json:"card_password"`
|
|
CardType string `json:"card_type"`
|
|
CardStatus string `json:"card_status"`
|
|
JPoints string `json:"j_points"`
|
|
LockedJPoints string `json:"locked_j_points"`
|
|
AvailableJPoints string `json:"available_j_points"`
|
|
SuccessTime string `json:"success_time"`
|
|
UseRecords []struct {
|
|
TransactionType string `json:"transaction_type"`
|
|
UsedJPoints string `json:"used_j_points"`
|
|
UsageNote string `json:"usage_note"`
|
|
UsageTime string `json:"usage_time"`
|
|
Result string `json:"result"`
|
|
} `json:"use_records,omitempty"`
|
|
} `json:"data,omitempty"`
|
|
}
|
|
|
|
const (
|
|
maxRetries = 3
|
|
retryDelay = time.Second
|
|
)
|
|
|
|
func (c *HeePayClient) QueryCard(ctx context.Context, input *QueryCardInput) (*QueryCardOutput, error) {
|
|
var lastErr error
|
|
webClient := resty.New().SetTimeout(time.Second * 30).SetRetryCount(3)
|
|
|
|
for i := range maxRetries {
|
|
response, err := webClient.R().SetContext(ctx).SetBody(input).Post("http://kami-spider-monorepo:8000/api/heepay/query")
|
|
if err != nil {
|
|
lastErr = err
|
|
continue
|
|
}
|
|
var output QueryCardOutput
|
|
err = json.Unmarshal(response.Body(), &output)
|
|
if err != nil {
|
|
lastErr = err
|
|
continue
|
|
}
|
|
|
|
// 检查是否需要重试的错误
|
|
if strings.Contains(output.Message, "验证码识别失败") {
|
|
lastErr = fmt.Errorf("需要重试的错误: %s", output.Message)
|
|
if i < maxRetries-1 {
|
|
time.Sleep(retryDelay)
|
|
continue
|
|
}
|
|
}
|
|
|
|
return &output, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("重试%d次后仍然失败: %v", maxRetries, lastErr)
|
|
}
|
|
|
|
func (c *HeePayClient) ToString(ctx context.Context, input *QueryCardInput) (string, error) {
|
|
output, err := c.QueryCard(ctx, input)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if output.Code == 6001 {
|
|
return "卡密不存在", nil
|
|
}
|
|
if len(output.Data.UseRecords) != 0 {
|
|
return fmt.Sprintf("该卡已被其他用户绑定,绑定时间:%s", output.Data.UseRecords[0].UsageTime), nil
|
|
}
|
|
if output.Data.JPoints != output.Data.AvailableJPoints {
|
|
return fmt.Sprintf("该卡已被其他用户绑定,绑定时间:%s", output.Data.SuccessTime), nil
|
|
}
|
|
if output.Code != 0 {
|
|
return output.Message, nil
|
|
}
|
|
return "正常", nil
|
|
}
|