Commit 4596c516c28a505b37a372d9dd126ee914f0bae0
1 parent
94b2940a
Exists in
master
提交接口
Showing
7 changed files
with
272 additions
and
7 deletions
Show diff stats
src/HttpServer/logic/constdef.go
... | ... | @@ -3,3 +3,11 @@ package logic |
3 | 3 | const ( |
4 | 4 | DAILY_FETCH_CNT = 5 //每日领取宝箱次数 |
5 | 5 | ) |
6 | + | |
7 | +const ( | |
8 | + ADDCOINTOTOUTIAOURL = "https://developer.toutiao.com/api/apps/gold/deliver" | |
9 | + GETTOUTIAOACCESSTOKENURL = "https://developer.toutiao.com/api/apps/token" | |
10 | + GETTOUTIAOCOINURL = "https://developer.toutiao.com/api/apps/gold/search" | |
11 | + TOUTIAOAPPID = "tt29e35f5eb6c9b9dd" | |
12 | + TOUTIAOSECRET = "100cb2286445027f606844fc156121dd7db06075" | |
13 | +) | ... | ... |
src/HttpServer/logic/datadef.go
1 | 1 | package logic |
2 | 2 | |
3 | -// | |
3 | +type AddcointotoutiaoReq struct { | |
4 | + Access_token string `json:"access_token"` | |
5 | + Open_id string `json:"open_id"` | |
6 | + Device_id int `json:"device_id"` | |
7 | + Amount int `json:"amount"` | |
8 | + Description string `json:"description"` | |
9 | + Bonus_type string `json:"bonus_type"` | |
10 | +} | |
11 | + | |
12 | +type AddcointotoutiaoResp struct { | |
13 | + Errcode int `json:"errcode"` | |
14 | + Errmsg string `json:"errmsg"` | |
15 | + Data int `json:"data"` | |
16 | +} | |
17 | + | |
18 | +type GetAccessTokenResp struct { | |
19 | + Access_token string `json:"access_token"` | |
20 | + Expires_in string `json:"expires_in"` | |
21 | + Errcode int `json:"errcode"` | |
22 | + Errmsg string `json:"errmsg"` | |
23 | +} | |
24 | + | |
25 | +//-------------------------------------------------------------------------- | |
4 | 26 | |
5 | 27 | type GetcurpropertyReq struct { |
6 | 28 | Openid string `json:"openid"` |
... | ... | @@ -19,6 +41,24 @@ type GetcurpropertyResp struct { |
19 | 41 | RetData GetcurpropertyData `json:"retData"` |
20 | 42 | } |
21 | 43 | |
44 | +type FetchproteryboxReq struct { | |
45 | + Openid string `json:"openid"` | |
46 | + Device_id int `json:"device_id"` | |
47 | +} | |
48 | + | |
49 | +type FetchproteryboxData struct { | |
50 | + Goldnum int `json:"goldnum"` | |
51 | + Sumgold int `json:"sumgold"` | |
52 | + Curlevle int64 `json:"curlevle"` | |
53 | + Leftcnt int `json:"leftcnt"` | |
54 | +} | |
55 | + | |
56 | +type FetchproteryboxResp struct { | |
57 | + ErrNum int `json:"errNum"` | |
58 | + RetMsg string `json:"retMsg"` | |
59 | + RetData FetchproteryboxData `json:"retData"` | |
60 | +} | |
61 | + | |
22 | 62 | //------------------------------------------------------------------------------------------------------ |
23 | 63 | |
24 | 64 | type UserData struct { | ... | ... |
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服务器错误 | |
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 //头条接口失败 | |
7 | 11 | |
8 | 12 | ) | ... | ... |
src/HttpServer/logic/function.go
... | ... | @@ -3,10 +3,14 @@ package logic |
3 | 3 | import ( |
4 | 4 | "HttpServer/jsonconf" |
5 | 5 | "HttpServer/redishandler" |
6 | + "bytes" | |
6 | 7 | "common/logger" |
7 | 8 | "common/redis" |
8 | 9 | "encoding/json" |
10 | + "errors" | |
11 | + "io/ioutil" | |
9 | 12 | "net/http" |
13 | + "strconv" | |
10 | 14 | "time" |
11 | 15 | ) |
12 | 16 | |
... | ... | @@ -139,3 +143,124 @@ func (u *UserData) CalcProperty() { |
139 | 143 | logger.Error("CalcProperty err=%v", err) |
140 | 144 | } |
141 | 145 | } |
146 | + | |
147 | +func GetAccessToken() (string, error) { | |
148 | + //首先从缓存取 | |
149 | + var token string | |
150 | + token, err := redishandler.GetRedisClient().GetString(redis.GAME_ACCESS_TOKEN) | |
151 | + if err == nil { | |
152 | + return token, nil | |
153 | + } | |
154 | + | |
155 | + //如果没找到 则取取 | |
156 | + url := GETTOUTIAOACCESSTOKENURL + "?appid=" + TOUTIAOAPPID + "&secret=" + TOUTIAOSECRET + "&grant_type=client_credential" | |
157 | + res, err := http.Get(url) | |
158 | + if err != nil { | |
159 | + logger.Error("GetAccessToken err=%v", err) | |
160 | + return "", err | |
161 | + } | |
162 | + result, _ := ioutil.ReadAll(res.Body) | |
163 | + defer res.Body.Close() | |
164 | + | |
165 | + var tmp GetAccessTokenResp | |
166 | + err = json.Unmarshal([]byte(result), &tmp) | |
167 | + if err != nil { | |
168 | + logger.Error("GetAccessToken err=%v", err) | |
169 | + return "", err | |
170 | + } | |
171 | + | |
172 | + if tmp.Access_token == "" { | |
173 | + logger.Error("GetAccessToken err=%v", tmp.Errmsg) | |
174 | + return "", errors.New(tmp.Errmsg) | |
175 | + } | |
176 | + | |
177 | + //保存token缓存 | |
178 | + expiretime, _ := strconv.Atoi(tmp.Expires_in) | |
179 | + redishandler.GetRedisClient().SetString(redis.GAME_ACCESS_TOKEN, tmp.Access_token) | |
180 | + redishandler.GetRedisClient().Expire(redis.GAME_ACCESS_TOKEN, expiretime-10) | |
181 | + | |
182 | + return tmp.Access_token, nil | |
183 | +} | |
184 | + | |
185 | +func DoHttpPost(bys []byte, url string) (string, error) { | |
186 | + body := bytes.NewBuffer(bys) | |
187 | + | |
188 | + res, err := http.Post(url, "application/json;charset=utf-8", body) | |
189 | + if err != nil { | |
190 | + logger.Error("DoHttpPost failed err=%v", err) | |
191 | + return "", err | |
192 | + } | |
193 | + result, _ := ioutil.ReadAll(res.Body) | |
194 | + defer res.Body.Close() | |
195 | + | |
196 | + return string(result), nil | |
197 | +} | |
198 | + | |
199 | +func GetCoinFromToutiao(openid string, deviceid int) (int, error) { | |
200 | + accesstoken, _ := GetAccessToken() | |
201 | + url := GETTOUTIAOCOINURL + "?access_token=" + accesstoken + "&open_id=" + openid + "&device_id=" + strconv.Itoa(deviceid) | |
202 | + res, err := http.Get(url) | |
203 | + if err != nil { | |
204 | + logger.Error("GetAccessToken err=%v", err) | |
205 | + return 0, err | |
206 | + } | |
207 | + result, _ := ioutil.ReadAll(res.Body) | |
208 | + defer res.Body.Close() | |
209 | + | |
210 | + var tmp AddcointotoutiaoResp | |
211 | + err = json.Unmarshal(result, &tmp) | |
212 | + if err != nil { | |
213 | + logger.Error("GetAccessToken err=%v", err) | |
214 | + return 0, err | |
215 | + } | |
216 | + if tmp.Errcode != 0 { | |
217 | + logger.Error("GetAccessToken err=%v", tmp.Errmsg) | |
218 | + return 0, errors.New(tmp.Errmsg) | |
219 | + } | |
220 | + return tmp.Data, nil | |
221 | +} | |
222 | + | |
223 | +func AddCoinToTouTiao(openid string, deviceid, amount int, descr string, btype string) (int, int, error) { | |
224 | + acctoken, err := GetAccessToken() | |
225 | + if err != nil { | |
226 | + logger.Error("AddCoinToTouTiao err=%v", err) | |
227 | + return 0, 0, err | |
228 | + } | |
229 | + var reqdata AddcointotoutiaoReq | |
230 | + reqdata.Access_token = acctoken | |
231 | + reqdata.Amount = amount | |
232 | + reqdata.Bonus_type = btype | |
233 | + reqdata.Description = descr | |
234 | + reqdata.Device_id = deviceid | |
235 | + reqdata.Open_id = openid | |
236 | + logger.Info("AddCoinToTouTiao req=%v", reqdata) | |
237 | + bys, err := json.Marshal(&reqdata) | |
238 | + if err != nil { | |
239 | + logger.Error("AddCoinToTouTiao failed=%v", err) | |
240 | + return 0, 0, err | |
241 | + } | |
242 | + | |
243 | + res, err := DoHttpPost(bys, ADDCOINTOTOUTIAOURL) | |
244 | + if err != nil { | |
245 | + logger.Error("AddCoinToTouTiao failed=%v", err) | |
246 | + return 0, 0, err | |
247 | + } | |
248 | + var resp AddcointotoutiaoResp | |
249 | + err = json.Unmarshal([]byte(res), &resp) | |
250 | + if err != nil { | |
251 | + logger.Error("AddCoinToTouTiao failed=%v", err) | |
252 | + return 0, 0, err | |
253 | + } | |
254 | + | |
255 | + if resp.Errcode != 0 { | |
256 | + logger.Error("AddCoinToTouTiao failed=%v", err) | |
257 | + return 0, 0, errors.New(resp.Errmsg) | |
258 | + } | |
259 | + | |
260 | + newcoin, err := GetCoinFromToutiao(openid, deviceid) | |
261 | + if err != nil { | |
262 | + logger.Error("AddCoinToTouTiao failed=%v", err) | |
263 | + return 0, 0, err | |
264 | + } | |
265 | + return resp.Data, newcoin, nil | |
266 | +} | ... | ... |
src/HttpServer/logic/httpserver.go
... | ... | @@ -43,11 +43,22 @@ func startServerHttpServe() { |
43 | 43 | http.HandleFunc("/eliminatestar/test", Testapi) //测试接口 |
44 | 44 | http.HandleFunc("/eliminatestar/clear", ClearData) //清除账号 |
45 | 45 | //---------------------------------------------------------------------------------------- |
46 | - http.HandleFunc("/daycs/getcurproperty", Getcurproperty) //请求当前物资 | |
46 | + http.HandleFunc("/daycs/getcurproperty", Getcurproperty) //请求当前物资 | |
47 | + http.HandleFunc("/daycs/fetchproterybox", Fetchproterybox) //请求领取物资宝箱 | |
47 | 48 | err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) |
48 | 49 | CheckErr(err) |
49 | 50 | } |
50 | 51 | |
52 | +func Fetchproterybox(w http.ResponseWriter, r *http.Request) { | |
53 | + result, _ := ioutil.ReadAll(r.Body) | |
54 | + r.Body.Close() | |
55 | + | |
56 | + s := string(result) | |
57 | + logger.Info("Fetchproterybox , body:%v", s) | |
58 | + | |
59 | + HandlerFetchproterybox(w, s) | |
60 | +} | |
61 | + | |
51 | 62 | func ClearData(w http.ResponseWriter, r *http.Request) { |
52 | 63 | |
53 | 64 | Uuid := 0 | ... | ... |
src/HttpServer/logic/logic.go
1 | 1 | package logic |
2 | 2 | |
3 | 3 | import ( |
4 | + "HttpServer/jsonconf" | |
4 | 5 | "common/logger" |
5 | 6 | "encoding/json" |
6 | 7 | "fmt" |
7 | 8 | "net/http" |
8 | 9 | ) |
9 | 10 | |
11 | +func HandlerFetchproterybox(w http.ResponseWriter, data string) { | |
12 | + SetHeader(w) | |
13 | + var resp FetchproteryboxResp | |
14 | + resp.ErrNum = 0 | |
15 | + var rdata FetchproteryboxReq | |
16 | + err := json.Unmarshal([]byte(data), &rdata) | |
17 | + for { | |
18 | + if err != nil { | |
19 | + logger.Info("json decode HandlerFetchproterybox data failed:%v,for:%v", err, data) | |
20 | + resp.RetMsg = "json解析失败" | |
21 | + resp.ErrNum = ERROR_UNMASH_JSONFAILED | |
22 | + break | |
23 | + } | |
24 | + | |
25 | + uinfo, err := GetUserInfo(rdata.Openid) | |
26 | + if err != nil || uinfo == nil { | |
27 | + logger.Info(" HandlerFetchproterybox getdata failed:%v,for:%v", err, data) | |
28 | + resp.RetMsg = "服务器读取数据失败" | |
29 | + resp.ErrNum = ERROR_SERVER_FAILED | |
30 | + break | |
31 | + } | |
32 | + | |
33 | + //判断当前剩余次数 | |
34 | + if uinfo.LeftCnt == 0 { | |
35 | + logger.Info(" HandlerFetchproterybox LeftCnt failed:%v,for:%v", err, data) | |
36 | + resp.RetMsg = "当天领取已达上限" | |
37 | + resp.ErrNum = ERROR_FETCHLIMIT | |
38 | + break | |
39 | + } | |
40 | + | |
41 | + //判断物资是否足够 | |
42 | + cfg := jsonconf.GetGoldChestConfig(uinfo.TotalFetchCnt + 1) | |
43 | + if cfg == nil { | |
44 | + logger.Info(" HandlerFetchproterybox cfg failed:%v,for:%v", err, data) | |
45 | + resp.RetMsg = "读取配置错误" | |
46 | + resp.ErrNum = ERROR_READCFG_FAILED | |
47 | + break | |
48 | + } | |
49 | + if uinfo.Property < cfg.Score { | |
50 | + logger.Info(" HandlerFetchproterybox property failed:%v,for:%v", err, data) | |
51 | + resp.RetMsg = "当前物资不足" | |
52 | + resp.ErrNum = ERROR_PROPERTY_NOTENOUGH | |
53 | + break | |
54 | + } | |
55 | + | |
56 | + //加金币 | |
57 | + addgold, sumgold, err := AddCoinToTouTiao(rdata.Openid, rdata.Device_id, cfg.Reward, "每日领取宝箱奖励", "other") | |
58 | + if err != nil { | |
59 | + logger.Info(" HandlerFetchproterybox property failed:%v,for:%v", err, data) | |
60 | + resp.RetMsg = "调用头条接口失败" | |
61 | + resp.ErrNum = ERROR_TOUTIAOAPI_FAILED | |
62 | + break | |
63 | + } | |
64 | + | |
65 | + uinfo.TotalFetchCnt++ | |
66 | + uinfo.Property -= cfg.Score | |
67 | + uinfo.LeftCnt-- | |
68 | + | |
69 | + resp.RetData.Goldnum = addgold | |
70 | + resp.RetData.Sumgold = sumgold | |
71 | + resp.RetData.Curlevle = uinfo.CalcCurLe() | |
72 | + resp.RetData.Leftcnt = uinfo.LeftCnt | |
73 | + | |
74 | + //保存 | |
75 | + err = SaveUserInfo(uinfo) | |
76 | + if err != nil { | |
77 | + logger.Error("HandlerFetchproterybox err=%v", err) | |
78 | + } | |
79 | + | |
80 | + break | |
81 | + } | |
82 | + //回包 | |
83 | + respstr, _ := json.Marshal(&resp) | |
84 | + fmt.Fprint(w, string(respstr)) | |
85 | +} | |
86 | + | |
10 | 87 | func HandlerGetcurproperty(w http.ResponseWriter, data string) { |
11 | 88 | SetHeader(w) |
12 | 89 | var resp GetcurpropertyResp | ... | ... |