多笔立减金
This commit is contained in:
parent
9493648272
commit
b8fc6a7650
|
|
@ -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
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"io"
|
||||
"voucher/internal/biz"
|
||||
"voucher/internal/biz/bo"
|
||||
"voucher/internal/pkg/helper"
|
||||
)
|
||||
|
||||
type TripartiteService struct {
|
||||
|
|
@ -20,6 +21,11 @@ func NewTripartiteService(multiBiz *biz.MultiBiz) *TripartiteService {
|
|||
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read body error: %v", err)
|
||||
|
|
@ -27,7 +33,7 @@ func (srv *TripartiteService) QiXingNotify(ctx http.Context) error {
|
|||
|
||||
var req *bo.WechatVoucherNotifyBo
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue