从"Blog"仓库中分离出来
This commit is contained in:
85
src/main/java/cn/celess/blog/service/ArticleService.java
Normal file
85
src/main/java/cn/celess/blog/service/ArticleService.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.model.ArticleModel;
|
||||
import cn.celess.blog.entity.request.ArticleReq;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 15:20
|
||||
*/
|
||||
@Service
|
||||
public interface ArticleService {
|
||||
/**
|
||||
* 新增一篇文章
|
||||
*
|
||||
* @param reqBody 请求文章的数据
|
||||
* @return 文章数据
|
||||
*/
|
||||
ArticleModel create(ArticleReq reqBody);
|
||||
|
||||
/**
|
||||
* 删除一篇文章
|
||||
*
|
||||
* @param articleID 文章id
|
||||
* @return 删除状态 true:删除成功 false:失败
|
||||
*/
|
||||
boolean delete(long articleID);
|
||||
|
||||
/**
|
||||
* 更新一篇文章
|
||||
*
|
||||
* @param reqBody 请求数据
|
||||
* @return 文章数据
|
||||
*/
|
||||
ArticleModel update(ArticleReq reqBody);
|
||||
|
||||
/**
|
||||
* 获取一篇文章的数据
|
||||
*
|
||||
* @param articleID 文章id
|
||||
* @param is4update 是否是因文章更新而请求数据
|
||||
* @return 文章数据
|
||||
*/
|
||||
ArticleModel retrieveOneByID(long articleID, boolean is4update);
|
||||
|
||||
/**
|
||||
* 管理员 获取分页数据
|
||||
*
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo adminArticles(int count, int page);
|
||||
|
||||
/**
|
||||
* 获取文章状态为开放的文章
|
||||
*
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo retrievePageForOpen(int count, int page);
|
||||
|
||||
/**
|
||||
* 根据分类名获取文章数据
|
||||
*
|
||||
* @param name 分类名
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo findByCategory(String name, int page, int count);
|
||||
|
||||
/**
|
||||
* 根据标签名获取文章数据
|
||||
*
|
||||
* @param name 标签名
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo findByTag(String name, int page, int count);
|
||||
}
|
||||
54
src/main/java/cn/celess/blog/service/CategoryService.java
Normal file
54
src/main/java/cn/celess/blog/service/CategoryService.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.Category;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 22:42
|
||||
*/
|
||||
@Service
|
||||
public interface CategoryService {
|
||||
/**
|
||||
* 增加一个分类
|
||||
*
|
||||
* @param name 分类名
|
||||
* @return 所增加的分类数据
|
||||
*/
|
||||
Category create(String name);
|
||||
|
||||
/**
|
||||
* 增加一个分类
|
||||
*
|
||||
* @param category 分类对象
|
||||
* @return 所增加的分类数据
|
||||
*/
|
||||
Category create(Category category);
|
||||
|
||||
/**
|
||||
* 通过id删除分类
|
||||
*
|
||||
* @param id 分类id
|
||||
* @return 删除状态
|
||||
*/
|
||||
boolean delete(long id);
|
||||
|
||||
/**
|
||||
* 编辑分类的名字
|
||||
*
|
||||
* @param id 分类id
|
||||
* @param name 分类名字
|
||||
* @return 更新后的分类的数据
|
||||
*/
|
||||
Category update(Long id, String name);
|
||||
|
||||
/**
|
||||
* 获取全部的分类数据
|
||||
*
|
||||
* @return 全部的分类数据
|
||||
*/
|
||||
List<Category> retrievePage();
|
||||
|
||||
}
|
||||
101
src/main/java/cn/celess/blog/service/CommentService.java
Normal file
101
src/main/java/cn/celess/blog/service/CommentService.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.model.CommentModel;
|
||||
import cn.celess.blog.entity.request.CommentReq;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/29 16:58
|
||||
*/
|
||||
@Service
|
||||
public interface CommentService {
|
||||
/**
|
||||
* 新增数据
|
||||
*
|
||||
* @param reqBody 请求数据体
|
||||
* @return 增加的comment数据
|
||||
*/
|
||||
CommentModel create(CommentReq reqBody);
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*
|
||||
* @param id comment的id
|
||||
* @return 删除状态
|
||||
*/
|
||||
boolean delete(long id);
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
*
|
||||
* @param reqBody comment请求体
|
||||
* @return 更新后的数据
|
||||
*/
|
||||
CommentModel update(CommentReq reqBody);
|
||||
|
||||
/**
|
||||
* 分页获取数据
|
||||
*
|
||||
* @param isComment true:评论 false:留言
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<CommentModel> retrievePage(Boolean isComment, int page, int count);
|
||||
|
||||
/**
|
||||
* 通过pid获取数据
|
||||
*
|
||||
* @param pid 父id
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<CommentModel> retrievePageByPid(long pid, int page, int count);
|
||||
|
||||
|
||||
/**
|
||||
* 根据评论者获取数据
|
||||
*
|
||||
* @param isComment true:评论 false:留言
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<CommentModel> retrievePageByAuthor(Boolean isComment, int page, int count);
|
||||
|
||||
/**
|
||||
* 根据文章获取数据
|
||||
*
|
||||
* @param articleID 文章id
|
||||
* @param pid 父id
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<CommentModel> retrievePageByArticle(long articleID, long pid, int page, int count);
|
||||
|
||||
/**
|
||||
* 根据数据的type和pid获取数据
|
||||
*
|
||||
* @param isComment true:评论 false:留言
|
||||
* @param pid 父id
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count);
|
||||
|
||||
/**
|
||||
* 根据type获取数据
|
||||
*
|
||||
* @param isComment true:评论 false:留言
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<CommentModel> retrievePageByType(Boolean isComment, int page, int count);
|
||||
|
||||
}
|
||||
66
src/main/java/cn/celess/blog/service/CountService.java
Normal file
66
src/main/java/cn/celess/blog/service/CountService.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/02 22:04
|
||||
*/
|
||||
@Service
|
||||
public interface CountService {
|
||||
/**
|
||||
* 获取评论的数据量
|
||||
*
|
||||
* @return 评论的数据量
|
||||
*/
|
||||
long getCommentCount();
|
||||
|
||||
/**
|
||||
* 获取文章的篇数
|
||||
*
|
||||
* @return 文章的篇数
|
||||
*/
|
||||
long getArticleCount();
|
||||
|
||||
/**
|
||||
* 获取分类数量
|
||||
*
|
||||
* @return 分类数量
|
||||
*/
|
||||
long getCategoriesCount();
|
||||
|
||||
/**
|
||||
* 获取标签数量
|
||||
*
|
||||
* @return 标签数量
|
||||
*/
|
||||
long getTagsCount();
|
||||
|
||||
/**
|
||||
* 获取留言数量
|
||||
*
|
||||
* @return 留言数量
|
||||
*/
|
||||
long getLeaveMessageCount();
|
||||
|
||||
/**
|
||||
* 获取用户量
|
||||
*
|
||||
* @return 用户量
|
||||
*/
|
||||
long getUserCount();
|
||||
|
||||
/**
|
||||
* 获取总访问量
|
||||
*
|
||||
* @return 总访问量
|
||||
*/
|
||||
long getVisitorCount();
|
||||
|
||||
/**
|
||||
* 获取日访问量
|
||||
*
|
||||
* @return 日访问量
|
||||
*/
|
||||
long getDayVisitCount();
|
||||
}
|
||||
27
src/main/java/cn/celess/blog/service/MailService.java
Normal file
27
src/main/java/cn/celess/blog/service/MailService.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/22 14:25
|
||||
*/
|
||||
@Service
|
||||
public interface MailService {
|
||||
/**
|
||||
* 异步发生邮件
|
||||
*
|
||||
* @param message SimpleMailMessage对象
|
||||
* @return //
|
||||
*/
|
||||
Boolean AsyncSend(SimpleMailMessage message);
|
||||
|
||||
/**
|
||||
* 同步发送邮件
|
||||
*
|
||||
* @param message SimpleMailMessage对象
|
||||
* @return 发送状态
|
||||
*/
|
||||
Boolean send(SimpleMailMessage message);
|
||||
}
|
||||
56
src/main/java/cn/celess/blog/service/PartnerSiteService.java
Normal file
56
src/main/java/cn/celess/blog/service/PartnerSiteService.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.PartnerSite;
|
||||
import cn.celess.blog.entity.request.LinkReq;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/05/12 11:42
|
||||
*/
|
||||
@Service
|
||||
public interface PartnerSiteService {
|
||||
/**
|
||||
* 新增数据
|
||||
*
|
||||
* @param reqBody 数据请求体
|
||||
* @return 新增数据
|
||||
*/
|
||||
PartnerSite create(LinkReq reqBody);
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*
|
||||
* @param id 数据id
|
||||
* @return 删除状态
|
||||
*/
|
||||
Boolean del(long id);
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
*
|
||||
* @param reqBody 数据请求体
|
||||
* @return 更新后的数据
|
||||
*/
|
||||
PartnerSite update(LinkReq reqBody);
|
||||
|
||||
/**
|
||||
* 分页获取数据
|
||||
*
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<PartnerSite> PartnerSitePages(int page, int count);
|
||||
|
||||
/**
|
||||
* 获取全部数据
|
||||
*
|
||||
* @return 全部友链数据
|
||||
*/
|
||||
List<PartnerSite> findAll();
|
||||
|
||||
}
|
||||
31
src/main/java/cn/celess/blog/service/QiniuService.java
Normal file
31
src/main/java/cn/celess/blog/service/QiniuService.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/25 18:15
|
||||
*/
|
||||
@Service
|
||||
public interface QiniuService {
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param is InputStream流
|
||||
* @param fileName 文件名
|
||||
* @return 响应数据
|
||||
*/
|
||||
QiniuResponse uploadFile(InputStream is, String fileName);
|
||||
|
||||
/**
|
||||
* 获取文件列表
|
||||
*
|
||||
* @return 文件列表
|
||||
*/
|
||||
FileInfo[] getFileList();
|
||||
|
||||
}
|
||||
82
src/main/java/cn/celess/blog/service/TagService.java
Normal file
82
src/main/java/cn/celess/blog/service/TagService.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.Tag;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 22:23
|
||||
*/
|
||||
@Service
|
||||
public interface TagService {
|
||||
/**
|
||||
* 新增数据
|
||||
*
|
||||
* @param name 标签名
|
||||
* @return 新增后的数据
|
||||
*/
|
||||
Tag create(String name);
|
||||
|
||||
/**
|
||||
* 新增数据
|
||||
*
|
||||
* @param tag tag对象
|
||||
* @return 新增后的数据
|
||||
*/
|
||||
|
||||
Tag create(Tag tag);
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*
|
||||
* @param tagId 标签id
|
||||
* @return 删除状态
|
||||
*/
|
||||
boolean delete(long tagId);
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
*
|
||||
* @param id 标签id
|
||||
* @param name 改名的name值
|
||||
* @return 更新后的数据
|
||||
*/
|
||||
Tag update(Long id, String name);
|
||||
|
||||
/**
|
||||
* 查询单个标签信息
|
||||
*
|
||||
* @param tagId id
|
||||
* @return 标签的数据
|
||||
*/
|
||||
Tag retrieveOneById(long tagId);
|
||||
|
||||
/**
|
||||
* 通过name查询标签的信息
|
||||
*
|
||||
* @param name tag的名称
|
||||
* @return 标签数据
|
||||
*/
|
||||
Tag retrieveOneByName(String name);
|
||||
|
||||
|
||||
/**
|
||||
* 分页获取标签数据
|
||||
*
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<Tag> retrievePage(int page, int count);
|
||||
|
||||
/**
|
||||
* 获取全部标签数据
|
||||
*
|
||||
* @return 标签数据列表
|
||||
*/
|
||||
List<Tag> findAll();
|
||||
|
||||
}
|
||||
177
src/main/java/cn/celess/blog/service/UserService.java
Normal file
177
src/main/java/cn/celess/blog/service/UserService.java
Normal file
@@ -0,0 +1,177 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.User;
|
||||
import cn.celess.blog.entity.model.UserModel;
|
||||
import cn.celess.blog.entity.request.LoginReq;
|
||||
import cn.celess.blog.entity.request.UserReq;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/30 18:40
|
||||
*/
|
||||
@Service
|
||||
public interface UserService {
|
||||
/**
|
||||
* 注册
|
||||
*
|
||||
* @param email 邮箱
|
||||
* @param password 密码
|
||||
* @return 注册状态
|
||||
*/
|
||||
Boolean registration(String email, String password);
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param loginReq 请求数据
|
||||
* @return 用户数据
|
||||
*/
|
||||
UserModel login(LoginReq loginReq);
|
||||
|
||||
/**
|
||||
* 注销登录
|
||||
*
|
||||
* @return **
|
||||
*/
|
||||
Object logout();
|
||||
|
||||
/**
|
||||
* 获取用户头像的链接
|
||||
*
|
||||
* @param id 用户id
|
||||
* @return 头像链接
|
||||
*/
|
||||
String getAvatarImg(long id);
|
||||
|
||||
/**
|
||||
* 更新用户数据
|
||||
*
|
||||
* @param desc 用户描述
|
||||
* @param displayName 显示昵称
|
||||
* @return 用户数据
|
||||
*/
|
||||
UserModel update(String desc, String displayName);
|
||||
|
||||
/**
|
||||
* 更新头像
|
||||
*
|
||||
* @param is 头像文件的输入流
|
||||
* @param mime 文件的mime
|
||||
* @return 响应数据
|
||||
*/
|
||||
Object updateUserAavatarImg(InputStream is, String mime);
|
||||
|
||||
/**
|
||||
* 获取session中存储的用户资料
|
||||
*
|
||||
* @return 用户资料
|
||||
*/
|
||||
UserModel getUserInfoBySession();
|
||||
|
||||
/**
|
||||
* 获取用户的角色
|
||||
*
|
||||
* @param email 用户的邮箱
|
||||
* @return role
|
||||
*/
|
||||
String getUserRoleByEmail(String email);
|
||||
|
||||
/**
|
||||
* 通过邮箱获取用户的信息
|
||||
*
|
||||
* @param email 用户邮箱
|
||||
* @return 用户信息
|
||||
*/
|
||||
User getUserInfoByEmail(String email);
|
||||
|
||||
/**
|
||||
* 获取邮箱是否注册过
|
||||
*
|
||||
* @param email 用户邮箱
|
||||
* @return 注册状态
|
||||
*/
|
||||
boolean isExistOfEmail(String email);
|
||||
|
||||
/**
|
||||
* 获取用户的name 优先返回displayName 否则返回email
|
||||
*
|
||||
* @param id 用户id
|
||||
* @return name
|
||||
*/
|
||||
String getNameById(long id);
|
||||
|
||||
/**
|
||||
* 发送重置密码邮件
|
||||
*
|
||||
* @param email 用户邮箱
|
||||
* @return 发送状态
|
||||
*/
|
||||
Object sendResetPwdEmail(String email);
|
||||
|
||||
/**
|
||||
* 发送验证邮箱邮件
|
||||
*
|
||||
* @param email 用户邮箱
|
||||
* @return 发送状态
|
||||
*/
|
||||
Object sendVerifyEmail(String email);
|
||||
|
||||
/**
|
||||
* 验证邮箱
|
||||
*
|
||||
* @param verifyId 验证码
|
||||
* @param email 邮箱
|
||||
* @return 验证状态
|
||||
*/
|
||||
Object verifyEmail(String verifyId, String email);
|
||||
|
||||
/**
|
||||
* 重置密码
|
||||
*
|
||||
* @param verifyId 验证码
|
||||
* @param email 邮箱
|
||||
* @param pwd 新密码
|
||||
* @return 修改状态
|
||||
*/
|
||||
Object reSetPwd(String verifyId, String email, String pwd);
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*
|
||||
* @param id 用户id的数组
|
||||
* @return 对应id 的删除状态
|
||||
*/
|
||||
Object deleteUser(Integer[] id);
|
||||
|
||||
/**
|
||||
* 获取用户列表
|
||||
*
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<UserModel> getUserList(Integer page, Integer count);
|
||||
|
||||
/**
|
||||
* 更改用户信息
|
||||
*
|
||||
* @param user 用户数据
|
||||
* @return 用户信息
|
||||
*/
|
||||
UserModel adminUpdate(UserReq user);
|
||||
|
||||
/**
|
||||
* 获取电子邮件的存在状态
|
||||
*
|
||||
* @param email email
|
||||
* @return true:存在 false:不存在
|
||||
*/
|
||||
boolean getStatusOfEmail(String email);
|
||||
}
|
||||
40
src/main/java/cn/celess/blog/service/VisitorService.java
Normal file
40
src/main/java/cn/celess/blog/service/VisitorService.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.model.VisitorModel;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/02 23:03
|
||||
*/
|
||||
@Service
|
||||
public interface VisitorService {
|
||||
/**
|
||||
* 分页获取访客数据
|
||||
*
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @param showLocation 是否显示位置信息 开启改选项数据响应超慢
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<VisitorModel> visitorPage(int page, int count, boolean showLocation);
|
||||
|
||||
/**
|
||||
* 新增访客
|
||||
*
|
||||
* @param request HttpServletRequest
|
||||
* @return 返回状态 null: 访客信息已记录、爬虫
|
||||
*/
|
||||
VisitorModel addVisitor(HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 获取位置信息
|
||||
*
|
||||
* @param ip ip地址
|
||||
* @return 位置信息
|
||||
*/
|
||||
String location(String ip);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package cn.celess.blog.service;
|
||||
|
||||
import cn.celess.blog.entity.model.WebUpdateModel;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/05/12 11:42
|
||||
*/
|
||||
@Service
|
||||
public interface WebUpdateInfoService {
|
||||
/**
|
||||
* 新增记录
|
||||
*
|
||||
* @param info 更新内容
|
||||
* @return 创建状态
|
||||
*/
|
||||
WebUpdateModel create(String info);
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*
|
||||
* @param id 数据id
|
||||
* @return 删除状态
|
||||
*/
|
||||
Boolean del(long id);
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
*
|
||||
* @param id 数据id
|
||||
* @param info 新内容
|
||||
* @return 数据
|
||||
*/
|
||||
WebUpdateModel update(long id, String info);
|
||||
|
||||
/**
|
||||
* 分页获取更新记录
|
||||
*
|
||||
* @param count 单页数据量
|
||||
* @param page 数据页
|
||||
* @return 分页数据
|
||||
*/
|
||||
PageInfo<WebUpdateModel> pages(int count, int page);
|
||||
|
||||
/**
|
||||
* 获取全部的更新记录
|
||||
*
|
||||
* @return 更新记录
|
||||
*/
|
||||
List<WebUpdateModel> findAll();
|
||||
|
||||
/**
|
||||
* 获取最后更新时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getLastestUpdateTime();
|
||||
}
|
||||
@@ -0,0 +1,544 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.LevelEnum;
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.*;
|
||||
import cn.celess.blog.entity.model.ArticleModel;
|
||||
import cn.celess.blog.entity.request.ArticleReq;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.mapper.ArticleMapper;
|
||||
import cn.celess.blog.mapper.CategoryMapper;
|
||||
import cn.celess.blog.mapper.CommentMapper;
|
||||
import cn.celess.blog.mapper.TagMapper;
|
||||
import cn.celess.blog.service.ArticleService;
|
||||
import cn.celess.blog.service.UserService;
|
||||
import cn.celess.blog.util.DateFormatUtil;
|
||||
import cn.celess.blog.util.RedisUserUtil;
|
||||
import cn.celess.blog.util.RegexUtil;
|
||||
import cn.celess.blog.util.StringFromHtmlUtil;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.youbenzi.mdtool.tool.MDTool;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 15:21
|
||||
*/
|
||||
@Service
|
||||
public class ArticleServiceImpl implements ArticleService {
|
||||
public static final Logger logger = LoggerFactory.getLogger(ArticleServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
@Autowired
|
||||
CommentMapper commentMapper;
|
||||
@Autowired
|
||||
UserService userService;
|
||||
@Autowired
|
||||
HttpServletRequest request;
|
||||
@Autowired
|
||||
RedisUserUtil redisUserUtil;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ArticleModel create(ArticleReq reqBody) {
|
||||
if (reqBody == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
//数据判断
|
||||
if (reqBody.getTitle() == null || reqBody.getTitle().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
} else if (reqBody.getMdContent() == null || reqBody.getMdContent().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
//转载 判断链接
|
||||
if (!reqBody.getType()) {
|
||||
if (reqBody.getUrl() == null || reqBody.getUrl().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
} else if (!RegexUtil.urlMatch(reqBody.getUrl())) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
||||
}
|
||||
}
|
||||
if (reqBody.getCategory() == null || reqBody.getCategory().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
if (reqBody.getTags() == null || reqBody.getTags().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
|
||||
|
||||
//写入数据库的数据
|
||||
Article article = new Article();
|
||||
article.setTitle(reqBody.getTitle());
|
||||
article.setOpen(reqBody.getOpen());
|
||||
article.setMdContent(reqBody.getMdContent());
|
||||
article.setUrl(reqBody.getUrl());
|
||||
article.setType(reqBody.getType());
|
||||
|
||||
article.setAuthorId(redisUserUtil.get(request).getId());
|
||||
article.setPublishDate(new Date());
|
||||
|
||||
//防止出现 “null,xxx”这种情况
|
||||
article.setTagsId("");
|
||||
|
||||
|
||||
//是否需要更新上一篇文章
|
||||
boolean isUpdatePreArticle = true;
|
||||
|
||||
Article preArticle = null;
|
||||
|
||||
|
||||
if (articleMapper.count() == 0) {
|
||||
isUpdatePreArticle = false;
|
||||
|
||||
|
||||
} else {
|
||||
//获取最新的一条数据
|
||||
preArticle = articleMapper.getLastestArticle();
|
||||
}
|
||||
|
||||
if (isUpdatePreArticle) {
|
||||
logger.info("上一篇文章的id为:" + preArticle.getId());
|
||||
//设置上一篇文章的id
|
||||
article.setPreArticleId(preArticle.getId());
|
||||
}
|
||||
|
||||
//markdown->html->summary
|
||||
String str = StringFromHtmlUtil.getString(MDTool.markdown2Html(article.getMdContent()));
|
||||
//获取摘要 摘要长度为255个字符
|
||||
String summary = str.length() > 240 ? str.substring(0, 240) + "......" : str;
|
||||
|
||||
//去除转换后存在的空格
|
||||
String tagStr = reqBody.getTags().replaceAll(" ", "");
|
||||
article.setSummary(summary);
|
||||
|
||||
if (articleMapper.existsByTitle(article.getTitle())) {
|
||||
throw new MyException(ResponseEnum.ARTICLE_HAS_EXIST);
|
||||
}
|
||||
|
||||
|
||||
//将分类写入数据库
|
||||
Category category1 = categoryMapper.findCategoryByName(reqBody.getCategory());
|
||||
if (category1 == null) {
|
||||
category1 = new Category();
|
||||
category1.setArticles("");
|
||||
category1.setName(reqBody.getCategory());
|
||||
categoryMapper.insert(category1);
|
||||
}
|
||||
|
||||
article.setCategoryId(category1.getId());
|
||||
|
||||
//文章存数据库
|
||||
articleMapper.insert(article);
|
||||
//获取新增的文章
|
||||
|
||||
if (isUpdatePreArticle) {
|
||||
//更新上一篇文章的“下一篇文章ID”
|
||||
articleMapper.updateNextArticleId(preArticle.getId(), article.getId());
|
||||
}
|
||||
|
||||
//无效
|
||||
// articleMapper.updatePreArticleId(article.getId(), preArticle == null ? -1 : preArticle.getId());
|
||||
article.setPreArticleId(preArticle == null ? -1 : preArticle.getId());
|
||||
|
||||
category1.setArticles(category1.getArticles() + article.getId() + ",");
|
||||
categoryMapper.update(category1);
|
||||
|
||||
|
||||
//将标签写入数据库
|
||||
for (String t : tagStr.split(",")) {
|
||||
if (t.replaceAll(" ", "").length() == 0) {
|
||||
//单个标签只含空格
|
||||
continue;
|
||||
}
|
||||
Tag tag = tagMapper.findTagByName(t);
|
||||
if (tag == null) {
|
||||
tag = new Tag();
|
||||
tag.setName(t);
|
||||
tag.setArticles("");
|
||||
tagMapper.insert(tag);
|
||||
}
|
||||
tag.setArticles(tag.getArticles() + article.getId() + ",");
|
||||
article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
||||
tagMapper.update(tag);
|
||||
}
|
||||
articleMapper.update(article);
|
||||
return fullTransform(articleMapper.getLastestArticle());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean delete(long articleID) {
|
||||
|
||||
Article articleForDel = articleMapper.findArticleById(articleID);
|
||||
|
||||
if (articleForDel == null) {
|
||||
throw new MyException(ResponseEnum.ARTICLE_NOT_EXIST);//文章不存在
|
||||
}
|
||||
|
||||
Article preArticle = articleMapper.findArticleById(articleForDel.getPreArticleId());
|
||||
Article nextArticle = articleMapper.findArticleById(articleForDel.getNextArticleId());
|
||||
|
||||
//对访问情况进行判断 非博主/非自己文章 拒绝访问
|
||||
User user = redisUserUtil.get(request);
|
||||
if (!user.getRole().contains("admin") && !articleForDel.getAuthorId().equals(user.getId())) {
|
||||
throw new MyException(ResponseEnum.PERMISSION_ERROR);
|
||||
}
|
||||
|
||||
//删除的文章处于中间位置
|
||||
if (nextArticle != null && preArticle != null) {
|
||||
|
||||
//修改上一篇文章的“下一篇文章”y
|
||||
articleMapper.updateNextArticleId(articleForDel.getPreArticleId(), articleForDel.getNextArticleId());
|
||||
|
||||
//修改下一篇文章的 “上一篇文章”
|
||||
articleMapper.updatePreArticleId(articleForDel.getNextArticleId(), articleForDel.getPreArticleId());
|
||||
}
|
||||
if (preArticle == null && nextArticle != null) {
|
||||
//删除的是第一篇文章
|
||||
articleMapper.updatePreArticleId(nextArticle.getId(), -1);
|
||||
}
|
||||
if (nextArticle == null && preArticle != null) {
|
||||
//删除的是最后一篇文章
|
||||
articleMapper.updateNextArticleId(preArticle.getId(), -1);
|
||||
}
|
||||
// delete count 为删除的数据数量
|
||||
int deleteCount = commentMapper.deleteByArticleId(articleID);
|
||||
|
||||
//删除标签中的文章id
|
||||
String tag = articleForDel.getTagsId();
|
||||
if (tag.length() > 0) {
|
||||
String[] tags = tag.split(",");
|
||||
for (String t : tags) {
|
||||
if (t != null) {
|
||||
//查询标签
|
||||
Tag tag1 = tagMapper.findTagById(Long.parseLong(t));
|
||||
//去除标签中的articleId中的待删除的文章id
|
||||
String s = tag1.getArticles().replaceAll(articleForDel.getId() + ",", "");
|
||||
tag1.setArticles(s);
|
||||
tagMapper.update(tag1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//删除分类中的文章id
|
||||
//获取文章的分类
|
||||
long categoryId = articleForDel.getCategoryId();
|
||||
Category category = categoryMapper.findCategoryById(categoryId);
|
||||
//删除文章id
|
||||
category.setArticles(category.getArticles().replaceAll(articleForDel.getId() + ",", ""));
|
||||
//更新
|
||||
categoryMapper.update(category);
|
||||
|
||||
//删除指定文章
|
||||
articleMapper.delete(articleID);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public ArticleModel update(ArticleReq reqBody) {
|
||||
if (reqBody == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
//数据判断
|
||||
if (reqBody.getTitle() == null || reqBody.getTitle().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
} else if (reqBody.getMdContent() == null || reqBody.getMdContent().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
//转载 判断链接
|
||||
if (!reqBody.getType()) {
|
||||
if (reqBody.getUrl() == null || reqBody.getUrl().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
} else if (!RegexUtil.urlMatch(reqBody.getUrl())) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
||||
}
|
||||
}
|
||||
if (reqBody.getCategory() == null || reqBody.getCategory().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
// 暂时不更新tags
|
||||
// if (reqBody.getTags() == null || reqBody.getTags().replaceAll(" ", "").isEmpty()) {
|
||||
// throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
// }
|
||||
|
||||
//写入数据库的数据
|
||||
Article article = new Article();
|
||||
if (reqBody.getId() == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR.getCode(), "id不能为空");
|
||||
}
|
||||
article.setId(reqBody.getId());
|
||||
article.setTitle(reqBody.getTitle());
|
||||
article.setOpen(reqBody.getOpen());
|
||||
article.setMdContent(reqBody.getMdContent());
|
||||
article.setUrl(reqBody.getUrl());
|
||||
article.setType(reqBody.getType());
|
||||
|
||||
|
||||
Article oldArticle = articleMapper.findArticleById(reqBody.getId());
|
||||
|
||||
Category category = categoryMapper.findCategoryById(oldArticle.getCategoryId());
|
||||
if (!(category.getName()).equals(reqBody.getCategory())) {
|
||||
//修改更新之前数据 的分类
|
||||
category.setArticles(category.getArticles().replace(reqBody.getId() + ",", ""));
|
||||
//更新
|
||||
categoryMapper.update(category);
|
||||
|
||||
//更新 更新之后的分类
|
||||
Category category1 = categoryMapper.findCategoryByName(reqBody.getCategory());
|
||||
if (category1 == null) {
|
||||
category1 = new Category();
|
||||
category1.setName(reqBody.getCategory());
|
||||
category1.setArticles(reqBody.getId() + ",");
|
||||
categoryMapper.insert(category1);
|
||||
}
|
||||
article.setCategoryId(category1.getId());
|
||||
} else {
|
||||
article.setCategoryId(oldArticle.getCategoryId());
|
||||
}
|
||||
|
||||
|
||||
// String[] newTags = reqBody.getTags().replaceAll(" ", "").split(",");
|
||||
|
||||
// //防止出现 ‘null2’这种情况
|
||||
// article.setTagsId("");
|
||||
// for (String t : newTags) {
|
||||
// Tag tag = tagMapper.findTagByName(t);
|
||||
// if (tag == null) {
|
||||
// tag = new Tag();
|
||||
// tag.setName(t);
|
||||
// tag.setArticles(oldArticle.getId() + ",");
|
||||
// tag = tagMapper.save(tag);
|
||||
// article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
||||
// continue;
|
||||
// }
|
||||
// article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
||||
// }
|
||||
|
||||
// TODO:::: tag的更新
|
||||
article.setTagsId(oldArticle.getTagsId());
|
||||
|
||||
|
||||
article.setUpdateDate(new Date());
|
||||
// TODO::::换用beansUtil
|
||||
// 设置不定参数
|
||||
article.setReadingNumber(oldArticle.getReadingNumber());
|
||||
article.setPublishDate(oldArticle.getPublishDate());
|
||||
article.setAuthorId(redisUserUtil.get(request).getId());
|
||||
article.setPreArticleId(oldArticle.getPreArticleId());
|
||||
article.setNextArticleId(oldArticle.getNextArticleId());
|
||||
String str = StringFromHtmlUtil.getString(MDTool.markdown2Html(article.getMdContent()));
|
||||
article.setSummary(str.length() > 240 ? str.substring(0, 240) + "......" : str);
|
||||
articleMapper.update(article);
|
||||
//更新完成移除
|
||||
request.getSession().removeAttribute("article4update");
|
||||
return fullTransform(article);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleModel retrieveOneByID(long articleID, boolean is4update) {
|
||||
Article article = articleMapper.findArticleById(articleID);
|
||||
if (article == null) {
|
||||
throw new MyException(ResponseEnum.ARTICLE_NOT_EXIST);
|
||||
}
|
||||
if (!article.getOpen()) {
|
||||
User user = redisUserUtil.getWithOutExc(request);
|
||||
if (user == null || "user".equals(user.getRole())) {
|
||||
throw new MyException(ResponseEnum.ARTICLE_NOT_PUBLIC);
|
||||
}
|
||||
}
|
||||
article.setReadingNumber(article.getReadingNumber() + 1);
|
||||
if (is4update) {
|
||||
//因更新而获取文章 不需要增加阅读量
|
||||
request.getSession().setAttribute("article4update", article);
|
||||
return fullTransform(article);
|
||||
}
|
||||
articleMapper.setReadingNumber(article.getReadingNumber() + 1, articleID);
|
||||
return fullTransform(article);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param count 数目
|
||||
* @param page 页面 默认减1
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PageInfo adminArticles(int count, int page) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Article> articleList = articleMapper.findAll();
|
||||
PageInfo pageInfo = new PageInfo(articleList);
|
||||
pageInfo.setList(list2list(articleList, LevelEnum.BETWEEN_M_AND_H));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo retrievePageForOpen(int count, int page) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Article> articleList = articleMapper.findAllByOpen(true);
|
||||
PageInfo pageInfo = new PageInfo(articleList);
|
||||
pageInfo.setList(list2list(articleList, LevelEnum.MIDDLE));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo findByCategory(String name, int page, int count) {
|
||||
Long idByName = categoryMapper.getIDByName(name);
|
||||
if (idByName == null) {
|
||||
throw new MyException(ResponseEnum.CATEGORY_NOT_EXIST);
|
||||
}
|
||||
PageHelper.startPage(page, count);
|
||||
PageInfo pageInfo = new PageInfo(articleMapper.getSimpleInfoByCategory(idByName));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo findByTag(String name, int page, int count) {
|
||||
Tag tag = tagMapper.findTagByName(name);
|
||||
if (tag == null) {
|
||||
throw new MyException(ResponseEnum.TAG_NOT_EXIST);
|
||||
}
|
||||
PageHelper.startPage(page, count);
|
||||
String[] split = tag.getArticles().split(",");
|
||||
List<String> list = Arrays.asList(split);
|
||||
List<Article> articleList = articleMapper.getSimpleInfoByTag(list);
|
||||
PageInfo pageInfo = new PageInfo(articleList);
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* page转换
|
||||
*
|
||||
* @param articleList 数据源
|
||||
* @param level 转换级别
|
||||
* @return list
|
||||
*/
|
||||
private List<ArticleModel> list2list(List<Article> articleList, LevelEnum level) {
|
||||
List<ArticleModel> content = new ArrayList<>();
|
||||
for (Article a : articleList) {
|
||||
ArticleModel model;
|
||||
switch (level.getLevelCode()) {
|
||||
case 0:
|
||||
model = simpleTransform(a);
|
||||
break;
|
||||
case 1:
|
||||
model = suitableTransform(a);
|
||||
break;
|
||||
case 2:
|
||||
model = suitableTransformForAdmin(a);
|
||||
break;
|
||||
case 3:
|
||||
default:
|
||||
model = fullTransform(a);
|
||||
}
|
||||
content.add(model);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的模型转换
|
||||
* [id,title,summary]
|
||||
*
|
||||
* @param a 源数据
|
||||
* @return 模型
|
||||
*/
|
||||
private ArticleModel simpleTransform(Article a) {
|
||||
ArticleModel model = new ArticleModel();
|
||||
model.setId(a.getId());
|
||||
model.setTitle(a.getTitle());
|
||||
model.setSummary(a.getSummary());
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 中等转换
|
||||
* [id,title,summary]
|
||||
* +
|
||||
* [original,tags,category]
|
||||
*
|
||||
* @param a
|
||||
* @return
|
||||
*/
|
||||
private ArticleModel suitableTransform(Article a) {
|
||||
ArticleModel model = simpleTransform(a);
|
||||
model.setAuthorName(userService.getNameById(a.getAuthorId()));
|
||||
model.setPublishDateFormat(DateFormatUtil.get(a.getPublishDate()));
|
||||
model.setOriginal(a.getType());
|
||||
model.setCategory(categoryMapper.getNameById(a.getCategoryId()));
|
||||
String[] split = a.getTagsId().split(",");
|
||||
String[] tags = new String[split.length];
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
if (split[i] == null || "".equals(split[i])) {
|
||||
continue;
|
||||
}
|
||||
tags[i] = tagMapper.getNameById(Long.parseLong(split[i]));
|
||||
}
|
||||
model.setTags(tags);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 中等转换 for admin页面
|
||||
* [id,title]
|
||||
* +
|
||||
* [original,UpdateDate,open,readingNumber]
|
||||
*
|
||||
* @param a
|
||||
* @return
|
||||
*/
|
||||
private ArticleModel suitableTransformForAdmin(Article a) {
|
||||
ArticleModel model = simpleTransform(a);
|
||||
model.setPublishDateFormat(DateFormatUtil.get(a.getPublishDate()));
|
||||
model.setUpdateDateFormat(DateFormatUtil.get(a.getUpdateDate()));
|
||||
model.setReadingNumber(a.getReadingNumber());
|
||||
model.setOpen(a.getOpen());
|
||||
model.setOriginal(a.getType());
|
||||
model.setSummary(null);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全转换
|
||||
* [id,title,summary,original,tags,category]
|
||||
* +
|
||||
* [UpdateDate,MdContent,NextArticleId,NextArticleTitle,preArticleId,preArticleTitle,open,url,readingNumber]
|
||||
*
|
||||
* @param a
|
||||
* @return
|
||||
*/
|
||||
private ArticleModel fullTransform(Article a) {
|
||||
ArticleModel model = suitableTransform(a);
|
||||
model.setUpdateDateFormat(DateFormatUtil.get(a.getUpdateDate()));
|
||||
model.setMdContent(a.getMdContent());
|
||||
model.setNextArticleId(a.getNextArticleId());
|
||||
model.setNextArticleTitle(a.getNextArticleId() == -1 ? "无" : articleMapper.getTitleById(a.getNextArticleId()));
|
||||
model.setPreArticleId(a.getPreArticleId());
|
||||
model.setPreArticleTitle(a.getPreArticleId() == -1 ? "无" : articleMapper.getTitleById(a.getPreArticleId()));
|
||||
model.setOpen(a.getOpen());
|
||||
model.setUrl(a.getUrl());
|
||||
model.setReadingNumber(a.getReadingNumber());
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.Article;
|
||||
import cn.celess.blog.entity.Category;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.mapper.ArticleMapper;
|
||||
import cn.celess.blog.mapper.CategoryMapper;
|
||||
import cn.celess.blog.service.CategoryService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 22:43
|
||||
*/
|
||||
@Service
|
||||
public class CategoryServiceImpl implements CategoryService {
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
@Autowired
|
||||
HttpServletRequest request;
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
|
||||
@Override
|
||||
public Category create(String name) {
|
||||
if (categoryMapper.existsByName(name)) {
|
||||
throw new MyException(ResponseEnum.CATEGORY_HAS_EXIST);
|
||||
}
|
||||
Category category = new Category();
|
||||
category.setName(name);
|
||||
category.setArticles("");
|
||||
categoryMapper.insert(category);
|
||||
return category;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Category create(Category category) {
|
||||
if (category == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
categoryMapper.insert(category);
|
||||
return category;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(long id) {
|
||||
Category category = categoryMapper.findCategoryById(id);
|
||||
|
||||
if (category == null) {
|
||||
throw new MyException(ResponseEnum.CATEGORY_NOT_EXIST);
|
||||
}
|
||||
String[] articleArray = category.getArticles().split(",");
|
||||
for (int i = 0; i < articleArray.length; i++) {
|
||||
if (articleArray[i] == null || "".equals(articleArray[i])) {
|
||||
continue;
|
||||
}
|
||||
long articleId = Long.parseLong(articleArray[i]);
|
||||
Article article = articleMapper.findArticleById(articleId);
|
||||
if (article == null) {
|
||||
continue;
|
||||
}
|
||||
article.setCategoryId(-1L);
|
||||
//一个 文章只对应一个分类,分类不存在则文章默认不可见
|
||||
article.setOpen(false);
|
||||
articleMapper.update(article);
|
||||
}
|
||||
return categoryMapper.delete(id) == 1;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Category update(Long id, String name) {
|
||||
if (id == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR.getCode(), "id不可为空");
|
||||
}
|
||||
Category category = categoryMapper.findCategoryById(id);
|
||||
category.setName(name);
|
||||
categoryMapper.update(category);
|
||||
return category;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Category> retrievePage() {
|
||||
return categoryMapper.findAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.Comment;
|
||||
import cn.celess.blog.entity.model.CommentModel;
|
||||
import cn.celess.blog.entity.request.CommentReq;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.mapper.ArticleMapper;
|
||||
import cn.celess.blog.mapper.CommentMapper;
|
||||
import cn.celess.blog.service.CommentService;
|
||||
import cn.celess.blog.service.UserService;
|
||||
import cn.celess.blog.util.DateFormatUtil;
|
||||
import cn.celess.blog.util.RedisUserUtil;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/29 17:05
|
||||
*/
|
||||
@Service
|
||||
public class CommentServiceImpl implements CommentService {
|
||||
@Autowired
|
||||
CommentMapper commentMapper;
|
||||
@Autowired
|
||||
UserService userService;
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
@Autowired
|
||||
HttpServletRequest request;
|
||||
@Autowired
|
||||
RedisUserUtil redisUserUtil;
|
||||
|
||||
@Override
|
||||
public CommentModel create(CommentReq reqBody) {
|
||||
|
||||
if (reqBody == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
long authorID = redisUserUtil.get(request).getId();
|
||||
Comment pComment = null;
|
||||
if (reqBody.getPid() != null && reqBody.getPid() != -1) {
|
||||
pComment = commentMapper.findCommentById(reqBody.getPid());
|
||||
}
|
||||
if (reqBody.getPid() == null) {
|
||||
reqBody.setPid(-1L);
|
||||
}
|
||||
//不是一级评论
|
||||
if (reqBody.getPid() != -1 && pComment == null) {
|
||||
//父评论不存在
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
Comment comment = new Comment();
|
||||
comment.setAuthorID(authorID);
|
||||
comment.setType(reqBody.getComment());
|
||||
if (reqBody.getComment()) {
|
||||
//若为评论
|
||||
if (reqBody.getArticleID() <= 0) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
comment.setArticleID(reqBody.getArticleID());
|
||||
} else {
|
||||
comment.setArticleID(-1L);
|
||||
}
|
||||
comment.setContent(reqBody.getContent());
|
||||
comment.setPid(reqBody.getPid());
|
||||
comment.setDate(new Date());
|
||||
comment.setResponseId("");
|
||||
commentMapper.insert(comment);
|
||||
if (reqBody.getPid() != -1) {
|
||||
commentMapper.updateResponder(pComment.getResponseId() + comment.getId() + ",", reqBody.getPid());
|
||||
}
|
||||
return trans(comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(long id) {
|
||||
boolean b = commentMapper.existsById(id);
|
||||
if (!b) {
|
||||
throw new MyException(ResponseEnum.COMMENT_NOT_EXIST);
|
||||
}
|
||||
commentMapper.delete(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommentModel update(CommentReq reqBody) {
|
||||
if (reqBody.getId() == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR.getCode(), "id不可为空");
|
||||
}
|
||||
Comment comment = commentMapper.findCommentById(reqBody.getId());
|
||||
if (!comment.getContent().equals(reqBody.getContent())) {
|
||||
commentMapper.updateContent(reqBody.getContent(), reqBody.getId());
|
||||
comment.setContent(reqBody.getContent());
|
||||
}
|
||||
if (!comment.getResponseId().equals(reqBody.getResponseId())) {
|
||||
commentMapper.updateResponder(reqBody.getResponseId(), reqBody.getId());
|
||||
comment.setResponseId(reqBody.getResponseId());
|
||||
}
|
||||
return trans(comment);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PageInfo<CommentModel> retrievePage(Boolean isComment, int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Comment> commentList = commentMapper.findAllByType(isComment);
|
||||
PageInfo pageInfo = new PageInfo(commentList);
|
||||
pageInfo.setList(list2List(commentList));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<CommentModel> retrievePageByPid(long pid, int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Comment> commentList = commentMapper.findAllByPId(pid);
|
||||
PageInfo pageInfo = new PageInfo(commentList);
|
||||
pageInfo.setList(list2List(commentList));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<CommentModel> retrievePageByArticle(long articleID, long pid, int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Comment> commentList = commentMapper.findAllByArticleIDAndPId(articleID, pid);
|
||||
PageInfo pageInfo = new PageInfo(commentList);
|
||||
pageInfo.setList(list2List(commentList));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Comment> commentList = commentMapper.findCommentsByTypeAndPId(isComment, pid);
|
||||
PageInfo pageInfo = new PageInfo(commentList);
|
||||
pageInfo.setList(list2List(commentList));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<CommentModel> retrievePageByAuthor(Boolean isComment, int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Comment> commentList = commentMapper.findAllByAuthorIDAndType(redisUserUtil.get(request).getId(), isComment);
|
||||
PageInfo pageInfo = new PageInfo(commentList);
|
||||
pageInfo.setList(list2List(commentList));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PageInfo<CommentModel> retrievePageByType(Boolean isComment, int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Comment> commentList = commentMapper.findAllByType(isComment);
|
||||
PageInfo pageInfo = new PageInfo(commentList);
|
||||
pageInfo.setList(list2List(commentList));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
private List<CommentModel> list2List(List<Comment> commentList) {
|
||||
List<CommentModel> content = new ArrayList<>();
|
||||
for (Comment c : commentList) {
|
||||
content.add(trans(c));
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
private CommentModel trans(Comment comment) {
|
||||
CommentModel commentModel = new CommentModel();
|
||||
commentModel.setId(comment.getId());
|
||||
commentModel.setComment(comment.getType());
|
||||
commentModel.setContent(comment.getContent());
|
||||
commentModel.setArticleID(comment.getArticleID());
|
||||
commentModel.setDate(DateFormatUtil.get(comment.getDate()));
|
||||
commentModel.setResponseId(comment.getResponseId());
|
||||
commentModel.setPid(comment.getPid());
|
||||
commentModel.setAuthorName(userService.getNameById(comment.getAuthorID()));
|
||||
commentModel.setAuthorAvatarImgUrl("http://cdn.celess.cn/" + userService.getAvatarImg(comment.getAuthorID()));
|
||||
|
||||
if (comment.getType() && commentModel.getArticleID() > 0) {
|
||||
commentModel.setArticleTitle(articleMapper.getTitleById(comment.getArticleID()));
|
||||
}
|
||||
return commentModel;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.mapper.*;
|
||||
import cn.celess.blog.service.CountService;
|
||||
import cn.celess.blog.util.RedisUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/02 22:06
|
||||
*/
|
||||
@Service
|
||||
public class CountServiceImpl implements CountService {
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
@Autowired
|
||||
CommentMapper commentMapper;
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
VisitorMapper visitorMapper;
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
|
||||
@Override
|
||||
public long getCommentCount() {
|
||||
return commentMapper.countByType(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getArticleCount() {
|
||||
return articleMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCategoriesCount() {
|
||||
return categoryMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getTagsCount() {
|
||||
return tagMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLeaveMessageCount() {
|
||||
return commentMapper.countByType(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getUserCount() {
|
||||
return userMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getVisitorCount() {
|
||||
return visitorMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getDayVisitCount() {
|
||||
String dayVisitCount = redisUtil.get("dayVisitCount");
|
||||
if (dayVisitCount == null) {
|
||||
return 1;
|
||||
}
|
||||
return Integer.parseInt(dayVisitCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.service.MailService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/22 14:26
|
||||
*/
|
||||
@Service
|
||||
public class MailServiceImpl implements MailService {
|
||||
public static final String FROM = "<xiaohai2271@163.com>";
|
||||
@Autowired
|
||||
JavaMailSender javaMailSender;
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public Boolean AsyncSend(SimpleMailMessage message) {
|
||||
this.send(message);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean send(SimpleMailMessage message) {
|
||||
String nick = null;
|
||||
try {
|
||||
nick = javax.mail.internet.MimeUtility.encodeText("小海博客");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
message.setFrom(nick + FROM);
|
||||
javaMailSender.send(message);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.PartnerSite;
|
||||
import cn.celess.blog.entity.request.LinkReq;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.mapper.PartnerMapper;
|
||||
import cn.celess.blog.service.PartnerSiteService;
|
||||
import cn.celess.blog.util.RegexUtil;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/05/12 11:43
|
||||
*/
|
||||
@Service
|
||||
public class PartnerSiteServiceImpl implements PartnerSiteService {
|
||||
@Autowired
|
||||
PartnerMapper partnerMapper;
|
||||
|
||||
@Override
|
||||
public PartnerSite create(LinkReq reqBody) {
|
||||
if (reqBody == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
//判空
|
||||
if (reqBody.getName() == null || reqBody.getUrl() == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
//判空
|
||||
if (reqBody.getName().replaceAll(" ", "").isEmpty() || reqBody.getUrl().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
//是否存在 同名
|
||||
if (partnerMapper.existsByName(reqBody.getName())) {
|
||||
throw new MyException(ResponseEnum.DATA_HAS_EXIST);
|
||||
}
|
||||
//url是否合法
|
||||
if (!RegexUtil.urlMatch(reqBody.getUrl())) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
||||
}
|
||||
PartnerSite partnerSite = new PartnerSite();
|
||||
reqBody.setId(0);
|
||||
if (!reqBody.getUrl().contains("http://") && !reqBody.getUrl().contains("https://")) {
|
||||
reqBody.setUrl("http://" + reqBody.getUrl());
|
||||
}
|
||||
BeanUtils.copyProperties(reqBody, partnerSite);
|
||||
partnerMapper.insert(partnerSite);
|
||||
return partnerSite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean del(long id) {
|
||||
//判断数据是否存在
|
||||
if (!partnerMapper.existsById(id)) {
|
||||
throw new MyException(ResponseEnum.DATA_NOT_EXIST);
|
||||
}
|
||||
return partnerMapper.delete(id) == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PartnerSite update(LinkReq reqBody) {
|
||||
PartnerSite partnerSite = partnerMapper.findById(reqBody.getId());
|
||||
if (partnerSite == null) {
|
||||
throw new MyException(ResponseEnum.DATA_NOT_EXIST);
|
||||
}
|
||||
if (partnerMapper.existsByName(reqBody.getName()) && !reqBody.getName().equals(partnerSite.getName())) {
|
||||
throw new MyException(ResponseEnum.DATA_HAS_EXIST);
|
||||
}
|
||||
if (!RegexUtil.urlMatch(reqBody.getUrl())) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
||||
}
|
||||
if (!reqBody.getUrl().contains("http://") && !reqBody.getUrl().contains("https://")) {
|
||||
reqBody.setUrl("http://" + reqBody.getUrl());
|
||||
}
|
||||
partnerSite.setName(reqBody.getName());
|
||||
partnerSite.setUrl(reqBody.getUrl());
|
||||
partnerSite.setOpen(reqBody.isOpen());
|
||||
partnerMapper.update(partnerSite);
|
||||
return partnerSite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<PartnerSite> PartnerSitePages(int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<PartnerSite> sitePage = partnerMapper.findAll();
|
||||
PageInfo pageInfo = new PageInfo(sitePage);
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PartnerSite> findAll() {
|
||||
List<PartnerSite> all = partnerMapper.findAll();
|
||||
return all;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.service.QiniuService;
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import com.qiniu.util.Auth;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/25 18:15
|
||||
*/
|
||||
@Service
|
||||
public class QiniuServiceImpl implements QiniuService {
|
||||
private Configuration cfg = new Configuration(Zone.zone2());
|
||||
private UploadManager uploadManager;
|
||||
private BucketManager bucketManager;
|
||||
private Auth auth;
|
||||
|
||||
private static String bucket;
|
||||
|
||||
|
||||
{
|
||||
/* ***** 必填 ******
|
||||
* 七牛的配置 *
|
||||
* ***** 必填 ******
|
||||
*/
|
||||
// accessKeyString,secretKeyString,bucketString:请替换为自己的值
|
||||
String accessKey = "accessKeyString";
|
||||
String secretKey = "secretKeyString";
|
||||
bucket = "bucketString";
|
||||
|
||||
auth = Auth.create(accessKey, secretKey);
|
||||
uploadManager = new UploadManager(cfg);
|
||||
bucketManager = new BucketManager(auth, cfg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
//文件存在则删除文件
|
||||
if (continueFile(fileName)) {
|
||||
try {
|
||||
System.out.println(bucketManager.delete(bucket, fileName).toString());
|
||||
} catch (QiniuException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//上传
|
||||
try {
|
||||
Response response = uploadManager.put(is, fileName, auth.uploadToken(bucket), null, null);
|
||||
return response.jsonToObject(QiniuResponse.class);
|
||||
} catch (QiniuException e) {
|
||||
Response r = e.response;
|
||||
System.err.println(r.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo[] getFileList() {
|
||||
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, "", 1000, "");
|
||||
FileInfo[] items = null;
|
||||
while (fileListIterator.hasNext()) {
|
||||
//处理获取的file list结果
|
||||
items = fileListIterator.next();
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private boolean continueFile(String key) {
|
||||
FileInfo[] allFile = getFileList();
|
||||
for (FileInfo fileInfo : allFile) {
|
||||
if (key.equals(fileInfo.key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.Article;
|
||||
import cn.celess.blog.entity.Tag;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.mapper.ArticleMapper;
|
||||
import cn.celess.blog.mapper.TagMapper;
|
||||
import cn.celess.blog.service.TagService;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/28 22:29
|
||||
*/
|
||||
@Service
|
||||
public class TagServiceImpl implements TagService {
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
@Autowired
|
||||
HttpServletRequest request;
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
|
||||
@Override
|
||||
public Tag create(String name) {
|
||||
boolean b = tagMapper.existsByName(name);
|
||||
if (b) {
|
||||
throw new MyException(ResponseEnum.TAG_HAS_EXIST);
|
||||
}
|
||||
Tag tag = new Tag();
|
||||
tag.setName(name);
|
||||
tagMapper.insert(tag);
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag create(Tag tag) {
|
||||
if (tag == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
tagMapper.insert(tag);
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(long tagId) {
|
||||
Tag tag = tagMapper.findTagById(tagId);
|
||||
if (tag == null) {
|
||||
throw new MyException(ResponseEnum.TAG_NOT_EXIST);
|
||||
}
|
||||
if (tag.getArticles()==null){
|
||||
return tagMapper.delete(tagId) == 1;
|
||||
}
|
||||
String[] articleArray = tag.getArticles().split(",");
|
||||
for (int i = 0; i < articleArray.length; i++) {
|
||||
if (articleArray[i] == null || "".equals(articleArray)) {
|
||||
continue;
|
||||
}
|
||||
long articleID = Long.parseLong(articleArray[i]);
|
||||
Article article = articleMapper.findArticleById(articleID);
|
||||
if (article == null) {
|
||||
continue;
|
||||
}
|
||||
article.setTagsId(article.getTagsId().replace(tagId + ",", ""));
|
||||
articleMapper.update(article);
|
||||
}
|
||||
return tagMapper.delete(tagId) == 1;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Tag update(Long id,String name) {
|
||||
if (id == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR.getCode(), "缺少ID");
|
||||
}
|
||||
Tag tagFromDB = tagMapper.findTagById(id);
|
||||
tagFromDB.setName(name);
|
||||
|
||||
tagMapper.update(tagFromDB);
|
||||
return tagFromDB;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag retrieveOneById(long tagId) {
|
||||
Tag tag = tagMapper.findTagById(tagId);
|
||||
if (tag == null) {
|
||||
throw new MyException(ResponseEnum.TAG_NOT_EXIST);
|
||||
}
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag retrieveOneByName(String name) {
|
||||
Tag tag = tagMapper.findTagByName(name);
|
||||
if (tag == null) {
|
||||
throw new MyException(ResponseEnum.TAG_NOT_EXIST);
|
||||
}
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<Tag> retrievePage(int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
PageInfo pageInfo = new PageInfo(tagMapper.findAll());
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tag> findAll() {
|
||||
return tagMapper.findAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,449 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.User;
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.entity.model.UserModel;
|
||||
import cn.celess.blog.entity.request.LoginReq;
|
||||
import cn.celess.blog.entity.request.UserReq;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.mapper.UserMapper;
|
||||
import cn.celess.blog.service.MailService;
|
||||
import cn.celess.blog.service.QiniuService;
|
||||
import cn.celess.blog.service.UserService;
|
||||
import cn.celess.blog.util.*;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.beans.Transient;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/03/30 18:41
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
private final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
HttpServletRequest request;
|
||||
@Autowired
|
||||
MailService mailService;
|
||||
@Autowired
|
||||
QiniuService qiniuService;
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
@Autowired
|
||||
JwtUtil jwtUtil;
|
||||
@Autowired
|
||||
RedisUserUtil redisUserUtil;
|
||||
|
||||
@Override
|
||||
@Transient
|
||||
public Boolean registration(String email, String password) {
|
||||
if (password.length() < 6 || password.length() > 16) {
|
||||
throw new MyException(ResponseEnum.PASSWORD_TOO_SHORT_OR_LONG);
|
||||
}
|
||||
if (!RegexUtil.emailMatch(email)) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_EMAIL_ERROR);
|
||||
}
|
||||
if (!RegexUtil.pwdMatch(password)) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_PWD_ERROR);
|
||||
}
|
||||
//验证码验证状态
|
||||
Boolean verifyStatus = (Boolean) request.getSession().getAttribute("verImgCodeStatus");
|
||||
if (verifyStatus == null || !verifyStatus) {
|
||||
throw new MyException(ResponseEnum.IMG_CODE_DIDNOTVERIFY);
|
||||
}
|
||||
if (userMapper.existsByEmail(email)) {
|
||||
throw new MyException(ResponseEnum.USERNAME_HAS_EXIST);
|
||||
}
|
||||
boolean b = userMapper.addUser(email, MD5Util.getMD5(password)) == 1;
|
||||
if (b) {
|
||||
String verifyId = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
redisUtil.setEx(email + "-verify", verifyId, 2, TimeUnit.DAYS);
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setTo(email);
|
||||
mailMessage.setSubject("邮箱验证");
|
||||
mailMessage.setText("欢迎注册小海博客,点击下面链接进行邮箱验证:\n https://www.celess.cn/emailVerify?email=" + email + "&verifyId=" +
|
||||
verifyId + "\n该链接两日内有效,若失效了,请登录后台进行重新激活。");
|
||||
mailService.send(mailMessage);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModel login(LoginReq loginReq) {
|
||||
if (loginReq == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
if (!RegexUtil.emailMatch(loginReq.getEmail())) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_EMAIL_ERROR);
|
||||
}
|
||||
if (!RegexUtil.pwdMatch(loginReq.getPassword())) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_PWD_ERROR);
|
||||
}
|
||||
//获取redis缓存中登录失败次数
|
||||
String s = redisUtil.get(loginReq.getEmail() + "-passwordWrongTime");
|
||||
if (s != null) {
|
||||
if (Integer.parseInt(s) == 5) {
|
||||
throw new MyException(ResponseEnum.LOGIN_LATER, loginReq.getEmail());
|
||||
}
|
||||
}
|
||||
User user = null;
|
||||
user = userMapper.findByEmail(loginReq.getEmail());
|
||||
String token = null;
|
||||
// 密码比对
|
||||
if (user == null) {
|
||||
// 用户不存在
|
||||
throw new MyException(ResponseEnum.USER_NOT_EXIST);
|
||||
}
|
||||
if (user.getPwd().equals(MD5Util.getMD5(loginReq.getPassword()))) {
|
||||
logger.info("====> {} 进行权限认证 状态:登录成功 <====", loginReq.getEmail());
|
||||
userMapper.updateLoginTime(loginReq.getEmail(), new Date());
|
||||
redisUtil.delete(loginReq.getEmail() + "-passwordWrongTime");
|
||||
// redis 标记
|
||||
redisUtil.setEx(loginReq.getEmail() + "-login", JSONObject.fromObject(user).toString(),
|
||||
(loginReq.getIsRememberMe() ? JwtUtil.EXPIRATION_LONG_TIME : JwtUtil.EXPIRATION_SHORT_TIME), TimeUnit.MILLISECONDS);
|
||||
token = jwtUtil.generateToken(user, loginReq.getIsRememberMe());
|
||||
} else {
|
||||
logger.info("====> {} 进行权限认证 状态:登录失败 <====", loginReq.getEmail());
|
||||
request.getSession().removeAttribute("code");
|
||||
//登录失败
|
||||
//设置登录失败的缓存
|
||||
if (s == null) {
|
||||
redisUtil.setEx(loginReq.getEmail() + "-passwordWrongTime", "1", 2, TimeUnit.HOURS);
|
||||
s = "0";
|
||||
}
|
||||
int count = Integer.parseInt(s);
|
||||
//登录次数++
|
||||
count++;
|
||||
//更新登录失败的次数
|
||||
redisUtil.setEx(loginReq.getEmail() + "-passwordWrongTime", count + "", 2, TimeUnit.HOURS);
|
||||
throw new MyException(ResponseEnum.LOGIN_FAILURE);
|
||||
}
|
||||
UserModel trans = trans(user);
|
||||
trans.setToken(token);
|
||||
return trans;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object logout() {
|
||||
String token = request.getHeader("Authorization");
|
||||
if (token == null || token.isEmpty()) {
|
||||
return "注销登录成功";
|
||||
}
|
||||
String email = jwtUtil.getUsernameFromToken(token);
|
||||
if (redisUtil.hasKey(email + "-login")) {
|
||||
redisUtil.delete(email + "-login");
|
||||
}
|
||||
return "注销登录成功";
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModel update(String desc, String displayName) {
|
||||
User user = redisUserUtil.get(request);
|
||||
user.setDesc(desc);
|
||||
user.setDisplayName(displayName);
|
||||
|
||||
userMapper.updateInfo(desc, displayName, user.getId());
|
||||
redisUserUtil.set(user);
|
||||
return trans(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUserRoleByEmail(String email) {
|
||||
String role = userMapper.getRoleByEmail(email);
|
||||
if (role == null) {
|
||||
throw new MyException(ResponseEnum.USER_NOT_EXIST);
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUserInfoByEmail(String email) {
|
||||
User user = userMapper.findByEmail(email);
|
||||
if (user == null) {
|
||||
throw new MyException(ResponseEnum.USER_NOT_EXIST);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAvatarImg(long id) {
|
||||
return userMapper.getAvatarImgUrlById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object updateUserAavatarImg(InputStream is, String mime) {
|
||||
User user = redisUserUtil.get(request);
|
||||
QiniuResponse upload = qiniuService.uploadFile(is, user.getEmail() + "_" + user.getId() + mime.toLowerCase());
|
||||
user.setAvatarImgUrl(upload.key);
|
||||
userMapper.updateAvatarImgUrl(upload.key, user.getId());
|
||||
redisUserUtil.set(user);
|
||||
return ResponseUtil.success(user.getAvatarImgUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModel getUserInfoBySession() {
|
||||
User user = redisUserUtil.get(request);
|
||||
return trans(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExistOfEmail(String email) {
|
||||
return userMapper.existsByEmail(email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameById(long id) {
|
||||
String name = userMapper.getDisPlayName(id);
|
||||
if (name == null) {
|
||||
name = userMapper.getEmail(id);
|
||||
if (name == null) {
|
||||
throw new MyException(ResponseEnum.USER_NOT_EXIST);
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 找回密码
|
||||
*/
|
||||
@Override
|
||||
public Object sendResetPwdEmail(String email) {
|
||||
if (!RegexUtil.emailMatch(email)) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_EMAIL_ERROR);
|
||||
}
|
||||
|
||||
User user = userMapper.findByEmail(email);
|
||||
if (user == null) {
|
||||
return "发送成功!";
|
||||
}
|
||||
|
||||
if (!user.getEmailStatus()) {
|
||||
throw new MyException(ResponseEnum.USEREMAIL_NOT_VERIFY);
|
||||
}
|
||||
|
||||
String verifyId = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
|
||||
redisUtil.setEx(user.getEmail() + "-resetPwd", verifyId, 2, TimeUnit.DAYS);
|
||||
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setTo(email);
|
||||
mailMessage.setSubject("密码重置");
|
||||
mailMessage.setText("点击下面链接进行重置密码:\n https://www.celess.cn/resetPwd?email=" + email + "&verifyId=" + verifyId);
|
||||
|
||||
mailService.send(mailMessage);
|
||||
return "发送成功!";
|
||||
}
|
||||
|
||||
//TODO
|
||||
@Override
|
||||
public Object sendVerifyEmail(String email) {
|
||||
if (!RegexUtil.emailMatch(email)) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_EMAIL_ERROR);
|
||||
}
|
||||
|
||||
User user = userMapper.findByEmail(email);
|
||||
if (user == null) {
|
||||
return "发送成功!";
|
||||
}
|
||||
|
||||
if (user.getEmailStatus()) {
|
||||
return "已经验证过了!";
|
||||
}
|
||||
|
||||
String verifyId = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
|
||||
redisUtil.setEx(user.getEmail() + "-verify", verifyId, 2, TimeUnit.DAYS);
|
||||
|
||||
SimpleMailMessage mailMessage = new SimpleMailMessage();
|
||||
mailMessage.setTo(email);
|
||||
mailMessage.setSubject("邮箱验证");
|
||||
mailMessage.setText("点击下面链接进行邮箱验证:\n https://www.celess.cn/emailVerify?email=" + email + "&verifyId=" + verifyId);
|
||||
mailService.send(mailMessage);
|
||||
return "发送成功!";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object verifyEmail(String verifyId, String email) {
|
||||
User user = userMapper.findByEmail(email);
|
||||
if (user == null) {
|
||||
throw new MyException(ResponseEnum.FAILURE);
|
||||
}
|
||||
if (user.getEmailStatus()) {
|
||||
throw new MyException(ResponseEnum.FAILURE.getCode(), "邮箱已验证过了");
|
||||
}
|
||||
String verifyIdFromCache = redisUtil.get(user.getEmail() + "-verify");
|
||||
if (verifyIdFromCache == null) {
|
||||
throw new MyException(ResponseEnum.FAILURE.getCode(), "验证链接无效");
|
||||
}
|
||||
if (verifyIdFromCache.equals(verifyId)) {
|
||||
userMapper.updateEmailStatus(email, true);
|
||||
redisUtil.delete(user.getEmail() + "-verify");
|
||||
user.setEmailStatus(true);
|
||||
redisUserUtil.set(user);
|
||||
return "验证成功";
|
||||
} else {
|
||||
throw new MyException(ResponseEnum.FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object reSetPwd(String verifyId, String email, String pwd) {
|
||||
User user = userMapper.findByEmail(email);
|
||||
if (user == null) {
|
||||
throw new MyException(ResponseEnum.USER_NOT_EXIST);
|
||||
}
|
||||
if (!RegexUtil.pwdMatch(pwd)) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_PWD_ERROR);
|
||||
}
|
||||
if (!user.getEmailStatus()) {
|
||||
throw new MyException(ResponseEnum.USEREMAIL_NOT_VERIFY);
|
||||
}
|
||||
String resetPwdIdFromCache = redisUtil.get(user.getEmail() + "-resetPwd");
|
||||
if (resetPwdIdFromCache == null) {
|
||||
throw new MyException(ResponseEnum.FAILURE.getCode(), "请先获取重置密码的邮件");
|
||||
}
|
||||
if (resetPwdIdFromCache.equals(verifyId)) {
|
||||
if (MD5Util.getMD5(pwd).equals(user.getPwd())) {
|
||||
throw new MyException(ResponseEnum.PWD_SAME);
|
||||
}
|
||||
userMapper.updatePwd(email, MD5Util.getMD5(pwd));
|
||||
redisUtil.delete(user.getEmail() + "-resetPwd");
|
||||
return "验证成功";
|
||||
} else {
|
||||
throw new MyException(ResponseEnum.FAILURE.getCode(), "标识码不一致");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object deleteUser(Integer[] id) {
|
||||
JSONArray status = new JSONArray();
|
||||
if (id == null || id.length == 0) {
|
||||
return null;
|
||||
}
|
||||
for (Integer integer : id) {
|
||||
String role = userMapper.getRoleById(integer);
|
||||
int deleteResult = 0;
|
||||
JSONObject deleteStatus = new JSONObject();
|
||||
deleteStatus.put("id", integer);
|
||||
// 管理员账户不可删
|
||||
if ("admin".equals(role)) {
|
||||
deleteStatus.put("msg", "用户为管理员,不可删除");
|
||||
deleteStatus.put("status", false);
|
||||
status.add(deleteStatus);
|
||||
logger.info("删除用户id为{}的用户,删除状态{}, 原因:用户为管理员,不可删除", integer, false);
|
||||
continue;
|
||||
}
|
||||
// 非管理员账户
|
||||
deleteResult = userMapper.delete(integer);
|
||||
deleteStatus.put("status", deleteResult == 1);
|
||||
logger.info("删除用户id为{}的用户,删除状态{}", integer, deleteResult == 1);
|
||||
if (deleteResult == 0) {
|
||||
deleteStatus.put("msg", "用户不存在");
|
||||
}
|
||||
status.add(deleteStatus);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<UserModel> getUserList(Integer page, Integer count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<User> all = userMapper.findAll();
|
||||
PageInfo pageInfo = PageInfo.of(all);
|
||||
List<UserModel> modelList = new ArrayList<>();
|
||||
all.forEach(user -> modelList.add(trans(user)));
|
||||
pageInfo.setList(modelList);
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModel adminUpdate(UserReq userReq) {
|
||||
if (userReq == null || userReq.getId() == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
User user = userMapper.findById(userReq.getId());
|
||||
// 设置数据
|
||||
if (userReq.getDesc() != null) {
|
||||
user.setDesc(userReq.getDesc());
|
||||
}
|
||||
if (userReq.getDisplayName() != null) {
|
||||
user.setDisplayName(userReq.getDisplayName());
|
||||
}
|
||||
if (userReq.getEmailStatus() != null) {
|
||||
user.setEmailStatus(userReq.getEmailStatus());
|
||||
}
|
||||
if (userReq.getPwd() != null) {
|
||||
if (userReq.getPwd().length() < 6 || userReq.getPwd().length() > 16) {
|
||||
throw new MyException(ResponseEnum.PASSWORD_TOO_SHORT_OR_LONG);
|
||||
}
|
||||
if (!RegexUtil.pwdMatch(userReq.getPwd())) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_PWD_ERROR);
|
||||
}
|
||||
user.setPwd(MD5Util.getMD5(userReq.getPwd()));
|
||||
}
|
||||
if (userReq.getRole() != null) {
|
||||
// TODO:用enum存放角色分类
|
||||
if ("user".equals(userReq.getRole()) || "admin".equals(userReq.getRole())) {
|
||||
user.setRole(userReq.getRole());
|
||||
} else {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
}
|
||||
if (userReq.getEmail() != null) {
|
||||
if (!RegexUtil.emailMatch(userReq.getEmail())) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_EMAIL_ERROR);
|
||||
}
|
||||
// TODO :: 邮件提醒
|
||||
user.setEmail(userReq.getEmail());
|
||||
}
|
||||
// 数据写入
|
||||
int updateResult = userMapper.update(user);
|
||||
if (updateResult == 0) {
|
||||
throw new MyException(ResponseEnum.FAILURE);
|
||||
}
|
||||
if (redisUserUtil.get(request).getId().equals(userReq.getId())) {
|
||||
redisUserUtil.set(user);
|
||||
}
|
||||
logger.info("修改了用户 [id={}] 的用户的资料", userReq.getId());
|
||||
return trans(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getStatusOfEmail(String email) {
|
||||
return userMapper.existsByEmail(email);
|
||||
}
|
||||
|
||||
private UserModel trans(User u) {
|
||||
UserModel user = new UserModel();
|
||||
user.setId(u.getId());
|
||||
user.setAvatarImgUrl(u.getAvatarImgUrl() == null ? null : "http://cdn.celess.cn/" + u.getAvatarImgUrl());
|
||||
user.setEmail(u.getEmail());
|
||||
user.setDesc(u.getDesc());
|
||||
user.setDisplayName(u.getDisplayName() == null ? u.getEmail() : u.getDisplayName());
|
||||
user.setEmailStatus(u.getEmailStatus());
|
||||
user.setRecentlyLandedDate(DateFormatUtil.get(u.getRecentlyLandedDate()));
|
||||
user.setRole(u.getRole());
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.Visitor;
|
||||
import cn.celess.blog.entity.model.VisitorModel;
|
||||
import cn.celess.blog.exception.MyException;
|
||||
import cn.celess.blog.mapper.VisitorMapper;
|
||||
import cn.celess.blog.service.VisitorService;
|
||||
import cn.celess.blog.util.DateFormatUtil;
|
||||
import cn.celess.blog.util.RedisUtil;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import eu.bitwalker.useragentutils.Browser;
|
||||
import eu.bitwalker.useragentutils.OperatingSystem;
|
||||
import eu.bitwalker.useragentutils.UserAgent;
|
||||
import eu.bitwalker.useragentutils.Version;
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.json.JsonParserFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/02 23:04
|
||||
*/
|
||||
@Service
|
||||
public class VisitorServiceImpl implements VisitorService {
|
||||
@Autowired
|
||||
VisitorMapper visitorMapper;
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
|
||||
@Override
|
||||
public String location(String ip) {
|
||||
return getLocation(ip);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<VisitorModel> visitorPage(int page, int count, boolean showLocation) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Visitor> visitorList = visitorMapper.findAll();
|
||||
PageInfo pageInfo = new PageInfo(visitorList);
|
||||
pageInfo.setList(list2List(visitorList, showLocation));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VisitorModel addVisitor(HttpServletRequest request) {
|
||||
//新session
|
||||
if (!request.getSession().isNew()) {
|
||||
return null;
|
||||
}
|
||||
if (isSpiderBot(request.getHeader("User-Agent"))) {
|
||||
return null;
|
||||
}
|
||||
Visitor visitor = new Visitor();
|
||||
visitor.setIp(request.getRemoteAddr());
|
||||
visitor.setDate(new Date());
|
||||
visitor.setUa(request.getHeader("User-Agent"));
|
||||
//记录当日的访问
|
||||
String dayVisitCount = redisUtil.get("dayVisitCount");
|
||||
long secondsLeftToday = 86400 - DateUtils.getFragmentInSeconds(Calendar.getInstance(), Calendar.DATE);
|
||||
Date date = new Date(Calendar.YEAR);
|
||||
if (dayVisitCount == null) {
|
||||
redisUtil.setEx("dayVisitCount", "1", secondsLeftToday, TimeUnit.SECONDS);
|
||||
} else {
|
||||
int count = Integer.parseInt(dayVisitCount) + 1;
|
||||
redisUtil.setEx("dayVisitCount", count + "", secondsLeftToday, TimeUnit.SECONDS);
|
||||
}
|
||||
if (visitorMapper.insert(visitor) == 0) {
|
||||
throw new MyException(ResponseEnum.FAILURE);
|
||||
}
|
||||
return trans(visitor);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 数据修改
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private List<VisitorModel> list2List(List<Visitor> visitorList, boolean showLocation) {
|
||||
List<VisitorModel> visitorModelList = new ArrayList<>();
|
||||
for (Visitor v : visitorList) {
|
||||
VisitorModel trans = trans(v);
|
||||
if (showLocation) {
|
||||
trans.setLocation(getLocation(v.getIp()));
|
||||
}
|
||||
visitorModelList.add(trans);
|
||||
}
|
||||
return visitorModelList;
|
||||
}
|
||||
|
||||
/***
|
||||
* 转化为model
|
||||
*
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
private VisitorModel trans(Visitor v) {
|
||||
UserAgent userAgent = UserAgent.parseUserAgentString(v.getUa());
|
||||
VisitorModel visitor = new VisitorModel();
|
||||
visitor.setId(v.getId());
|
||||
visitor.setDate(DateFormatUtil.get(v.getDate()));
|
||||
visitor.setIp(v.getIp());
|
||||
Browser browser = userAgent.getBrowser();
|
||||
visitor.setBrowserName(browser == null ? "" : browser.getName());
|
||||
OperatingSystem operatingSystem = userAgent.getOperatingSystem();
|
||||
visitor.setOSName(operatingSystem == null ? "" : operatingSystem.getName());
|
||||
Version browserVersion = userAgent.getBrowserVersion();
|
||||
visitor.setBrowserVersion(browserVersion == null ? "" : browserVersion.getVersion());
|
||||
return visitor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ua判断是不是爬虫
|
||||
*
|
||||
* @param ua ua
|
||||
* @return true:爬虫 false :不是爬虫
|
||||
*/
|
||||
private boolean isSpiderBot(String ua) {
|
||||
if (ua == null) {
|
||||
return false;
|
||||
}
|
||||
//服务器端的缓存抓取
|
||||
if (ua.contains("https://github.com/prerender/prerender")) {
|
||||
return true;
|
||||
}
|
||||
//搜索引擎得爬虫ua一般有链接
|
||||
if (ua.contains("http://")) {
|
||||
return true;
|
||||
}
|
||||
//防止没有匹配到http
|
||||
return ua.toLowerCase().contains("spider");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ip的地址
|
||||
*
|
||||
* @param ip
|
||||
* @return
|
||||
*/
|
||||
private String getLocation(String ip) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
URL url;
|
||||
HttpURLConnection conn = null;
|
||||
InputStream inputStream = null;
|
||||
InputStreamReader inputStreamReader = null;
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setConnectTimeout(3000);
|
||||
conn.setDoInput(true);
|
||||
conn.setRequestMethod("GET");
|
||||
inputStream = conn.getInputStream();
|
||||
inputStreamReader = new InputStreamReader(inputStream);
|
||||
bufferedReader = new BufferedReader(inputStreamReader);
|
||||
String tmp;
|
||||
while ((tmp = bufferedReader.readLine()) != null) {
|
||||
result.append(tmp);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
} finally {
|
||||
try {
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
if (inputStreamReader != null) {
|
||||
inputStreamReader.close();
|
||||
}
|
||||
if (bufferedReader != null) {
|
||||
bufferedReader.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if ("".equals(result.toString())) {
|
||||
throw new MyException(ResponseEnum.FAILURE);
|
||||
}
|
||||
Map<String, Object> stringObjectMap = JsonParserFactory.getJsonParser().parseMap(result.toString());
|
||||
if ((Integer) stringObjectMap.get("code") == 0) {
|
||||
LinkedHashMap data = (LinkedHashMap) stringObjectMap.get("data");
|
||||
sb.append(data.get("country"))
|
||||
.append("-")
|
||||
.append(data.get("region"))
|
||||
.append("-")
|
||||
.append(data.get("city"));
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package cn.celess.blog.service.serviceimpl;
|
||||
|
||||
import cn.celess.blog.enmu.ResponseEnum;
|
||||
import cn.celess.blog.entity.WebUpdate;
|
||||
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 com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/05/12 11:43
|
||||
*/
|
||||
@Service
|
||||
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, new Date());
|
||||
if (webUpdateInfoMapper.insert(webUpdate) == 0) {
|
||||
throw new MyException(ResponseEnum.FAILURE);
|
||||
}
|
||||
return trans(webUpdate);
|
||||
}
|
||||
|
||||
@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 trans(webUpdate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageInfo<WebUpdateModel> pages(int count, int page) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<WebUpdate> updateList = webUpdateInfoMapper.findAll();
|
||||
PageInfo pageInfo = new PageInfo(updateList);
|
||||
pageInfo.setList(list2List(updateList));
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<WebUpdateModel> findAll() {
|
||||
List<WebUpdate> all = webUpdateInfoMapper.findAll();
|
||||
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
|
||||
for (WebUpdate w : all) {
|
||||
webUpdateModels.add(trans(w));
|
||||
}
|
||||
return webUpdateModels;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLastestUpdateTime() {
|
||||
return DateFormatUtil.get(webUpdateInfoMapper.getLastestOne());
|
||||
}
|
||||
|
||||
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {
|
||||
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
|
||||
for (WebUpdate w : webUpdates) {
|
||||
webUpdateModels.add(trans(w));
|
||||
}
|
||||
return webUpdateModels;
|
||||
}
|
||||
|
||||
private WebUpdateModel trans(WebUpdate webUpdate) {
|
||||
return new WebUpdateModel(webUpdate.getId(), webUpdate.getUpdateInfo(), DateFormatUtil.get(webUpdate.getUpdateTime()));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user