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))