From 2fdc9c812749bbb23010adeadf989677300f30e2 Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Wed, 17 Dec 2025 19:33:56 -0800 Subject: [PATCH] =?UTF-8?q?refactor(iap/apple):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=95=86=E5=93=81ID=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BB=A5=E6=8F=90=E9=AB=98=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原有的字符串索引查找方式改为分割字符串后遍历检查,使代码更清晰易读 --- .../iap/apple/attachTransactionLogic.go | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/internal/logic/public/iap/apple/attachTransactionLogic.go b/internal/logic/public/iap/apple/attachTransactionLogic.go index e7345fc..bceb8b6 100644 --- a/internal/logic/public/iap/apple/attachTransactionLogic.go +++ b/internal/logic/public/iap/apple/attachTransactionLogic.go @@ -63,32 +63,33 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest var parsedQuantity int64 { pid := strings.ToLower(txPayload.ProductId) - var unitIdx = -1 - var unitLen = 0 - if i := strings.Index(pid, "day"); i >= 0 { - unitIdx, unitLen, parsedUnit = i, 3, "Day" - } - if i := strings.Index(pid, "month"); i >= 0 { - if unitIdx == -1 || i < unitIdx { - unitIdx, unitLen, parsedUnit = i, 5, "Month" - } - } - if i := strings.Index(pid, "year"); i >= 0 { - if unitIdx == -1 || i < unitIdx { - unitIdx, unitLen, parsedUnit = i, 4, "Year" - } - } - if unitIdx >= 0 { - sub := pid[unitIdx+unitLen:] - for i := 0; i < len(sub); i++ { - if sub[i] < '0' || sub[i] > '9' { - sub = sub[:i] + parts := strings.Split(pid, ".") + for i := len(parts) - 1; i >= 0; i-- { + p := parts[i] + if strings.HasPrefix(p, "day") || strings.HasPrefix(p, "month") || strings.HasPrefix(p, "year") { + switch { + case strings.HasPrefix(p, "day"): + parsedUnit = "Day" + p = p[len("day"):] + case strings.HasPrefix(p, "month"): + parsedUnit = "Month" + p = p[len("month"):] + case strings.HasPrefix(p, "year"): + parsedUnit = "Year" + p = p[len("year"):] + } + digits := p + for j := 0; j < len(digits); j++ { + if digits[j] < '0' || digits[j] > '9' { + digits = digits[:j] + break + } + } + if q, e := strconv.ParseInt(digits, 10, 64); e == nil && q > 0 { + parsedQuantity = q break } } - if q, e := strconv.ParseInt(sub, 10, 64); e == nil && q > 0 { - parsedQuantity = q - } } } l.Infow("商品映射解析", logger.Field("productId", txPayload.ProductId), logger.Field("解析单位", parsedUnit), logger.Field("解析数量", parsedQuantity))