zwhui 2 years ago
parent
commit
1eb8abd1b0

+ 33 - 6
midjourney/src/main/java/com/yhlxj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -138,6 +138,11 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             midjourneyUserSettings.setSettings(SETTINGS);
             midjourneyUserSettings.setPrompt(PROMPT);
             midjourneyUserSettings.setUserId(midjourneyUser.getId());
+            try {
+                midjourneyUserSettingsService.save(midjourneyUserSettings);
+            }catch (Exception e){
+                log.error("新增setting error:{}",e.getMessage());
+            }
         }
         midjourneyUser.setSettings(midjourneyUserSettings);
         return midjourneyUser;
@@ -667,7 +672,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             Long count = redisService.incr(queryKey + id, 1L);
             if (count%5 == 0) {
                 try {
-                    listByIds(mode,List.of(id)).forEach(json ->{
+                    listByIds(mode,List.of(id),null).forEach(json ->{
                         JSONObject jsons = JSONUtil.parseObj(json);
                         MidjourneyUserConversation conversation = JSONUtil.toBean(jsons, MidjourneyUserConversation.class);
                         if (StringUtil.isNotBlank(conversation.getImageUrl())) {
@@ -808,10 +813,31 @@ public class MidjourneyServiceImpl implements MidjourneyService {
         }
         return list;
     }
-    public JSONArray listByIds(Integer mode, List<Long> ids) throws Exception {
+    public JSONArray listByIds(Integer mode, List<Long> ids, Long apiChannelId) throws Exception {
         Map<String, Object> param = MapUtil.builder(new HashMap<String, Object>()).put("ids", ids).build();
-        HttpRequest request = HttpRequest.post((mode == 1 ? FAST_HOST : RELAX_HOST) + "/mj/task/list-by-condition");
-        request = mode == 1 ? request.header("Authorization", FAST_TOKEN) : request.header("mj-api-secret", RELAX_TOKEN);
+        String url = "";
+        String token = "";
+        if (mode == 1) {
+            MidjourneyApiChannel midjourneyApiChannel = midjourneyApiChannelMapper.selectOne(QueryWrapperUtils.buildWrapper((wrapper)->{
+                if(apiChannelId != null && apiChannelId != 0){
+                    wrapper.eq(MidjourneyApiChannel::getId,apiChannelId);
+                }else {
+                    wrapper.eq(MidjourneyApiChannel::getIsDefault, true);
+                }
+            }));
+            if (midjourneyApiChannel == null) {
+                throw BusinessRuntimeException.getInstance("FAST模式账号异常");
+            }
+
+            url = midjourneyApiChannel.getUrl();
+            token = midjourneyApiChannel.getToken();
+
+        } else if (mode == 2) {
+            url = RELAX_HOST;
+            token = RELAX_TOKEN;
+        }
+        HttpRequest request = HttpRequest.post(url + "/mj/task/list-by-condition");
+        request = mode == 1 ? request.header("Authorization", token) : request.header("mj-api-secret", token);
         String body = request.body(Jsons.toJson(param)).execute().body();
         log.info("listByIds body:{}", body);
         return JSONUtil.parseArray(body);
@@ -851,7 +877,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
         conversation.setUserId(jsons.getLong("state"));
         conversation.setTaskId(jsons.getLong("id"));
         JSONObject jsonObject = new JSONObject(conversation.getProperties());
-        if ("SUBMITTED".equals(conversation.getStatus())){
+        String content = jsonObject.getStr("messageContent");
+        if(StringUtils.isNotBlank(content)){
             jsonObject.set("messageContent","**"+conversation.getPrompt()+"** - <@mj>");
             conversation.setProperties(jsonObject.toString());
         }
@@ -1015,7 +1042,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
                 .eq(MidjourneyUserConversation::getStatus, "IN_PROGRESS").orderByAsc(MidjourneyUserConversation::getId).last("limit 10"));
         midjourneyUserConversations.forEach(dbConversation -> {
             try {
-                JSONArray jsonArray = listByIds(dbConversation.getMode(), List.of(dbConversation.getTaskId()));
+                JSONArray jsonArray = listByIds(dbConversation.getMode(), List.of(dbConversation.getTaskId()),dbConversation.getApiChannelId());
                 for (Object json : jsonArray) {
                     notifyHook(Jsons.toJson(json));
                 }

+ 1 - 10
midjourney/src/main/java/com/yhlxj/web/mirror/MidjourneyController.java

@@ -486,16 +486,7 @@ public class MidjourneyController {
     @GetMapping("/get/settings")
     public Result<MidjourneyUserSettings> getSettings(){
         MidjourneyUser user = getUser();
-
-        MidjourneyUserSettings midjourneyUserSettings = midjourneyUserSettingsService.getById(user.getSettings().getId());
-        if (midjourneyUserSettings == null){
-            midjourneyUserSettings = new MidjourneyUserSettings();
-            midjourneyUserSettings.setSettings(SETTINGS);
-            midjourneyUserSettings.setPrompt(PROMPT);
-            midjourneyUserSettings.setUserId(user.getId());
-            midjourneyUserSettingsService.save(midjourneyUserSettings);
-        }
-        return GatewayResponse.SUCCESS.newBuilder().toResult(midjourneyUserSettings);
+        return GatewayResponse.SUCCESS.newBuilder().toResult(midjourneyUserSettingsService.getById(user.getSettings().getId()));
     }
 
     /**