Compare commits
10 Commits
9306091d3b
...
2d104b5211
Author | SHA1 | Date | |
---|---|---|---|
2d104b5211 | |||
a9f423e014 | |||
cdcec317b9 | |||
615b40e1b0 | |||
e0a720714a | |||
5846f5f22f | |||
f7d68b1dab | |||
811537d09c | |||
ce34857d8a | |||
fe2a9612c1 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -31,3 +31,4 @@ build/
|
|||||||
|
|
||||||
### VS Code ###
|
### VS Code ###
|
||||||
.vscode/
|
.vscode/
|
||||||
|
/src/test/
|
||||||
|
6
pom.xml
6
pom.xml
@ -9,9 +9,9 @@
|
|||||||
<relativePath/>
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>com.zayac</groupId>
|
<groupId>com.zayac</groupId>
|
||||||
<artifactId>changeURL</artifactId>
|
<artifactId>changeUrl</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-2023.9.9</version>
|
||||||
<name>changeURL</name>
|
<name>changeUrl</name>
|
||||||
<description>通过http请求修改指定模板内指定关键词的小工具</description>
|
<description>通过http请求修改指定模板内指定关键词的小工具</description>
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.zayac.changeurl.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zayac
|
||||||
|
* @since 2023-09-08 23:16
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented // 表示该注解会被文档工具生成文档
|
||||||
|
public @interface MsgAnnotation {
|
||||||
|
String startWith() default "";
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
29
src/main/java/com/zayac/changeurl/api/ChangeUrlApi.java
Normal file
29
src/main/java/com/zayac/changeurl/api/ChangeUrlApi.java
Normal file
@ -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<Boolean> changeUrl(String text) {
|
||||||
|
if (StrUtil.isBlank(text)) {
|
||||||
|
throw new BizException(400, "发送信息不能为空");
|
||||||
|
}
|
||||||
|
return Result.success(changeUrlService.change(text));
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/com/zayac/changeurl/common/Constant.java
Normal file
11
src/main/java/com/zayac/changeurl/common/Constant.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package com.zayac.changeurl.common;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zayac
|
||||||
|
* @since 2023-09-09 0:06
|
||||||
|
*/
|
||||||
|
public class Constant {
|
||||||
|
public static final String URL_PATTERN = "(https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6})(:[0-9]{1,5})?";
|
||||||
|
|
||||||
|
public static final String PREFER = "新";
|
||||||
|
}
|
55
src/main/java/com/zayac/changeurl/entity/HthMsgEntity.java
Normal file
55
src/main/java/com/zayac/changeurl/entity/HthMsgEntity.java
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package com.zayac.changeurl.entity;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zayac
|
||||||
|
* @since 2023-09-09 1:05
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class HthMsgEntity {
|
||||||
|
@MsgAnnotation(startWith = "web")
|
||||||
|
private String pc;
|
||||||
|
@MsgAnnotation(startWith = "h5")
|
||||||
|
private String h5;
|
||||||
|
@MsgAnnotation(startWith = "全站APP")
|
||||||
|
private String app;
|
||||||
|
@MsgAnnotation(startWith = "体育APP")
|
||||||
|
private String tyApp;
|
||||||
|
|
||||||
|
public HthMsgEntity(String[] array) {
|
||||||
|
for (Field field : this.getClass().getDeclaredFields()) {
|
||||||
|
MsgAnnotation msgAnnotation = field.getAnnotation(MsgAnnotation.class);
|
||||||
|
if (msgAnnotation != null) {
|
||||||
|
// 获取注解的startWith属性值
|
||||||
|
String startWith = msgAnnotation.startWith();
|
||||||
|
String val = "";
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
//如果以开头字符开头并且字符长度大于startWith,认为在一行 并且不包含新字
|
||||||
|
if (array[i].startsWith(startWith) && !array[i].contains(Constant.PREFER) && StrUtil.cleanBlank(array[i]).length() > startWith.length()) {
|
||||||
|
val = ReUtil.get(Constant.URL_PATTERN, array[i],0);
|
||||||
|
} else if (array[i].startsWith(startWith)) {
|
||||||
|
val = ReUtil.get(Constant.URL_PATTERN, array[i + 1],0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (StrUtil.isBlank(val)) {
|
||||||
|
throw new BizException(ErrorEnum.MSG_ERROR);
|
||||||
|
}
|
||||||
|
field.setAccessible(true);
|
||||||
|
try {
|
||||||
|
field.set(this, val);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.zayac.changeurl.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zayac
|
||||||
|
* @since 2023-09-09 10:57
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class HthTemplateEntity {
|
||||||
|
private String app1;
|
||||||
|
private String app2;
|
||||||
|
private String pc;
|
||||||
|
private String h5;
|
||||||
|
private String templatePath;
|
||||||
|
private List<String> targets;
|
||||||
|
|
||||||
|
public String getApp1() {
|
||||||
|
return convert(app1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getApp2() {
|
||||||
|
return convert(app2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPc() {
|
||||||
|
return convert(pc);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getH5() {
|
||||||
|
return convert(h5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String convert(String text) {
|
||||||
|
return "{{" + text + "}}";
|
||||||
|
}
|
||||||
|
}
|
79
src/main/java/com/zayac/changeurl/entity/KyMsgEntity.java
Normal file
79
src/main/java/com/zayac/changeurl/entity/KyMsgEntity.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package com.zayac.changeurl.entity;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.hutool.core.util.ReUtil;
|
||||||
|
import com.zayac.changeurl.annotation.MsgAnnotation;
|
||||||
|
import com.zayac.changeurl.common.Constant;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zayac
|
||||||
|
* @since 2023-09-08 22:54
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class KyMsgEntity {
|
||||||
|
@MsgAnnotation(startWith = "WEB")
|
||||||
|
private List<String> pc;
|
||||||
|
@MsgAnnotation(startWith = "H5")
|
||||||
|
private List<String> h5;
|
||||||
|
@MsgAnnotation(startWith = "全站")
|
||||||
|
private List<String> app;
|
||||||
|
@MsgAnnotation(startWith = "体育")
|
||||||
|
private List<String> ty;
|
||||||
|
@MsgAnnotation(startWith = "WEB")
|
||||||
|
private String fljPc;
|
||||||
|
@MsgAnnotation(startWith = "H5")
|
||||||
|
private String fljH5;
|
||||||
|
|
||||||
|
public KyMsgEntity(String[] array) {
|
||||||
|
for (Field field : this.getClass().getDeclaredFields()) {
|
||||||
|
// 获取字段上的MsgAnnotation注解
|
||||||
|
MsgAnnotation msgAnnotation = field.getAnnotation(MsgAnnotation.class);
|
||||||
|
// 如果注解不为空,说明该字段使用了MsgAnnotation注解
|
||||||
|
if (msgAnnotation != null) {
|
||||||
|
// 获取注解的startWith属性值
|
||||||
|
String startWith = msgAnnotation.startWith();
|
||||||
|
// 创建一个列表,用于存储匹配的字符串
|
||||||
|
List<String> matched = new ArrayList<>();
|
||||||
|
// 遍历字符串数组,查找以startWith开头的字符串
|
||||||
|
for (String s : array) {
|
||||||
|
if (s.startsWith(startWith)) {
|
||||||
|
// 如果找到匹配的字符串,添加到列表中
|
||||||
|
matched.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置字段的访问权限为true,以便赋值操作
|
||||||
|
field.setAccessible(true);
|
||||||
|
try {
|
||||||
|
// 判断字段的类型是否是List 如果是List类型,直接将匹配的列表赋值给该字段
|
||||||
|
if (field.getType().isAssignableFrom(List.class)) {
|
||||||
|
//优先将包含新字的放到List第一位置
|
||||||
|
matched = matched.stream()
|
||||||
|
.sorted(Comparator.comparing(str -> !str.contains(Constant.PREFER)))
|
||||||
|
.map(x -> ReUtil.get(Constant.URL_PATTERN, x, 0))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
//断言 list中不含有null值,防止修改数据为空
|
||||||
|
Assert.noNullElements(ArrayUtil.toArray(matched, String.class));
|
||||||
|
field.set(this, matched);
|
||||||
|
} else {
|
||||||
|
// 如果不是List类型,判断匹配的列表是否为空
|
||||||
|
if (!matched.isEmpty()) {
|
||||||
|
// 如果不为空,取第一个元素赋值给该字段
|
||||||
|
field.set(this, ReUtil.get(Constant.URL_PATTERN, matched.get(0), 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.zayac.changeurl.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zayac
|
||||||
|
* @since 2023-09-09 10:57
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class KyTemplateEntity {
|
||||||
|
private String app1;
|
||||||
|
private String app2;
|
||||||
|
private String pc1;
|
||||||
|
private String pc2;
|
||||||
|
private String h51;
|
||||||
|
private String h52;
|
||||||
|
private String templatePath;
|
||||||
|
private List<String> targets;
|
||||||
|
|
||||||
|
public String getApp1() {
|
||||||
|
return convert(app1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getApp2() {
|
||||||
|
return convert(app2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPc1() {
|
||||||
|
return convert(pc1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPc2() {
|
||||||
|
return convert(pc2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getH51() {
|
||||||
|
return convert(h51);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getH52() {
|
||||||
|
return convert(h52);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String convert(String text) {
|
||||||
|
return "{{" + text + "}}";
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +0,0 @@
|
|||||||
package com.zayac.changeurl.entity;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zayac
|
|
||||||
* @since 2023-08-30 15:19
|
|
||||||
*/
|
|
||||||
public record Msg2JSEntity(String original, String target) { }
|
|
39
src/main/java/com/zayac/changeurl/enums/ErrorEnum.java
Normal file
39
src/main/java/com/zayac/changeurl/enums/ErrorEnum.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.zayac.changeurl.exception;
|
||||||
|
|
||||||
|
import com.zayac.changeurl.response.Result;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
@ResponseBody
|
||||||
|
public <T> Result<T> handleAllException(Exception e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
return Result.error(INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(BizException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public <T> Result<T> handleBizException(Exception e) {
|
||||||
|
BizException bizException = (BizException) e;
|
||||||
|
return Result.error(bizException.getCode(), bizException.getMsg());
|
||||||
|
}
|
||||||
|
}
|
45
src/main/java/com/zayac/changeurl/response/Result.java
Normal file
45
src/main/java/com/zayac/changeurl/response/Result.java
Normal file
@ -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<T> {
|
||||||
|
//状态码
|
||||||
|
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 <T> Result<T> success(T data) {
|
||||||
|
return new Result<>(200, "success", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> success(String msg, T data) {
|
||||||
|
return new Result<>(200, msg, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(ErrorEnum error) {
|
||||||
|
return new Result<>(error.getCode(), error.getMsg(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<T> error(int code, String msg) {
|
||||||
|
return new Result<>(code, msg, null);
|
||||||
|
}
|
||||||
|
}
|
@ -1,171 +0,0 @@
|
|||||||
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.entity.Msg2JSEntity;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zayac
|
|
||||||
* @since 2023-08-30 12:08
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class ChangeURLServiceImpl implements ChangeURLService {
|
|
||||||
|
|
||||||
public static final String URL_PATTERN = "(https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6})(:[0-9]{1,5})?";
|
|
||||||
private static final String PREFER = "新";
|
|
||||||
@Value("${js.path.targets}")
|
|
||||||
//开云唯一词
|
|
||||||
private String[] targets;
|
|
||||||
|
|
||||||
@Value("${js.path.template}")
|
|
||||||
//开云唯一词
|
|
||||||
private String templatePath;
|
|
||||||
|
|
||||||
@Value("${msg.ky.startWith.web}")
|
|
||||||
private String msgKyWeb;
|
|
||||||
|
|
||||||
@Value("${msg.ky.startWith.h5}")
|
|
||||||
private String msgKyH5;
|
|
||||||
|
|
||||||
@Value("${msg.ky.startWith.app}")
|
|
||||||
private String msgKyApp;
|
|
||||||
|
|
||||||
|
|
||||||
@Value("${msg.hth.startWith.web}")
|
|
||||||
private String msgHthWeb;
|
|
||||||
@Value("${msg.hth.startWith.h5}")
|
|
||||||
private String msgHthH5;
|
|
||||||
|
|
||||||
@Value("${msg.hth.startWith.app1}")
|
|
||||||
private String msgHthApp1;
|
|
||||||
|
|
||||||
@Value("${msg.hth.startWith.app2}")
|
|
||||||
private String msgHthApp2;
|
|
||||||
|
|
||||||
//js修改部分
|
|
||||||
@Value("${js.ky.kyweb1}")
|
|
||||||
private String jsKyWeb1;
|
|
||||||
|
|
||||||
@Value("${js.ky.kyweb2}")
|
|
||||||
private String jsKyWeb2;
|
|
||||||
|
|
||||||
@Value("${js.ky.kyh51}")
|
|
||||||
private String jsKyH51;
|
|
||||||
|
|
||||||
@Value("${js.ky.kyh52}")
|
|
||||||
private String jsKyH52;
|
|
||||||
|
|
||||||
@Value("${js.ky.kyApp1}")
|
|
||||||
private String jsKyApp1;
|
|
||||||
|
|
||||||
@Value("${js.ky.kyApp2}")
|
|
||||||
private String jsKyApp2;
|
|
||||||
|
|
||||||
@Value("${js.hth.hthweb}")
|
|
||||||
private String jsHthWeb;
|
|
||||||
|
|
||||||
@Value("${js.hth.hthh5}")
|
|
||||||
private String jsHthH5;
|
|
||||||
|
|
||||||
@Value("${js.hth.hthApp1}")
|
|
||||||
private String jsHthApp1;
|
|
||||||
|
|
||||||
@Value("${js.hth.hthApp2}")
|
|
||||||
private String jsHthApp2;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean change(String text) {
|
|
||||||
//如果传入参数为空,返回修改失败
|
|
||||||
if (StrUtil.isEmpty(text)) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
text = StrUtil.subBefore(text, "SEO防拦截域名", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
//处理传入参数
|
|
||||||
String[] strings = StrUtil.splitToArray(text, "\n");
|
|
||||||
|
|
||||||
List<String> msgKyWebList = getByStartWith(strings, msgKyWeb, URL_PATTERN, PREFER);
|
|
||||||
List<String> msgKyH5List = getByStartWith(strings, msgKyH5, URL_PATTERN, PREFER);
|
|
||||||
List<String> msgKyAppList = getByStartWith(strings, msgKyApp, URL_PATTERN, PREFER);
|
|
||||||
String template = loadTemplate(templatePath);
|
|
||||||
List<Msg2JSEntity> listEntity = toListEntity(msgKyWebList, msgKyH5List, msgKyAppList);
|
|
||||||
for (Msg2JSEntity msg2JSEntity : listEntity) {
|
|
||||||
template = template.replace(msg2JSEntity.original(), msg2JSEntity.target());
|
|
||||||
}
|
|
||||||
for (String target : targets) {
|
|
||||||
if (FileUtil.exist(target)) {
|
|
||||||
FileUtil.del(target);
|
|
||||||
} else {
|
|
||||||
FileUtil.newFile(target);
|
|
||||||
}
|
|
||||||
FileUtil.writeString(template, target, "UTF-8");
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从模板文件中读取文件
|
|
||||||
*
|
|
||||||
* @param path 文件路径
|
|
||||||
* @return js文件模板中行List
|
|
||||||
*/
|
|
||||||
@Cacheable("template")
|
|
||||||
public String loadTemplate(String path) {
|
|
||||||
if (FileUtil.exist(path)) {
|
|
||||||
return FileUtil.readUtf8String(path);
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException("模板文件不存在");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理传入飞机信息
|
|
||||||
*
|
|
||||||
* @param arr 字符数组
|
|
||||||
* @param startWith 开始标识
|
|
||||||
* @param pattern url正则字符
|
|
||||||
* @param prefer 优先
|
|
||||||
* @return List
|
|
||||||
*/
|
|
||||||
public List<String> getByStartWith(String[] arr, String startWith, String pattern, String prefer) {
|
|
||||||
List<String> list = Arrays.stream(arr)
|
|
||||||
.filter(item -> item.startsWith(startWith))
|
|
||||||
.map(x -> ReUtil.getGroup1(pattern, x))
|
|
||||||
.sorted(Comparator.comparing(str -> str.contains(prefer)))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
if (list.size() == 0) {
|
|
||||||
throw new RuntimeException("飞机信息处理错误");
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Msg2JSEntity> toListEntity(List<String> msgKyWebList,
|
|
||||||
List<String> msgKyH5List,
|
|
||||||
List<String> msgKyAppList) {
|
|
||||||
ArrayList<Msg2JSEntity> msg2JSEntities = new ArrayList<>();
|
|
||||||
msg2JSEntities.add(build(jsKyWeb1, msgKyWebList.get(0)));
|
|
||||||
msg2JSEntities.add(build(jsKyWeb2, msgKyWebList.get(1)));
|
|
||||||
msg2JSEntities.add(build(jsKyH51, msgKyH5List.get(0)));
|
|
||||||
msg2JSEntities.add(build(jsKyH52, msgKyH5List.get(1)));
|
|
||||||
msg2JSEntities.add(build(jsKyApp1, msgKyAppList.get(0)));
|
|
||||||
msg2JSEntities.add(build(jsKyApp2, msgKyAppList.get(1)));
|
|
||||||
return msg2JSEntities.stream().filter(Objects::nonNull).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Msg2JSEntity build(String js, String msg) {
|
|
||||||
if (StrUtil.isNotEmpty(js) && StrUtil.isNotEmpty(msg)) {
|
|
||||||
return new Msg2JSEntity("{{" + js + "}}", msg);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -4,6 +4,6 @@ package com.zayac.changeurl.service;
|
|||||||
* @author zayac
|
* @author zayac
|
||||||
* @since 2023-08-30 12:07
|
* @since 2023-08-30 12:07
|
||||||
*/
|
*/
|
||||||
public interface ChangeURLService {
|
public interface ChangeUrlService {
|
||||||
Boolean change(String text);
|
Boolean change(String text);
|
||||||
}
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
package com.zayac.changeurl.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.util.ReUtil;
|
||||||
|
import com.zayac.changeurl.common.Constant;
|
||||||
|
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 com.zayac.changeurl.exception.BizException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zayac
|
||||||
|
* @since 2023-08-30 12:08
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class ChangeUrlServiceImpl implements ChangeUrlService {
|
||||||
|
@Value("${path.ky.target}")
|
||||||
|
private String kyjs;
|
||||||
|
@Value("${path.hth.target}")
|
||||||
|
private String hthjs;
|
||||||
|
|
||||||
|
@Value("${path.ky.template}")
|
||||||
|
private String kyjsTemplate;
|
||||||
|
@Value("${path.hth.template}")
|
||||||
|
private String hthjsTemplate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean change(String text) {
|
||||||
|
log.info(text);
|
||||||
|
String[] arr = text.split("\n");
|
||||||
|
|
||||||
|
//传入text 包含SEO 专用域名 则为开云
|
||||||
|
if (text.contains("SEO 专用域名")) {
|
||||||
|
KyMsgEntity kyMsg = new KyMsgEntity(arr);
|
||||||
|
List<String> kyTargets = new ArrayList<>();
|
||||||
|
kyTargets.add(kyjs);
|
||||||
|
|
||||||
|
KyTemplateEntity kyTemplate =
|
||||||
|
new KyTemplateEntity(
|
||||||
|
"kyApp1",
|
||||||
|
"kyApp2",
|
||||||
|
"kyPc1",
|
||||||
|
"kyPc2",
|
||||||
|
"kyH51",
|
||||||
|
"kyH52",
|
||||||
|
kyjsTemplate,
|
||||||
|
kyTargets);
|
||||||
|
//读取模板文件
|
||||||
|
String template = loadTemplate(kyTemplate.getTemplatePath());
|
||||||
|
template = template
|
||||||
|
.replace(kyTemplate.getApp1(), kyMsg.getApp().get(0))
|
||||||
|
.replace(kyTemplate.getApp2(), kyMsg.getApp().get(1))
|
||||||
|
.replace(kyTemplate.getH51(), kyMsg.getH5().get(0))
|
||||||
|
.replace(kyTemplate.getH52(), kyMsg.getH5().get(1))
|
||||||
|
.replace(kyTemplate.getPc1(), kyMsg.getPc().get(0))
|
||||||
|
.replace(kyTemplate.getPc2(), kyMsg.getPc().get(1));
|
||||||
|
|
||||||
|
modifyFile(template, kyTemplate.getTargets());
|
||||||
|
//读取修改过的文件 校验修改成功失败
|
||||||
|
String result = FileUtil.readUtf8String(kyjs);
|
||||||
|
if (ReUtil.count(Constant.URL_PATTERN, result) < 6) {
|
||||||
|
throw new BizException(500, "开云链接更新失败,请手动检查文件");
|
||||||
|
}
|
||||||
|
log.info("开云js更新成功");
|
||||||
|
} else {
|
||||||
|
HthMsgEntity hthMsg = new HthMsgEntity(arr);
|
||||||
|
List<String> hthTargets = new ArrayList<>();
|
||||||
|
hthTargets.add(hthjs);
|
||||||
|
HthTemplateEntity hthTemplate = new HthTemplateEntity("hthApp", "hthtyApp", "hthPc", "hthH5", hthjsTemplate, hthTargets);
|
||||||
|
//读取模板文件
|
||||||
|
String template = loadTemplate(hthTemplate.getTemplatePath());
|
||||||
|
template = template
|
||||||
|
.replace(hthTemplate.getApp1(), hthMsg.getApp())
|
||||||
|
.replace(hthTemplate.getApp2(), hthMsg.getTyApp())
|
||||||
|
.replace(hthTemplate.getPc(), hthMsg.getPc())
|
||||||
|
.replace(hthTemplate.getH5(), hthMsg.getH5());
|
||||||
|
List<File> files = modifyFile(template, hthTemplate.getTargets());
|
||||||
|
//读取修改过的文件 校验修改成功失败
|
||||||
|
String result = FileUtil.readUtf8String(files.get(0));
|
||||||
|
//如果修改后的js文件中,
|
||||||
|
if (ReUtil.count(Constant.URL_PATTERN, result) < 4) {
|
||||||
|
throw new BizException(500, "华体会链接更新失败,请手动检查文件");
|
||||||
|
}
|
||||||
|
log.info("华体会js更新成功");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从模板文件中读取文件
|
||||||
|
*
|
||||||
|
* @param path 文件路径
|
||||||
|
* @return js文件模板中行List
|
||||||
|
*/
|
||||||
|
@Cacheable("template")
|
||||||
|
public String loadTemplate(String path) {
|
||||||
|
if (FileUtil.exist(path)) {
|
||||||
|
return FileUtil.readUtf8String(path);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("模板文件不存在");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<File> modifyFile(String text, List<String> paths) {
|
||||||
|
List<File> files = new ArrayList<>();
|
||||||
|
for (String target : paths) {
|
||||||
|
files.add(FileUtil.writeString(text, target, "UTF-8"));
|
||||||
|
}
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +0,0 @@
|
|||||||
package com.zayac.changeurl.util;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zayac
|
|
||||||
* @since 2023-08-30 13:12
|
|
||||||
*/
|
|
||||||
public class util {
|
|
||||||
}
|
|
16
src/main/resources/application-dev.yml
Normal file
16
src/main/resources/application-dev.yml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
path:
|
||||||
|
ky:
|
||||||
|
template: "ky.js.template"
|
||||||
|
target: "C:\\Users\\Administrator\\Desktop\\js\\ky.js"
|
||||||
|
hth:
|
||||||
|
template: "hth.js.template"
|
||||||
|
target: "C:\\Users\\Administrator\\Desktop\\js\\hth.js"
|
||||||
|
server:
|
||||||
|
port: 8888
|
||||||
|
spring:
|
||||||
|
cache:
|
||||||
|
type: CAFFEINE
|
||||||
|
caffeine:
|
||||||
|
spec: maximumSize=100,expireAfterWrite=10m
|
||||||
|
ip:
|
||||||
|
white: "127.0.0.1"
|
@ -1,38 +0,0 @@
|
|||||||
msg:
|
|
||||||
ky:
|
|
||||||
startWith:
|
|
||||||
web: "WEB"
|
|
||||||
h5: "H5"
|
|
||||||
app: "全站"
|
|
||||||
hth:
|
|
||||||
startWith:
|
|
||||||
web: ""
|
|
||||||
h5: ""
|
|
||||||
app1: ""
|
|
||||||
app2: ""
|
|
||||||
js:
|
|
||||||
ky:
|
|
||||||
kyweb1: "kyPc"
|
|
||||||
kyweb2: ""
|
|
||||||
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"
|
|
17
src/main/resources/application-prod.yml
Normal file
17
src/main/resources/application-prod.yml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
server:
|
||||||
|
port: 8359
|
||||||
|
spring:
|
||||||
|
cache:
|
||||||
|
type: CAFFEINE
|
||||||
|
caffeine:
|
||||||
|
spec: maximumSize=100,expireAfterWrite=10m
|
||||||
|
ip:
|
||||||
|
white: "127.0.0.1"
|
||||||
|
|
||||||
|
path:
|
||||||
|
ky:
|
||||||
|
template: "/www/wwwroot/cdn.static.cdcseo.com/template/ky.js.template"
|
||||||
|
target: "/www/wwwroot/cdn.static.cdcseo.com/js/ky.js"
|
||||||
|
hth:
|
||||||
|
template: "/www/wwwroot/cdn.static.cdcseo.com/template/hth.js.template"
|
||||||
|
target: "/www/wwwroot/cdn.static.cdcseo.com/js/hth.js"
|
@ -1,33 +1,3 @@
|
|||||||
|
|
||||||
msg:
|
|
||||||
ky:
|
|
||||||
startWith:
|
|
||||||
web: "WEB"
|
|
||||||
h5: "H5"
|
|
||||||
app: "全站"
|
|
||||||
hth:
|
|
||||||
startWith:
|
|
||||||
web: "web"
|
|
||||||
h5: "h5"
|
|
||||||
app1: "全站APP"
|
|
||||||
app2: "体育APP"
|
|
||||||
js:
|
|
||||||
ky:
|
|
||||||
kyweb1: "kyweb1"
|
|
||||||
kyweb2: "kyweb2"
|
|
||||||
kyh51: "kyh51"
|
|
||||||
kyh52: "kyh52"
|
|
||||||
kyApp1: "kyApp"
|
|
||||||
kyApp2: "kyApp2"
|
|
||||||
hth:
|
|
||||||
hthweb: "hthweb"
|
|
||||||
hthh5: "hthh5"
|
|
||||||
hthApp1: "hthApp1"
|
|
||||||
hthApp2: "hthApp2"
|
|
||||||
path:
|
|
||||||
template: "link.js.template"
|
|
||||||
targets: "link.js"
|
|
||||||
|
|
||||||
server:
|
server:
|
||||||
port: 8888
|
port: 8888
|
||||||
spring:
|
spring:
|
||||||
@ -36,4 +6,10 @@ spring:
|
|||||||
caffeine:
|
caffeine:
|
||||||
spec: maximumSize=100,expireAfterWrite=10m
|
spec: maximumSize=100,expireAfterWrite=10m
|
||||||
profiles:
|
profiles:
|
||||||
active: ky
|
active: "prod"
|
||||||
|
|
||||||
|
logging:
|
||||||
|
logback:
|
||||||
|
rollingpolicy:
|
||||||
|
max-file-size: 10MB
|
||||||
|
max-history: 10
|
12
src/main/resources/hth.js.template
Normal file
12
src/main/resources/hth.js.template
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
const hthCode = '3016341'
|
||||||
|
|
||||||
|
var hth_link = {
|
||||||
|
hthApp: '{{hthApp}}/?i_code='+hthCode,
|
||||||
|
hthtyApp: '{{hthtyApp}}/?i_code='+hthCode,
|
||||||
|
hthPc: '{{hthPc}}/register/?i_code='+hthCode,
|
||||||
|
hthH5: '{{hthH5}}/entry/register?i_code='+hthCode,
|
||||||
|
}
|
||||||
|
|
||||||
|
function visit_hth(key) {
|
||||||
|
window['open'](hth_link[key] )
|
||||||
|
}
|
0
src/main/resources/ky.js.template
Normal file
0
src/main/resources/ky.js.template
Normal file
@ -1,38 +0,0 @@
|
|||||||
let code = '97238304',
|
|
||||||
ybty_link = {
|
|
||||||
kyApp: '{{kyApp}}/?i_code=' + code,// 开云全站app
|
|
||||||
kyPc: '{{kyPc}}/register/?i_code=' + code,//·开云体育电脑端
|
|
||||||
kyH5: '{{kyH5}}/entry/register/?i_code=' + code,//·开云体育手机端
|
|
||||||
}
|
|
||||||
|
|
||||||
function ybty_visit(key) {
|
|
||||||
window.location.href = ybty_link[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
function ybty_visit_newopen(key) {
|
|
||||||
window.open(ybty_link[key])
|
|
||||||
}
|
|
||||||
|
|
||||||
function ybty_kf(url) {
|
|
||||||
window.open(url)
|
|
||||||
}
|
|
||||||
|
|
||||||
function getMyUrl(key) {
|
|
||||||
return ybty_link[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
function deviceYBRegister(code) {
|
|
||||||
if (window.innerWidth < 768) {
|
|
||||||
window.open(ybty_link['ybH5']);
|
|
||||||
} else {
|
|
||||||
window.open(ybty_link['ybPc']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function deviceLYRegister(code) {
|
|
||||||
if (window.innerWidth < 768) {
|
|
||||||
window.open(ybty_link['ybH55']);
|
|
||||||
} else {
|
|
||||||
window.open(ybty_link['ybPc']);
|
|
||||||
}
|
|
||||||
}
|
|
25
src/main/resources/util.js.template
Normal file
25
src/main/resources/util.js.template
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
function isMobile() {
|
||||||
|
let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
if (!isMobile()) {
|
||||||
|
let getBody = document.getElementsByTagName("body")[0];
|
||||||
|
window.stop ? window.stop() : document.execCommand("Stop");
|
||||||
|
console.log("404");
|
||||||
|
document.getElementsByTagName("title")[0].innerHTML = "404 Not Found";
|
||||||
|
document.oncontextmenu = function() {
|
||||||
|
event.returnValue = false;
|
||||||
|
}
|
||||||
|
document.onkeydown = function(e) {
|
||||||
|
var currKey = 0,
|
||||||
|
evt = e || window.event;
|
||||||
|
currKey = evt.keyCode || evt.which || evt.charCode;
|
||||||
|
if (currKey == 123) {
|
||||||
|
window.event.cancelBubble = true;
|
||||||
|
window.event.returnValue = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var wap_domain = "{{kyApp}}/?i_code=97238304";
|
||||||
|
location.href =wap_domain;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user