同步历史版本代码
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package apple
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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/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"
|
||||
)
|
||||
|
||||
type GetStatusLogic struct {
|
||||
logger.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStatusLogic {
|
||||
return &GetStatusLogic{
|
||||
Logger: logger.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetStatusLogic) GetStatus() (*types.GetAppleStatusResponse, error) {
|
||||
u, ok := l.ctx.Value(constant.CtxKeyUser).(*struct{ Id int64 })
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
if latest == nil {
|
||||
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())
|
||||
return &types.GetAppleStatusResponse{
|
||||
Active: active,
|
||||
ExpiresAt: exp,
|
||||
Tier: m.Tier,
|
||||
}, err
|
||||
}
|
||||
Reference in New Issue
Block a user