hi-server/internal/logic/public/iap/apple/getStatusLogic.go
shanshanzhong a0ae7b1c8d
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 8m21s
map对齐
2026-03-07 05:54:49 -08:00

68 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package apple
import (
"context"
"strings"
"time"
"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"
"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")
}
// 查该用户所有状态的订阅,找 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 bestExpire.IsZero() {
return &types.GetAppleStatusResponse{Active: false, ExpiresAt: 0, Tier: ""}, nil
}
active := bestExpire.After(now)
return &types.GetAppleStatusResponse{
Active: active,
ExpiresAt: bestExpire.Unix(),
Tier: bestTier,
}, nil
}