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
|
var parsedQuantity int64
|
||||||
{
|
{
|
||||||
pid := strings.ToLower(txPayload.ProductId)
|
pid := strings.ToLower(txPayload.ProductId)
|
||||||
var unitIdx = -1
|
parts := strings.Split(pid, ".")
|
||||||
var unitLen = 0
|
for i := len(parts) - 1; i >= 0; i-- {
|
||||||
if i := strings.Index(pid, "day"); i >= 0 {
|
p := parts[i]
|
||||||
unitIdx, unitLen, parsedUnit = i, 3, "Day"
|
if strings.HasPrefix(p, "day") || strings.HasPrefix(p, "month") || strings.HasPrefix(p, "year") {
|
||||||
}
|
switch {
|
||||||
if i := strings.Index(pid, "month"); i >= 0 {
|
case strings.HasPrefix(p, "day"):
|
||||||
if unitIdx == -1 || i < unitIdx {
|
parsedUnit = "Day"
|
||||||
unitIdx, unitLen, parsedUnit = i, 5, "Month"
|
p = p[len("day"):]
|
||||||
}
|
case strings.HasPrefix(p, "month"):
|
||||||
}
|
parsedUnit = "Month"
|
||||||
if i := strings.Index(pid, "year"); i >= 0 {
|
p = p[len("month"):]
|
||||||
if unitIdx == -1 || i < unitIdx {
|
case strings.HasPrefix(p, "year"):
|
||||||
unitIdx, unitLen, parsedUnit = i, 4, "Year"
|
parsedUnit = "Year"
|
||||||
}
|
p = p[len("year"):]
|
||||||
}
|
}
|
||||||
if unitIdx >= 0 {
|
digits := p
|
||||||
sub := pid[unitIdx+unitLen:]
|
for j := 0; j < len(digits); j++ {
|
||||||
for i := 0; i < len(sub); i++ {
|
if digits[j] < '0' || digits[j] > '9' {
|
||||||
if sub[i] < '0' || sub[i] > '9' {
|
digits = digits[:j]
|
||||||
sub = sub[:i]
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if q, e := strconv.ParseInt(digits, 10, 64); e == nil && q > 0 {
|
||||||
|
parsedQuantity = q
|
||||||
break
|
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))
|
l.Infow("商品映射解析", logger.Field("productId", txPayload.ProductId), logger.Field("解析单位", parsedUnit), logger.Field("解析数量", parsedQuantity))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user