最近的一些修改 #7
@@ -10,18 +10,22 @@ import cn.celess.blog.mapper.PartnerMapper;
|
|||||||
import cn.celess.blog.service.MailService;
|
import cn.celess.blog.service.MailService;
|
||||||
import cn.celess.blog.service.PartnerSiteService;
|
import cn.celess.blog.service.PartnerSiteService;
|
||||||
import cn.celess.blog.util.HttpUtil;
|
import cn.celess.blog.util.HttpUtil;
|
||||||
|
import cn.celess.blog.util.RedisUtil;
|
||||||
import cn.celess.blog.util.RegexUtil;
|
import cn.celess.blog.util.RegexUtil;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import com.github.pagehelper.PageInfo;
|
import com.github.pagehelper.PageInfo;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.mail.SimpleMailMessage;
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.validation.constraints.Email;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : xiaohai
|
* @author : xiaohai
|
||||||
@@ -33,6 +37,8 @@ public class PartnerSiteServiceImpl implements PartnerSiteService {
|
|||||||
PartnerMapper partnerMapper;
|
PartnerMapper partnerMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
MailService mailService;
|
MailService mailService;
|
||||||
|
@Autowired
|
||||||
|
RedisUtil redisUtil;
|
||||||
private static final String SITE_NAME = "小海博客";
|
private static final String SITE_NAME = "小海博客";
|
||||||
private static final String SITE_URL = "celess.cn";
|
private static final String SITE_URL = "celess.cn";
|
||||||
private static final String SITE_EMAIL = "a@celess.cn";
|
private static final String SITE_EMAIL = "a@celess.cn";
|
||||||
@@ -120,28 +126,36 @@ public class PartnerSiteServiceImpl implements PartnerSiteService {
|
|||||||
return all;
|
return all;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public PartnerSite apply(LinkApplyReq linkApplyReq) {
|
public PartnerSite apply(LinkApplyReq linkApplyReq) {
|
||||||
|
// 空值字段
|
||||||
if (StringUtils.isEmpty(linkApplyReq.getName())
|
if (StringUtils.isEmpty(linkApplyReq.getName())
|
||||||
|| StringUtils.isEmpty(linkApplyReq.getUrl())
|
|| StringUtils.isEmpty(linkApplyReq.getUrl())
|
||||||
|| StringUtils.isEmpty(linkApplyReq.getEmail())
|
|| StringUtils.isEmpty(linkApplyReq.getEmail())
|
||||||
|| StringUtils.isEmpty(linkApplyReq.getLinkUrl())) {
|
|| StringUtils.isEmpty(linkApplyReq.getLinkUrl())) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||||
}
|
}
|
||||||
if (RegexUtil.emailMatch(linkApplyReq.getEmail())) {
|
// 链接不合法
|
||||||
|
if (!RegexUtil.emailMatch(linkApplyReq.getEmail())) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_EMAIL_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_EMAIL_ERROR);
|
||||||
}
|
}
|
||||||
if (RegexUtil.urlMatch(linkApplyReq.getLinkUrl()) || RegexUtil.urlMatch(linkApplyReq.getUrl())
|
if (!RegexUtil.urlMatch(linkApplyReq.getLinkUrl()) || !RegexUtil.urlMatch(linkApplyReq.getUrl())) {
|
||||||
|| (!StringUtils.isEmpty(linkApplyReq.getIconPath()) && RegexUtil.urlMatch(linkApplyReq.getIconPath()))) {
|
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
||||||
}
|
}
|
||||||
|
if (!StringUtils.isEmpty(linkApplyReq.getIconPath()) && !RegexUtil.urlMatch(linkApplyReq.getIconPath())) {
|
||||||
|
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
||||||
|
}
|
||||||
|
// 非强制字段 设置空
|
||||||
if (StringUtils.isEmpty(linkApplyReq.getIconPath())) {
|
if (StringUtils.isEmpty(linkApplyReq.getIconPath())) {
|
||||||
linkApplyReq.setIconPath("");
|
linkApplyReq.setIconPath("");
|
||||||
}
|
}
|
||||||
|
// 抓取页面
|
||||||
String resp = HttpUtil.getAfterRendering(linkApplyReq.getLinkUrl());
|
String resp = HttpUtil.getAfterRendering(linkApplyReq.getLinkUrl());
|
||||||
assert resp != null;
|
assert resp != null;
|
||||||
if (resp.contains(SITE_URL)) {
|
|
||||||
PartnerSite ps = new PartnerSite();
|
PartnerSite ps = new PartnerSite();
|
||||||
|
if (resp.contains(SITE_URL)) {
|
||||||
|
//包含站点
|
||||||
BeanUtils.copyProperties(linkApplyReq, ps);
|
BeanUtils.copyProperties(linkApplyReq, ps);
|
||||||
ps.setOpen(false);
|
ps.setOpen(false);
|
||||||
partnerMapper.insert(ps);
|
partnerMapper.insert(ps);
|
||||||
@@ -152,8 +166,21 @@ public class PartnerSiteServiceImpl implements PartnerSiteService {
|
|||||||
smm.setSentDate(new Date());
|
smm.setSentDate(new Date());
|
||||||
mailService.send(smm);
|
mailService.send(smm);
|
||||||
} else {
|
} else {
|
||||||
throw new MyException(ResponseEnum.APPLY_LINK_NO_ADD_THIS_SITE);
|
// 不包含站点
|
||||||
|
String uuid;
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
if (redisUtil.hasKey(linkApplyReq.getUrl())) {
|
||||||
|
uuid = redisUtil.get(linkApplyReq.getUrl());
|
||||||
|
if (!redisUtil.hasKey(uuid)) {
|
||||||
|
redisUtil.setEx(uuid, mapper.writeValueAsString(linkApplyReq), 10, TimeUnit.MINUTES);
|
||||||
}
|
}
|
||||||
return null;
|
} else {
|
||||||
|
uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||||
|
redisUtil.setEx(uuid, mapper.writeValueAsString(linkApplyReq), 10, TimeUnit.MINUTES);
|
||||||
|
redisUtil.setEx(linkApplyReq.getUrl(), uuid, 10, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
throw new MyException(ResponseEnum.APPLY_LINK_NO_ADD_THIS_SITE, null, uuid);
|
||||||
|
}
|
||||||
|
return ps;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user