Commit 443d5d297303f6c53470f603329c39901725818a

Authored by 陆恒
1 parent c3089ea8
Exists in master

推荐好友接口

src/HttpServer/logic/constdef.go
@@ -5,4 +5,5 @@ const ( @@ -5,4 +5,5 @@ const (
5 FRIEND_MAX_NUM = 50 //好友人数上限 5 FRIEND_MAX_NUM = 50 //好友人数上限
6 FRIEND_APPLY_LIMIT = 50 //好友申请当日上限 6 FRIEND_APPLY_LIMIT = 50 //好友申请当日上限
7 FRIEND_APPROVE_LIMIT = 50 //好友批准当日上限 7 FRIEND_APPROVE_LIMIT = 50 //好友批准当日上限
  8 + FRIEND_RECOMMAND_NUM = 3 //推荐好友数量
8 ) 9 )
9 \ No newline at end of file 10 \ No newline at end of file
src/HttpServer/logic/datadef.go
@@ -468,6 +468,41 @@ type GetAuditListResp struct { @@ -468,6 +468,41 @@ type GetAuditListResp struct {
468 } 468 }
469 469
470 470
  471 +type GetRecommendListReq struct{
  472 + Token string `json:"token"`
  473 +}
  474 +
  475 +type GetRecommendListInfo struct {
  476 + Userid int `json:"userid"`
  477 + Nickname string `json:"nickname"`
  478 + Avatar_url string `json:"avatar_url"`
  479 + Gender int `json:"gender"`
  480 + City string `json:"city"`
  481 + Hot int `json:"hot"`
  482 + Coin int64 `json:"coin"`
  483 + Love_exp int `json:"love_exp"`
  484 + Bean int `json:"bean"`
  485 + Shop_num int `json:"shop_num"`
  486 + Cat_num int `json:"cat_num"`
  487 + Lv int `json:"lv"`
  488 + Exp int `json:"exp"`
  489 + Reg_time int `json:"reg_time"`
  490 +}
  491 +
  492 +type GetRecommendListDesc struct {
  493 + Recommend_list []GetRecommendListInfo `json:"recommend_list"`
  494 +}
  495 +
  496 +type GetRecommendListResult struct {
  497 + Code int `json:"code"`
  498 + Data GetRecommendListDesc `json:"data"`
  499 +}
  500 +
  501 +type GetRecommendListResp struct {
  502 + Status string `json:"status"`
  503 + Result GetRecommendListResult `json:"result"`
  504 +}
  505 +
471 //********************************************************************************************************** 506 //**********************************************************************************************************
472 507
473 508
src/HttpServer/logic/errordef.go
@@ -29,4 +29,5 @@ const ( @@ -29,4 +29,5 @@ const (
29 RROR_FRIENDGETAPPROVELISTFAILED = 24 //获取申请好友列表失败 29 RROR_FRIENDGETAPPROVELISTFAILED = 24 //获取申请好友列表失败
30 RROR_FRIENDINAPPROVELISTFAILED = 25 //已经在等待批准列表 30 RROR_FRIENDINAPPROVELISTFAILED = 25 //已经在等待批准列表
31 ERROR_FRIENDAPPROVELIMIT = 26 //当日批准已达上限 31 ERROR_FRIENDAPPROVELIMIT = 26 //当日批准已达上限
  32 + ERROR_FRIENDGETRECOMMADNFAILED = 27 //获取推荐好友失败
32 ) 33 )
33 \ No newline at end of file 34 \ No newline at end of file
src/HttpServer/logic/function.go
@@ -18,6 +18,7 @@ func SetHeader(w http.ResponseWriter) { @@ -18,6 +18,7 @@ func SetHeader(w http.ResponseWriter) {
18 w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Uuid") 18 w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Uuid")
19 } 19 }
20 20
  21 +
21 func (tinfo *TeamInfo) DoAddBuffTimee(btype int,muti int,resp *AddTeamBuffResp) { 22 func (tinfo *TeamInfo) DoAddBuffTimee(btype int,muti int,resp *AddTeamBuffResp) {
22 addtime := 1800*muti 23 addtime := 1800*muti
23 index := 0 24 index := 0
@@ -532,4 +533,159 @@ func (flist *FriendList) IsInFreiendList(uuid int) bool { @@ -532,4 +533,159 @@ func (flist *FriendList) IsInFreiendList(uuid int) bool {
532 } 533 }
533 } 534 }
534 return false 535 return false
  536 +}
  537 +
  538 +func SaveCacheRecommandFriendList(uuid int,data *GetRecommendListDesc) {
  539 + savestr,err := json.Marshal(data)
  540 + if err != nil {
  541 + logger.Error("SaveCacheRecommandFriendList err=%v",err)
  542 + return
  543 + }
  544 + err = redishandler.GetRedisClient().HSet(redis.FRIEND_RECOMMANDLIST_KEY,strconv.Itoa(uuid),string(savestr))
  545 + if err != nil {
  546 + logger.Error("SaveCacheRecommandFriendList err=%v",err)
  547 + return
  548 + }
  549 +}
  550 +
  551 +func GetCacheRecommandFriendList(uuid int) (*GetRecommendListDesc,error) {
  552 + rt := new(GetRecommendListDesc)
  553 + val,err:= redishandler.GetRedisClient().HGet(redis.FRIEND_RECOMMANDLIST_KEY,strconv.Itoa(uuid))
  554 + if err != nil {
  555 + logger.Error("GetCacheRecommandFriendList err=%v",err)
  556 + return rt,err
  557 + }
  558 +
  559 + err = json.Unmarshal([]byte(val),rt)
  560 + if err != nil {
  561 + logger.Error("GetCacheRecommandFriendList err=%v",err)
  562 + return nil,err
  563 + }
  564 +
  565 + return rt,nil
  566 +}
  567 +
  568 +func DelCacheRecommandFriendList(uuid int) error {
  569 + err := redishandler.GetRedisClient().HDel(redis.FRIEND_RECOMMANDLIST_KEY,strconv.Itoa(uuid))
  570 + return err
  571 +}
  572 +
  573 +
  574 +//获取推荐好友列表
  575 +func (flist *FriendList)GetRecommandFriendList(uuid,friendnum int) (*GetRecommendListDesc,error) {
  576 +
  577 + rlist,err := GetCacheRecommandFriendList(uuid)
  578 + if err == nil {
  579 + //有缓存 ,直接返回
  580 + return rlist,err
  581 + }
  582 +
  583 + selfext,err := GetUserExt(uuid)
  584 + if err != nil {
  585 + return nil,err
  586 + }
  587 + selfbasic,err := GetUserBasic(uuid)
  588 + if err != nil {
  589 + return nil,err
  590 + }
  591 +
  592 +
  593 +
  594 + var randlist []int //记录随机的几个好友
  595 + var first []int //初步筛选的id列表
  596 + var second []int //第二步
  597 + var third []int
  598 + vv,err := redishandler.GetRedisClient().HGetAllValues(redis.USER_EXT_DATA)
  599 + if err != nil {
  600 + return nil ,err
  601 + }
  602 +
  603 + for _,val := range vv {
  604 + one := new(UserExtData)
  605 + bytestr := val.([]byte)
  606 + err = json.Unmarshal(bytestr, one)
  607 + if err == nil {
  608 + if selfext.Hot <= one.Hot+500 && selfext.Hot >= one.Hot-500 {
  609 + first = append(first,one.User_id)
  610 + }
  611 + }
  612 + if len(randlist) < friendnum {
  613 + randlist = append(randlist,one.User_id)
  614 + }
  615 + }
  616 +
  617 + applylist,err := GetUserApplyList(uuid)
  618 + if err != nil {
  619 + logger.Error("GetRecommandFriendList failed err=%v",err)
  620 + second = append(second,first...)
  621 + }else {
  622 + //第二步判断是否已经在申请列表,不再才加入
  623 + for _,val := range first {
  624 + if !applylist.InApplyList(val) {
  625 + second = append(second,val)
  626 + }
  627 + }
  628 + }
  629 +
  630 + //第三步判断性别
  631 + for _,val := range second {
  632 + if len(third) >= friendnum {
  633 + //数量已经满了,直接退出
  634 + break
  635 + }
  636 +
  637 + tmpbasic,err:= GetUserBasic(val)
  638 + if err != nil {
  639 + continue
  640 + }
  641 + if tmpbasic.User_gender != selfbasic.User_gender {
  642 + third = append(third,val)
  643 + }
  644 + }
  645 +
  646 + //最后判断一下数量是否足够
  647 + if len(third) < friendnum {
  648 + //将随机的id补充进去
  649 + for i:=0;i<friendnum-len(third);i++ {
  650 + third = append(third,randlist[i])
  651 + }
  652 + }
  653 +
  654 + //最后赋值
  655 + realrt := new(GetRecommendListDesc)
  656 + for _,val := range third {
  657 + tmpext,err := GetUserExt(val)
  658 + if err != nil {
  659 + logger.Error("GetRecommendListDesc third err=%v",err)
  660 + continue
  661 + }
  662 + tmpbasic,err := GetUserBasic(val)
  663 + if err != nil {
  664 + logger.Error("GetRecommendListDesc third err=%v",err)
  665 + continue
  666 + }
  667 + var tmpinfo GetRecommendListInfo
  668 + tmpinfo.Hot = tmpext.Hot
  669 + tmpinfo.Bean = tmpext.Bean
  670 + tmpinfo.Userid = tmpext.User_id
  671 + tmpinfo.Coin = tmpext.Coin
  672 + tmpinfo.Reg_time = tmpext.Reg_time
  673 + tmpinfo.Lv = tmpext.Lv
  674 + tmpinfo.Exp = tmpext.Exp
  675 + tmpinfo.Nickname = tmpbasic.User_nickname
  676 + tmpinfo.Avatar_url = tmpbasic.User_avatar_url
  677 + tmpinfo.Cat_num = 0
  678 + tmpinfo.City = tmpbasic.User_city
  679 + tmpinfo.Gender = tmpbasic.User_gender
  680 + tmpinfo.Love_exp = tmpext.LoveExp
  681 + tmpinfo.Shop_num = tmpext.ShopNum
  682 + realrt.Recommend_list = append(realrt.Recommend_list,tmpinfo)
  683 +
  684 + }
  685 +
  686 +
  687 +
  688 + //保存到缓存
  689 + SaveCacheRecommandFriendList(uuid,realrt)
  690 + return realrt,nil
535 } 691 }
536 \ No newline at end of file 692 \ No newline at end of file
src/HttpServer/logic/httpserver.go
@@ -49,12 +49,25 @@ func startServerHttpServe() { @@ -49,12 +49,25 @@ func startServerHttpServe() {
49 http.HandleFunc("/catcafe/friend/setFriendRequest", SetFriendRequest) //申请添加好友 49 http.HandleFunc("/catcafe/friend/setFriendRequest", SetFriendRequest) //申请添加好友
50 http.HandleFunc("/catcafe/friend/getList", GetFriendList) //获取好友列表 50 http.HandleFunc("/catcafe/friend/getList", GetFriendList) //获取好友列表
51 http.HandleFunc("/catcafe/friend/getAuditList", GetAuditList) //获取待审核好友列表 51 http.HandleFunc("/catcafe/friend/getAuditList", GetAuditList) //获取待审核好友列表
  52 + http.HandleFunc("/catcafe/friend/getRecommendList", GetRecommendList) //获取推荐好友列表
52 53
53 54
54 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) 55 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil)
55 CheckErr(err) 56 CheckErr(err)
56 } 57 }
57 58
  59 +
  60 +func GetRecommendList(w http.ResponseWriter, r *http.Request) {
  61 +
  62 + result, _ := ioutil.ReadAll(r.Body)
  63 + r.Body.Close()
  64 +
  65 + s := string(result)
  66 + logger.Info("GetRecommendList , body:%v,uuid=%v", s)
  67 +
  68 + HandleGetRecommendList(w,s)
  69 +}
  70 +
58 func GetAuditList(w http.ResponseWriter, r *http.Request) { 71 func GetAuditList(w http.ResponseWriter, r *http.Request) {
59 72
60 result, _ := ioutil.ReadAll(r.Body) 73 result, _ := ioutil.ReadAll(r.Body)
src/HttpServer/logic/logic.go
@@ -1240,7 +1240,7 @@ func HandleSetFriendRequest(w http.ResponseWriter, data string) { @@ -1240,7 +1240,7 @@ func HandleSetFriendRequest(w http.ResponseWriter, data string) {
1240 SaveUserApproveList(rdata.User_id,uuid) 1240 SaveUserApproveList(rdata.User_id,uuid)
1241 1241
1242 //清空好友推荐缓存 1242 //清空好友推荐缓存
1243 - //TODO 1243 + DelCacheRecommandFriendList(uuid)
1244 1244
1245 resp.Result.Code = ERROR_OK 1245 resp.Result.Code = ERROR_OK
1246 break 1246 break
@@ -1328,3 +1328,57 @@ func HandleGetAuditList(w http.ResponseWriter, data string) { @@ -1328,3 +1328,57 @@ func HandleGetAuditList(w http.ResponseWriter, data string) {
1328 fmt.Fprint(w, string(respstr)) 1328 fmt.Fprint(w, string(respstr))
1329 } 1329 }
1330 1330
  1331 +
  1332 +func HandleGetRecommendList(w http.ResponseWriter, data string) {
  1333 + SetHeader(w)
  1334 + var resp GetRecommendListResp
  1335 + resp.Status = "true"
  1336 + resp.Result.Code = ERROR_OK
  1337 + var rdata GetRecommendListReq
  1338 + err := json.Unmarshal([]byte(data), &rdata)
  1339 + for {
  1340 + if err != nil {
  1341 + logger.Error("HandleGetRecommendList json unmarshal failed=%v", err)
  1342 + resp.Result.Code = ERROR_JSONUNMASHFAILED
  1343 + break
  1344 + }
  1345 +
  1346 + uuid,err := GetTouristUid(rdata.Token)
  1347 + if err != nil || uuid==0{
  1348 + logger.Error("HandleGetRecommendList GetTouristUid failed=%v", err)
  1349 + resp.Result.Code = ERROR_GETUSERIDFAILED
  1350 + break
  1351 + }
  1352 +
  1353 + //需要判断自己的好友位置是否已满
  1354 + friendlist,err := GetUserFriendList(uuid)
  1355 + if err != nil {
  1356 + logger.Error("HandleGetRecommendList GetFriendList failed=%v", err)
  1357 + resp.Result.Code = ERROR_GETFRIENDLISTAILED
  1358 + break
  1359 + }
  1360 + if len(friendlist.Friends) >= FRIEND_MAX_NUM {
  1361 + //好友位已经满了不再做推荐
  1362 + resp.Result.Code = ERROR_OK
  1363 + break
  1364 + }
  1365 +
  1366 + rlist,err := friendlist.GetRecommandFriendList(uuid,FRIEND_RECOMMAND_NUM)
  1367 + if err != nil {
  1368 + logger.Error("HandleGetRecommendList GetRecommandFriendList failed=%v", err)
  1369 + resp.Result.Code = ERROR_FRIENDGETRECOMMADNFAILED
  1370 + break
  1371 + }
  1372 +
  1373 + for _,val := range rlist.Recommend_list {
  1374 + resp.Result.Data.Recommend_list = append(resp.Result.Data.Recommend_list,val)
  1375 + }
  1376 +
  1377 + resp.Result.Code = ERROR_OK
  1378 + break
  1379 + }
  1380 +
  1381 + //回包
  1382 + respstr, _ := json.Marshal(&resp)
  1383 + fmt.Fprint(w, string(respstr))
  1384 +}
1331 \ No newline at end of file 1385 \ No newline at end of file
src/common/redis/def.go
@@ -14,4 +14,5 @@ const ( @@ -14,4 +14,5 @@ const (
14 FRIEND_LIST_KEY = "CATCAFE_FRIEND_LIST_KEY" //玩家的好友列表的key,需要在末尾加死":uuid" 14 FRIEND_LIST_KEY = "CATCAFE_FRIEND_LIST_KEY" //玩家的好友列表的key,需要在末尾加死":uuid"
15 FRIEND_APPLYLIST_KEY = "CATCAFE_FRIEND_APPLYLIST_KEY" //玩家申请好友列表,需要在末尾加死":uuid" 15 FRIEND_APPLYLIST_KEY = "CATCAFE_FRIEND_APPLYLIST_KEY" //玩家申请好友列表,需要在末尾加死":uuid"
16 FRIEND_APPROVELIST_KEY = "CATCAFE_FRIEND_APPROVELIST_KEY" //待批准好友列表,需要在末尾加死":uuid" 16 FRIEND_APPROVELIST_KEY = "CATCAFE_FRIEND_APPROVELIST_KEY" //待批准好友列表,需要在末尾加死":uuid"
  17 + FRIEND_RECOMMANDLIST_KEY = "CATCAFE_FRIEND_RECOMMANDLIST_KEY" //推荐好友缓存key
17 ) 18 )