chenbiao 2 жил өмнө
parent
commit
23d76f3c05

+ 159 - 0
midjourney/src/main/java/com/yhlxj/dao/model/dto/AlphaSubmitRequestDTO.java

@@ -0,0 +1,159 @@
+package com.yhlxj.dao.model.dto;
+
+import java.util.List;
+
+/**
+ * @author chan
+ * @date 2024/8/5 16:56
+ */
+public class AlphaSubmitRequestDTO {
+
+    private String prompt;
+    private List<String> prompts;
+    private Flags flags;
+    private String jobType;
+    private String id;
+    private int index;
+    private String roomId;
+    private String channelId;
+    private Metadata metadata;
+
+    // Getters and setters
+
+    public String getPrompt() {
+        return prompt;
+    }
+
+    public void setPrompt(String prompt) {
+        this.prompt = prompt;
+    }
+
+    public List<String> getPrompts() {
+        return prompts;
+    }
+
+    public void setPrompts(List<String> prompts) {
+        this.prompts = prompts;
+    }
+
+    public Flags getFlags() {
+        return flags;
+    }
+
+    public void setFlags(Flags flags) {
+        this.flags = flags;
+    }
+
+    public String getJobType() {
+        return jobType;
+    }
+
+    public void setJobType(String jobType) {
+        this.jobType = jobType;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public int getIndex() {
+        return index;
+    }
+
+    public void setIndex(int index) {
+        this.index = index;
+    }
+
+    public String getRoomId() {
+        return roomId;
+    }
+
+    public void setRoomId(String roomId) {
+        this.roomId = roomId;
+    }
+
+    public String getChannelId() {
+        return channelId;
+    }
+
+    public void setChannelId(String channelId) {
+        this.channelId = channelId;
+    }
+
+    public Metadata getMetadata() {
+        return metadata;
+    }
+
+    public void setMetadata(Metadata metadata) {
+        this.metadata = metadata;
+    }
+
+    public static class Flags {
+        private String mode;
+        private boolean isPrivate;
+
+        // Getters and setters
+
+        public String getMode() {
+            return mode;
+        }
+
+        public void setMode(String mode) {
+            this.mode = mode;
+        }
+
+        public boolean isPrivate() {
+            return isPrivate;
+        }
+
+        public void setPrivate(boolean isPrivate) {
+            this.isPrivate = isPrivate;
+        }
+    }
+
+    public static class Metadata {
+        private List<String> imagePrompts;
+        private List<String> imageReferences;
+        private List<String> characterReferences;
+        private boolean autoPrompt;
+
+        // Getters and setters
+
+        public List<String> getImagePrompts() {
+            return imagePrompts;
+        }
+
+        public void setImagePrompts(List<String> imagePrompts) {
+            this.imagePrompts = imagePrompts;
+        }
+
+        public List<String> getImageReferences() {
+            return imageReferences;
+        }
+
+        public void setImageReferences(List<String> imageReferences) {
+            this.imageReferences = imageReferences;
+        }
+
+        public List<String> getCharacterReferences() {
+            return characterReferences;
+        }
+
+        public void setCharacterReferences(List<String> characterReferences) {
+            this.characterReferences = characterReferences;
+        }
+
+        public boolean isAutoPrompt() {
+            return autoPrompt;
+        }
+
+        public void setAutoPrompt(boolean autoPrompt) {
+            this.autoPrompt = autoPrompt;
+        }
+    }
+
+}

+ 13 - 0
midjourney/src/main/java/com/yhlxj/dao/model/response/AlphaAuditLimitResponse.java

@@ -0,0 +1,13 @@
+package com.yhlxj.dao.model.response;
+
+import lombok.Data;
+
+
+/**
+ * @author chan
+ * @date 2024/8/5 17:01
+ */
+@Data
+public class AlphaAuditLimitResponse {
+    private String message;
+}

+ 2 - 0
midjourney/src/main/java/com/yhlxj/service/midjourney/MidjourneyService.java

@@ -46,4 +46,6 @@ public interface MidjourneyService {
     void syncMidjourneyStatus();
 
     void syncPicture();
+
+    Long checkUserLimit(MidjourneyUser user,Integer mode);
 }

+ 41 - 0
midjourney/src/main/java/com/yhlxj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -1390,6 +1390,47 @@ public class MidjourneyServiceImpl implements MidjourneyService {
        }
     }
 
+    @Override
+    public Long checkUserLimit(MidjourneyUser user,Integer mode){
+        Long num = 0L;
+        if (user.getExpireTime().before(new Date())){
+            throw BusinessRuntimeException.getInstance("账号已过期");
+        }
+        if (mode == 1){
+
+            num = redisService.decr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + user.getId(), 1L);
+
+            if (num < 0) {
+                redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + user.getId(), 1L);
+            }else {
+                int i = midjourneyUserMapper.decFastNum(user.getId(), 1, user.getMjFastNum());
+                if (i == 0) {
+                    redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + user.getId(), 1L);
+                    throw BusinessRuntimeException.getInstance("网络异常,请稍后再试");
+                }
+            }
+        }
+        if (mode == 2){
+            Object relax = redisService.get(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + user.getId());
+
+            if (relax != null){
+                num = redisService.decr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + user.getId(), 1L);
+                if (num < 0){
+                    redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + user.getId(), 1L);
+                }else {
+                    int i = midjourneyUserMapper.decRelaxNum(user.getId(), 1, user.getMjRelaxNum());
+                    if (i == 0) {
+                        redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + user.getId(), 1L);
+                        throw BusinessRuntimeException.getInstance("网络异常,请稍后再试");
+                    }
+                }
+            }else {
+                num = null;
+            }
+        }
+        return num;
+    }
+
     public static int extractNumberOrDefault(String input) {
        try {
            Pattern pattern = Pattern.compile("\\d+");

+ 0 - 1
midjourney/src/main/java/com/yhlxj/service/task/Scheduler.java

@@ -31,7 +31,6 @@ public class Scheduler {
         midjourneyAccountService.syncAccountByMJPlus();
     }
 
-
     @Scheduled(cron = "0 0/2 * * * ?")
     public void syncMidjourneyStatus() throws Exception {
         midjourneyService.syncMidjourneyStatus();

+ 133 - 0
midjourney/src/main/java/com/yhlxj/web/mirror/AlphaMidjourneyController.java

@@ -0,0 +1,133 @@
+package com.yhlxj.web.mirror;
+
+import com.cyksj.dto.Result;
+import com.cyksj.enums.GatewayResponse;
+import com.yhlxj.dao.model.dto.AlphaSubmitRequestDTO;
+import com.yhlxj.dao.model.entity.MidjourneyUser;
+import com.yhlxj.dao.model.response.AlphaAuditLimitResponse;
+import com.yhlxj.service.midjourney.MidjourneyService;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @author chan
+ * @date 2024/8/5 16:57
+ */
+@Slf4j
+@RequiredArgsConstructor
+@RestController
+@RequestMapping("/applets/alpha-midjourney")
+public class AlphaMidjourneyController {
+
+    private final HttpServletRequest request;
+
+    private final HttpServletResponse response;
+
+    private final MidjourneyService  midjourneyService;
+
+
+    private static final String AUTHORIZATION_HEADER = "Authorization";
+    private static final String MODE_HEADER = "MJ-Mode";
+    private static final String MODE_FAST = "fast";
+    private static final String MODE_RELAX = "relax";
+    private static final String MODE_TURBO = "turbo";
+
+    @RequestMapping("/auditLimit")
+    public AlphaAuditLimitResponse auditLimit(AlphaSubmitRequestDTO submitRequestDTO) {
+        String userToken = getUserTokenFromRequest();
+
+        if (userToken == null) {
+            return badRequestResponse("请重新至银河录像局登录");
+        }
+        log.info("userToken:{}", userToken);
+
+        MidjourneyUser user;
+        try {
+            user = midjourneyService.getUser(userToken);
+        } catch (Exception e) {
+            log.error("官网版登录异常 error:{}", e);
+            return badRequestResponse("请重新至银河录像局登录");
+        }
+
+        String mode = request.getHeader(MODE_HEADER);
+        log.info("mode:{}", mode);
+        if (StringUtils.isBlank(mode)) {
+            return badRequestResponse("模式错误.请选择fast 或者 relax模式");
+        }
+
+        if (MODE_TURBO.equals(mode)) {
+            return badRequestResponse("暂不支持turbo 模式.请选择fast 或者 relax模式");
+        }
+
+        if (MODE_FAST.equals(mode) && user.getMjFastNum() < 1) {
+            return badRequestResponse("您已经没有次数啦。模式:" + mode);
+        } else if (MODE_RELAX.equals(mode) && user.getMjRelaxNum() < 1) {
+            return badRequestResponse("您已经没有次数啦。模式:" + mode);
+        }
+
+        return new AlphaAuditLimitResponse();
+    }
+
+    private String getUserTokenFromRequest() {
+        String authorization = request.getHeader(AUTHORIZATION_HEADER);
+        return (authorization != null && authorization.length() > 7) ? authorization.substring(7) : null;
+    }
+
+    private AlphaAuditLimitResponse badRequestResponse(String message) {
+        AlphaAuditLimitResponse response = new AlphaAuditLimitResponse();
+        response.setMessage(message);
+        this.response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+        return response;
+    }
+
+    @RequestMapping("/notifyUrl")
+    public Result<String> notifyUrl() {
+        log.info("提交成功 通知扣次数");
+        String userToken = getUserTokenFromRequest();
+        String carid = request.getHeader("Carid");
+
+        String mode = request.getHeader(MODE_HEADER);
+        if (userToken == null) {
+            return GatewayResponse.SUCCESS.newBuilder().toResult();
+        }
+        log.info("token:{}, mode:{}", userToken, mode);
+        MidjourneyUser user = midjourneyService.getUser(userToken);
+
+
+        midjourneyService.checkUserLimit(user, MODE_RELAX.equals(mode) ? 2 : 1);
+        return GatewayResponse.SUCCESS.newBuilder().toResult();
+    }
+
+
+    /**
+     * GPT车票跳转登录
+     *
+     * @param userToken 用户token
+     * @param carId     车票id
+     * @return 登陆结果
+     */
+    @RequestMapping("/oauth")
+    public Result<String> gptOauth(@RequestParam("usertoken") String userToken, @RequestParam("carid") String carId) {
+        try {
+            log.info("用户通过userToken:{}访问镜像车队carId:{}", userToken, carId);
+            MidjourneyUser user = midjourneyService.getUser(userToken);
+            if (user == null) {
+                return GatewayResponse.SUCCESS.newBuilder().setMsg("用户不存在或已过期").setCode(0).toResult();
+            }
+            return GatewayResponse.SUCCESS.newBuilder().setMsg("登陆成功").setCode(1).toResult();
+        } catch (Exception e) {
+            log.error("服务器错误", e);
+            return GatewayResponse.FAIL.newBuilder().setMsg("服务器错误").setCode(0).toResult();
+        }
+    }
+
+
+
+}