This commit is contained in:
aozhiwei 2024-04-10 16:17:51 +08:00
parent 86cd3c116f
commit 78c7625b9c
5 changed files with 39 additions and 71 deletions

View File

@ -2,23 +2,25 @@ package middleware
import (
"github.com/gin-gonic/gin"
"net/http"
// "net/http"
)
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
/*
method := c.Request.Method
//origin := c.Request.Header.Get("Origin")
//if origin != "" {
c.Header("Access-Control-Allow-Origin", "*") // 可将将 * 替换为指定的域名
//c.Header("Access-Control-Allow-Origin", "*") // 可将将 * 替换为指定的域名
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
//}
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusOK)
c.AbortWithStatus(200)
}
c.Next()*/
c.Next()
}
}

View File

@ -4,6 +4,7 @@ import (
"f5"
"q5"
"fmt"
"cs"
"main/common"
"main/constant"
)
@ -138,6 +139,14 @@ func (this *cacheMgr) internalGetUsers(accountIds []string, cb func(int32, strin
})
}
func (this* cacheMgr) AsyncGetUsersAndFillMFUser([]string, *[]*cs.MFUser, func(int32, string)) {
}
func (this* cacheMgr) AsyncGetUsersAndFillMFGuildMember([]string, *[]*cs.MFGuildMember, func(int32, string)) {
}
func (this *cacheMgr) getUser(accountId string) *userProfile {
if val, ok := this.userHash[accountId]; ok {
return val

View File

@ -103,12 +103,11 @@ type GuildMgr interface {
type CacheMgr interface {
PreLoadUsers([]string)
GetUserProfile(string) UserProfile
AsyncGetUsers([]string, func(int32, string))
AsyncSearch(int64, string, func(int32, string, int64, []string))
AsyncGetUsersAndFillMFUser([]string, *[]*cs.MFUser, func(int32, string))
AsyncGetUsersAndFillMFGuildMember([]string, *[]*cs.MFGuildMember, func(int32, string))
AsyncSearch(int64, string, *[]*cs.MFUser, func(int32, string, int64))
}
type UserProfile interface {
GetAccountId() string
FillMFUser(*cs.MFUser)
FillMFGuildMember(*cs.MFGuildMember)
}

View File

@ -148,22 +148,15 @@ func (this *guild) AsyncFillMFGuild(pbGuild *cs.MFGuild, cb func(int32, string))
for _, m := range(this.idHash) {
q5.AppendSlice(&accountIds, m.memberId)
}
GetCacheMgr().AsyncGetUsers(
q5.NewSlice(&pbGuild.Members, 0, int32(len(accountIds)))
GetCacheMgr().AsyncGetUsersAndFillMFGuildMember(
accountIds,
&pbGuild.Members,
func (errCode int32, errMsg string) {
if errCode != 0 {
cb(500, "server internal error")
return
}
q5.NewSlice(&pbGuild.Members, 0, int32(len(accountIds)))
for _, accountId := range(accountIds) {
u := GetCacheMgr().GetUserProfile(accountId)
if u != nil {
pbMember := new(cs.MFGuildMember)
u.FillMFGuildMember(pbMember)
q5.AppendSlice(&pbGuild.Members, pbMember)
}
}
cb(0, "")
})
}

View File

@ -53,41 +53,33 @@ func (this *player) CMPing(hdr *f5.MsgHdr, msg *cs.CMPing) {
}
func (this *player) CMSearchUser(hdr *f5.MsgHdr, msg *cs.CMSearchUser) {
rspMsg := new(cs.SMSearchUser)
GetCacheMgr().AsyncSearch(
msg.GetSinceId(),
msg.GetUsername(),
func (errCode int32, errMsg string, sinceId int64, accountIds []string) {
rspMsg := new(cs.SMSearchUser)
&rspMsg.Users,
func (errCode int32, errMsg string, sinceId int64) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
rspMsg.SinceId = proto.Int64(sinceId)
q5.NewSlice(&rspMsg.Users, 0, int32(len(accountIds)))
for _, accountId := range(accountIds) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := new(cs.MFUser)
q5.AppendSlice(&rspMsg.Users, ele)
userProfile.FillMFUser(ele)
}
}
this.SendMsg(rspMsg)
})
}
func (this *player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByAccountId) {
GetCacheMgr().AsyncGetUsers(
rspMsg := new(cs.SMSearchUserByAccountId)
users := []*cs.MFUser{}
GetCacheMgr().AsyncGetUsersAndFillMFUser(
[]string{msg.GetAccountId()},
&users,
func (errCode int32, errMsg string) {
rspMsg := new(cs.SMSearchUserByAccountId)
if errCode != 0 {
if errCode != 0 || len(users) <= 0{
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
rspMsg.Users = new(cs.MFUser)
userProfile := GetCacheMgr().GetUserProfile(msg.GetAccountId())
userProfile.FillMFUser(rspMsg.Users)
rspMsg.Users = users[0]
this.SendMsg(rspMsg)
})
}
@ -102,65 +94,45 @@ func (this *player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPen
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
GetCacheMgr().AsyncGetUsers(
GetCacheMgr().AsyncGetUsersAndFillMFUser(
accountIds,
&rspMsg.Users,
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
for _, accountId := range(accountIds) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := new(cs.MFUser)
q5.AppendSlice(&rspMsg.Users, ele)
userProfile.FillMFUser(ele)
}
}
this.SendMsg(rspMsg)
})
})
}
func (this *player) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) {
rspMsg := new(cs.SMListFriend)
friendList := GetFriendMgr().GetFriendList(this.GetAccountId())
GetCacheMgr().AsyncGetUsers(
GetCacheMgr().AsyncGetUsersAndFillMFUser(
friendList,
&rspMsg.Users,
func (errCode int32, errMsg string) {
rspMsg := new(cs.SMListFriend)
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
for _, accountId := range(friendList) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := new(cs.MFUser)
q5.AppendSlice(&rspMsg.Users, ele)
userProfile.FillMFUser(ele)
}
}
this.SendMsg(rspMsg)
})
}
func (this *player) CMBlacklist(hdr *f5.MsgHdr, msg *cs.CMBlacklist) {
rspMsg := new(cs.SMBlacklist)
blackList := GetFriendMgr().GetBlackList(this.GetAccountId())
GetCacheMgr().AsyncGetUsers(
GetCacheMgr().AsyncGetUsersAndFillMFUser(
blackList,
&rspMsg.Users,
func (errCode int32, errMsg string) {
rspMsg := new(cs.SMBlacklist)
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
for _, accountId := range(blackList) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := q5.NewSliceElement(&rspMsg.Users)
userProfile.FillMFUser(*ele)
}
}
this.SendMsg(rspMsg)
})
}
@ -517,21 +489,14 @@ func (this *player) CMApplyList(hdr *f5.MsgHdr, msg *cs.CMApplyList) {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
GetCacheMgr().AsyncGetUsers(
GetCacheMgr().AsyncGetUsersAndFillMFGuildMember(
accountIds,
&rspMsg.Members,
func (errCode int32, errMsg string) {
if errCode != 0 {
this.SendMsg(rspMsg.Err(1, "internal server error"))
return
}
for _, accountId := range(accountIds) {
userProfile := GetCacheMgr().GetUserProfile(accountId)
if userProfile != nil {
ele := new(cs.MFGuildMember)
q5.AppendSlice(&rspMsg.Members, ele)
userProfile.FillMFGuildMember(ele)
}
}
this.SendMsg(rspMsg)
})
})