Commit b54d20a9617eea804741c97033c8bc61287954bb

Authored by 李旭
1 parent cacc58db
Exists in master

新增发放金币 并发处理

src/HttpServer/logic/bytedance.go
... ... @@ -12,8 +12,11 @@ import (
12 12 "encoding/hex"
13 13 "encoding/json"
14 14 "errors"
  15 + "fmt"
15 16 "io/ioutil"
16 17 "net/http"
  18 + "strconv"
  19 + "time"
17 20 )
18 21  
19 22 // 计算签名算法
... ... @@ -86,3 +89,39 @@ func GetBytedanceGlobalID(access_token, openid string) (globalid int64, err erro
86 89 return
87 90  
88 91 }
  92 +
  93 +//并发次数
  94 +func GetConcurrency(key, openid string) int {
  95 + rediskey := key + "_" + openid
  96 + defaultval := fmt.Sprintf("%d", DAILY_FETCH_CNT)
  97 + b, _ := redishandler.GetRedisClient().Exists(rediskey)
  98 + if b == true {
  99 + defaultval, _ = redishandler.GetRedisClient().GetString(rediskey)
  100 + } else {
  101 + redishandler.GetRedisClient().SetExString(rediskey, defaultval, 86400)
  102 + }
  103 +
  104 + intval, _ := strconv.Atoi(defaultval)
  105 +
  106 +
  107 + return intval
  108 +}
  109 +
  110 +//并发次数
  111 +func Decrcurrency(key, openid string) int {
  112 + rediskey := key + "_" + openid
  113 + b, _ := redishandler.GetRedisClient().Exists(rediskey)
  114 + intval := 0
  115 + if b == true {
  116 + defaultval, _ := redishandler.GetRedisClient().Decr(rediskey)
  117 + intval, _ = strconv.Atoi(defaultval)
  118 + }
  119 +
  120 + return intval
  121 +}
  122 +
  123 +//当前日期字符串
  124 +func DateNowStr() string {
  125 + tim := time.Unix(time.Now().Unix(), 0).Format("060102")
  126 + return fmt.Sprintf("%s", tim)
  127 +}
... ...
src/HttpServer/logic/errordef.go
1 1 package logic
2 2  
3 3 const (
4   - ERROR_OK = 0 //没有错误
5   - ERROR_UNMASH_JSONFAILED = 1 //json解析失败
6   - ERROR_SERVER_FAILED = 2 //j服务器错误
7   - ERROR_FETCHLIMIT = 3 //当天领取次数达到上限
8   - ERROR_READCFG_FAILED = 4 //读取配置错误
9   - ERROR_PROPERTY_NOTENOUGH = 5 //当前物资不足
10   - ERROR_TOUTIAOAPI_FAILED = 6 //头条接口失败
11   - ERROR_PARAM_INVALID = 7 //等级参数无效
12   - ERROR_GOLD_LIMIT = 8 //当日合成获得金币已达上限
13   - ERROR_ALREADY_SIGNED = 9 //当日已签到
  4 + ERROR_OK = 0 //没有错误
  5 + ERROR_UNMASH_JSONFAILED = 1 //json解析失败
  6 + ERROR_SERVER_FAILED = 2 //j服务器错误
  7 + ERROR_FETCHLIMIT = 3 //当天领取次数达到上限
  8 + ERROR_READCFG_FAILED = 4 //读取配置错误
  9 + ERROR_PROPERTY_NOTENOUGH = 5 //当前物资不足
  10 + ERROR_TOUTIAOAPI_FAILED = 6 //头条接口失败
  11 + ERROR_PARAM_INVALID = 7 //等级参数无效
  12 + ERROR_GOLD_LIMIT = 8 //当日合成获得金币已达上限
  13 + ERROR_ALREADY_SIGNED = 9 //当日已签到
14 14  
15 15 )
... ...
src/HttpServer/logic/logic.go
... ... @@ -432,6 +432,15 @@ func HandlerFetchproterybox(w http.ResponseWriter, data string) {
432 432 break
433 433 }
434 434  
  435 + currkey := "leftcnt_" + DateNowStr()
  436 + newleftcnt := GetConcurrency(currkey, rdata.Openid)
  437 + if newleftcnt < 1 {
  438 + logger.Info(" HandlerFetchproterybox GetConcurrency LeftCnt failed:%v,for:%v", err, data)
  439 + resp.RetMsg = "当天领取已达上限"
  440 + resp.ErrNum = ERROR_FETCHLIMIT
  441 + break
  442 + }
  443 +
435 444 //判断当前剩余次数
436 445 if uinfo.LeftCnt == 0 {
437 446 logger.Info(" HandlerFetchproterybox LeftCnt failed:%v,for:%v", err, data)
... ... @@ -472,7 +481,8 @@ func HandlerFetchproterybox(w http.ResponseWriter, data string) {
472 481 resp.RetData.Goldnum = addgold
473 482 resp.RetData.Sumgold = sumgold
474 483 resp.RetData.Curlevle = uinfo.CalcCurLe()
475   - resp.RetData.Leftcnt = uinfo.LeftCnt
  484 + //resp.RetData.Leftcnt = uinfo.LeftCnt
  485 + resp.RetData.Leftcnt = Decrcurrency(currkey, rdata.Openid)
476 486 resp.RetData.Curpoerty = uinfo.Property
477 487  
478 488 //保存
... ...
src/common/redis/redis.go
... ... @@ -406,3 +406,14 @@ func (client *RedisClient) HMGets(key string) ([]interface{}, error) {
406 406 }
407 407 return v, err
408 408 }
  409 +
  410 +// 递减
  411 +func (client *RedisClient) Decr(key string) (string, error) {
  412 + conn := client.m_pool.Get()
  413 + defer conn.Close()
  414 + value, err := redis.String(conn.Do("DECR", key))
  415 + if err != nil {
  416 + //fmt.Println("redis set failed:", err)
  417 + }
  418 + return value, err
  419 +}
... ...