84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
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)
|
|
// }
|