hi-server/internal/logic/public/iap/apple/getStatusLogic.go
shanshanzhong 62186ca672
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m37s
feat(iap/apple): 实现苹果IAP非续期订阅功能
新增苹果IAP相关接口与逻辑,包括产品列表查询、交易绑定、状态查询和恢复购买功能。移除旧的IAP验证逻辑,重构订阅系统以支持苹果IAP交易记录存储和权益计算。

- 新增/pkg/iap/apple包处理JWS解析和产品映射
- 实现GET /products、POST /attach、POST /restore和GET /status接口
- 新增apple_iap_transactions表存储交易记录
- 更新文档说明配置方式和接口规范
- 移除旧的AppleIAP验证和通知处理逻辑
2025-12-13 20:54:50 -08:00

63 lines
1.7 KiB
Go

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
}