2023-07-25 09:10:50 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-08-07 06:16:09 +00:00
|
|
|
"context"
|
2023-08-07 04:31:31 +00:00
|
|
|
"encoding/json"
|
2023-07-26 03:53:06 +00:00
|
|
|
"fusenapi/constants"
|
2023-08-07 04:31:31 +00:00
|
|
|
"fusenapi/utils/auth"
|
2023-07-25 09:10:50 +00:00
|
|
|
"fusenapi/utils/basic"
|
2023-08-07 03:25:06 +00:00
|
|
|
"fusenapi/utils/websocket_data"
|
2023-07-25 09:10:50 +00:00
|
|
|
|
2023-08-07 04:31:31 +00:00
|
|
|
"fusenapi/server/render/internal/svc"
|
|
|
|
"fusenapi/server/render/internal/types"
|
2023-07-25 09:10:50 +00:00
|
|
|
|
|
|
|
"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-08-07 04:31:31 +00:00
|
|
|
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
2023-08-07 06:16:09 +00:00
|
|
|
/*if time.Now().Unix()-120 > req.Time || req.Time > time.Now().Unix() {
|
2023-07-27 04:32:03 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param time")
|
2023-08-07 06:16:09 +00:00
|
|
|
}*/
|
|
|
|
if req.Info.TaskId == "" {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id")
|
|
|
|
}
|
|
|
|
if req.Info.Image == "" {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image")
|
2023-07-26 03:53:06 +00:00
|
|
|
}
|
2023-08-09 06:12:58 +00:00
|
|
|
if req.Info.UserId == 0 && req.Info.GuestId == 0 {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid user_id or guest_id")
|
|
|
|
}
|
2023-08-07 06:16:09 +00:00
|
|
|
/* if req.Sign == "" {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param sign")
|
|
|
|
}*/
|
2023-07-26 03:53:06 +00:00
|
|
|
//验证签名 sha256
|
2023-07-27 09:33:50 +00:00
|
|
|
/*notifyByte, _ := json.Marshal(req.Info)
|
2023-07-26 03:53:06 +00:00
|
|
|
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 {
|
2023-07-26 09:48:51 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign")
|
2023-07-27 09:33:50 +00:00
|
|
|
}*/
|
2023-08-09 06:12:58 +00:00
|
|
|
//创建/更新资源
|
|
|
|
|
|
|
|
//发送消息到对应的rabbitmq
|
2023-08-07 04:31:31 +00:00
|
|
|
data := websocket_data.RenderImageNotify{
|
|
|
|
TaskId: req.Info.TaskId,
|
|
|
|
Image: req.Info.Image,
|
|
|
|
}
|
|
|
|
d, _ := json.Marshal(data)
|
|
|
|
if err := l.svcCtx.RabbitMq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatus(basic.CodeServiceErr, "failed to send data")
|
|
|
|
}
|
2023-07-25 09:10:50 +00:00
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|