From b54d20a9617eea804741c97033c8bc61287954bb Mon Sep 17 00:00:00 2001 From: 李旭 Date: Fri, 4 Jun 2021 10:18:34 +0800 Subject: [PATCH] 新增发放金币 并发处理 --- src/HttpServer/logic/bytedance.go | 39 +++++++++++++++++++++++++++++++++++++++ src/HttpServer/logic/errordef.go | 20 ++++++++++---------- src/HttpServer/logic/logic.go | 12 +++++++++++- src/common/redis/redis.go | 11 +++++++++++ 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/HttpServer/logic/bytedance.go b/src/HttpServer/logic/bytedance.go index 20da235..0318c44 100644 --- a/src/HttpServer/logic/bytedance.go +++ b/src/HttpServer/logic/bytedance.go @@ -12,8 +12,11 @@ import ( "encoding/hex" "encoding/json" "errors" + "fmt" "io/ioutil" "net/http" + "strconv" + "time" ) // 计算签名算法 @@ -86,3 +89,39 @@ func GetBytedanceGlobalID(access_token, openid string) (globalid int64, err erro return } + +//并发次数 +func GetConcurrency(key, openid string) int { + rediskey := key + "_" + openid + defaultval := fmt.Sprintf("%d", DAILY_FETCH_CNT) + b, _ := redishandler.GetRedisClient().Exists(rediskey) + if b == true { + defaultval, _ = redishandler.GetRedisClient().GetString(rediskey) + } else { + redishandler.GetRedisClient().SetExString(rediskey, defaultval, 86400) + } + + intval, _ := strconv.Atoi(defaultval) + + + return intval +} + +//并发次数 +func Decrcurrency(key, openid string) int { + rediskey := key + "_" + openid + b, _ := redishandler.GetRedisClient().Exists(rediskey) + intval := 0 + if b == true { + defaultval, _ := redishandler.GetRedisClient().Decr(rediskey) + intval, _ = strconv.Atoi(defaultval) + } + + return intval +} + +//当前日期字符串 +func DateNowStr() string { + tim := time.Unix(time.Now().Unix(), 0).Format("060102") + return fmt.Sprintf("%s", tim) +} diff --git a/src/HttpServer/logic/errordef.go b/src/HttpServer/logic/errordef.go index c18a062..4e30cad 100644 --- a/src/HttpServer/logic/errordef.go +++ b/src/HttpServer/logic/errordef.go @@ -1,15 +1,15 @@ package logic const ( - ERROR_OK = 0 //没有错误 - ERROR_UNMASH_JSONFAILED = 1 //json解析失败 - ERROR_SERVER_FAILED = 2 //j服务器错误 - ERROR_FETCHLIMIT = 3 //当天领取次数达到上限 - ERROR_READCFG_FAILED = 4 //读取配置错误 - ERROR_PROPERTY_NOTENOUGH = 5 //当前物资不足 - ERROR_TOUTIAOAPI_FAILED = 6 //头条接口失败 - ERROR_PARAM_INVALID = 7 //等级参数无效 - ERROR_GOLD_LIMIT = 8 //当日合成获得金币已达上限 - ERROR_ALREADY_SIGNED = 9 //当日已签到 + ERROR_OK = 0 //没有错误 + ERROR_UNMASH_JSONFAILED = 1 //json解析失败 + ERROR_SERVER_FAILED = 2 //j服务器错误 + ERROR_FETCHLIMIT = 3 //当天领取次数达到上限 + ERROR_READCFG_FAILED = 4 //读取配置错误 + ERROR_PROPERTY_NOTENOUGH = 5 //当前物资不足 + ERROR_TOUTIAOAPI_FAILED = 6 //头条接口失败 + ERROR_PARAM_INVALID = 7 //等级参数无效 + ERROR_GOLD_LIMIT = 8 //当日合成获得金币已达上限 + ERROR_ALREADY_SIGNED = 9 //当日已签到 ) diff --git a/src/HttpServer/logic/logic.go b/src/HttpServer/logic/logic.go index 7f63ca1..1927c52 100644 --- a/src/HttpServer/logic/logic.go +++ b/src/HttpServer/logic/logic.go @@ -432,6 +432,15 @@ func HandlerFetchproterybox(w http.ResponseWriter, data string) { break } + currkey := "leftcnt_" + DateNowStr() + newleftcnt := GetConcurrency(currkey, rdata.Openid) + if newleftcnt < 1 { + logger.Info(" HandlerFetchproterybox GetConcurrency LeftCnt failed:%v,for:%v", err, data) + resp.RetMsg = "当天领取已达上限" + resp.ErrNum = ERROR_FETCHLIMIT + break + } + //判断当前剩余次数 if uinfo.LeftCnt == 0 { logger.Info(" HandlerFetchproterybox LeftCnt failed:%v,for:%v", err, data) @@ -472,7 +481,8 @@ func HandlerFetchproterybox(w http.ResponseWriter, data string) { resp.RetData.Goldnum = addgold resp.RetData.Sumgold = sumgold resp.RetData.Curlevle = uinfo.CalcCurLe() - resp.RetData.Leftcnt = uinfo.LeftCnt + //resp.RetData.Leftcnt = uinfo.LeftCnt + resp.RetData.Leftcnt = Decrcurrency(currkey, rdata.Openid) resp.RetData.Curpoerty = uinfo.Property //保存 diff --git a/src/common/redis/redis.go b/src/common/redis/redis.go index 5defd5e..5f61eeb 100644 --- a/src/common/redis/redis.go +++ b/src/common/redis/redis.go @@ -406,3 +406,14 @@ func (client *RedisClient) HMGets(key string) ([]interface{}, error) { } return v, err } + +// 递减 +func (client *RedisClient) Decr(key string) (string, error) { + conn := client.m_pool.Get() + defer conn.Close() + value, err := redis.String(conn.Do("DECR", key)) + if err != nil { + //fmt.Println("redis set failed:", err) + } + return value, err +} -- libgit2 0.21.0