bytedance.go
2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package logic
//2021-04-14添加
//https://bytedance.feishu.cn/docs/doccnkUkS6ivKQgbeGPxjN3mgNc#
import (
"HttpServer/redishandler"
"common/logger"
"common/redis"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
// 计算签名算法
func CreateCommonBytedanceSign(post_body []byte, openid string) string {
sessionkey := GetBytedanceSessionKey(openid)
if sessionkey != "" {
return CreateBytedanceSign(sessionkey, post_body)
}
return ""
}
// 计算签名算法
func CreateBytedanceSign(session_key string, post_body []byte) string {
h := hmac.New(sha256.New, []byte(session_key))
h.Write(post_body)
hashcode2 := hex.EncodeToString(h.Sum(nil))
//logtest
//log.Println("CreateBytedanceSign::,session_key:", session_key, ",post_body:", string(post_body), ",hashcode2:", hashcode2)
//logtest、
return hashcode2
}
// 保存sessionkey
func SetBytedanceSessionKey(openid, sessionkey string) {
rediskey := redis.USER_SESSION_KEY + "_" + openid
expire := 86400 * 30
redishandler.GetRedisClient().SetExString(rediskey, sessionkey, expire)
}
// 获取sessionkey
func GetBytedanceSessionKey(openid string) string {
rediskey := redis.USER_SESSION_KEY + "_" + openid
val, _ := redishandler.GetRedisClient().GetString(rediskey)
return val
}
//获取global_id
func GetBytedanceGlobalID(access_token, openid string) (globalid int64, err error) {
url := GETGLOBALIDURL + "?access_token=" + access_token + "&open_id=" + openid
res, err := http.Get(url)
if err != nil {
logger.Error("GetBytedanceGlobalID err=%v", err)
return
}
result, _ := ioutil.ReadAll(res.Body)
defer res.Body.Close()
var tmp GetGlobalIDResp
err = json.Unmarshal(result, &tmp)
if err != nil {
logger.Error("GetBytedanceGlobalID err=%v", err)
return
}
if tmp.Errcode != 0 {
logger.Error("GetBytedanceGlobalID err=%v", tmp.Errmsg)
err = errors.New("GetBytedanceGlobalID error:tmp:" + string(result))
return
}
//log.Println("GetBytedanceGlobalID::", string(result))
globalid = tmp.GlobalID
//log.Println("globalid::", globalid)
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)
}