Compare commits

...

11 Commits

Author SHA1 Message Date
70a5b3db89 新增自定义时间记录 2025-09-17 17:40:31 +08:00
40bd9bb254 游戏日志方法封装 2025-09-12 18:47:14 +08:00
ayflying
4473003a36 荣耀消耗后打印更多日志 2025-09-10 12:15:25 +08:00
ayflying
e5e6068337 修改荣耀消耗使用ClientSecret 2025-09-09 15:56:46 +08:00
ayflying
1355634c22 返回荣耀消耗的错误类型 2025-09-09 14:51:26 +08:00
ayflying
2709af041b 新增写失败也会跳过,下次再执行 2025-09-03 14:37:05 +08:00
ayflying
33c8712c72 去掉更新日志 2025-09-03 13:46:39 +08:00
ayflying
26763c04e3 修改act持久化通道安全 2025-09-03 12:19:35 +08:00
ayflying
8c60a1f6c7 执行通道进入协程,预防拥堵 2025-09-03 11:14:32 +08:00
ayflying
62b0e429b3 如果检测到当前正在活跃,不删除缓存key 2025-09-03 10:02:34 +08:00
ayflying
e1f1bea0e7 增加有优雅的持久化管道,提高上传速度与抗打断机制 2025-09-02 18:16:55 +08:00
7 changed files with 301 additions and 66 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/ayflying/utility_go/internal/model/do" "github.com/ayflying/utility_go/internal/model/do"
@@ -14,6 +15,7 @@ import (
service2 "github.com/ayflying/utility_go/service" service2 "github.com/ayflying/utility_go/service"
"github.com/ayflying/utility_go/tools" "github.com/ayflying/utility_go/tools"
"github.com/gogf/gf/v2/container/gset" "github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/os/gtime"
@@ -286,28 +288,54 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
// @Description: 保存游戏活动数据 // @Description: 保存游戏活动数据
// @receiver s *sGameAct: 游戏活动服务结构体指针 // @receiver s *sGameAct: 游戏活动服务结构体指针
// @return err error: 返回错误信息 // @return err error: 返回错误信息
// SavesV2 保存游戏活动数据
func (s *sGameAct) SavesV2() (err error) { func (s *sGameAct) SavesV2() (err error) {
var ctx = gctx.New() var ctx = gctx.New()
g.Log().Debug(ctx, "开始执行游戏act数据保存了") g.Log().Debug(ctx, "开始执行游戏act数据保存了")
//如果没有执行过,设置时间戳
// 最大允许执行时间
RunTimeMax = gtime.Now().Add(time.Minute * 30) RunTimeMax = gtime.Now().Add(time.Minute * 30)
//cacheKey := fmt.Sprintf("act:%v:*", actId) // 使用局部通道替代包级通道,避免并发冲突
var add = make([]*entity.GameAct, 0) addChan := make(chan *entity.GameAct, 1000)
var update = make([]*entity.GameAct, 0) updateChan := make(chan *entity.GameAct, 1000)
//循环获取缓存数据 errChan := make(chan error, 1)
err = tools.Redis.RedisScanV2("act:*", func(keys []string) (err error) { var wg sync.WaitGroup
for _, key := range keys { wg.Add(1) // 仅需添加1次对应Cache2SqlChan协程
//格式化数据 // wg.Add(1) // 移除多余的Add调用避免计数不平衡
err = s.SaveV2(ctx, key, add, update)
//持久化数据 go func() {
err = s.Cache2Sql(ctx, add, update) defer wg.Done() // Cache2SqlChan协程完成后减1
s.Cache2SqlChan(ctx, addChan, updateChan)
}()
go func() {
scanErr := tools.Redis.RedisScanV2("act:*", func(keys []string) error {
if gtime.Now().After(RunTimeMax) {
return errors.New("Redis扫描超时")
} }
return err for _, key := range keys {
if keyErr := s.SaveV2(ctx, key, addChan, updateChan); keyErr != nil {
g.Log().Errorf(ctx, "处理key %s失败: %v", key, keyErr)
}
}
return nil
}) })
close(addChan)
close(updateChan)
errChan <- scanErr
}()
// 等待扫描和处理完成,同时监听上下文取消
select {
case scanErr := <-errChan:
wg.Wait() // 等待Cache2SqlChan处理完剩余数据
if scanErr != nil {
return gerror.New(fmt.Sprintf("Redis扫描失败: %v", scanErr))
}
case <-ctx.Done():
wg.Wait()
return ctx.Err() // 返回上下文取消原因
}
return return
} }
@@ -320,7 +348,7 @@ func (s *sGameAct) SavesV2() (err error) {
// @param add []*entity.GameAct: 添加数据 // @param add []*entity.GameAct: 添加数据
// @param update []*entity.GameAct: 更新数据 // @param update []*entity.GameAct: 更新数据
// @return err error: 返回错误信息 // @return err error: 返回错误信息
func (s *sGameAct) SaveV2(ctx context.Context, cacheKey string, add, update []*entity.GameAct) (err error) { func (s *sGameAct) SaveV2(ctx context.Context, cacheKey string, addChan, updateChan chan *entity.GameAct) (err error) {
result := strings.Split(cacheKey, ":") result := strings.Split(cacheKey, ":")
actId := gconv.Int(result[1]) actId := gconv.Int(result[1])
@@ -359,20 +387,27 @@ func (s *sGameAct) SaveV2(ctx context.Context, cacheKey string, add, update []*e
g.Log().Errorf(ctx, "当前数据错误: %v", cacheKey) g.Log().Errorf(ctx, "当前数据错误: %v", cacheKey)
return return
} }
//如果没有数据,添加 //如果没有数据,添加
actionData := cacheGet.String() actionData := cacheGet.String()
if data == nil { if data == nil {
add = append(add, &entity.GameAct{ //add = append(add, &entity.GameAct{
// ActId: actId,
// Uid: uid,
// Action: actionData,
//})
addChan <- &entity.GameAct{
ActId: actId, ActId: actId,
Uid: uid, Uid: uid,
Action: actionData, Action: actionData,
}) }
} else { } else {
//覆盖数据 //覆盖数据
data.ActId = actId data.ActId = actId
data.Uid = uid data.Uid = uid
data.Action = actionData data.Action = actionData
update = append(update, data) //update = append(update, data)
updateChan <- data
} }
return return
@@ -385,10 +420,10 @@ func (s *sGameAct) SaveV2(ctx context.Context, cacheKey string, add, update []*e
// @param add []*entity.GameAct: 添加数据 // @param add []*entity.GameAct: 添加数据
// @param update []*entity.GameAct: 更新数据 // @param update []*entity.GameAct: 更新数据
// @return err error: 返回错误信息 // @return err error: 返回错误信息
func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) (err error) { func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) {
//批量写入数据库 //批量写入数据库
updateCount := 0 updateCount := 0
if len(update) > 100 { if len(update) > 0 {
for _, v := range update { for _, v := range update {
v.UpdatedAt = gtime.Now() v.UpdatedAt = gtime.Now()
updateRes, err2 := g.Model(Name).Where(do.GameAct{ updateRes, err2 := g.Model(Name).Where(do.GameAct{
@@ -399,25 +434,20 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct)
g.Log().Error(ctx, err2) g.Log().Error(ctx, err2)
continue continue
} }
if row, _ := updateRes.RowsAffected(); row == 0 { if row, _ := updateRes.RowsAffected(); row == 0 {
g.Log().Error(ctx, "本次更新为0更新数据失败: %v", v) g.Log().Error(ctx, "本次更新为0更新数据失败: %v", v)
continue continue
} }
//删除缓存 //删除缓存
go s.DelCacheKey(ctx, v.ActId, v.Uid) s.DelCacheKey(ctx, v.ActId, v.Uid)
updateCount++ updateCount++
update = make([]*entity.GameAct, 0)
} }
g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount) g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount)
update = make([]*entity.GameAct, 0) update = (update)[:0]
} }
var addCount int64 var addCount int64
if len(add) > 100 { if len(add) > 0 {
for _, v := range add { for _, v := range add {
addRes, err2 := g.Model(Name).Data(v).Insert() addRes, err2 := g.Model(Name).Data(v).Insert()
if err2 != nil { if err2 != nil {
@@ -430,16 +460,95 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct)
} }
addCount++ addCount++
//删除缓存 //删除缓存
go s.DelCacheKey(ctx, v.ActId, v.Uid) s.DelCacheKey(ctx, v.ActId, v.Uid)
} }
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount) g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
add = make([]*entity.GameAct, 0) add = (add)[:0]
} }
return return
} }
// Cache2AddChan 批量添加数据库
func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *entity.GameAct) {
//批量写入数据库计数
var addCount int64
//批量更新数据库计数
var updateCount int64
//通道关闭标志
addClosed := false
updateClosed := false
// 使用链式安全模式
var db = g.Model(Name).Safe()
for {
//检查是否两个通道都已关闭且为空
if addClosed && updateClosed {
break
}
select {
case v, ok := <-addChan:
if !ok {
addClosed = true // 仅标记关闭,不立即日志
continue
}
addRes, err2 := db.Data(v).Insert()
if err2 != nil {
g.Log().Error(ctx, err2)
continue
}
if row, _ := addRes.RowsAffected(); row == 0 {
//g.Log().Error(ctx, "本次新增为0新增数据失败: %v", v)
continue
}
row, _ := addRes.RowsAffected()
addCount += row
//删除缓存
s.DelCacheKey(ctx, v.ActId, v.Uid)
case v, ok := <-updateChan:
if !ok {
updateClosed = true // 仅标记关闭,不立即日志
continue
}
v.UpdatedAt = gtime.Now()
updateRes, err2 := db.Where(do.GameAct{
Uid: v.Uid,
ActId: v.ActId,
}).Data(v).Update()
if err2 != nil {
g.Log().Error(ctx, err2)
continue
}
if row, _ := updateRes.RowsAffected(); row == 0 {
//g.Log().Error(ctx, "本次更新为0更新数据失败: %v", v)
continue
}
//删除缓存
s.DelCacheKey(ctx, v.ActId, v.Uid)
updateCount++
case <-ctx.Done():
g.Log().Debug(ctx, "act协程被上下文取消")
return
}
}
// 仅在所有通道处理完毕后打印最终计数(移除中间冗余日志)
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount)
return
}
// 删除缓存key // 删除缓存key
func (s *sGameAct) DelCacheKey(ctx context.Context, aid int, uid int64) { func (s *sGameAct) DelCacheKey(ctx context.Context, aid int, uid int64) {
//如果有活跃,跳过删除
if getBool, _ := pkg.Cache("redis").
Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool {
return
}
cacheKey := fmt.Sprintf("act:%v:%v", aid, uid) cacheKey := fmt.Sprintf("act:%v:%v", aid, uid)
_, err := g.Redis().Del(ctx, cacheKey) _, err := g.Redis().Del(ctx, cacheKey)
if err != nil { if err != nil {

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -15,6 +14,7 @@ import (
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
) )
var ( var (
@@ -72,19 +72,17 @@ func (s *sGameKv) SavesV1() (err error) {
//uid := v.Int64() //uid := v.Int64()
//cacheKey = "user:kv:" + strconv.FormatInt(uid, 10) //cacheKey = "user:kv:" + strconv.FormatInt(uid, 10)
result := strings.Split(cacheKey, ":") result := strings.Split(cacheKey, ":")
var uid int64 var uid = gconv.Int64(result[2])
uid, err = strconv.ParseInt(result[2], 10, 64) if uid == 0 {
continue
}
//uid, err = strconv.ParseInt(result[2], 10, 64)
if err != nil { if err != nil {
g.Log().Error(ctx, err) g.Log().Error(ctx, err)
g.Redis().Del(ctx, cacheKey) g.Redis().Del(ctx, cacheKey)
continue continue
} }
////如果1天没有活跃跳过
//user, _ := service.MemberUser().Info(uid)
//if user.UpdatedAt.Seconds < gtime.Now().Add(consts.ActSaveTime).Unix() {
// continue
//}
//如果有活跃,跳过持久化 //如果有活跃,跳过持久化
if getBool, _ := pkg.Cache("redis"). if getBool, _ := pkg.Cache("redis").
Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool { Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool {
@@ -94,6 +92,9 @@ func (s *sGameKv) SavesV1() (err error) {
get, _ := g.Redis().Get(ctx, cacheKey) get, _ := g.Redis().Get(ctx, cacheKey)
var data interface{} var data interface{}
get.Scan(&data) get.Scan(&data)
if data == nil {
continue
}
list = append(list, &ListData{ list = append(list, &ListData{
Uid: uid, Uid: uid,
Kv: data, Kv: data,
@@ -103,21 +104,17 @@ func (s *sGameKv) SavesV1() (err error) {
// 将列表数据保存到数据库 // 将列表数据保存到数据库
if len(list) > 100 { if len(list) > 100 {
_, err2 := g.Model("game_kv").Data(list).Save() _, err2 := g.Model("game_kv").Data(list).Save()
if err2 != nil { if err2 != nil {
g.Log().Error(ctx, err2) g.Log().Error(ctx, "当前kv数据入库失败: %v", err2)
err = err2
return return
} }
//删除当前key //删除当前key
for _, v := range list { for _, v := range list {
go s.DelCacheKey(ctx, v.Uid) s.DelCacheKey(ctx, v.Uid)
} }
list = make([]*ListData, 0) list = make([]*ListData, 0)
} }
if err != nil {
g.Log().Error(ctx, "当前kv数据入库失败: %v", err)
}
return return
}) })
@@ -126,6 +123,12 @@ func (s *sGameKv) SavesV1() (err error) {
// 删除缓存key // 删除缓存key
func (s *sGameKv) DelCacheKey(ctx context.Context, uid int64) { func (s *sGameKv) DelCacheKey(ctx context.Context, uid int64) {
//如果有活跃,跳过删除
if getBool, _ := pkg.Cache("redis").
Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool {
return
}
cacheKey := fmt.Sprintf("user:kv:%v", uid) cacheKey := fmt.Sprintf("user:kv:%v", uid)
_, err := g.Redis().Del(ctx, cacheKey) _, err := g.Redis().Del(ctx, cacheKey)
if err != nil { if err != nil {

View File

@@ -268,25 +268,66 @@ func (sdk *SDK) Shutdown() {
const datetimeFmt = time.DateOnly + " " + time.TimeOnly const datetimeFmt = time.DateOnly + " " + time.TimeOnly
// 记录日志 // 记录日志
func (sdk *SDK) Log(uid, event string, property map[string]any, timezone string) { func (sdk *SDK) Log(uid, event string, property map[string]any, timezone string, customEventTime ...time.Time) {
loc := time.Local loc := time.Local
if _loc := getLocationMapValue(timezone); _loc != nil { if _loc := getLocationMapValue(timezone); _loc != nil {
loc = _loc loc = _loc
} }
if len(property) == 0 {
property = map[string]any{"ts": gtime.Now().Timestamp()}
}
var et *gtime.Time
if len(customEventTime) > 0 {
et = gtime.NewFromTime(customEventTime[0])
} else {
et = gtime.Now()
}
log := GameLog{ log := GameLog{
Uid: uid, Uid: uid,
Event: event, Event: event,
Property: property, Property: property,
EventTimems: gtime.Now().TimestampMilli(), EventTimems: et.TimestampMilli(),
EventTimeLoc: gtime.Now().In(loc).Format(datetimeFmt), EventTimeLoc: et.In(loc).Format(datetimeFmt),
} }
// 线程安全 // 线程安全
sdk.bufferChan <- log sdk.bufferChan <- log
} }
// 按服务器时区记录日志 // 按服务器时区记录日志
func (sdk *SDK) LogLtz(uid, event string, property map[string]any) { func (sdk *SDK) LogLtz(uid, event string, property map[string]any, customEventTime ...time.Time) {
sdk.Log(uid, event, property, time.Local.String()) sdk.Log(uid, event, property, time.Local.String(), customEventTime...)
}
// 用户属性初始化
func (sdk *SDK) Uinit(uid string, property map[string]any, timezone string, customEventTime ...time.Time) {
sdk.Log(uid, "u_init", property, timezone, customEventTime...)
}
func (sdk *SDK) UinitLtz(uid string, property map[string]any, customEventTime ...time.Time) {
sdk.Uinit(uid, property, time.Local.String(), customEventTime...)
}
// 用户属性设置
func (sdk *SDK) Uset(uid string, property map[string]any, timezone string, customEventTime ...time.Time) {
sdk.Log(uid, "u_set", property, timezone, customEventTime...)
}
func (sdk *SDK) UsetLtz(uid string, property map[string]any, customEventTime ...time.Time) {
sdk.Uset(uid, property, time.Local.String(), customEventTime...)
}
// 用户属性删除
func (sdk *SDK) Uunset(uid string, property map[string]any, timezone string, customEventTime ...time.Time) {
sdk.Log(uid, "u_unset", property, timezone, customEventTime...)
}
func (sdk *SDK) UunsetLtz(uid string, property map[string]any, customEventTime ...time.Time) {
sdk.Uunset(uid, property, time.Local.String(), customEventTime...)
}
// 用户属性自增/减
func (sdk *SDK) Uinc(uid string, property map[string]any, timezone string, customEventTime ...time.Time) {
sdk.Log(uid, "u_inc", property, timezone, customEventTime...)
}
func (sdk *SDK) UincLtz(uid string, property map[string]any, customEventTime ...time.Time) {
sdk.Uinc(uid, property, time.Local.String(), customEventTime...)
} }
// 这个方法只会在内部协程调用 // 这个方法只会在内部协程调用

View File

@@ -1,5 +1,6 @@
package honor package honor
const ( const (
Host = "https://iap-api.cloud.honor.com" Host = "https://iap-api-drcn.cloud.honor.com"
TokenHost = "https://hnoauth-login-drcn.cloud.honor.com"
) )

View File

@@ -9,19 +9,50 @@ import (
"encoding/base64" "encoding/base64"
"encoding/pem" "encoding/pem"
"errors" "errors"
"time"
"github.com/ayflying/utility_go/package/pay/common" "github.com/ayflying/utility_go/package/pay/common"
"github.com/ayflying/utility_go/pkg"
"github.com/gogf/gf/v2/frame/g"
) )
type Pay struct { type Pay struct {
PubKey string `json:"pubKey"` PubKey string `json:"pubKey"`
AppId string `json:"appId"` AppId string `json:"appId"`
ClientSecret string `json:"client_secret"`
} }
func New(pay *Pay) *Pay { func New(pay *Pay) *Pay {
return &Pay{ return pay
AppId: pay.AppId,
PubKey: pay.PubKey,
} }
func (p *Pay) GetToken(ctx context.Context) (accessToken string, err error) {
type TokenResp struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
}
get, err := pkg.Cache("redis", "cache").GetOrSetFunc(ctx, "pay:honor:Sign:token", func(ctx context.Context) (value interface{}, err error) {
url := TokenHost + "/oauth2/v3/token"
get, err := g.Client().Post(ctx, url, g.Map{
"client_id": p.AppId,
"client_secret": p.ClientSecret,
"grant_type": "client_credentials",
})
//var res *TokenResp
//gjson.DecodeTo(get, &res)
value = get.ReadAllString()
return
}, time.Hour)
var res *TokenResp
err = get.Scan(&res)
accessToken = res.AccessToken
return
} }
// VerifyRSASignature 验证RSA数字签名 // VerifyRSASignature 验证RSA数字签名
@@ -29,13 +60,13 @@ func New(pay *Pay) *Pay {
// sign: 签名的Base64编码字符串 // sign: 签名的Base64编码字符串
// pubKey: PEM格式的公钥字符串 // pubKey: PEM格式的公钥字符串
// 返回验证结果和可能的错误 // 返回验证结果和可能的错误
func (p *Pay) VerifyRSASignature(ctx context.Context, data []byte, sign string) (bool, error) { func (p *Pay) VerifyRSASignature(ctx context.Context, data []byte, signature string) (bool, error) {
//req := g.RequestFromCtx(ctx).Request //req := g.RequestFromCtx(ctx).Request
//post, err := common.ParseNotifyToBodyMap(req) //post, err := common.ParseNotifyToBodyMap(req)
//var data = gjson.MustEncode(post) //var data = gjson.MustEncode(post)
// 解码Base64格式的签名 // 解码Base64格式的签名
signBytes, err := base64.StdEncoding.DecodeString(sign) signBytes, err := base64.StdEncoding.DecodeString(signature)
if err != nil { if err != nil {
return false, errors.New("签名解码失败: " + err.Error()) return false, errors.New("签名解码失败: " + err.Error())
} }

View File

@@ -1,11 +1,40 @@
package honor package honor
import ( import (
"errors"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/util/grand"
"net/http" "net/http"
) )
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
PurchaseProductInfo string `json:"purchaseProductInfo"`
DataSig string `json:"dataSig"`
SigAlgorithm string `json:"sigAlgorithm"`
} `json:"data"`
}
//// 响应结果结构体
//type Response struct {
// Code int `json:"code"` // 结果码 0: 成功,其他: 失败
// Message string `json:"message"` // 错误信息
// Data *DataContent `json:"data"` // 包含购买信息的结构体
//}
//
//// 数据内容结构体对应data字段
//type DataContent struct {
// PurchaseProductInfo string `json:"purchaseProductInfo"` // 消耗结果数据的JSON字符串
// DataSig string `json:"dataSig"` // purchaseProductInfo的签名
// SigAlgorithm string `json:"sigAlgorithm"` // 签名算法,云侧加密算法为"RSA"
//}
func (p *Pay) Notification(r *http.Request) { func (p *Pay) Notification(r *http.Request) {
} }
@@ -13,14 +42,33 @@ func (p *Pay) Notification(r *http.Request) {
// ConsumeProduct 商品消耗 // ConsumeProduct 商品消耗
func (p *Pay) ConsumeProduct(purchaseToken string) (err error) { func (p *Pay) ConsumeProduct(purchaseToken string) (err error) {
url := Host + "/iap/server/consumeProduct" url := Host + "/iap/server/consumeProduct"
_, err = g.Client().ContentJson().Post(gctx.New(), url, g.Map{ //获取token
"purchaseToken": purchaseToken, token, err := p.GetToken(gctx.New())
"developerChallenge": "",
})
if err != nil { if err != nil {
return return
} }
var header = g.MapStrStr{
"access-token": token,
"x-iap-appid": p.AppId,
"purchaseToken": purchaseToken,
}
var params = g.Map{
"purchaseToken": purchaseToken,
"developerChallenge": grand.S(16),
}
get := g.Client().ContentJson().Header(header).PostContent(gctx.New(), url, params)
g.Log().Debugf(gctx.New(), "商品消耗请求发送:url=%v, header=%v, params=%v", url, header, params)
g.Log().Debugf(gctx.New(), "商品消耗请求收到回复: %s", get)
var res *Response
err = gjson.DecodeTo(get, &res)
if err != nil {
return
}
if res.Code != 0 {
g.Log().Error(gctx.New(), "商品消耗失败: "+res.Message)
return errors.New(res.Message)
}
return return
} }

View File

@@ -63,7 +63,7 @@ type (
// @param add []*entity.GameAct: 添加数据 // @param add []*entity.GameAct: 添加数据
// @param update []*entity.GameAct: 更新数据 // @param update []*entity.GameAct: 更新数据
// @return err error: 返回错误信息 // @return err error: 返回错误信息
SaveV2(ctx context.Context, cacheKey string, add []*entity.GameAct, update []*entity.GameAct) (err error) SaveV2(ctx context.Context, cacheKey string, addChan chan *entity.GameAct, updateChan chan *entity.GameAct) (err error)
// Cache2Sql 缓存持久化到数据库 // Cache2Sql 缓存持久化到数据库
// @Description: 缓存持久化到数据库 // @Description: 缓存持久化到数据库
// @receiver s *sGameAct: 游戏活动服务结构体指针 // @receiver s *sGameAct: 游戏活动服务结构体指针
@@ -71,7 +71,9 @@ type (
// @param add []*entity.GameAct: 添加数据 // @param add []*entity.GameAct: 添加数据
// @param update []*entity.GameAct: 更新数据 // @param update []*entity.GameAct: 更新数据
// @return err error: 返回错误信息 // @return err error: 返回错误信息
Cache2Sql(ctx context.Context, add []*entity.GameAct, update []*entity.GameAct) (err error) Cache2Sql(ctx context.Context, add []*entity.GameAct, update []*entity.GameAct)
// Cache2AddChan 批量添加数据库
Cache2SqlChan(ctx context.Context, addChan chan *entity.GameAct, updateChan chan *entity.GameAct)
// 删除缓存key // 删除缓存key
DelCacheKey(ctx context.Context, aid int, uid int64) DelCacheKey(ctx context.Context, aid int, uid int64)
// 清空GetRedDot缓存 // 清空GetRedDot缓存