👽 代码优化

pull/21/head
ruibaby 2018-07-16 23:32:54 +08:00
parent 8e9c4b14c3
commit d75509918a
12 changed files with 63 additions and 28 deletions

View File

@ -40,13 +40,14 @@ public class FreeMarkerConfig {
@PostConstruct @PostConstruct
public void setSharedVariable() { public void setSharedVariable() {
try {
//自定义标签
configuration.setSharedVariable("commonTag", commonTagDirective); configuration.setSharedVariable("commonTag", commonTagDirective);
configuration.setSharedVariable("articleTag", articleTagDirective); configuration.setSharedVariable("articleTag", articleTagDirective);
try {
configuration.setSharedVariable("options", optionsService.findAllOptions()); configuration.setSharedVariable("options", optionsService.findAllOptions());
configuration.setSharedVariable("user", userService.findUser()); configuration.setSharedVariable("user", userService.findUser());
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
e.printStackTrace(); log.error("自定义标签加载失败:{}", e.getMessage());
} }
} }
} }

View File

@ -0,0 +1,28 @@
package cc.ryanc.halo.model.enums;
/**
* @author : RYAN0UP
* @date : 2018/7/16
*/
public enum TrueFalse {
/**
*
*/
TRUE("true"),
/**
*
*/
FALSE("false");
private String desc;
TrueFalse(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
}

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.service.impl; package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.domain.User; import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.repository.UserRepository; import cc.ryanc.halo.repository.UserRepository;
import cc.ryanc.halo.service.UserService; import cc.ryanc.halo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -127,7 +128,7 @@ public class UserServiceImpl implements UserService {
@Override @Override
public User updateUserNormal() { public User updateUserNormal() {
User user = this.findUser(); User user = this.findUser();
user.setLoginEnable("true"); user.setLoginEnable(TrueFalse.TRUE.getDesc());
user.setLoginError(0); user.setLoginError(0);
user.setLoginLast(new Date()); user.setLoginLast(new Date());
userRepository.save(user); userRepository.save(user);

View File

@ -9,6 +9,7 @@ import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.dto.LogsRecord; import cc.ryanc.halo.model.dto.LogsRecord;
import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.enums.ResultCode; import cc.ryanc.halo.model.enums.ResultCode;
import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.LogsService; import cc.ryanc.halo.service.LogsService;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
@ -134,7 +135,7 @@ public class AdminController extends BaseController {
loginLast = aUser.getLoginLast(); loginLast = aUser.getLoginLast();
} }
Long between = DateUtil.between(loginLast, DateUtil.date(), DateUnit.MINUTE); Long between = DateUtil.between(loginLast, DateUtil.date(), DateUnit.MINUTE);
if (StringUtils.equals(aUser.getLoginEnable(), "false") && (between < 10)) { if (StringUtils.equals(aUser.getLoginEnable(), TrueFalse.FALSE.getDesc()) && (between < 10)) {
return new JsonResult(ResultCode.FAIL.getCode(), "已禁止登录请10分钟后再试"); return new JsonResult(ResultCode.FAIL.getCode(), "已禁止登录请10分钟后再试");
} }
//验证用户名和密码 //验证用户名和密码
@ -158,7 +159,7 @@ public class AdminController extends BaseController {
Integer errorCount = userService.updateUserLoginError(); Integer errorCount = userService.updateUserLoginError();
//超过五次禁用账户 //超过五次禁用账户
if (errorCount >= 5) { if (errorCount >= 5) {
userService.updateUserLoginEnable("false"); userService.updateUserLoginEnable(TrueFalse.FALSE.getDesc());
} }
logsService.saveByLogs( logsService.saveByLogs(
new Logs( new Logs(

View File

@ -5,6 +5,7 @@ import cc.ryanc.halo.model.domain.Logs;
import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult; import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.dto.LogsRecord; import cc.ryanc.halo.model.dto.LogsRecord;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.enums.ResultCode; import cc.ryanc.halo.model.enums.ResultCode;
import cc.ryanc.halo.service.AttachmentService; import cc.ryanc.halo.service.AttachmentService;
import cc.ryanc.halo.service.LogsService; import cc.ryanc.halo.service.LogsService;
@ -91,7 +92,7 @@ public class AttachmentController {
Page<Attachment> attachments = attachmentService.findAllAttachments(pageable); Page<Attachment> attachments = attachmentService.findAllAttachments(pageable);
model.addAttribute("attachments", attachments); model.addAttribute("attachments", attachments);
model.addAttribute("id", id); model.addAttribute("id", id);
if (StringUtils.equals(type, "post")) { if (StringUtils.equals(type, PostType.POST_TYPE_POST.getDesc())) {
return "admin/widget/_attachment-select-post"; return "admin/widget/_attachment-select-post";
} }
return "admin/widget/_attachment-select"; return "admin/widget/_attachment-select";
@ -207,7 +208,7 @@ public class AttachmentController {
* *
* @param attachId * @param attachId
* @param request request * @param request request
* @return truefalse * @return JsonResult
*/ */
@GetMapping(value = "/remove") @GetMapping(value = "/remove")
@ResponseBody @ResponseBody

View File

@ -5,8 +5,10 @@ import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.dto.BackupDto; import cc.ryanc.halo.model.dto.BackupDto;
import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult; import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.enums.ResultCode; import cc.ryanc.halo.model.enums.ResultCode;
import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.service.MailService; import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.HaloUtils; import cc.ryanc.halo.utils.HaloUtils;
@ -201,7 +203,7 @@ public class BackupController {
if (null == user.getUserEmail() || StringUtils.equals(user.getUserEmail(), "")) { if (null == user.getUserEmail() || StringUtils.equals(user.getUserEmail(), "")) {
return new JsonResult(ResultCode.FAIL.getCode(), "博主邮箱没有配置!"); return new JsonResult(ResultCode.FAIL.getCode(), "博主邮箱没有配置!");
} }
if (StringUtils.equals(HaloConst.OPTIONS.get("smtp_email_enable"), "false")) { if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.FALSE.getDesc())) {
return new JsonResult(ResultCode.FAIL.getCode(), "发信邮箱没有配置!"); return new JsonResult(ResultCode.FAIL.getCode(), "发信邮箱没有配置!");
} }
new EmailToAdmin(srcPath, user).start(); new EmailToAdmin(srcPath, user).start();

View File

@ -7,6 +7,7 @@ import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.CommentStatus; import cc.ryanc.halo.model.enums.CommentStatus;
import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.MailService; import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
@ -120,7 +121,7 @@ public class CommentController extends BaseController {
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY); User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
//判断是否启用邮件服务 //判断是否启用邮件服务
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), "true") && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), "true")) { if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
try { try {
if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) { if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
@ -211,7 +212,7 @@ public class CommentController extends BaseController {
commentService.saveByComment(comment); commentService.saveByComment(comment);
//邮件通知 //邮件通知
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), "true") && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), "true")) { if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
if (Validator.isEmail(lastComment.getCommentAuthorEmail())) { if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("blogTitle", HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp())); map.put("blogTitle", HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp()));

View File

@ -41,7 +41,7 @@ public class OptionController {
* *
* *
* @param options options * @param options options
* @return truefalse * @return JsonResult
*/ */
@PostMapping(value = "/save") @PostMapping(value = "/save")
@ResponseBody @ResponseBody

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.LogsRecord; import cc.ryanc.halo.model.dto.LogsRecord;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.service.*; import cc.ryanc.halo.service.*;
import cc.ryanc.halo.utils.HaloUtils; import cc.ryanc.halo.utils.HaloUtils;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
@ -63,7 +64,7 @@ public class InstallController {
@GetMapping @GetMapping
public String install(Model model) { public String install(Model model) {
try { try {
if (StringUtils.equals("true", HaloConst.OPTIONS.get(BlogProperties.IS_INSTALL.getProp()))) { if (StringUtils.equals(TrueFalse.TRUE.getDesc(), HaloConst.OPTIONS.get(BlogProperties.IS_INSTALL.getProp()))) {
model.addAttribute("isInstall", true); model.addAttribute("isInstall", true);
} else { } else {
model.addAttribute("isInstall", false); model.addAttribute("isInstall", false);
@ -96,7 +97,7 @@ public class InstallController {
@RequestParam("userPwd") String userPwd, @RequestParam("userPwd") String userPwd,
HttpServletRequest request) { HttpServletRequest request) {
try { try {
if (StringUtils.equals("true", HaloConst.OPTIONS.get(BlogProperties.IS_INSTALL.getProp()))) { if (StringUtils.equals(TrueFalse.TRUE.getDesc(), HaloConst.OPTIONS.get(BlogProperties.IS_INSTALL.getProp()))) {
return false; return false;
} }
//创建新的用户 //创建新的用户
@ -148,7 +149,7 @@ public class InstallController {
comment.setIsAdmin(0); comment.setIsAdmin(0);
commentService.saveByComment(comment); commentService.saveByComment(comment);
optionsService.saveOption(BlogProperties.IS_INSTALL.getProp(), "true"); optionsService.saveOption(BlogProperties.IS_INSTALL.getProp(), TrueFalse.TRUE.getDesc());
//保存博客标题和博客地址设置 //保存博客标题和博客地址设置
optionsService.saveOption(BlogProperties.BLOG_TITLE.getProp(), blogTitle); optionsService.saveOption(BlogProperties.BLOG_TITLE.getProp(), blogTitle);
@ -164,12 +165,12 @@ public class InstallController {
optionsService.saveOption(BlogProperties.COMMENT_SYSTEM.getProp(), "native"); optionsService.saveOption(BlogProperties.COMMENT_SYSTEM.getProp(), "native");
//默认不配置邮件系统 //默认不配置邮件系统
optionsService.saveOption(BlogProperties.SMTP_EMAIL_ENABLE.getProp(), "false"); optionsService.saveOption(BlogProperties.SMTP_EMAIL_ENABLE.getProp(), TrueFalse.FALSE.getDesc());
//新评论,审核通过,回复,默认不通知 //新评论,审核通过,回复,默认不通知
optionsService.saveOption(BlogProperties.NEW_COMMENT_NOTICE.getProp(), "false"); optionsService.saveOption(BlogProperties.NEW_COMMENT_NOTICE.getProp(), TrueFalse.FALSE.getDesc());
optionsService.saveOption(BlogProperties.COMMENT_PASS_NOTICE.getProp(), "false"); optionsService.saveOption(BlogProperties.COMMENT_PASS_NOTICE.getProp(), TrueFalse.FALSE.getDesc());
optionsService.saveOption(BlogProperties.COMMENT_REPLY_NOTICE.getProp(), "false"); optionsService.saveOption(BlogProperties.COMMENT_REPLY_NOTICE.getProp(), TrueFalse.FALSE.getDesc());
//更新日志 //更新日志
logsService.saveByLogs( logsService.saveByLogs(

View File

@ -4,10 +4,7 @@ import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult; import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.*;
import cc.ryanc.halo.model.enums.CommentStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.enums.ResultCode;
import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.MailService; import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
@ -153,7 +150,7 @@ public class FrontCommentController {
} }
@Override @Override
public void run(){ public void run(){
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), "true") && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), "true")) { if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
try { try {
//发送邮件到博主 //发送邮件到博主
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
@ -191,7 +188,7 @@ public class FrontCommentController {
@Override @Override
public void run() { public void run() {
//发送通知给对方 //发送通知给对方
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), "true") && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), "true")) { if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
if(Validator.isEmail(lastComment.getCommentAuthorEmail())){ if(Validator.isEmail(lastComment.getCommentAuthorEmail())){
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("blogTitle",HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp())); map.put("blogTitle",HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp()));

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.web.interceptor;
import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.TrueFalse;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
@ -18,7 +19,7 @@ import javax.servlet.http.HttpServletResponse;
public class ApiInterceptor implements HandlerInterceptor { public class ApiInterceptor implements HandlerInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (StringUtils.equals("true", HaloConst.OPTIONS.get(BlogProperties.API_STATUS.getProp()))) { if (StringUtils.equals(TrueFalse.TRUE.getDesc(), HaloConst.OPTIONS.get(BlogProperties.API_STATUS.getProp()))) {
return true; return true;
} }
response.sendRedirect("/404"); response.sendRedirect("/404");

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.web.interceptor;
import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.TrueFalse;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
@ -19,7 +20,7 @@ public class InstallInterceptor implements HandlerInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
if (StringUtils.equals("true", HaloConst.OPTIONS.get(BlogProperties.IS_INSTALL.getProp()))) { if (StringUtils.equals(TrueFalse.TRUE.getDesc(), HaloConst.OPTIONS.get(BlogProperties.IS_INSTALL.getProp()))) {
return true; return true;
} }
response.sendRedirect("/install"); response.sendRedirect("/install");