Commit 3afa305d97e668bda2497c40f9471821171bc591
1 parent
2d01fcec
Exists in
master
提交邀请功能的接口
Showing
6 changed files
with
128 additions
and
38 deletions
Show diff stats
src/HttpServer/logic/datadef.go
... | ... | @@ -115,6 +115,17 @@ type FetchInviteResp struct { |
115 | 115 | Message string `json:"message"` |
116 | 116 | } |
117 | 117 | |
118 | +type EnterInviteReq struct{ | |
119 | + Selfuuid int `json:"selfuuid"` | |
120 | + Inviteuuid int `json:"inviteuuid"` | |
121 | +} | |
122 | + | |
123 | + | |
124 | +type EnterInviteResp struct { | |
125 | + Code int `json:"code"` | |
126 | + Message string `json:"message"` | |
127 | +} | |
128 | + | |
118 | 129 | |
119 | 130 | type RegeisterReq struct{ |
120 | 131 | Account string `json:"account"` | ... | ... |
src/HttpServer/logic/errordef.go
src/HttpServer/logic/httpserver.go
... | ... | @@ -81,6 +81,7 @@ func startServerHttpServe() { |
81 | 81 | //................................................................................................................... |
82 | 82 | http.HandleFunc("/catcafe/user/queryInvite", QueryInvite) //查询玩家对应邀请关系 |
83 | 83 | http.HandleFunc("/catcafe/user/fetchInviteReward",FetchInviteReward) //领取邀请奖励 |
84 | + http.HandleFunc("/catcafe/user/enterInvite",EnterInvite) //别的玩家(新玩家)通过邀请连接进来 | |
84 | 85 | |
85 | 86 | |
86 | 87 | err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) |
... | ... | @@ -226,6 +227,20 @@ func ClearData(w http.ResponseWriter, r *http.Request) { |
226 | 227 | //HandleRegeister(w,s) |
227 | 228 | } |
228 | 229 | |
230 | + | |
231 | +func EnterInvite(w http.ResponseWriter, r *http.Request) { | |
232 | + | |
233 | + result, _ := ioutil.ReadAll(r.Body) | |
234 | + r.Body.Close() | |
235 | + | |
236 | + s := string(result) | |
237 | + logger.Info("EnterInvite , body:%v,uuid=%v", s) | |
238 | + | |
239 | + HandleEnterInvite(w,s) | |
240 | + | |
241 | +} | |
242 | + | |
243 | + | |
229 | 244 | func FetchInviteReward(w http.ResponseWriter, r *http.Request) { |
230 | 245 | |
231 | 246 | result, _ := ioutil.ReadAll(r.Body) | ... | ... |
src/HttpServer/logic/logic.go
... | ... | @@ -273,6 +273,56 @@ func HandleQueryInvite(w http.ResponseWriter, data string) { |
273 | 273 | fmt.Fprint(w, string(respstr)) |
274 | 274 | } |
275 | 275 | |
276 | + | |
277 | +func HandleEnterInvite(w http.ResponseWriter, data string) { | |
278 | + | |
279 | + SetHeader(w) | |
280 | + var resp EnterInviteResp | |
281 | + resp.Code = 0 | |
282 | + var rdata EnterInviteReq | |
283 | + err := json.Unmarshal([]byte(data), &rdata) | |
284 | + for { | |
285 | + if err != nil { | |
286 | + logger.Error("HandleEnterInvite json unmarshal failed=%v", err) | |
287 | + resp.Code = ERROR_JSONUNMASHFAILED | |
288 | + resp.Message = "json unmarshal failed" | |
289 | + break | |
290 | + } | |
291 | + | |
292 | + //首先要判断一下这个玩家是否已经被邀请过了 | |
293 | + exist,err := redishandler.GetRedisClient().HExists(redis.USER_BEINVITE_UUIDRELATION,strconv.Itoa(rdata.Selfuuid)) | |
294 | + if err != nil { | |
295 | + logger.Error("HandleEnterInvite redis failed=%v", err) | |
296 | + resp.Code = ERROR_JSONUNMASHFAILED | |
297 | + resp.Message = fmt.Sprintf("%s",err) | |
298 | + break | |
299 | + } | |
300 | + | |
301 | + if exist { | |
302 | + logger.Error("HandleEnterInvite already invited") | |
303 | + resp.Code = ERROR_ALREADY_INVITED | |
304 | + resp.Message = fmt.Sprintf("%s",err) | |
305 | + break | |
306 | + } | |
307 | + | |
308 | + | |
309 | + //记录被邀请关系 | |
310 | + redishandler.GetRedisClient().HSet(redis.USER_BEINVITE_UUIDRELATION,strconv.Itoa(rdata.Selfuuid),strconv.Itoa(rdata.Inviteuuid)) | |
311 | + | |
312 | + //记录邀请关系 | |
313 | + rkey := redis.USER_INVITEREWARD_FETCH_REWARD + ":" + strconv.Itoa(rdata.Selfuuid) | |
314 | + redishandler.GetRedisClient().HSet(rkey,strconv.Itoa(rdata.Inviteuuid),"0") | |
315 | + | |
316 | + resp.Code = ERROR_OK | |
317 | + break | |
318 | + } | |
319 | + | |
320 | + //回包 | |
321 | + respstr, _ := json.Marshal(&resp) | |
322 | + fmt.Fprint(w, string(respstr)) | |
323 | +} | |
324 | + | |
325 | + | |
276 | 326 | func HandleFetchInviteReward(w http.ResponseWriter, data string) { |
277 | 327 | |
278 | 328 | SetHeader(w) |
... | ... | @@ -289,7 +339,7 @@ func HandleFetchInviteReward(w http.ResponseWriter, data string) { |
289 | 339 | } |
290 | 340 | |
291 | 341 | rkey := redis.USER_INVITEREWARD_FETCH_REWARD + ":" + strconv.Itoa(rdata.Selfuuid) |
292 | - isexist,err := redishandler.GetRedisClient().HExists(rkey,strconv.Itoa(rdata.Fuuid)) | |
342 | + fetch,err := redishandler.GetRedisClient().HGet(rkey,strconv.Itoa(rdata.Fuuid)) | |
293 | 343 | if err != nil { |
294 | 344 | logger.Error("HandleFetchInviteReward redis failed=%v", err) |
295 | 345 | resp.Code = ERROR_JSONUNMASHFAILED |
... | ... | @@ -297,7 +347,7 @@ func HandleFetchInviteReward(w http.ResponseWriter, data string) { |
297 | 347 | break |
298 | 348 | } |
299 | 349 | |
300 | - if isexist { | |
350 | + if fetch == "1" { | |
301 | 351 | logger.Error("HandleFetchInviteReward alreadyfetched failed=%v", err) |
302 | 352 | resp.Code = ERROR_INVITEREAWARD_FETCHED |
303 | 353 | resp.Message = "already fetched" |
... | ... | @@ -305,7 +355,7 @@ func HandleFetchInviteReward(w http.ResponseWriter, data string) { |
305 | 355 | } |
306 | 356 | |
307 | 357 | //将领取记录记录 |
308 | - redishandler.GetRedisClient().HSet(rkey,strconv.Itoa(rdata.Fuuid),strconv.Itoa(rdata.Fuuid)) | |
358 | + redishandler.GetRedisClient().HSet(rkey,strconv.Itoa(rdata.Fuuid),"1") | |
309 | 359 | |
310 | 360 | resp.Code = ERROR_OK |
311 | 361 | break | ... | ... |
src/common/redis/def.go
... | ... | @@ -16,5 +16,6 @@ const ( |
16 | 16 | FRIEND_APPROVELIST_KEY = "CATCAFE_FRIEND_APPROVELIST_KEY" //待批准好友列表,需要在末尾加死":uuid" |
17 | 17 | FRIEND_RECOMMANDLIST_KEY = "CATCAFE_FRIEND_RECOMMANDLIST_KEY" //推荐好友缓存key |
18 | 18 | USER_ACCOUNT_PASSWORD_KEY = "CATCAFE_USER_ACCOUNT_PASSWORD_KEY" //玩家账号密码的key |
19 | - USER_INVITEREWARD_FETCH_REWARD = "CATCAFE_USER_INVITEREWARD_FETCH_REWARD" //玩家邀请奖励领取状况 hset | |
19 | + USER_INVITEREWARD_FETCH_REWARD = "CATCAFE_USER_INVITEREWARD_FETCH_REWARD" //玩家邀请记录 hset key + uuid field为被邀请者的uuid value为领取状态 | |
20 | + USER_BEINVITE_UUIDRELATION = "CATCAFE_USER_BEINVITE_UUIDRELATION" //玩家被邀请关系记录表 | |
20 | 21 | ) | ... | ... |
src/mysql/dbmysql.go
... | ... | @@ -20,6 +20,7 @@ var ( |
20 | 20 | type QuerInviteDesc struct { |
21 | 21 | Uuid int |
22 | 22 | NickName string |
23 | + HeadUrl string | |
23 | 24 | IsFecthed int |
24 | 25 | } |
25 | 26 | |
... | ... | @@ -151,49 +152,60 @@ func QueryAllData(f *os.File) error{ |
151 | 152 | |
152 | 153 | func QueryInvite(uuid int) ([]QuerInviteDesc,error){ |
153 | 154 | var rtslice []QuerInviteDesc |
154 | - for i:=0;i<10;i++ { | |
155 | - tablename := "b_user_ext_0" + strconv.Itoa(i) | |
156 | - cmd := "SELECT user_id,nickname, from " + tablename + " where invite_uid= " + strconv.Itoa(uuid) | |
157 | - rows, err := m_game_db.Query(cmd) | |
158 | - defer func() { | |
159 | - if rows != nil { | |
160 | - rows.Close() //可以关闭掉未scan连接一直占用 | |
161 | - } | |
162 | - }() | |
163 | - | |
164 | - if err != nil { | |
165 | - logger.Error("Query failed,err:%v", err) | |
166 | - continue | |
167 | - } | |
168 | - for rows.Next() { | |
169 | - var tmp QuerInviteDesc | |
170 | - err = rows.Scan(&tmp.Uuid, &tmp.NickName) //不scan会导致连接不释放 | |
171 | - if err != nil { | |
172 | - logger.Error("Scan failed,err:%v", err) | |
173 | - continue | |
174 | - } | |
175 | 155 | |
176 | - tmp.IsFecthed = 0 | |
177 | - rtslice = append(rtslice,tmp) | |
178 | - } | |
156 | + //首先取出邀请关系id | |
157 | + rkey := redis.USER_INVITEREWARD_FETCH_REWARD + ":" + strconv.Itoa(uuid) | |
158 | + vv,err := redishandler.GetRedisClient().HGetAllKeys(rkey) | |
159 | + if err != nil { | |
160 | + logger.Error("QueryInvite err=%v",err) | |
161 | + return nil,err | |
179 | 162 | } |
180 | 163 | |
181 | - //需要查询一下当前领取状态 | |
182 | - rkey := redis.USER_INVITEREWARD_FETCH_REWARD + ":" + strconv.Itoa(uuid) | |
183 | - for k,val := range rtslice{ | |
184 | - isexist,err := redishandler.GetRedisClient().HExists(rkey,strconv.Itoa(val.Uuid)) | |
164 | + for _,val := range vv { | |
165 | + var tmp QuerInviteDesc | |
166 | + | |
167 | + bytestr := string(val.([]byte)) | |
168 | + bytenum,_ := strconv.Atoi(bytestr) | |
169 | + tmp.Uuid =bytenum | |
170 | + val,err :=redishandler.GetRedisClient().HGet(rkey,bytestr) | |
185 | 171 | if err != nil { |
186 | - logger.Error("USER_INVITEREWARD_FETCH_REWARD failed err=%v",err) | |
187 | - rtslice[k].IsFecthed = 1 | |
172 | + tmp.IsFecthed = 1 | |
173 | + logger.Error("QueryInvite failed err=%v",err) | |
188 | 174 | continue |
189 | 175 | } |
190 | 176 | |
191 | - if isexist { | |
192 | - //已经存在 | |
193 | - rtslice[k].IsFecthed = 1 | |
194 | - } | |
177 | + stat,_ := strconv.Atoi(val) | |
178 | + tmp.IsFecthed = stat | |
179 | + | |
180 | + rtslice = append(rtslice,tmp) | |
195 | 181 | } |
196 | 182 | |
183 | + //需要查询对应的数据 | |
184 | + for k,val := range rtslice { | |
185 | + for i:=0;i<10;i++ { | |
186 | + tablename := "b_user_ext_0" + strconv.Itoa(i) | |
187 | + cmd := "SELECT nickname,avatar_url from " + tablename + " where user_id= " + strconv.Itoa(val.Uuid) | |
188 | + rows, err := m_game_db.Query(cmd) | |
189 | + defer func() { | |
190 | + if rows != nil { | |
191 | + rows.Close() //可以关闭掉未scan连接一直占用 | |
192 | + } | |
193 | + }() | |
194 | + | |
195 | + if err != nil { | |
196 | + logger.Error("Query failed,err:%v", err) | |
197 | + continue | |
198 | + } | |
199 | + for rows.Next() { | |
197 | 200 | |
201 | + err = rows.Scan(&rtslice[k].NickName, &rtslice[k].HeadUrl) //不scan会导致连接不释放 | |
202 | + if err != nil { | |
203 | + logger.Error("Scan failed,err:%v", err) | |
204 | + continue | |
205 | + } | |
206 | + } | |
207 | + } | |
208 | + } | |
209 | + | |
198 | 210 | return rtslice,nil |
199 | 211 | } |
200 | 212 | \ No newline at end of file | ... | ... |