diff --git a/api/handler/mj_handler.go b/api/handler/mj_handler.go index 56340af1..88221f3b 100644 --- a/api/handler/mj_handler.go +++ b/api/handler/mj_handler.go @@ -406,7 +406,7 @@ func (h *MidJourneyHandler) JobList(c *gin.Context) { func (h *MidJourneyHandler) getData(finish bool, userId uint, page int, pageSize int, publish bool) (error, []vo.MidJourneyJob) { session := h.DB.Session(&gorm.Session{}) if finish { - session = session.Where("progress = ?", 100).Order("id DESC") + session = session.Where("progress >= ?", 100).Order("id DESC") } else { session = session.Where("progress < ?", 100).Order("id ASC") } diff --git a/api/service/mj/plus_client.go b/api/service/mj/plus_client.go index beb8943c..736c52b2 100644 --- a/api/service/mj/plus_client.go +++ b/api/service/mj/plus_client.go @@ -67,25 +67,7 @@ func (c *PlusClient) Imagine(task types.MjTask) (ImageRes, error) { } } - logger.Info("API URL: ", apiURL) - var res ImageRes - var errRes ErrRes - r, err := c.client.R(). - SetHeader("Authorization", "Bearer "+c.Config.ApiKey). - SetBody(body). - SetSuccessResult(&res). - SetErrorResult(&errRes). - Post(apiURL) - if err != nil { - return ImageRes{}, fmt.Errorf("请求 API %s 出错:%v", apiURL, err) - } - - if r.IsErrorState() { - errStr, _ := io.ReadAll(r.Body) - return ImageRes{}, fmt.Errorf("API 返回错误:%s,%v", errRes.Error.Message, string(errStr)) - } - - return res, nil + return c.doRequest(body, apiURL) } // Blend 融图 @@ -112,23 +94,7 @@ func (c *PlusClient) Blend(task types.MjTask) (ImageRes, error) { } } } - var res ImageRes - var errRes ErrRes - r, err := c.client.R(). - SetHeader("Authorization", "Bearer "+c.Config.ApiKey). - SetBody(body). - SetSuccessResult(&res). - SetErrorResult(&errRes). - Post(apiURL) - if err != nil { - return ImageRes{}, fmt.Errorf("请求 API %s 出错:%v", apiURL, err) - } - - if r.IsErrorState() { - return ImageRes{}, fmt.Errorf("API 返回错误:%s", errRes.Error.Message) - } - - return res, nil + return c.doRequest(body, apiURL) } // SwapFace 换脸 @@ -165,23 +131,7 @@ func (c *PlusClient) SwapFace(task types.MjTask) (ImageRes, error) { }, "state": "", } - var res ImageRes - var errRes ErrRes - r, err := c.client.SetTimeout(time.Minute).R(). - SetHeader("Authorization", "Bearer "+c.Config.ApiKey). - SetBody(body). - SetSuccessResult(&res). - SetErrorResult(&errRes). - Post(apiURL) - if err != nil { - return ImageRes{}, fmt.Errorf("请求 API %s 出错:%v", apiURL, err) - } - - if r.IsErrorState() { - return ImageRes{}, fmt.Errorf("API 返回错误:%s", errRes.Error.Message) - } - - return res, nil + return c.doRequest(body, apiURL) } // Upscale 放大指定的图片 @@ -195,24 +145,7 @@ func (c *PlusClient) Upscale(task types.MjTask) (ImageRes, error) { "taskId": task.MessageId, } apiURL := fmt.Sprintf("%s/mj-%s/mj/submit/action", c.apiURL, c.Config.Mode) - logger.Info("API URL: ", apiURL) - var res ImageRes - var errRes ErrRes - r, err := c.client.R(). - SetHeader("Authorization", "Bearer "+c.Config.ApiKey). - SetBody(body). - SetSuccessResult(&res). - SetErrorResult(&errRes). - Post(apiURL) - if err != nil { - return ImageRes{}, fmt.Errorf("请求 API 出错:%v", err) - } - - if r.IsErrorState() { - return ImageRes{}, fmt.Errorf("API 返回错误:%s", errRes.Error.Message) - } - - return res, nil + return c.doRequest(body, apiURL) } // Variation 以指定的图片的视角进行变换再创作,注意需要在对应的频道中关闭 Remix 变换,否则 Variation 指令将不会生效 @@ -226,9 +159,14 @@ func (c *PlusClient) Variation(task types.MjTask) (ImageRes, error) { "taskId": task.MessageId, } apiURL := fmt.Sprintf("%s/mj-%s/mj/submit/action", c.apiURL, c.Config.Mode) - logger.Info("API URL: ", apiURL) + + return c.doRequest(body, apiURL) +} + +func (c *PlusClient) doRequest(body interface{}, apiURL string) (ImageRes, error) { var res ImageRes var errRes ErrRes + logger.Info("API URL: ", apiURL) r, err := req.C().R(). SetHeader("Authorization", "Bearer "+c.Config.ApiKey). SetBody(body). @@ -236,7 +174,13 @@ func (c *PlusClient) Variation(task types.MjTask) (ImageRes, error) { SetErrorResult(&errRes). Post(apiURL) if err != nil { - return ImageRes{}, fmt.Errorf("请求 API 出错:%v", err) + errMsg := err.Error() + if r != nil { + errStr, _ := io.ReadAll(r.Body) + logger.Error("请求 API 出错:", string(errStr)) + errMsg = errMsg + " " + string(errStr) + } + return ImageRes{}, fmt.Errorf("请求 API 出错:%v", errMsg) } if r.IsErrorState() { diff --git a/api/service/mj/pool.go b/api/service/mj/pool.go index 7cf84a60..d6ab1f60 100644 --- a/api/service/mj/pool.go +++ b/api/service/mj/pool.go @@ -189,7 +189,7 @@ func (p *ServicePool) SyncTaskProgress() { for _, job := range jobs { // 失败或者 30 分钟还没完成的任务删除并退回算力 - if time.Now().Sub(job.CreatedAt) > time.Minute*30 || job.Progress == -1 { + if time.Now().Sub(job.CreatedAt) > time.Minute*30 { p.db.Delete(&job) // 退回算力 tx := p.db.Model(&model.User{}).Where("id = ?", job.UserId).UpdateColumn("power", gorm.Expr("power + ?", job.Power)) diff --git a/api/service/mj/service.go b/api/service/mj/service.go index baccd281..cba2b396 100644 --- a/api/service/mj/service.go +++ b/api/service/mj/service.go @@ -42,6 +42,8 @@ func NewService(name string, taskQueue *store.RedisQueue, notifyQueue *store.Red } } +const failedProgress = 101 + func (s *Service) Run() { logger.Infof("Starting MidJourney job consumer for %s", s.Name) for s.running { @@ -116,7 +118,7 @@ func (s *Service) Run() { } logger.Error("绘画任务执行失败:", errMsg) - job.Progress = -1 + job.Progress = failedProgress job.ErrMsg = errMsg // update the task progress s.db.Updates(&job) @@ -164,7 +166,7 @@ func (s *Service) Notify(job model.MidJourneyJob) error { // 任务执行失败了 if task.FailReason != "" { s.db.Model(&model.MidJourneyJob{Id: job.Id}).UpdateColumns(map[string]interface{}{ - "progress": -1, + "progress": failedProgress, "err_msg": task.FailReason, }) s.notifyQueue.RPush(sd.NotifyMessage{UserId: job.UserId, JobId: int(job.Id), Message: sd.Failed}) diff --git a/web/src/assets/css/image-mj.styl b/web/src/assets/css/image-mj.styl index dd4775a8..2f08175f 100644 --- a/web/src/assets/css/image-mj.styl +++ b/web/src/assets/css/image-mj.styl @@ -420,9 +420,31 @@ flex-flow column justify-content center align-items center - min-height 200px + min-height 220px color #ffffff + overflow hidden + .err-msg-container { + overflow hidden + word-break break-all + padding 0 10px 20px 10px + .title { + font-size 16px + text-align center + font-weight bold + color #f56c6c + margin-bottom 20px + } + + .text { + font-size 14px + color #E9F1F6 + line-height 1.5 + text-overflow ellipsis + height 100px + overflow hidden + } + } .iconfont { font-size 50px margin-bottom 10px diff --git a/web/src/components/ChatMidJourney.vue b/web/src/components/ChatMidJourney.vue deleted file mode 100644 index 8f3a5842..00000000 --- a/web/src/components/ChatMidJourney.vue +++ /dev/null @@ -1,244 +0,0 @@ - - - - - - - - - - - - - - - 正在加载图片... - - - - - - - - - - - - - - - - - U1 - U2 - U3 - U4 - - - - - - V1 - V2 - V3 - V4 - - - - - - {{ createdAt }} - tokens: {{ tokens }} - - - - - - - - - - \ No newline at end of file diff --git a/web/src/components/ChatPrompt.vue b/web/src/components/ChatPrompt.vue index 746a9907..0603c037 100644 --- a/web/src/components/ChatPrompt.vue +++ b/web/src/components/ChatPrompt.vue @@ -144,15 +144,26 @@ onMounted(() => { if (links) { httpPost("/api/upload/list", {urls: links}).then(res => { files.value = res.data + + for (let link of links) { + if (isExternalImg(link, files.value)) { + files.value.push({url:link, ext: ".png"}) + } + } }).catch(() => { }) for (let link of links) { content.value = content.value.replace(link,"") } + } content.value = md.render(content.value.trim()) }) + +const isExternalImg = (link, files) => { + return isImage(link) && !files.find(file => file.url === link) +}