|
|
@@ -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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|