fix
This commit is contained in:
59
server/websocket/internal/logic/commonnotifylogic.go
Normal file
59
server/websocket/internal/logic/commonnotifylogic.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CommonNotifyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCommonNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommonNotifyLogic {
|
||||
return &CommonNotifyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *CommonNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *CommonNotifyLogic) CommonNotify(req *types.CommonNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.Data == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty")
|
||||
}
|
||||
//websocket连接id不能为空
|
||||
if req.WebsocketId == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "websocket connect id is empty")
|
||||
}
|
||||
//查询websocket连接
|
||||
value, ok := mapConnPool.Load(req.WebsocketId)
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found")
|
||||
}
|
||||
//断言连接
|
||||
ws, ok := value.(wsConnectItem)
|
||||
if !ok {
|
||||
logx.Error("渲染回调断言websocket连接失败")
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误")
|
||||
}
|
||||
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_COMMON_NOTIFY, req.Data))
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *CommonNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
@@ -1,83 +0,0 @@
|
||||
package logic
|
||||
|
||||
//登录回调
|
||||
import (
|
||||
"encoding/json"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/encryption_decryption"
|
||||
"fusenapi/utils/websocket_data"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginNotifyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewLoginNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginNotifyLogic {
|
||||
return &LoginNotifyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *LoginNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *LoginNotifyLogic) LoginNotify(req *types.LoginNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.Data == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty")
|
||||
}
|
||||
//解密数据
|
||||
data, err := encryption_decryption.CBCDecrypt(req.Data)
|
||||
if err != nil {
|
||||
logx.Error("解密失败:", err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ")
|
||||
}
|
||||
//真正的数据结构
|
||||
var parseInfo websocket_data.NotifyData
|
||||
if err = json.Unmarshal([]byte(data), &parseInfo); err != nil {
|
||||
logx.Error("failed to parse json data:", err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid format of parse data")
|
||||
}
|
||||
//websocket连接id不能为空
|
||||
if parseInfo.WebsocketConnectId == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "websocket connect id is empty")
|
||||
}
|
||||
now := time.Now().UTC().Unix()
|
||||
//请求时间前后20秒都会失效
|
||||
if parseInfo.RequestTime < now-20 || parseInfo.RequestTime > now+20 {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ,time is not in allowed range")
|
||||
}
|
||||
//查询websocket连接
|
||||
value, ok := mapConnPool.Load(parseInfo.WebsocketConnectId)
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found")
|
||||
}
|
||||
//断言连接
|
||||
ws, ok := value.(wsConnectItem)
|
||||
if !ok {
|
||||
logx.Error("渲染回调断言websocket连接失败")
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误")
|
||||
}
|
||||
//发送消息到出口缓冲池
|
||||
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_LOGIN_NOTIFY, parseInfo.Data))
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *LoginNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
@@ -1,83 +0,0 @@
|
||||
package logic
|
||||
|
||||
//注册帐号回调
|
||||
import (
|
||||
"encoding/json"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/encryption_decryption"
|
||||
"fusenapi/utils/websocket_data"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RegisterAccountNotifyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewRegisterAccountNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterAccountNotifyLogic {
|
||||
return &RegisterAccountNotifyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *RegisterAccountNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *RegisterAccountNotifyLogic) RegisterAccountNotify(req *types.RegisterAccountNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.Data == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty")
|
||||
}
|
||||
//解密数据
|
||||
data, err := encryption_decryption.CBCDecrypt(req.Data)
|
||||
if err != nil {
|
||||
logx.Error("解密失败:", err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ")
|
||||
}
|
||||
//真正的数据结构
|
||||
var parseInfo websocket_data.NotifyData
|
||||
if err = json.Unmarshal([]byte(data), &parseInfo); err != nil {
|
||||
logx.Error("failed to parse json data:", err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid format of parse data")
|
||||
}
|
||||
//websocket连接id不能为空
|
||||
if parseInfo.WebsocketConnectId == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "websocket connect id is empty")
|
||||
}
|
||||
now := time.Now().UTC().Unix()
|
||||
//请求时间前后20秒都会失效
|
||||
if parseInfo.RequestTime < now-20 || parseInfo.RequestTime > now+20 {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ,time is not in allowed range")
|
||||
}
|
||||
//查询websocket连接
|
||||
value, ok := mapConnPool.Load(parseInfo.WebsocketConnectId)
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found")
|
||||
}
|
||||
//断言连接
|
||||
ws, ok := value.(wsConnectItem)
|
||||
if !ok {
|
||||
logx.Error("渲染回调断言websocket连接失败")
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误")
|
||||
}
|
||||
//发送消息到出口缓冲池
|
||||
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_REGISTER_NOTIFY, parseInfo.Data))
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *RegisterAccountNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
Reference in New Issue
Block a user