Files
kami_shop/internal/integrations/scan_shop.go
danial a49e73bf33 refactor(conf): 删除未使用的配置项
- 移除了 conf/app.conf 中的 [gateway2]配置节
- 删除了 internal/config/config.go 中的 GetGateway2 和 GetRechargeTMall 函数
- 在 internal/utils/client/client.go 和 internal/integrations/scan_shop.go 中添加了请求超时设置
2025-02-23 19:33:06 +08:00

93 lines
2.3 KiB
Go

package integrations
import (
"context"
"fmt"
"net/http"
"shop/internal/config"
"shop/internal/service"
"shop/internal/traceRouter"
"shop/internal/utils"
"strconv"
"time"
"github.com/beego/beego/v2/client/httplib"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/widuu/gojson"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
type ScanShopController struct {
Params map[string]string
}
type DataJSON struct {
Code int
Msg string
}
type ResponseJSON struct {
Code int
Msg string
OrderNo string
Url string
Qrcode string
}
var (
HOST = config.GetGateway()
ScanHost = HOST + "/gateway/scan"
CreateOrderHost = HOST + "/gateway/createOrder"
)
func init() {
HOST, _ = web.AppConfig.String("gateway::url")
ScanHost = HOST + "/gateway/scan"
}
func (c *ScanShopController) Prepare() {
c.Params = make(map[string]string)
c.Params["orderPeriod"] = "24"
}
func (c *ScanShopController) Shop(ctx context.Context, requestHost, payKey string) *ResponseJSON {
ctx, span := traceRouter.CreateSpan(ctx, "ScanShopController", "ShopHttp")
defer span.End()
responseJSON := new(ResponseJSON)
reqUrl := ScanHost
c.Params["payKey"] = payKey
c.Params["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
merchantInfo := service.GetMerchantByPasskey(ctx, payKey)
sign := utils.GetMD5SignMF(c.Params, merchantInfo.MerchantSecret)
c.Params["sign"] = sign
logs.Info(reqUrl, c.Params)
//添加超时时间
req := httplib.NewBeegoRequestWithCtx(ctx, reqUrl, "POST")
req.SetTimeout(30*time.Second, 30*time.Second)
req.SetTransport(otelhttp.NewTransport(http.DefaultTransport))
for k, v := range c.Params {
req.Param(k, v)
}
response, err := req.String()
if err != nil {
traceRouter.Logger.WithContext(ctx).Error(fmt.Sprintf("err %s", err.Error()))
responseJSON.Code = -1
responseJSON.Msg = response + " ;"
} else {
statusCode := gojson.Json(response).Get("statusCode").Tostring()
if statusCode != "00" {
msg := gojson.Json(response).Get("msg").Tostring()
responseJSON.Code = -1
responseJSON.Msg = msg
} else {
responseJSON.Code = 200
payUrl := gojson.Json(response).Get("payURL").Tostring()
orderNo := gojson.Json(response).Get("orderNo").Tostring()
responseJSON.OrderNo = orderNo
responseJSON.Url = payUrl
}
}
return responseJSON
}