refactor(iap/apple): 优化商品ID解析逻辑以提高可读性
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m22s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m22s
将原有的字符串索引查找方式改为分割字符串后遍历检查,使代码更清晰易读
This commit is contained in:
parent
e98709b511
commit
2fdc9c8127
@ -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))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user