From f7d68b1dab80ed531fa7d85cdfbc1489117fbbdf Mon Sep 17 00:00:00 2001 From: zayac Date: Sat, 9 Sep 2023 19:45:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=EF=BC=8C=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E4=BA=86=E9=83=A8=E5=88=86=E7=B1=BB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../com/zayac/changeurl/api/ChangeURLApi.java | 23 ---------- .../com/zayac/changeurl/api/ChangeUrlApi.java | 29 ++++++++++++ .../zayac/changeurl/entity/HthMsgEntity.java | 7 ++- .../com/zayac/changeurl/enums/ErrorEnum.java | 39 ++++++++++++++++ .../changeurl/exception/BizException.java | 44 ++++++++++++++++++ .../exception/GlobalExceptionHandler.java | 29 ++++++++++++ .../com/zayac/changeurl/response/Result.java | 45 +++++++++++++++++++ ...eURLService.java => ChangeUrlService.java} | 2 +- ...iceImpl.java => ChangeUrlServiceImpl.java} | 30 +++++++------ src/main/resources/application-dev.yml | 38 ++++++++++++++++ src/main/resources/application-ky.yml | 2 +- src/main/resources/hth.js.template | 2 +- 13 files changed, 249 insertions(+), 42 deletions(-) delete mode 100644 src/main/java/com/zayac/changeurl/api/ChangeURLApi.java create mode 100644 src/main/java/com/zayac/changeurl/api/ChangeUrlApi.java create mode 100644 src/main/java/com/zayac/changeurl/enums/ErrorEnum.java create mode 100644 src/main/java/com/zayac/changeurl/exception/BizException.java create mode 100644 src/main/java/com/zayac/changeurl/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/zayac/changeurl/response/Result.java rename src/main/java/com/zayac/changeurl/service/{ChangeURLService.java => ChangeUrlService.java} (77%) rename src/main/java/com/zayac/changeurl/service/{ChangeURLServiceImpl.java => ChangeUrlServiceImpl.java} (82%) create mode 100644 src/main/resources/application-dev.yml diff --git a/.gitignore b/.gitignore index 549e00a..a2d7c92 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ build/ ### VS Code ### .vscode/ +/src/test/ diff --git a/src/main/java/com/zayac/changeurl/api/ChangeURLApi.java b/src/main/java/com/zayac/changeurl/api/ChangeURLApi.java deleted file mode 100644 index fff9aba..0000000 --- a/src/main/java/com/zayac/changeurl/api/ChangeURLApi.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.zayac.changeurl.api; - -import com.zayac.changeurl.service.ChangeURLService; -import jakarta.annotation.Resource; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * @author zayac - * @since 2023-08-30 12:07 - */ -@RestController -@RequestMapping("/api/v1") -public class ChangeURLApi { - @Resource - private ChangeURLService changeURLService; - - @GetMapping("/change") - public Boolean changeURL(String text) { - return changeURLService.change(text); - } -} diff --git a/src/main/java/com/zayac/changeurl/api/ChangeUrlApi.java b/src/main/java/com/zayac/changeurl/api/ChangeUrlApi.java new file mode 100644 index 0000000..653aa88 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/api/ChangeUrlApi.java @@ -0,0 +1,29 @@ +package com.zayac.changeurl.api; + +import cn.hutool.core.util.StrUtil; +import com.zayac.changeurl.exception.BizException; +import com.zayac.changeurl.response.Result; +import com.zayac.changeurl.service.ChangeUrlService; +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author zayac + * @since 2023-08-30 12:07 + */ +@RestController +@RequestMapping("/api/v1") +public class ChangeUrlApi { + @Resource + private ChangeUrlService changeUrlService; + + @GetMapping("/change") + public Result changeUrl(String text) { + if (StrUtil.isBlank(text)) { + throw new BizException(400, "发送信息不能为空"); + } + return Result.success(changeUrlService.change(text)); + } +} diff --git a/src/main/java/com/zayac/changeurl/entity/HthMsgEntity.java b/src/main/java/com/zayac/changeurl/entity/HthMsgEntity.java index fde3c36..82d9252 100644 --- a/src/main/java/com/zayac/changeurl/entity/HthMsgEntity.java +++ b/src/main/java/com/zayac/changeurl/entity/HthMsgEntity.java @@ -1,10 +1,11 @@ package com.zayac.changeurl.entity; -import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import com.zayac.changeurl.annotation.MsgAnnotation; import com.zayac.changeurl.common.Constant; +import com.zayac.changeurl.enums.ErrorEnum; +import com.zayac.changeurl.exception.BizException; import lombok.Data; import java.lang.reflect.Field; @@ -39,7 +40,9 @@ public class HthMsgEntity { val = ReUtil.getGroup1(Constant.URL_PATTERN, array[i + 1]); } } - Assert.notBlank(val); + if (StrUtil.isBlank(val)) { + throw new BizException(ErrorEnum.MSG_ERROR); + } field.setAccessible(true); try { field.set(this, val); diff --git a/src/main/java/com/zayac/changeurl/enums/ErrorEnum.java b/src/main/java/com/zayac/changeurl/enums/ErrorEnum.java new file mode 100644 index 0000000..61b84b1 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/enums/ErrorEnum.java @@ -0,0 +1,39 @@ +package com.zayac.changeurl.enums; + +/** + * @author zayac + * @since 2023-09-09 18:01 + */ +public enum ErrorEnum { + // 数据操作错误定义 + SUCCESS(200, "nice"), + NO_PERMISSION(403, "你没得权限"), + NO_AUTH(401, "你能不能先登录一下"), + MSG_ERROR(400,"飞机信息转换错误"), + NOT_FOUND(404, "未找到该资源!"), + INTERNAL_SERVER_ERROR(500, "服务器跑路了"), + ; + + /** + * 错误码 + */ + private final Integer code; + + /** + * 错误信息 + */ + private final String msg; + + ErrorEnum(Integer code, String msg) { + this.code = code; + this.msg = msg; + } + + public Integer getCode() { + return code; + } + + public String getMsg() { + return msg; + } +} diff --git a/src/main/java/com/zayac/changeurl/exception/BizException.java b/src/main/java/com/zayac/changeurl/exception/BizException.java new file mode 100644 index 0000000..3158c84 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/exception/BizException.java @@ -0,0 +1,44 @@ +package com.zayac.changeurl.exception; + +import com.zayac.changeurl.enums.ErrorEnum; + +import java.io.Serial; + +/** + * @author zayac + * @since 2023-09-09 17:44 + */ + +public class BizException extends RuntimeException{ + + @Serial + private static final long serialVersionUID = -6016636225858202925L; + /** + * 错误码 + */ + protected Integer code; + /** + * 错误信息 + */ + protected String msg; + + public BizException(Integer code, String msg) { + super(); + this.code = code; + this.msg = msg; + } + + public BizException(ErrorEnum error) { + super(); + this.code = error.getCode(); + this.msg = error.getMsg(); + } + + public Integer getCode() { + return code; + } + + public String getMsg() { + return msg; + } +} diff --git a/src/main/java/com/zayac/changeurl/exception/GlobalExceptionHandler.java b/src/main/java/com/zayac/changeurl/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..44fc0d1 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/exception/GlobalExceptionHandler.java @@ -0,0 +1,29 @@ +package com.zayac.changeurl.exception; + +import com.zayac.changeurl.response.Result; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import static com.zayac.changeurl.enums.ErrorEnum.INTERNAL_SERVER_ERROR; + +/** + * @author zayac + * @since 2023-09-09 17:36 + */ +@ControllerAdvice +public class GlobalExceptionHandler{ + + @ExceptionHandler(Exception.class) + @ResponseBody + public Result handleAllException(Exception e) { + return Result.error(INTERNAL_SERVER_ERROR); + } + + @ExceptionHandler(BizException.class) + @ResponseBody + public Result handleBizException(Exception e) { + BizException bizException = (BizException) e; + return Result.error(bizException.getCode(), bizException.getMsg()); + } +} \ No newline at end of file diff --git a/src/main/java/com/zayac/changeurl/response/Result.java b/src/main/java/com/zayac/changeurl/response/Result.java new file mode 100644 index 0000000..6255f15 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/response/Result.java @@ -0,0 +1,45 @@ +package com.zayac.changeurl.response; + +import com.zayac.changeurl.enums.ErrorEnum; +import lombok.Data; + +/** + * @author zayac + * @since 2023-09-09 17:59 + */ +@Data +public class Result { + //状态码 + private Integer code; + //提示信息 + private String msg; + //数据 + private T data; + + public Result() { + + } + + //自定义返回结果的构造方法 + public Result(Integer code, String msg, T data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public static Result success(T data) { + return new Result<>(200, "success", data); + } + + public static Result success(String msg, T data) { + return new Result<>(200, msg, data); + } + + public static Result error(ErrorEnum error) { + return new Result<>(error.getCode(), error.getMsg(), null); + } + + public static Result error(int code, String msg) { + return new Result<>(code, msg, null); + } +} diff --git a/src/main/java/com/zayac/changeurl/service/ChangeURLService.java b/src/main/java/com/zayac/changeurl/service/ChangeUrlService.java similarity index 77% rename from src/main/java/com/zayac/changeurl/service/ChangeURLService.java rename to src/main/java/com/zayac/changeurl/service/ChangeUrlService.java index 7723ded..dc9b170 100644 --- a/src/main/java/com/zayac/changeurl/service/ChangeURLService.java +++ b/src/main/java/com/zayac/changeurl/service/ChangeUrlService.java @@ -4,6 +4,6 @@ package com.zayac.changeurl.service; * @author zayac * @since 2023-08-30 12:07 */ -public interface ChangeURLService { +public interface ChangeUrlService { Boolean change(String text); } diff --git a/src/main/java/com/zayac/changeurl/service/ChangeURLServiceImpl.java b/src/main/java/com/zayac/changeurl/service/ChangeUrlServiceImpl.java similarity index 82% rename from src/main/java/com/zayac/changeurl/service/ChangeURLServiceImpl.java rename to src/main/java/com/zayac/changeurl/service/ChangeUrlServiceImpl.java index 77de5d0..0f9c293 100644 --- a/src/main/java/com/zayac/changeurl/service/ChangeURLServiceImpl.java +++ b/src/main/java/com/zayac/changeurl/service/ChangeUrlServiceImpl.java @@ -2,13 +2,17 @@ package com.zayac.changeurl.service; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.ReUtil; -import cn.hutool.core.util.StrUtil; import com.zayac.changeurl.common.Constant; -import com.zayac.changeurl.entity.*; +import com.zayac.changeurl.entity.HthMsgEntity; +import com.zayac.changeurl.entity.HthTemplateEntity; +import com.zayac.changeurl.entity.KyMsgEntity; +import com.zayac.changeurl.entity.KyTemplateEntity; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; -import java.util.*; +import java.io.File; +import java.util.ArrayList; +import java.util.List; /** @@ -16,16 +20,13 @@ import java.util.*; * @since 2023-08-30 12:08 */ @Service -public class ChangeURLServiceImpl implements ChangeURLService { +public class ChangeUrlServiceImpl implements ChangeUrlService { private static final String KYJS = "ky.js"; private static final String HTHJS = "hth.js"; @Override public Boolean change(String text) { - //如果传入参数为空,返回修改失败 - if (StrUtil.isEmpty(text)) { - return false; - } + String[] arr = text.split("\n"); //传入text 包含SEO 专用域名 则为开云 @@ -71,12 +72,11 @@ public class ChangeURLServiceImpl implements ChangeURLService { .replace(hthTemplate.getApp2(), hthMsg.getTyApp()) .replace(hthTemplate.getPc(), hthMsg.getPc()) .replace(hthTemplate.getH5(), hthMsg.getH5()); - modifyFile(template, hthTemplate.getTargets()); + List files = modifyFile(template, hthTemplate.getTargets()); //读取修改过的文件 校验修改成功失败 - String result = FileUtil.readUtf8String(HTHJS); + String result = FileUtil.readUtf8String(files.get(0)); //如果修改后的js文件中, return ReUtil.count(Constant.URL_PATTERN, result) >= 4; - } } @@ -96,14 +96,16 @@ public class ChangeURLServiceImpl implements ChangeURLService { } } - public void modifyFile(String text, List paths) { + public List modifyFile(String text, List paths) { + List files = new ArrayList<>(); for (String target : paths) { if (FileUtil.exist(target)) { FileUtil.del(target); } else { - FileUtil.newFile(target); + File file = FileUtil.newFile(target); + files.add(FileUtil.writeString(text, file, "UTF-8")); } - FileUtil.writeString(text, target, "UTF-8"); } + return files; } } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 0000000..16c0ce7 --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,38 @@ +msg: + ky: + startWith: + web: "WEB" + h5: "H5" + app: "全站" + hth: + startWith: + web: "" + h5: "" + app1: "" + app2: "" +js: + ky: + kyweb1: "kyPc1" + kyweb2: "kyPc2" + kyh51: "kyH5" + kyh52: "" + kyApp1: "kyApp" + kyApp2: "" + hth: + hthweb: "" + hthh5: "" + hthApp1: "" + hthApp2: "" + path: + template: "link.js.template" + targets: "link.js" + +server: + port: 8888 +spring: + cache: + type: CAFFEINE + caffeine: + spec: maximumSize=100,expireAfterWrite=10m +ip: + white: "127.0.0.1" \ No newline at end of file diff --git a/src/main/resources/application-ky.yml b/src/main/resources/application-ky.yml index 4fbba67..5336d45 100644 --- a/src/main/resources/application-ky.yml +++ b/src/main/resources/application-ky.yml @@ -6,4 +6,4 @@ spring: caffeine: spec: maximumSize=100,expireAfterWrite=10m ip: - white: "0.0.0.0" \ No newline at end of file + white: "127.0.0.1" \ No newline at end of file diff --git a/src/main/resources/hth.js.template b/src/main/resources/hth.js.template index 95f3ae3..e369bbb 100644 --- a/src/main/resources/hth.js.template +++ b/src/main/resources/hth.js.template @@ -1,4 +1,4 @@ -const hthCode = '3016341', +const hthCode = '3016341' var hth_link = { hthApp: '{{hthApp}}/?i_code='+hthCode,