120 lines
4.7 KiB
Java
120 lines
4.7 KiB
Java
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.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 {
|
||
private static final String KYJS = "/www/wwwroot/cdn.static.cdcseo.com/js/ky.js";
|
||
private static final String HTHJS = "/www/wwwroot/cdn.static.cdcseo.com/js/hth.js";
|
||
|
||
@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",
|
||
"/www/wwwroot/cdn.static.cdcseo.com/template/ky.js.template",
|
||
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", "/www/wwwroot/cdn.static.cdcseo.com/template/ky.js.template/hth.js.template", 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) {
|
||
if (FileUtil.exist(target)) {
|
||
FileUtil.del(target);
|
||
} else {
|
||
File file = FileUtil.newFile(target);
|
||
files.add(FileUtil.writeString(text, file, "UTF-8"));
|
||
}
|
||
}
|
||
return files;
|
||
}
|
||
}
|