This commit is contained in:
123 2025-04-27 14:54:59 +08:00
parent 1a7c7f2225
commit 85a8d27795
2 changed files with 714 additions and 739 deletions

View File

@ -1,20 +1,17 @@
package cc.iteachyou.cms.common;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.web.bind.annotation.ModelAttribute;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
/**
*
* controller基类实现将JSON格式结果的输出方法
*/
@SuppressWarnings("unchecked")
@ -26,22 +23,22 @@ public class BaseController implements Serializable {
protected HttpSession session;
@ModelAttribute
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
this.session = request.getSession();
}
/**
* 从Session中取得用户信息
*
* @return 用户信息
*/
public Object getLoginUser() {
HttpSession session = this.getRequest().getSession();
Object obj = session.getAttribute(Constant.SESSION_USER);
if(null == obj){
if (null == obj) {
throw new RuntimeException("PlatSysUser为空,登陆出错!");
}
return obj;
@ -49,6 +46,7 @@ public class BaseController implements Serializable {
/**
* 取得HttpServletRequest对象
*
* @return HttpServletRequest对象
*/
public HttpServletRequest getRequest() {
@ -57,6 +55,7 @@ public class BaseController implements Serializable {
/**
* 取得Response对象
*
* @return
*/
public HttpServletResponse getResponse() {
@ -65,6 +64,7 @@ public class BaseController implements Serializable {
/**
* 转换字符串为json 格式
*
* @param str 普通字符串
*/
public void outJsonString(String str) {
@ -74,15 +74,17 @@ public class BaseController implements Serializable {
/**
* 转换字符串为json 格式并设置ContentType为text/html
*
* @param str 普通字符串
*/
public void outJson(Object obj) {
getResponse().setContentType("text/html;charset=UTF-8");
outJsonString(JSONObject.toJSONString(obj,SerializerFeature.WriteMapNullValue));
outJsonString(JSONObject.toJSONString(obj, SerializerFeature.WriteMapNullValue));
}
/**
* 输出字符串到页面
*
* @param str 字符
*/
public void outString(String str) {
@ -100,6 +102,7 @@ public class BaseController implements Serializable {
/**
* 输出xml文本串到页面
*
* @param xmlStr xml串
*/
public void outXMLString(String xmlStr) {

View File

@ -1,67 +1,29 @@
package cc.iteachyou.cms.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import cc.iteachyou.cms.common.BaseController;
import cc.iteachyou.cms.common.ExceptionEnum;
import cc.iteachyou.cms.common.ResponseResult;
import cc.iteachyou.cms.common.SearchEntity;
import cc.iteachyou.cms.common.StateCodeEnum;
import cc.iteachyou.cms.entity.Archives;
import cc.iteachyou.cms.entity.Attachment;
import cc.iteachyou.cms.entity.Category;
import cc.iteachyou.cms.entity.Field;
import cc.iteachyou.cms.entity.Form;
import cc.iteachyou.cms.entity.SearchRecord;
import cc.iteachyou.cms.common.*;
import cc.iteachyou.cms.entity.System;
import cc.iteachyou.cms.entity.Theme;
import cc.iteachyou.cms.exception.AdminGeneralException;
import cc.iteachyou.cms.exception.CmsException;
import cc.iteachyou.cms.exception.FormParameterException;
import cc.iteachyou.cms.exception.TemplateNotFoundException;
import cc.iteachyou.cms.exception.TemplateReadException;
import cc.iteachyou.cms.exception.TransactionException;
import cc.iteachyou.cms.service.ArchivesService;
import cc.iteachyou.cms.service.AttachmentService;
import cc.iteachyou.cms.service.CategoryService;
import cc.iteachyou.cms.service.FieldService;
import cc.iteachyou.cms.service.FormService;
import cc.iteachyou.cms.service.SearchRecordService;
import cc.iteachyou.cms.service.SystemService;
import cc.iteachyou.cms.service.ThemeService;
import cc.iteachyou.cms.entity.*;
import cc.iteachyou.cms.exception.*;
import cc.iteachyou.cms.service.*;
import cc.iteachyou.cms.taglib.ParseEngine;
import cc.iteachyou.cms.taglib.utils.URLUtils;
import cc.iteachyou.cms.utils.FileConfiguration;
import cc.iteachyou.cms.utils.StringUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
@Controller
@Scope("prototype")
@ -90,6 +52,7 @@ public class FrontController extends BaseController {
/**
* 首页方法
*
* @throws CmsException
* @throws IOException
*/
@ -97,14 +60,14 @@ public class FrontController extends BaseController {
public void index() throws CmsException, IOException {
System system = systemService.getSystem();
String staticdir = system.getStaticdir();
if(staticdir.startsWith("/")) {
if (staticdir.startsWith("/")) {
staticdir = staticdir.substring(1);
}
//如果为静态浏览则重写向到静态文件
if(2 == system.getBrowseType()) {
if (2 == system.getBrowseType()) {
String url = fileConfiguration.getResourceDir() + staticdir + "/index.html";
File staticFile = new File(url);
if(!staticFile.exists()) {
if (!staticFile.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.HTTP_NOT_FOUND.getCode(),
ExceptionEnum.HTTP_NOT_FOUND.getMessage(),
@ -120,7 +83,7 @@ public class FrontController extends BaseController {
String path = templateDir + templatePath;
File template = new File(path);
if(!template.exists()) {
if (!template.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getCode(),
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getMessage(),
@ -141,6 +104,7 @@ public class FrontController extends BaseController {
/**
* 封面方法
*
* @param typeid 栏目编码
* @param visitUrl 访问URL
* @throws CmsException
@ -153,11 +117,11 @@ public class FrontController extends BaseController {
//查询栏目
Category category = categoryService.queryCategoryByCode(typeid);
//如果为静态浏览则重写向到静态文件
if(2 == system.getBrowseType()) {
if (2 == system.getBrowseType()) {
String url = URLUtils.parseURL(system, category, "S");
String fileUrl = fileConfiguration.getResourceDir() + url;
File staticFile = new File(fileUrl);
if(!staticFile.exists()) {
if (!staticFile.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.HTTP_NOT_FOUND.getCode(),
ExceptionEnum.HTTP_NOT_FOUND.getMessage(),
@ -169,27 +133,27 @@ public class FrontController extends BaseController {
Theme theme = themeService.getCurrentTheme();
String templateDir = fileConfiguration.getResourceDir() + "templates/";
if(theme == null) {
if (theme == null) {
}
if(!visitUrl.startsWith("/")) {
if (!visitUrl.startsWith("/")) {
visitUrl = "/" + visitUrl;
}
StringBuffer templatePath = new StringBuffer();
templatePath.append(theme.getThemePath());
if(category.getCatModel() == 1) {
if (category.getCatModel() == 1) {
templatePath.append(category.getCoverTemp());
}else if(category.getCatModel() == 2) {
} else if (category.getCatModel() == 2) {
templatePath.append(category.getListTemp());
}else if(category.getCatModel() == 3) {
} else if (category.getCatModel() == 3) {
templatePath.append(category.getLinkUrl());
}
try {
String path = templateDir + templatePath;
File template = new File(path);
if(!template.exists()) {
if (!template.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getCode(),
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getMessage(),
@ -198,7 +162,7 @@ public class FrontController extends BaseController {
String newHtml = "";
String html = FileUtils.readFileToString(template, "UTF-8");
newHtml = parseEngine.parse(html);
newHtml = parseEngine.parseCategory(newHtml,typeid);
newHtml = parseEngine.parseCategory(newHtml, typeid);
outHtml(newHtml);
} catch (IOException e) {
throw new TemplateReadException(
@ -210,6 +174,7 @@ public class FrontController extends BaseController {
/**
* 列表方法
*
* @param typeid 栏目编码
* @param visitUrl 访问URL
* @param pageNum 当前页
@ -224,17 +189,17 @@ public class FrontController extends BaseController {
, @PathVariable Integer pageSize) throws CmsException, IOException {
System system = systemService.getSystem();
String staticdir = system.getStaticdir();
if(staticdir.startsWith("/")) {
if (staticdir.startsWith("/")) {
staticdir = staticdir.substring(1);
}
//查询栏目
Category category = categoryService.queryCategoryByCode(typeid);
//如果为静态浏览则重写向到静态文件
if(2 == system.getBrowseType()) {
if (2 == system.getBrowseType()) {
String url = staticdir + URLUtils.parseFileName(category, pageNum);
String fileUrl = fileConfiguration.getResourceDir() + url;
File staticFile = new File(fileUrl);
if(!staticFile.exists()) {
if (!staticFile.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.HTTP_NOT_FOUND.getCode(),
ExceptionEnum.HTTP_NOT_FOUND.getMessage(),
@ -246,26 +211,26 @@ public class FrontController extends BaseController {
Theme theme = themeService.getCurrentTheme();
String templateDir = fileConfiguration.getResourceDir() + "templates/";
if(theme == null) {
if (theme == null) {
}
if(!visitUrl.startsWith("/")) {
if (!visitUrl.startsWith("/")) {
visitUrl = "/" + visitUrl;
}
StringBuffer templatePath = new StringBuffer();
templatePath.append(theme.getThemePath());
if(category.getCatModel() == 1) {
if (category.getCatModel() == 1) {
templatePath.append(category.getCoverTemp());
}else if(category.getCatModel() == 2) {
} else if (category.getCatModel() == 2) {
templatePath.append(category.getListTemp());
}else if(category.getCatModel() == 3) {
} else if (category.getCatModel() == 3) {
templatePath.append(category.getLinkUrl());
}
try {
String path = templateDir + templatePath;
File template = new File(path);
if(!template.exists()) {
if (!template.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getCode(),
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getMessage(),
@ -287,25 +252,26 @@ public class FrontController extends BaseController {
/**
* 文章详情方法
*
* @param id 文章ID
* @throws CmsException
* @throws IOException
*/
@RequestMapping("/article/{id}")
public void article(@PathVariable String id) throws CmsException, IOException{
public void article(@PathVariable String id) throws CmsException, IOException {
System system = systemService.getSystem();
String staticdir = system.getStaticdir();
if(staticdir.startsWith("/")) {
if (staticdir.startsWith("/")) {
staticdir = staticdir.substring(1);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Archives archives = archivesService.selectByPrimaryKey(id);
//如果为静态浏览则重写向到静态文件
if(2 == system.getBrowseType()) {
if (2 == system.getBrowseType()) {
Category temp = null;
if(!"-1".equals(archives.getCategoryId())) {
if (!"-1".equals(archives.getCategoryId())) {
temp = categoryService.selectById(archives.getCategoryId());
}else {//顶级分类走该模版
} else {//顶级分类走该模版
temp = new Category();
temp.setId("-1");
temp.setCnname("顶级分类");
@ -316,7 +282,7 @@ public class FrontController extends BaseController {
String dateDir = sdf.format(createTime);
File staticFile = new File(fileConfiguration.getResourceDir() + staticdir + catDir + "/" + dateDir + "/" + archives.getId() + ".html");
if(!staticFile.exists()) {
if (!staticFile.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.HTTP_NOT_FOUND.getCode(),
ExceptionEnum.HTTP_NOT_FOUND.getMessage(),
@ -330,18 +296,18 @@ public class FrontController extends BaseController {
StringBuffer templatePath = new StringBuffer();
Theme theme = themeService.getCurrentTheme();
String templateDir = fileConfiguration.getResourceDir() + "templates/";
if(theme == null) {
if (theme == null) {
}
templatePath.append(theme.getThemePath());
Category category = null;
if(!"-1".equals(archives.getCategoryId())) {
if (!"-1".equals(archives.getCategoryId())) {
category = categoryService.selectById(archives.getCategoryId());
//构建路径
templatePath.append("/" + category.getArticleTemp());
}else {//顶级分类走该模版
} else {//顶级分类走该模版
templatePath.append("/article.html");
category = new Category();
category.setId("-1");
@ -350,7 +316,7 @@ public class FrontController extends BaseController {
try {
String path = templateDir + templatePath;
File template = new File(path);
if(!template.exists()) {
if (!template.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getCode(),
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getMessage(),
@ -381,14 +347,15 @@ public class FrontController extends BaseController {
/**
* 文章详情方法
*
* @param id 文章ID
* @throws CmsException
* @throws IOException
*/
@RequestMapping("/clicks/{id}")
public void clicks(@PathVariable String id, HttpServletResponse response) throws CmsException, IOException{
public void clicks(@PathVariable String id, HttpServletResponse response) throws CmsException, IOException {
response.setContentType("text/javascript");
if(StrUtil.isBlank(id)){
if (StrUtil.isBlank(id)) {
throw new TemplateReadException(
ExceptionEnum.ARTICLE_NOTFOUND_EXCEPTION.getCode(),
ExceptionEnum.ARTICLE_NOTFOUND_EXCEPTION.getMessage(),
@ -407,6 +374,7 @@ public class FrontController extends BaseController {
/**
* 附件下载
*
* @param id
* @throws AdminGeneralException
*/
@ -434,7 +402,7 @@ public class FrontController extends BaseController {
// 这里主要关闭
os.close();
inputStream.close();
}catch (Exception e) {
} catch (Exception e) {
throw new AdminGeneralException(
ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(),
ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getMessage(),
@ -444,6 +412,7 @@ public class FrontController extends BaseController {
/**
* 搜索
*
* @param params
* @throws CmsException
*/
@ -453,30 +422,30 @@ public class FrontController extends BaseController {
StringBuffer templatePath = new StringBuffer();
Theme theme = themeService.getCurrentTheme();
String templateDir = fileConfiguration.getResourceDir() + "templates/";
if(theme == null) {
if (theme == null) {
}
templatePath.append(theme.getThemePath());
templatePath.append("/search.html");
if(params.getPageNum() == null)
if (params.getPageNum() == null)
params.setPageNum(1);
if(params.getPageSize() == null)
if (params.getPageSize() == null)
params.setPageSize(10);
try {
Map<String, Object> entity = params.getEntity();
if(entity == null || entity.size() <= 0) {
if (entity == null || entity.size() <= 0) {
throw new FormParameterException(
ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(),
ExceptionEnum.FORM_PARAMETER_EXCEPTION.getMessage(),
"请仔细检查Form表单参数结构正确参数格式应该包含entity['keywords']、pageNum、pageSize。");
}
String keywords = "";
if(entity.containsKey("keywords")) {
if (entity.containsKey("keywords")) {
keywords = params.getEntity().get("keywords").toString();
if(keywords.getBytes("GBK").length < 3) {
if (keywords.getBytes("GBK").length < 3) {
throw new FormParameterException(
ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(),
ExceptionEnum.FORM_PARAMETER_EXCEPTION.getMessage(),
@ -486,7 +455,7 @@ public class FrontController extends BaseController {
String path = templateDir + templatePath;
File template = new File(path);
if(!template.exists()) {
if (!template.exists()) {
throw new TemplateNotFoundException(
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getCode(),
ExceptionEnum.TEMPLATE_NOTFOUND_EXCEPTION.getMessage(),
@ -495,9 +464,9 @@ public class FrontController extends BaseController {
String newHtml = "";
String html = FileUtils.readFileToString(template, "UTF-8");
//如果为静态浏览则生成页面
if(2 == system.getBrowseType()) {
if (2 == system.getBrowseType()) {
newHtml = parseEngine.generate(html);
}else {
} else {
newHtml = parseEngine.parse(html);
}
newHtml = parseEngine.parsePageList(newHtml, params);
@ -521,24 +490,25 @@ public class FrontController extends BaseController {
/**
* 前端投稿
*
* @param params
* @return
* @throws CmsException
*/
@ResponseBody
@RequestMapping(value = "/input",method = RequestMethod.POST)
public ResponseResult input(@RequestParam Map<String,Object> params) throws CmsException {
@RequestMapping(value = "/input", method = RequestMethod.POST)
public ResponseResult input(@RequestParam Map<String, Object> params) throws CmsException {
// 验证码校验
if(!params.containsKey("captcha") || StringUtil.isBlank(params.get("captcha"))) {
if (!params.containsKey("captcha") || StringUtil.isBlank(params.get("captcha"))) {
return ResponseResult.Factory.newInstance(Boolean.FALSE, ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(), null, "缺少验证码参数,请添加该参数后重试。");
}
if(!CaptchaUtil.ver(params.get("captcha").toString(), request)) {
if (!CaptchaUtil.ver(params.get("captcha").toString(), request)) {
return ResponseResult.Factory.newInstance(Boolean.FALSE, ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(), null, "验证码输入错误或已超时,请仔细检查后再试。");
}
if(!params.containsKey("typeid") || StringUtil.isBlank(params.get("typeid"))) {
if (!params.containsKey("typeid") || StringUtil.isBlank(params.get("typeid"))) {
return ResponseResult.Factory.newInstance(Boolean.FALSE, ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(), null, "缺少[typeid]参数,请添加该参数后重试。");
}
if(!params.containsKey("formkey") || StringUtil.isBlank(params.get("formkey"))) {
if (!params.containsKey("formkey") || StringUtil.isBlank(params.get("formkey"))) {
return ResponseResult.Factory.newInstance(Boolean.FALSE, ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(), null, "缺少[formkey]参数,请添加该参数后重试。");
}
@ -549,16 +519,16 @@ public class FrontController extends BaseController {
String formkey = params.get("formkey").toString();
Category categoryWithBLOBs = categoryService.queryCategoryByCode(typeid);
if(categoryWithBLOBs == null) {
if (categoryWithBLOBs == null) {
return ResponseResult.Factory.newInstance(Boolean.FALSE, ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(), null, "栏目不存在,请仔细检查[typeid]参数是否有误,核实后重试。");
}
if(categoryWithBLOBs.getIsInput() != 1) {
if (categoryWithBLOBs.getIsInput() != 1) {
return ResponseResult.Factory.newInstance(Boolean.FALSE, ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(), null, "栏目不允许投稿,请仔细检查栏目的详情并设置是否允许投稿为是后重试。");
}
Form form = formService.queryFormByCode(formkey);
if(form == null) {
if (form == null) {
return ResponseResult.Factory.newInstance(Boolean.FALSE, ExceptionEnum.FORM_PARAMETER_EXCEPTION.getCode(), null, "表单模型不存在,请仔细检查[formkey]参数是否有误,核实后重试。");
}
@ -580,16 +550,16 @@ public class FrontController extends BaseController {
List<Field> fields = fieldService.queryFieldByFormId(form.getId());
Map<String,Object> additional = new LinkedHashMap<String,Object>();
Map<String, Object> additional = new LinkedHashMap<String, Object>();
additional.put("id", IdUtil.getSnowflakeNextIdStr());
additional.put("aid", archives.getId());
for(int i = 0;i < fields.size();i++) {
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
additional.put(field.getFieldName(), params.get(field.getFieldName()));
//用MAP接收参数checkbox需要特殊处理
if("checkbox".equals(field.getDataType())) {
if ("checkbox".equals(field.getDataType())) {
String[] arr = request.getParameterValues(field.getFieldName());
if(arr != null && arr.length > 0) {
if (arr != null && arr.length > 0) {
StringBuffer checkboxVal = new StringBuffer();
for (String string : arr) {
checkboxVal.append(string + ",");
@ -601,7 +571,7 @@ public class FrontController extends BaseController {
String tableName = "system_" + form.getTableName();
try {
archivesService.save(archives,tableName,additional);
archivesService.save(archives, tableName, additional);
} catch (TransactionException e) {
return ResponseResult.Factory.newInstance(Boolean.TRUE, ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(), null, ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getMessage());
}
@ -654,6 +624,7 @@ public class FrontController extends BaseController {
/**
* 输出字符串到页面
*
* @param html 字符
*/
public void outHtml(String html) {
@ -664,6 +635,7 @@ public class FrontController extends BaseController {
httpServletResponse.setHeader("Cache-Control", "no-cache");
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setHeader("Cache-Control", "public");
httpServletResponse.setDateHeader("Expires", 0L);
httpServletResponse.getWriter().write(html);
httpServletResponse.flushBuffer();