多笔立减金

This commit is contained in:
ziming 2025-12-11 14:28:34 +08:00
parent 9493648272
commit b8fc6a7650
2 changed files with 72 additions and 1 deletions

65
internal/pkg/helper/ip.go Normal file
View File

@ -0,0 +1,65 @@
package helper
import (
"net"
"strings"
"github.com/go-kratos/kratos/v2/transport/http"
)
// GetClientIP 获取客户端真实 IP
func GetClientIP(ctx http.Context) string {
// 检查 X-Forwarded-For 头(多个代理时格式为 "client, proxy1, proxy2"
if xff := ctx.Header().Get("X-Forwarded-For"); xff != "" {
ips := strings.Split(xff, ",")
for _, ip := range ips {
ip = strings.TrimSpace(ip)
if ip != "" {
// 验证是否为合法 IP
if isValidIP(ip) {
return ip
}
}
}
}
// 检查 X-Real-IP 头
if realIP := ctx.Header().Get("X-Real-IP"); realIP != "" {
if isValidIP(realIP) {
return realIP
}
}
// 检查 X-Forwarded
if forwarded := ctx.Header().Get("X-Forwarded"); forwarded != "" {
// 格式可能为 "for=client-ip;host=example.com;proto=https"
parts := strings.Split(forwarded, ";")
for _, part := range parts {
part = strings.TrimSpace(part)
if strings.HasPrefix(part, "for=") {
ip := strings.TrimPrefix(part, "for=")
ip = strings.Trim(ip, `"`) // 可能被引号包围
if isValidIP(ip) {
return ip
}
}
}
}
// 直接从 RemoteAddr 获取
remoteAddr := ctx.Request().RemoteAddr
if ip, _, err := net.SplitHostPort(remoteAddr); err == nil {
if isValidIP(ip) {
return ip
}
}
return ""
}
// 验证是否为合法 IP
func isValidIP(ip string) bool {
parsedIP := net.ParseIP(ip)
return parsedIP != nil
}

View File

@ -8,6 +8,7 @@ import (
"io" "io"
"voucher/internal/biz" "voucher/internal/biz"
"voucher/internal/biz/bo" "voucher/internal/biz/bo"
"voucher/internal/pkg/helper"
) )
type TripartiteService struct { type TripartiteService struct {
@ -20,6 +21,11 @@ func NewTripartiteService(multiBiz *biz.MultiBiz) *TripartiteService {
func (srv *TripartiteService) QiXingNotify(ctx http.Context) error { func (srv *TripartiteService) QiXingNotify(ctx http.Context) error {
ip := helper.GetClientIP(ctx)
if ip == "" {
return fmt.Errorf("获取请求 IP 失败")
}
bodyBytes, err := io.ReadAll(ctx.Request().Body) bodyBytes, err := io.ReadAll(ctx.Request().Body)
if err != nil { if err != nil {
return fmt.Errorf("read body error: %v", err) return fmt.Errorf("read body error: %v", err)
@ -27,7 +33,7 @@ func (srv *TripartiteService) QiXingNotify(ctx http.Context) error {
var req *bo.WechatVoucherNotifyBo var req *bo.WechatVoucherNotifyBo
if err = json.Unmarshal(bodyBytes, &req); err != nil { if err = json.Unmarshal(bodyBytes, &req); err != nil {
log.Errorf("qixing notify error:%v,body:%s", err, string(bodyBytes)) log.Errorf("qixing notify ip:%s,error:%v,body:%s", ip, err, string(bodyBytes))
return fmt.Errorf("json unmarshal bodyBytes error: %v", err) return fmt.Errorf("json unmarshal bodyBytes error: %v", err)
} }