用户服务负责注册,更改密码,用户登录,添加好友,获取好友关系,下线上线通知等。
添加好友需要两段处理,首先将验证请求并存入数据库,再判断好友用户是否在线,在线则直接发送请求给好友用户,否则将在用户登陆时检索请求数据库再发送好友请求。
大致代码如下:
// 用户处理好友请求
func DealAddFriend(delivery *amqp.Delivery, transfer1 *transfer) {
// Ack := false
// defer func() {
// delivery.Ack(Ack)
// fmt.Println("ACK", Ack)
// }()
Userid := transfer1.Id
var addfriendresult OtherDealAddUserResult
err := mapstructure.Decode(transfer1.Data.Data, &addfriendresult)
if err != nil {
fmt.Println("DealAddFriend json err :", err)
return
}
// 判断数据库是否有此添加信息
request, err := msql.GetRequest(addfriendresult.Id)
if err != nil {
fmt.Println("DealAddFriend err :", err)
return
}
if len(request.id) == 0 {
fmt.Println("DealAddFriend 没找到请求 :", err)
return
}
// 判断是否有关
if Userid != request.ReceiveID {
fmt.Println("DealAddFriend ID不匹配", Userid, request)
return
}
// 数据库更新请求
var result int
if addfriendresult.Result {
result = Agree
} else {
result = DisAgree
}
err = msql.UpdateRequest(addfriendresult.Id, result)
if err != nil {
fmt.Println("DealAddFriend err :", err)
return
}
// 如果同意 添加好友关系
if addfriendresult.Result {
// 判断是否为好友关系
err = msql.InsertRelationShip(request.ReceiveID, request.SponsorID)
if err != nil {
fmt.Println("DealAddFriend err :", err)
return
}
}
// 假如添加好友在线 通知结果
u, err := redisconn.RedisGetUser(request.SponsorID)
if err != nil {
fmt.Println("DealAddFriend err :", err)
return
}
if u != nil && u.Status == OnLine {
u1, err := GetUser(request.ReceiveID)
if err != nil {
fmt.Println("DealAddFriend err :", err)
return
}
var addresult AdduserResult
addresult.Result = addfriendresult.Result
if addfriendresult.Result {
addresult.User = *u1
} else {
addresult.Error = fmt.Sprintf("ID:%v 名称:%v 拒绝了你的好友请求!", u1.Userid, u1.Name)
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
data2, err := json.Marshal(addresult)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
err = RabbitMqPublish(mq, data2, request.SponsorID, AddUser, u.GateWay)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
// 读取数据库 更新到redis中
if addfriendresult.Result {
err := UpdateFriendShip(request.SponsorID)
if err != nil {
fmt.Println(err)
}
err = UpdateFriendShip(request.ReceiveID)
if err != nil {
fmt.Println(err)
}
}
}
// Ack = true
}
//用户添加好友
func AddUserDeal(delivery *amqp.Delivery, transfer1 *transfer) {
// Ack := false
// defer func() {
// delivery.Ack(Ack)
// fmt.Println("ACK", Ack)
// }()
Userid := transfer1.Id
var addfriend Adduser
err := mapstructure.Decode(transfer1.Data.Data, &addfriend)
if err != nil {
fmt.Println("AddUserDeal json err :", err)
return
}
AddFriendId := addfriend.FriendId
// 首先判断用户是否存在
exit, err := msql.UserExit(AddFriendId)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
// 不存在 发送用户不存在
if !exit {
var addresult AdduserResult
addresult.Result = false
addresult.Error = "用户不存在"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
data2, err := json.Marshal(addresult)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
err = RabbitMqPublish(mq, data2, Userid, AddUser, transfer1.From)
if err != nil {
fmt.Println(err)
return
}
// Ack = true
return
}
// 判断是否为其好友
result, err := IsFriend(Userid, AddFriendId)
if err != nil {
fmt.Println(err)
return
}
// 判断是否有多个请求
num, err := msql.GetRepeatRequest(Userid, AddFriendId, AddFriend, 1)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
if num > 3 {
var addresult AdduserResult
addresult.Result = false
addresult.Error = "添加该好友请求过多"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
data2, err := json.Marshal(addresult)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
err = RabbitMqPublish(mq, data2, Userid, AddUser, transfer1.From)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
// Ack = true
return
}
if result {
var addresult AdduserResult
addresult.Result = false
addresult.Error = "该用户已经是你的好友了!"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
data2, err := json.Marshal(addresult)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
err = RabbitMqPublish(mq, data2, Userid, AddUser, transfer1.From)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
// Ack = true
return
}
// 添加好友 数据库存储添加好友请求
sqlid, err := msql.SaveUserRequest(Userid, AddFriendId, AddFriend, "")
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
var addresult AdduserResult
addresult.Result = false
addresult.Error = "等待用户同意"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
data2, err := json.Marshal(addresult)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
err = RabbitMqPublish(mq, data2, Userid, AddUser, transfer1.From)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
// 将其发送给被添加者
u, err := redisconn.RedisGetUser(AddFriendId)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
// 在线就将信息发送给用户
if u != nil && (u.Status == OnLine && u.GateWay != "") {
var o OtherDealAddUser
// 首先
user, err := GetUser(Userid)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
o.Sponsor = *user
o.Id = sqlid
var json = jsoniter.ConfigCompatibleWithStandardLibrary
data3, err := json.Marshal(o)
if err != nil {
fmt.Println("AddUserDeal", err)
return
}
RabbitMqPublish(mq, data3, u.Userid, OtherAddFriend, u.GateWay)
}
// Ack = true
}
其他功能代码就不发了,大致都是这样。