Merge branch 'dev' into feature-trans2jackson

This commit is contained in:
禾几海
2020-08-15 15:33:04 +08:00
committed by GitHub
42 changed files with 5950 additions and 1145 deletions

View File

@@ -59,7 +59,7 @@ public class CommentServiceImpl implements CommentService {
Comment comment = new Comment();
comment.setFromUser(user);
User userTo = new User();
userTo.setId(-1L);
userTo.setId(null);
if (reqBody.getToUserId() != -1) {
userTo = userMapper.findById(reqBody.getToUserId());
comment.setToUser(userTo);

View File

@@ -3,18 +3,30 @@ package cn.celess.blog.service.serviceimpl;
import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.PartnerSite;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.request.LinkApplyReq;
import cn.celess.blog.entity.request.LinkReq;
import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.PartnerMapper;
import cn.celess.blog.service.MailService;
import cn.celess.blog.service.PartnerSiteService;
import cn.celess.blog.util.HttpUtil;
import cn.celess.blog.util.JwtUtil;
import cn.celess.blog.util.RedisUtil;
import cn.celess.blog.util.RegexUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @author : xiaohai
@@ -24,6 +36,13 @@ import java.util.List;
public class PartnerSiteServiceImpl implements PartnerSiteService {
@Autowired
PartnerMapper partnerMapper;
@Autowired
MailService mailService;
@Autowired
RedisUtil redisUtil;
private static final String SITE_NAME = "小海博客";
private static final String SITE_URL = "celess.cn";
private static final String SITE_EMAIL = "a@celess.cn";
@Override
public PartnerSite create(LinkReq reqBody) {
@@ -86,10 +105,21 @@ public class PartnerSiteServiceImpl implements PartnerSiteService {
if (!reqBody.getUrl().contains("http://") && !reqBody.getUrl().contains("https://")) {
reqBody.setUrl("http://" + reqBody.getUrl());
}
if (reqBody.isOpen() != partnerSite.getOpen() && !partnerSite.getNotification() && !StringUtils.isEmpty(partnerSite.getEmail())) {
SimpleMailMessage smm = new SimpleMailMessage();
smm.setTo(partnerSite.getEmail());
smm.setText("您的友链申请,已通过");
smm.setSubject("友链申请通过");
smm.setSentDate(new Date());
mailService.send(smm);
partnerSite.setNotification(true);
}
BeanUtils.copyProperties(reqBody, partnerSite);
partnerMapper.update(partnerSite);
partnerSite.setName(reqBody.getName());
partnerSite.setUrl(reqBody.getUrl());
partnerSite.setOpen(reqBody.isOpen());
partnerMapper.update(partnerSite);
return partnerSite;
}
@@ -108,5 +138,92 @@ public class PartnerSiteServiceImpl implements PartnerSiteService {
return all;
}
@SneakyThrows
@Override
public PartnerSite apply(LinkApplyReq linkApplyReq) {
// 空值字段
if (StringUtils.isEmpty(linkApplyReq.getName())
|| StringUtils.isEmpty(linkApplyReq.getUrl())
|| StringUtils.isEmpty(linkApplyReq.getEmail())
|| StringUtils.isEmpty(linkApplyReq.getLinkUrl())) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
// 链接不合法
if (!RegexUtil.emailMatch(linkApplyReq.getEmail())) {
throw new MyException(ResponseEnum.PARAMETERS_EMAIL_ERROR);
}
if (!RegexUtil.urlMatch(linkApplyReq.getLinkUrl()) || !RegexUtil.urlMatch(linkApplyReq.getUrl())) {
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())) {
linkApplyReq.setIconPath("");
}
// 抓取页面
String resp = HttpUtil.getAfterRendering(linkApplyReq.getLinkUrl());
if (resp == null) {
throw new MyException(ResponseEnum.CANNOT_GET_DATA);
}
PartnerSite ps = new PartnerSite();
if (resp.contains(SITE_URL)) {
//包含站点
BeanUtils.copyProperties(linkApplyReq, ps);
ps.setNotification(false);
ps.setOpen(false);
boolean exists = partnerMapper.existsByUrl(linkApplyReq.getUrl());
if (!exists) {
partnerMapper.insert(ps);
} else {
ps.setId(partnerMapper.findByUrl(linkApplyReq.getUrl()).getId());
}
SimpleMailMessage smm = new SimpleMailMessage();
smm.setSubject("友链申请");
smm.setText("有一条友链申请" + (exists ? ",已存在的友链链接" : "") + "[\n" + linkApplyReq.toString() + "\n]");
smm.setTo(SITE_EMAIL);
smm.setSentDate(new Date());
mailService.send(smm);
} else {
// 不包含站点
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);
}
} 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;
}
@SneakyThrows
@Override
public String reapply(String key) {
if (!redisUtil.hasKey(key)) {
throw new MyException(ResponseEnum.DATA_EXPIRED);
}
String s = redisUtil.get(key);
ObjectMapper mapper = new ObjectMapper();
LinkApplyReq linkApplyReq = mapper.readValue(s, LinkApplyReq.class);
if (linkApplyReq == null) {
throw new MyException(ResponseEnum.DATA_NOT_EXIST);
}
SimpleMailMessage smm = new SimpleMailMessage();
smm.setSubject("友链申请");
smm.setText("有一条未抓取到信息的友链申请,[\n" + linkApplyReq.toString() + "\n]");
smm.setTo(SITE_EMAIL);
smm.setSentDate(new Date());
mailService.send(smm);
redisUtil.delete(key);
redisUtil.delete(linkApplyReq.getUrl());
return "success";
}
}

View File

@@ -1,110 +1,111 @@
package cn.celess.blog.service.serviceimpl;
import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.WebUpdate;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.model.WebUpdateModel;
import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.WebUpdateInfoMapper;
import cn.celess.blog.service.WebUpdateInfoService;
import cn.celess.blog.util.DateFormatUtil;
import cn.celess.blog.util.HttpUtil;
import cn.celess.blog.util.ModalTrans;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
/**
* @author : xiaohai
* @date : 2019/05/12 11:43
*/
@Service
@Slf4j
public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
@Autowired
WebUpdateInfoMapper webUpdateInfoMapper;
@Override
public WebUpdateModel create(String info) {
if (info == null || info.replaceAll(" ", "").isEmpty()) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
WebUpdate webUpdate = new WebUpdate(info);
if (webUpdateInfoMapper.insert(webUpdate) == 0) {
throw new MyException(ResponseEnum.FAILURE);
}
return ModalTrans.webUpdate(webUpdateInfoMapper.findById(webUpdate.getId()));
}
@Override
public Boolean del(long id) {
if (!webUpdateInfoMapper.existsById(id)) {
throw new MyException(ResponseEnum.DATA_NOT_EXIST);
}
return webUpdateInfoMapper.delete(id) == 1;
}
@Override
public WebUpdateModel update(long id, String info) {
WebUpdate webUpdate = webUpdateInfoMapper.findById(id);
if (webUpdate == null) {
throw new MyException(ResponseEnum.DATA_NOT_EXIST);
}
if (info == null || info.replaceAll(" ", "").isEmpty()) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
webUpdate.setUpdateInfo(info);
webUpdateInfoMapper.update(id, info);
return ModalTrans.webUpdate(webUpdate);
}
@Override
public PageData<WebUpdateModel> pages(int count, int page) {
PageHelper.startPage(page, count);
List<WebUpdate> updateList = webUpdateInfoMapper.findAll();
return new PageData<WebUpdateModel>(new PageInfo<WebUpdate>(updateList), list2List(updateList));
}
@Override
public List<WebUpdateModel> findAll() {
List<WebUpdate> all = webUpdateInfoMapper.findAll();
return list2List(all);
}
@Override
public Map<String, Object> getLastestUpdateTime() {
Map<String, Object> map = new HashMap<>();
map.put("lastUpdateTime", DateFormatUtil.get(webUpdateInfoMapper.getLastestOne().getUpdateTime()));
map.put("lastUpdateInfo", webUpdateInfoMapper.getLastestOne().getUpdateInfo());
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(HttpUtil.get("https://api.github.com/repos/xiaohai2271/blog-frontEnd/commits?page=1&per_page=1"));
Iterator<JsonNode> elements = root.elements();
JsonNode next = elements.next();
JsonNode commit = next.get("commit");
map.put("lastCommit", commit.get("message"));
map.put("committerAuthor", commit.get("committer").get("name"));
Instant parse = Instant.parse(commit.get("committer").get("date").asText());
map.put("committerDate", DateFormatUtil.get(Date.from(parse)));
map.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + next.get("sha").asText());
} catch (IOException e) {
log.info("网络请求失败{}", e.getMessage());
}
return map;
}
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
webUpdates.forEach(update -> webUpdateModels.add(ModalTrans.webUpdate(update)));
return webUpdateModels;
}
}
package cn.celess.blog.service.serviceimpl;
import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.WebUpdate;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.model.WebUpdateModel;
import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.WebUpdateInfoMapper;
import cn.celess.blog.service.WebUpdateInfoService;
import cn.celess.blog.util.DateFormatUtil;
import cn.celess.blog.util.HttpUtil;
import cn.celess.blog.util.ModalTrans;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
/**
* @author : xiaohai
* @date : 2019/05/12 11:43
*/
@Service
@Slf4j
public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
@Autowired
WebUpdateInfoMapper webUpdateInfoMapper;
@Override
public WebUpdateModel create(String info) {
if (info == null || info.replaceAll(" ", "").isEmpty()) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
WebUpdate webUpdate = new WebUpdate(info);
if (webUpdateInfoMapper.insert(webUpdate) == 0) {
throw new MyException(ResponseEnum.FAILURE);
}
return ModalTrans.webUpdate(webUpdateInfoMapper.findById(webUpdate.getId()));
}
@Override
public Boolean del(long id) {
if (!webUpdateInfoMapper.existsById(id)) {
throw new MyException(ResponseEnum.DATA_NOT_EXIST);
}
return webUpdateInfoMapper.delete(id) == 1;
}
@Override
public WebUpdateModel update(long id, String info) {
WebUpdate webUpdate = webUpdateInfoMapper.findById(id);
if (webUpdate == null) {
throw new MyException(ResponseEnum.DATA_NOT_EXIST);
}
if (info == null || info.replaceAll(" ", "").isEmpty()) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
webUpdate.setUpdateInfo(info);
webUpdateInfoMapper.update(id, info);
return ModalTrans.webUpdate(webUpdate);
}
@Override
public PageData<WebUpdateModel> pages(int count, int page) {
PageHelper.startPage(page, count);
List<WebUpdate> updateList = webUpdateInfoMapper.findAll();
return new PageData<WebUpdateModel>(new PageInfo<WebUpdate>(updateList), list2List(updateList));
}
@Override
public List<WebUpdateModel> findAll() {
List<WebUpdate> all = webUpdateInfoMapper.findAll();
return list2List(all);
}
@Override
public Map<String, Object> getLastestUpdateTime() {
Map<String, Object> map = new HashMap<>();
map.put("lastUpdateTime", DateFormatUtil.get(webUpdateInfoMapper.getLastestOne().getUpdateTime()));
map.put("lastUpdateInfo", webUpdateInfoMapper.getLastestOne().getUpdateInfo());
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(HttpUtil.get("https://api.github.com/repos/xiaohai2271/blog-frontEnd/commits?page=1&per_page=1"));
Iterator<JsonNode> elements = root.elements();
JsonNode next = elements.next();
JsonNode commit = next.get("commit");
map.put("lastCommit", commit.get("message"));
map.put("committerAuthor", commit.get("committer").get("name"));
Instant parse = Instant.parse(commit.get("committer").get("date").asText());
map.put("committerDate", DateFormatUtil.get(Date.from(parse)));
map.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + next.get("sha").asText());
} catch (IOException e) {
log.info("网络请求失败{}", e.getMessage());
}
return map;
}
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
webUpdates.forEach(update -> webUpdateModels.add(ModalTrans.webUpdate(update)));
return webUpdateModels;
}
}