2023-07-25 09:10:50 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-07-26 03:53:06 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"fusenapi/constants"
|
2023-07-25 09:10:50 +00:00
|
|
|
"fusenapi/utils/basic"
|
2023-07-26 03:53:06 +00:00
|
|
|
"time"
|
2023-07-25 09:10:50 +00:00
|
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fusenapi/server/websocket/internal/svc"
|
|
|
|
"fusenapi/server/websocket/internal/types"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RenderNotifyLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RenderNotifyLogic {
|
|
|
|
return &RenderNotifyLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
// func (l *RenderNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// }
|
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
// func (l *RenderNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
// }
|
|
|
|
|
2023-07-26 03:53:06 +00:00
|
|
|
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basic.Response) {
|
|
|
|
if len(req.NotifyList) == 0 {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "notify list is empty")
|
|
|
|
}
|
|
|
|
if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "time is expire")
|
|
|
|
}
|
|
|
|
//验证签名 sha256
|
|
|
|
notifyByte, _ := json.Marshal(req.NotifyList)
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time)))
|
|
|
|
signHex := h.Sum(nil)
|
|
|
|
sign := hex.EncodeToString(signHex)
|
|
|
|
if req.Sign != sign {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid")
|
|
|
|
}
|
2023-07-26 03:53:57 +00:00
|
|
|
//遍历websocket链接把数据传进去
|
2023-07-26 03:53:06 +00:00
|
|
|
mapConnPool.Range(func(key, value any) bool {
|
2023-07-26 04:27:34 +00:00
|
|
|
//断言连接
|
2023-07-26 03:53:06 +00:00
|
|
|
ws, ok := value.(wsConnectItem)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-26 04:27:34 +00:00
|
|
|
dataList := make([][]byte, 0, len(req.NotifyList))
|
|
|
|
//遍历数据
|
|
|
|
for _, notifyItem := range req.NotifyList {
|
|
|
|
renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId)
|
2023-07-26 07:05:53 +00:00
|
|
|
//查询有无该渲染任务
|
|
|
|
_, ok = ws.renderImageTask[renderKey]
|
2023-07-26 04:27:34 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-26 08:47:53 +00:00
|
|
|
rspData := types.DataTransferData{
|
|
|
|
T: constants.WEBSOCKET_RENDER_IMAGE,
|
|
|
|
D: types.RenderImageRspMsg{
|
|
|
|
ProductId: notifyItem.ProductId,
|
|
|
|
SizeId: notifyItem.SizeId,
|
|
|
|
TemplateId: notifyItem.TemplateId,
|
|
|
|
Source: "我是渲染资源",
|
|
|
|
},
|
2023-07-26 04:27:34 +00:00
|
|
|
}
|
2023-07-26 08:47:53 +00:00
|
|
|
b, _ := json.Marshal(rspData)
|
2023-07-26 04:27:34 +00:00
|
|
|
dataList = append(dataList, b)
|
2023-07-26 07:01:59 +00:00
|
|
|
//删除对应的需要渲染的图片map
|
2023-07-26 07:05:53 +00:00
|
|
|
ws.renderImageTaskCtlChan <- renderImageControlChanItem{
|
2023-07-26 07:01:59 +00:00
|
|
|
Option: 0,
|
|
|
|
Key: renderKey,
|
|
|
|
}
|
2023-07-26 04:27:34 +00:00
|
|
|
}
|
|
|
|
//发送数据
|
|
|
|
ws.sendToOutChan(dataList)
|
2023-07-26 03:53:06 +00:00
|
|
|
return true
|
|
|
|
})
|
2023-07-25 09:10:50 +00:00
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|