Commit 900acc73acc49abff83f492fcaa59b5961fb4f86
1 parent
425e2c97
Exists in
master
新增签名等功能
Showing
6 changed files
with
110 additions
and
5 deletions
Show diff stats
... | ... | @@ -0,0 +1,78 @@ |
1 | +package logic | |
2 | + | |
3 | +//2021-04-14添加 | |
4 | +//https://bytedance.feishu.cn/docs/doccnkUkS6ivKQgbeGPxjN3mgNc# | |
5 | + | |
6 | +import ( | |
7 | + "HttpServer/redishandler" | |
8 | + "common/logger" | |
9 | + "common/redis" | |
10 | + "crypto/hmac" | |
11 | + "crypto/sha256" | |
12 | + "encoding/hex" | |
13 | + "encoding/json" | |
14 | + "errors" | |
15 | + "io/ioutil" | |
16 | + "net/http" | |
17 | +) | |
18 | + | |
19 | +// 计算签名算法 | |
20 | +func CreateCommonBytedanceSign(post_body []byte, openid string) string { | |
21 | + sessionkey := GetBytedanceSessionKey(openid) | |
22 | + if sessionkey != "" { | |
23 | + return CreateBytedanceSign(sessionkey, post_body) | |
24 | + } | |
25 | + return "" | |
26 | +} | |
27 | + | |
28 | +// 计算签名算法 | |
29 | +func CreateBytedanceSign(session_key string, post_body []byte) string { | |
30 | + h := hmac.New(sha256.New, []byte(session_key)) | |
31 | + h.Write(post_body) | |
32 | + hashcode2 := hex.EncodeToString(h.Sum(nil)) | |
33 | + return hashcode2 | |
34 | +} | |
35 | + | |
36 | +// 保存sessionkey | |
37 | +func SetBytedanceSessionKey(openid, sessionkey string) { | |
38 | + rediskey := redis.USER_SESSION_KEY + "_" + openid | |
39 | + expire := 86400 * 30 | |
40 | + redishandler.GetRedisClient().SetExString(rediskey, sessionkey, expire) | |
41 | +} | |
42 | + | |
43 | +// 获取sessionkey | |
44 | +func GetBytedanceSessionKey(openid string) string { | |
45 | + rediskey := redis.USER_SESSION_KEY + "_" + openid | |
46 | + val, _ := redishandler.GetRedisClient().GetString(rediskey) | |
47 | + return val | |
48 | +} | |
49 | + | |
50 | +//获取global_id | |
51 | +func GetBytedanceGlobalID(access_token, openid string) (globalid int, err error) { | |
52 | + url := GETGLOBALIDURL + "?access_token=" + access_token + "&open_id=" + openid | |
53 | + res, err := http.Get(url) | |
54 | + if err != nil { | |
55 | + logger.Error("GetBytedanceGlobalID err=%v", err) | |
56 | + return | |
57 | + } | |
58 | + | |
59 | + result, _ := ioutil.ReadAll(res.Body) | |
60 | + defer res.Body.Close() | |
61 | + | |
62 | + var tmp GetGlobalIDResp | |
63 | + err = json.Unmarshal(result, &tmp) | |
64 | + if err != nil { | |
65 | + logger.Error("GetBytedanceGlobalID err=%v", err) | |
66 | + return | |
67 | + } | |
68 | + | |
69 | + if tmp.Errcode != 0 { | |
70 | + logger.Error("GetBytedanceGlobalID err=%v", tmp.Errmsg) | |
71 | + err = errors.New("GetBytedanceGlobalID error:tmp:" + string(result)) | |
72 | + return | |
73 | + } | |
74 | + | |
75 | + globalid = tmp.GlobalID | |
76 | + return | |
77 | + | |
78 | +} | ... | ... |
src/HttpServer/logic/constdef.go
... | ... | @@ -5,10 +5,13 @@ const ( |
5 | 5 | ) |
6 | 6 | |
7 | 7 | const ( |
8 | + GETGLOBALIDURL = "https://developer.toutiao.com/api/apps/gold/gen_global_id" //获取global_id | |
8 | 9 | ADDCOINTOTOUTIAOURL = "https://developer.toutiao.com/api/apps/gold/deliver" |
9 | 10 | GETTOUTIAOACCESSTOKENURL = "https://developer.toutiao.com/api/apps/token" |
10 | 11 | GETTOUTIAOOPENIDURL = "https://developer.toutiao.com/api/apps/jscode2session" |
11 | 12 | GETTOUTIAOCOINURL = "https://developer.toutiao.com/api/apps/gold/search" |
12 | - TOUTIAOAPPID = "tt841ff2a21b254572" //"tt29e35f5eb6c9b9dd" | |
13 | - TOUTIAOSECRET = "14a09009e4ea999261876a4b26b09ec2ebc1e7ee" //"100cb2286445027f606844fc156121dd7db06075" | |
13 | + //TOUTIAOAPPID = "tt841ff2a21b254572" //"tt29e35f5eb6c9b9dd 正式tt841ff2a21b254572" | |
14 | + //TOUTIAOSECRET = "14a09009e4ea999261876a4b26b09ec2ebc1e7ee" //"100cb2286445027f606844fc156121dd7db06075 正式14a09009e4ea999261876a4b26b09ec2ebc1e7ee" | |
15 | + TOUTIAOAPPID = "tted769bcb724e5b1201" | |
16 | + TOUTIAOSECRET = "daa13cc0d0e27960438bb2071c950d59ab80ab14" | |
14 | 17 | ) | ... | ... |
src/HttpServer/logic/datadef.go
... | ... | @@ -7,6 +7,7 @@ type AddcointotoutiaoReq struct { |
7 | 7 | Amount int `json:"amount"` |
8 | 8 | Description string `json:"description"` |
9 | 9 | Bonus_type string `json:"bonus_type"` |
10 | + GlobalID int `json:"global_id"` | |
10 | 11 | } |
11 | 12 | |
12 | 13 | type AddcointotoutiaoResp struct { |
... | ... | @@ -29,6 +30,12 @@ type GetOpenidResp struct { |
29 | 30 | Errmsg string `json:"errmsg"` |
30 | 31 | } |
31 | 32 | |
33 | +type GetGlobalIDResp struct { | |
34 | + GlobalID int `json:"global_id"` | |
35 | + Errcode int `json:"errcode"` | |
36 | + Errmsg string `json:"errmsg"` | |
37 | +} | |
38 | + | |
32 | 39 | //-------------------------------------------------------------------------- |
33 | 40 | |
34 | 41 | type GetcurpropertyReq struct { | ... | ... |
src/HttpServer/logic/function.go
... | ... | @@ -188,6 +188,9 @@ func GetOpenid(code string) (string, error) { |
188 | 188 | return "", errors.New(tmp.Errmsg) |
189 | 189 | } |
190 | 190 | |
191 | + //保存头条session key | |
192 | + go SetBytedanceSessionKey(tmp.Openid, tmp.Session_key) | |
193 | + | |
191 | 194 | return tmp.Openid, nil |
192 | 195 | } |
193 | 196 | |
... | ... | @@ -273,6 +276,14 @@ func AddCoinToTouTiao(openid string, deviceid, amount int, descr string, btype s |
273 | 276 | logger.Error("AddCoinToTouTiao err=%v", err) |
274 | 277 | return 0, 0, err |
275 | 278 | } |
279 | + | |
280 | + //获取 GlobalID | |
281 | + globalid, err := GetBytedanceGlobalID(acctoken, openid) | |
282 | + if err != nil { | |
283 | + logger.Error("AddCoinToTouTiao err=%v", err) | |
284 | + return 0, 0, err | |
285 | + } | |
286 | + | |
276 | 287 | var reqdata AddcointotoutiaoReq |
277 | 288 | reqdata.Access_token = acctoken |
278 | 289 | reqdata.Amount = amount |
... | ... | @@ -280,6 +291,7 @@ func AddCoinToTouTiao(openid string, deviceid, amount int, descr string, btype s |
280 | 291 | reqdata.Description = descr |
281 | 292 | reqdata.Device_id = deviceid |
282 | 293 | reqdata.Open_id = openid |
294 | + reqdata.GlobalID = globalid | |
283 | 295 | logger.Info("AddCoinToTouTiao req=%v", reqdata) |
284 | 296 | bys, err := json.Marshal(&reqdata) |
285 | 297 | if err != nil { |
... | ... | @@ -287,11 +299,17 @@ func AddCoinToTouTiao(openid string, deviceid, amount int, descr string, btype s |
287 | 299 | return 0, 0, err |
288 | 300 | } |
289 | 301 | |
290 | - res, err := DoHttpPost(bys, ADDCOINTOTOUTIAOURL) | |
302 | + //计算签名 | |
303 | + signbody, _ := json.Marshal(reqdata) | |
304 | + signature := CreateCommonBytedanceSign(signbody, openid) | |
305 | + apiurl := ADDCOINTOTOUTIAOURL + "?signature=" + signature | |
306 | + | |
307 | + res, err := DoHttpPost(bys, apiurl) | |
291 | 308 | if err != nil { |
292 | 309 | logger.Error("AddCoinToTouTiao failed=%v", err) |
293 | 310 | return 0, 0, err |
294 | 311 | } |
312 | + | |
295 | 313 | var resp AddcointotoutiaoResp |
296 | 314 | err = json.Unmarshal([]byte(res), &resp) |
297 | 315 | if err != nil { | ... | ... |
src/common/redis/def.go
src/mysql/dbmysql.go
... | ... | @@ -145,8 +145,6 @@ func GetFromBackUp(openid string) (err error, Openid string, LastGetTime, LeftCn |
145 | 145 | defer rows.Close() |
146 | 146 | } |
147 | 147 | |
148 | - logger.Error("cmd", cmd) | |
149 | - | |
150 | 148 | if err != nil { |
151 | 149 | logger.Notic("GetFromBackUp mysql select error:%v", err) |
152 | 150 | return | ... | ... |