This commit is contained in:
laodaming 2023-07-27 17:07:54 +08:00
parent d843fff73d
commit cf30fc2b88
6 changed files with 163 additions and 2 deletions

View File

@ -12,8 +12,12 @@ const (
WEBSOCKET_RENDER_IMAGE = "WEBSOCKET_RENDER_IMAGE" WEBSOCKET_RENDER_IMAGE = "WEBSOCKET_RENDER_IMAGE"
//数据格式错误 //数据格式错误
WEBSOCKET_ERR_DATA_FORMAT = "WEBSOCKET_ERR_DATA_FORMAT" WEBSOCKET_ERR_DATA_FORMAT = "WEBSOCKET_ERR_DATA_FORMAT"
// //第三方登录通知
WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY = "WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY"
) )
// 云渲染完成通知api需要的签名字符串 // 云渲染完成通知api需要的签名字符串
const RENDER_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d" const RENDER_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d"
// 第三方登录通知api需要的签名字符串
const THIRD_PARTY_LOGIN_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d"

View File

@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/websocket/render_notify", Path: "/api/websocket/render_notify",
Handler: RenderNotifyHandler(serverCtx), Handler: RenderNotifyHandler(serverCtx),
}, },
{
Method: http.MethodPost,
Path: "/api/websocket/third_party_login_notify",
Handler: ThirdPartyLoginNotifyHandler(serverCtx),
},
}, },
) )
} }

View File

@ -0,0 +1,35 @@
package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/websocket/internal/logic"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
)
func ThirdPartyLoginNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ThirdPartyLoginNotifyReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewThirdPartyLoginNotifyLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.ThirdPartyLoginNotify(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@ -0,0 +1,86 @@
package logic
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"context"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ThirdPartyLoginNotifyLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewThirdPartyLoginNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ThirdPartyLoginNotifyLogic {
return &ThirdPartyLoginNotifyLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *ThirdPartyLoginNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *ThirdPartyLoginNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *ThirdPartyLoginNotifyLogic) ThirdPartyLoginNotify(req *types.ThirdPartyLoginNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param: time is invalid")
}
if req.Info.WebsocketId <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:websocket_id is required")
}
if req.Info.Token == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:token is required")
}
//验证签名 sha256
notifyByte, _ := json.Marshal(req.Info)
h := sha256.New()
h.Write([]byte(fmt.Sprintf(constants.THIRD_PARTY_LOGIN_NOTIFY_SIGN_KEY, string(notifyByte), req.Time)))
signHex := h.Sum(nil)
sign := hex.EncodeToString(signHex)
//fmt.Println(sign)
if req.Sign != sign {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign")
}
//查询对应websocket连接
val, ok := mapConnPool.Load(req.Info.WebsocketId)
if !ok {
return resp.SetStatusWithMessage(basic.CodeOK, "success:websocket connection is not exists")
}
ws, ok := val.(wsConnectItem)
if !ok {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "type of websocket connect object is err")
}
data := types.DataTransferData{
T: constants.WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY,
D: types.ThirdPartyLoginRspMsg{
Token: req.Info.Token,
},
}
b, _ := json.Marshal(data)
select {
case <-ws.closeChan:
return resp.SetStatusWithMessage(basic.CodeOK, "websocket connect object is closed")
case ws.outChan <- b:
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
}

View File

@ -23,6 +23,10 @@ type RenderImageRspMsg struct {
Image string `json:"image"` //渲染后的图片 Image string `json:"image"` //渲染后的图片
} }
type ThirdPartyLoginRspMsg struct {
Token string `json:"token"`
}
type RenderNotifyReq struct { type RenderNotifyReq struct {
Sign string `json:"sign"` Sign string `json:"sign"`
Time int64 `json:"time"` Time int64 `json:"time"`
@ -36,6 +40,17 @@ type NotifyInfo struct {
Image string `json:"image"` Image string `json:"image"`
} }
type ThirdPartyLoginNotifyReq struct {
Sign string `json:"sign"`
Time int64 `json:"time"`
Info ThirdPartyLoginNotify `json:"info"`
}
type ThirdPartyLoginNotify struct {
WebsocketId uint64 `json:"websocket_id"`
Token string `json:"token"`
}
type Request struct { type Request struct {
} }

View File

@ -12,9 +12,12 @@ service websocket {
//websocket数据交互 //websocket数据交互
@handler DataTransferHandler @handler DataTransferHandler
get /api/websocket/data_transfer(request) returns (response); get /api/websocket/data_transfer(request) returns (response);
//渲染完了通知接口 //渲染完了通知接口
@handler RenderNotifyHandler @handler RenderNotifyHandler
post /api/websocket/render_notify(RenderNotifyReq) returns (response); post /api/websocket/render_notify(RenderNotifyReq) returns (response);
//第三方登录通知接口
@handler ThirdPartyLoginNotifyHandler
post /api/websocket/third_party_login_notify(ThirdPartyLoginNotifyReq) returns (response);
} }
//websocket数据交互 //websocket数据交互
@ -33,6 +36,9 @@ type RenderImageRspMsg { //websocket发送渲染完的数据
AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本
Image string `json:"image"` //渲染后的图片 Image string `json:"image"` //渲染后的图片
} }
type ThirdPartyLoginRspMsg { //websocket三方登录的通知数据
Token string `json:"token"`
}
//渲染完了通知接口 //渲染完了通知接口
type RenderNotifyReq { type RenderNotifyReq {
Sign string `json:"sign"` Sign string `json:"sign"`
@ -44,4 +50,14 @@ type NotifyInfo {
TemplateTagId int64 `json:"template_tag_id"` //模板标签id TemplateTagId int64 `json:"template_tag_id"` //模板标签id
AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本
Image string `json:"image"` Image string `json:"image"`
}
//第三方登录通知接口
type ThirdPartyLoginNotifyReq {
Sign string `json:"sign"`
Time int64 `json:"time"`
Info ThirdPartyLoginNotify `json:"info"`
}
type ThirdPartyLoginNotify {
WebsocketId uint64 `json:"websocket_id"`
Token string `json:"token"`
} }