map对齐
Build docker and publish / build (20.15.1) (push) Successful in 8m21s

This commit is contained in:
2026-03-07 05:54:49 -08:00
parent 71f4bd1f5f
commit a0ae7b1c8d
4 changed files with 150 additions and 53 deletions
@@ -2,15 +2,14 @@ package apple
import (
"context"
"strings"
"time"
iapmodel "github.com/perfect-panel/server/internal/model/iap/apple"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/constant"
"github.com/perfect-panel/server/pkg/logger"
"github.com/perfect-panel/server/pkg/xerr"
iapapple "github.com/perfect-panel/server/pkg/iap/apple"
"github.com/perfect-panel/server/pkg/constant"
"github.com/pkg/errors"
)
@@ -33,30 +32,36 @@ func (l *GetStatusLogic) GetStatus() (*types.GetAppleStatusResponse, error) {
if !ok || u == nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "invalid access")
}
pm, _ := iapapple.ParseProductMap(l.svcCtx.Config.Site.CustomData)
var latest *iapmodel.Transaction
var err error
for pid := range pm.Items {
item, e := iapmodel.NewModel(l.svcCtx.DB, l.svcCtx.Redis).FindByUserAndProduct(l.ctx, u.Id, pid)
if e == nil && item != nil && item.Id != 0 {
if latest == nil || item.PurchaseAt.After(latest.PurchaseAt) {
latest = item
// 查该用户所有状态的订阅,找 IAP 相关的(token 以 iap: 开头)
subs, err := l.svcCtx.UserModel.QueryUserSubscribe(l.ctx, u.Id, 1)
if err != nil {
return &types.GetAppleStatusResponse{Active: false, ExpiresAt: 0, Tier: ""}, nil
}
now := time.Now()
var bestExpire time.Time
var bestTier string
for _, sub := range subs {
if sub == nil {
continue
}
// 仅处理 IAP 创建的订阅(token 以 "iap:" 开头)
if !strings.HasPrefix(sub.Token, "iap:") {
continue
}
if sub.ExpireTime.After(bestExpire) {
bestExpire = sub.ExpireTime
if sub.Subscribe != nil {
bestTier = sub.Subscribe.Name
}
}
}
if latest == nil {
return &types.GetAppleStatusResponse{
Active: false,
ExpiresAt: 0,
Tier: "",
}, nil
if bestExpire.IsZero() {
return &types.GetAppleStatusResponse{Active: false, ExpiresAt: 0, Tier: ""}, nil
}
m := pm.Items[latest.ProductId]
exp := iapapple.CalcExpire(latest.PurchaseAt, m.DurationDays).Unix()
active := latest.RevocationAt == nil && (exp == 0 || exp > time.Now().Unix())
active := bestExpire.After(now)
return &types.GetAppleStatusResponse{
Active: active,
ExpiresAt: exp,
Tier: m.Tier,
}, err
ExpiresAt: bestExpire.Unix(),
Tier: bestTier,
}, nil
}