From 13b5d35d0ca597f04dd1fceb66dc0c54fbf45d57 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 25 Jan 2024 10:39:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E8=8F=9C=E5=8D=95=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?query=20=E5=8F=82=E6=95=B0=E8=8E=B7=E5=8F=96=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/system/controller/sys_menu.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/modules/system/controller/sys_menu.go b/src/modules/system/controller/sys_menu.go index 256718d1..3cb023b7 100644 --- a/src/modules/system/controller/sys_menu.go +++ b/src/modules/system/controller/sys_menu.go @@ -34,10 +34,10 @@ type SysMenuController struct { // GET /list func (s *SysMenuController) List(c *gin.Context) { query := model.SysMenu{} - if v, ok := c.GetQuery("menuName"); ok { + if v, ok := c.GetQuery("menuName"); ok && v != "" { query.MenuName = v } - if v, ok := c.GetQuery("status"); ok { + if v, ok := c.GetQuery("status"); ok && v != "" { query.Status = v } @@ -289,10 +289,10 @@ func (s *SysMenuController) Remove(c *gin.Context) { // GET /treeSelect func (s *SysMenuController) TreeSelect(c *gin.Context) { query := model.SysMenu{} - if v, ok := c.GetQuery("menuName"); ok { + if v, ok := c.GetQuery("menuName"); ok && v != "" { query.MenuName = v } - if v, ok := c.GetQuery("status"); ok { + if v, ok := c.GetQuery("status"); ok && v != "" { query.Status = v } @@ -331,10 +331,10 @@ func (s *SysMenuController) RoleMenuTreeSelect(c *gin.Context) { } query := model.SysMenu{} - if v, ok := c.GetQuery("menuName"); ok { + if v, ok := c.GetQuery("menuName"); ok && v != "" { query.MenuName = v } - if v, ok := c.GetQuery("status"); ok { + if v, ok := c.GetQuery("status"); ok && v != "" { query.Status = v } From ea8b197e8b10e7db5f0244c09789e390afcd117e Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 25 Jan 2024 10:40:00 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20ws=20=E8=BA=AB=E4=BB=BD=E8=AE=A4?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/utils/ctx/ctx.go | 15 ++++++++++++++- src/modules/ws/service/ws.impl.go | 21 ++++++++++++++------- src/modules/ws/ws.go | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/framework/utils/ctx/ctx.go b/src/framework/utils/ctx/ctx.go index ff9efe0a..b2ba35d5 100644 --- a/src/framework/utils/ctx/ctx.go +++ b/src/framework/utils/ctx/ctx.go @@ -65,6 +65,11 @@ func IPAddrLocation(c *gin.Context) (string, string) { // Authorization 解析请求头 func Authorization(c *gin.Context) string { + // Query请求查询 + if authQuery, ok := c.GetQuery(token.RESPONSE_FIELD); ok && authQuery != "" { + return authQuery + } + // Header请求头 authHeader := c.GetHeader(token.HEADER_KEY) if authHeader == "" { return "" @@ -99,7 +104,15 @@ func UaOsBrowser(c *gin.Context) (string, string) { // AcceptLanguage 解析客户端接收语言 zh:中文 en: 英文 func AcceptLanguage(c *gin.Context) string { preferredLanguage := language.English - acceptLanguage := c.GetHeader("Accept-Language") + acceptLanguage := "en_US" + // Query请求查询 + if v, ok := c.GetQuery("lang"); ok && v != "" { + acceptLanguage = v + } + // Header请求头 + if v := c.GetHeader("Accept-Language"); v != "" { + acceptLanguage = v + } tags, _, _ := language.ParseAcceptLanguage(acceptLanguage) if len(tags) > 0 { preferredLanguage = tags[0] diff --git a/src/modules/ws/service/ws.impl.go b/src/modules/ws/service/ws.impl.go index 5add5595..46fa2866 100644 --- a/src/modules/ws/service/ws.impl.go +++ b/src/modules/ws/service/ws.impl.go @@ -31,6 +31,7 @@ type WSImpl struct{} // UpgraderWs http升级ws请求 func (s *WSImpl) UpgraderWs(w http.ResponseWriter, r *http.Request) *websocket.Conn { wsUpgrader := websocket.Upgrader{ + Subprotocols: []string{"omc-ws"}, // 设置消息发送缓冲区大小(byte),如果这个值设置得太小,可能会导致服务端在发送大型消息时遇到问题 WriteBufferSize: 1024, // 消息包启用压缩 @@ -61,7 +62,7 @@ func (s *WSImpl) NewClient(uid string, groupIDs []string, conn *websocket.Conn) BindUid: uid, SubGroup: groupIDs, MsgChan: make(chan []byte, 100), - StopChan: make(chan struct{}, 1), // 请求卡死循环标记 + StopChan: make(chan struct{}, 1), // 卡死循环标记 } // 存入客户端 @@ -111,10 +112,12 @@ func (s *WSImpl) clientRead(wsClient *model.WSClient) { s.CloseClient(wsClient.ID) return } + // 文本和二进制类型,只处理文本json if messageType == websocket.TextMessage { var reqMsg model.WSRequest err := json.Unmarshal(msg, &reqMsg) + // fmt.Println(messageType, string(msg)) if err != nil { msgByte, _ := json.Marshal(result.ErrMsg("message format not supported")) wsClient.MsgChan <- msgByte @@ -177,11 +180,13 @@ func (s *WSImpl) CloseClient(clientID string) { if clientIds, ok := WsUsers.Load(client.BindUid); ok { uidClientIds := clientIds.(*[]string) if len(*uidClientIds) > 0 { - for i, clientId := range *uidClientIds { - if clientId == client.ID { - *uidClientIds = append((*uidClientIds)[:i], (*uidClientIds)[i+1:]...) + tempClientIds := make([]string, 0, len(*uidClientIds)) + for _, v := range *uidClientIds { + if v != client.ID { + tempClientIds = append(tempClientIds, v) } } + *uidClientIds = tempClientIds } } } @@ -196,11 +201,13 @@ func (s *WSImpl) CloseClient(clientID string) { groupUIDs := uids.(*[]string) if len(*groupUIDs) > 0 { - for i, v := range *groupUIDs { - if v == client.BindUid { - *groupUIDs = append((*groupUIDs)[:i], (*groupUIDs)[i+1:]...) + tempUIDs := make([]string, 0, len(*groupUIDs)) + for _, v := range *groupUIDs { + if v != client.BindUid { + tempUIDs = append(tempUIDs, v) } } + *groupUIDs = tempUIDs } } } diff --git a/src/modules/ws/ws.go b/src/modules/ws/ws.go index e0ad3d1f..ee83ea3f 100644 --- a/src/modules/ws/ws.go +++ b/src/modules/ws/ws.go @@ -18,7 +18,7 @@ func Setup(router *gin.Engine) { { wsGroup.GET("", middleware.PreAuthorize(nil), - collectlogs.OperateLog(collectlogs.OptionNew("WS 订阅", collectlogs.BUSINESS_TYPE_OTHER)), + collectlogs.OperateLog(collectlogs.OptionNew("WS Subscription", collectlogs.BUSINESS_TYPE_OTHER)), controller.NewWSController.WS, ) From 557a4d23c7f627175db652d0dd4ca78527da4c9f Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 25 Jan 2024 11:15:28 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E8=AF=B7=E6=B1=82=E8=A7=A3=E6=9E=90?= =?UTF-8?q?language=E8=AF=AD=E8=A8=80=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/utils/ctx/ctx.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/framework/utils/ctx/ctx.go b/src/framework/utils/ctx/ctx.go index b2ba35d5..b005e13e 100644 --- a/src/framework/utils/ctx/ctx.go +++ b/src/framework/utils/ctx/ctx.go @@ -104,19 +104,22 @@ func UaOsBrowser(c *gin.Context) (string, string) { // AcceptLanguage 解析客户端接收语言 zh:中文 en: 英文 func AcceptLanguage(c *gin.Context) string { preferredLanguage := language.English - acceptLanguage := "en_US" + // Query请求查询 - if v, ok := c.GetQuery("lang"); ok && v != "" { - acceptLanguage = v + if v, ok := c.GetQuery("language"); ok && v != "" { + tags, _, _ := language.ParseAcceptLanguage(v) + if len(tags) > 0 { + preferredLanguage = tags[0] + } } // Header请求头 if v := c.GetHeader("Accept-Language"); v != "" { - acceptLanguage = v - } - tags, _, _ := language.ParseAcceptLanguage(acceptLanguage) - if len(tags) > 0 { - preferredLanguage = tags[0] + tags, _, _ := language.ParseAcceptLanguage(v) + if len(tags) > 0 { + preferredLanguage = tags[0] + } } + // 只取前缀 lang := preferredLanguage.String() arr := strings.Split(lang, "-")