Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
momo
2023-08-23 15:10:31 +08:00
36 changed files with 320 additions and 78 deletions

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 LoginNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.LoginNotifyReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewLoginNotifyLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.LoginNotify(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

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 RegisterAccountNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RegisterAccountNotifyReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewRegisterAccountNotifyLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.RegisterAccountNotify(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

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

View File

@@ -1,5 +1,6 @@
package logic
//websocket连接
import (
"bytes"
"encoding/json"
@@ -323,7 +324,7 @@ func (w *wsConnectItem) sendToOutChan(data []byte) {
}
// 格式化为websocket标准返回格式
func (w *wsConnectItem) respondDataFormat(msgType string, data interface{}) []byte {
func (w *wsConnectItem) respondDataFormat(msgType constants.Websocket, data interface{}) []byte {
d := websocket_data.DataTransferData{
T: msgType,
D: data,

View File

@@ -0,0 +1,56 @@
package logic
//登录回调
import (
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"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.WebsocketConnId == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,连接标识不能为空")
}
value, ok := mapConnPool.Load(req.Data.WebsocketConnId)
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, req.Data.Info))
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)
// }

View File

@@ -0,0 +1,56 @@
package logic
//注册帐号回调
import (
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"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.WebsocketConnId == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,连接标识不能为空")
}
value, ok := mapConnPool.Load(req.Data.WebsocketConnId)
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, req.Data.Info))
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)
// }

View File

@@ -1,5 +1,6 @@
package logic
//云渲染回调
import (
"context"
"fusenapi/server/websocket/internal/svc"
@@ -37,11 +38,11 @@ func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Rend
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if req.TaskId == "" {
logx.Error("渲染回调参数错误:invalid param task_id")
logx.Error("渲染回调参数错误:任务标识")
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id")
}
if req.Image == "" {
logx.Error("渲染回调参数错误:invalid param image")
logx.Error("渲染回调参数错误:渲染结果图片数据")
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image")
}
//存base64打印测试

View File

@@ -1,5 +1,6 @@
package logic
//处理websocket云渲染任务数据
import (
"bytes"
"encoding/json"
@@ -249,7 +250,7 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ModelId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("无渲染设置信息,产品id:%d ,model_id:%d", info.RenderData.ProductId, *productTemplate.ModelId)))
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("无渲染设置信息,产品id:%d ,模板id%d,模型id:%d", info.RenderData.ProductId, productTemplate.Id, *productTemplate.ModelId)))
logx.Error("element info is not found,model_id = ", *productTemplate.ModelId)
return err
}

View File

@@ -1,5 +1,6 @@
package logic
//复用websocket连接标识
import (
"encoding/json"
"fmt"

View File

@@ -12,6 +12,28 @@ type RenderNotifyReq struct {
Image string `json:"image"`
}
type RegisterAccountNotifyReq struct {
Data RegisterAccountData `json:"data"`
Time int64 `json:"time,optional"` //utc时间戳(用于验证签名)
Sign string `json:"sign,optional"` //签名
}
type RegisterAccountData struct {
WebsocketConnId string `json:"websocket_conn_id"` //连接标识
Info map[string]interface{} `json:"info"`
}
type LoginNotifyReq struct {
Data LoginNotifyData `json:"data"`
Time int64 `json:"time,optional"` //utc时间戳(用于验证签名)
Sign string `json:"sign,optional"` //签名
}
type LoginNotifyData struct {
WebsocketConnId string `json:"websocket_conn_id"` //连接标识
Info map[string]interface{} `json:"info"`
}
type Request struct {
}
@@ -79,4 +101,4 @@ func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string,
newResp.Data = data[0]
}
return newResp
}
}