net.mingsoft.cms.action.CategoryAction Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2012-present 铭软科技(mingsoft.net)
* 本软件及相关文档文件(以下简称“软件”)的版权归 铭软科技 所有
* 遵循 铭软科技《服务协议》中的《保密条款》
*/
package net.mingsoft.cms.action;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.PinYinUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.cms.biz.ICategoryBiz;
import net.mingsoft.cms.constant.e.CategoryTypeEnum;
import net.mingsoft.cms.entity.CategoryEntity;
import net.mingsoft.mdiy.util.ParserUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
/**
* 分类管理控制层
* @author 铭飞开发团队
* 创建日期:2019-11-28 15:12:32
* 历史修订:
*/
@Api(tags = {"后端-内容模块接口"})
@Controller("cmsCategoryAction")
@RequestMapping("/${ms.manager.path}/cms/category")
public class CategoryAction extends BaseAction {
/**
* 注入分类业务层
*/
@Autowired
private ICategoryBiz categoryBiz;
/**
* 返回主界面index
* @return
*/
@ApiIgnore
@GetMapping("/index")
@RequiresPermissions("cms:category:view")
public String index() {
return "/cms/category/index";
}
/**
* 查询分类列表接口
* @param category 栏目实体
* @return
*/
@ApiOperation(value = "查询分类列表接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "categoryTitle", value = "栏目管理名称", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryParentId", value = "父类型编号", required = false, paramType = "query"),
})
@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public ResultData list(@ModelAttribute @ApiIgnore CategoryEntity category) {
List categoryList = categoryBiz.list(new LambdaQueryWrapper(category));
return ResultData.build().success(new EUListBean(categoryList, categoryList.size()));
}
/**
* 返回编辑界面category_form
* @param category 栏目
* @return
*/
@ApiIgnore
@GetMapping("/form")
public String form(@ModelAttribute CategoryEntity category, ModelMap model) {
model.addAttribute("appId", BasicUtil.getApp().getAppId());
return "/cms/category/form";
}
/**
* 获取分类
* @param category 分类实体
*/
@ApiOperation(value = "获取分类详情接口")
@ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "query")
@GetMapping("/get")
@RequiresPermissions("cms:category:view")
@ResponseBody
public ResultData get(@ModelAttribute @ApiIgnore CategoryEntity category) {
if (StringUtils.isBlank(category.getId())) {
return ResultData.build().error(getResString("err.empty",this.getResString("id")));
}
CategoryEntity _category = (CategoryEntity) categoryBiz.getById(category.getId());
return ResultData.build().success(_category);
}
/**
* 保存分类
* @param category 分类实体
*/
@ApiOperation(value = "保存分类列表接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "categoryTitle", value = "栏目管理名称", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryType", value = "栏目类型,1:列表,2:单篇,3:链接", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryDisplay", value = "栏目是否显示,enable:显示 disable:不显示", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryIsSearch", value = "栏目是否可搜索,enable:可搜索 disable:不可搜索", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryId", value = "所属栏目", required = false, paramType = "query"),
@ApiImplicitParam(name = "categorySort", value = "自定义顺序", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryListUrl", value = "列表模板", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryUrl", value = "内容模板", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryKeyword", value = "栏目管理关键字", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryDescrip", value = "栏目管理描述", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryImg", value = "缩略图", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryIco", value = "栏目小图", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryDiyUrl", value = "自定义链接", required = false, paramType = "query"),
@ApiImplicitParam(name = "mdiyModelId", value = "栏目管理的内容模型id", required = false, paramType = "query"),
@ApiImplicitParam(name = "dictId", value = "字典对应编号", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryFlag", value = "栏目属性", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryPath", value = "栏目路径", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryParentIds", value = "父类型编号", required = false, paramType = "query"),
})
@PostMapping("/save")
@ResponseBody
@LogAnn(title = "保存分类", businessType = BusinessTypeEnum.INSERT)
@RequiresPermissions("cms:category:save")
public ResultData save(@ModelAttribute @ApiIgnore CategoryEntity category) {
//验证缩略图参数值是否合法
if (category.getCategoryImg() == null || !category.getCategoryImg().matches("^\\[.{1,}]$")) {
category.setCategoryImg("");
}
//验证栏目管理名称的值是否合法
if (StringUtil.isBlank(category.getCategoryTitle())) {
return ResultData.build().error(getResString("err.empty", this.getResString("category.title")));
}
if (!StringUtil.checkLength(category.getCategoryTitle() + "", 1, 100)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.title"), "1", "100"));
}
//验证栏目描述的值是否合法
if (!StringUtil.checkLength(category.getCategoryDescrip() + "", 0, 500)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.descrip"), "0", "500"));
}
//验证栏目关键字的值是否合法
if (!StringUtil.checkLength(category.getCategoryKeyword() + "", 0, 300)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.keyword"), "0", "300"));
}
//验证栏目路径的值是否合法
if (!StringUtil.checkLength(category.getCategoryPath() + "", 0, 500)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.path"), "0", "500"));
}
//验证栏目列表模板的值是否合法
if (!StringUtil.checkLength(category.getCategoryListUrl() + "", 0, 50)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.list.url"), "0", "50"));
}
//验证栏目内容模板的值是否合法
if (!StringUtil.checkLength(category.getCategoryUrl() + "", 0, 50)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.url"), "0", "50"));
}
//验证栏目自定义链接的值是否合法
if (!StringUtil.checkLength(category.getCategoryDiyUrl() + "", 0, 255)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.diy.url"), "0", "255"));
}
//验证栏目属性的值是否合法
if (!StringUtil.checkLength(category.getCategoryFlag() + "", 0, 20)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.flag"), "0", "20"));
}
//验证栏目类型的值是否合法
if (!StringUtil.checkLength(category.getCategoryType() + "", 0, 1)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.type"), "0", "1"));
}
//验证栏目内容模型id的值是否合法
if (!StringUtil.checkLength(category.getMdiyModelId() + "", 0, 50)) {
return ResultData.build().error(getResString("err.length", this.getResString("mdiy.model.id"), "0", "50"));
}
// 判断前端拼音传值是否正常
if (!StringUtil.checkLength(category.getCategoryPinyin() + "", 0, 100)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.pinyin"), "1", "100"));
}
// 过滤非法路径
if (FileNameUtil.containsInvalid(category.getCategoryPinyin())) {
return ResultData.build().error(this.getResString("err.error",this.getResString("category.pinyin")));
}
//验证栏目父ids是否正常
if (!StringUtil.checkLength(category.getCategoryParentIds() + "", 1, 100)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.parent.id"), "1", "100"));
}
//判断拼音是否重复
if (StrUtil.isNotBlank(category.getCategoryPinyin())) {
if (!category.getCategoryPinyin().matches("^[a-zA-Z0-9]*$")){
return ResultData.build().error(this.getResString("err.error",this.getResString("category.pinyin")));
}
CategoryEntity _category = new CategoryEntity();
_category.setCategoryPinyin(category.getCategoryPinyin());
List query = categoryBiz.query(_category);
if (query.size() > 0) {
return ResultData.build().error(getResString("err.exist", this.getResString("category.pinyin")));
}
}
categoryBiz.saveEntity(category);
return ResultData.build().success(category);
}
/**
* 批量删除分类列表接口
* @param categorys
* @return
*/
@ApiOperation(value = "批量删除分类列表接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "categorys", value = "删除的栏目集合", allowMultiple = true, dataType = "CategoryEntity", required = true)
})
@PostMapping("/delete")
@ResponseBody
@LogAnn(title = "删除分类", businessType = BusinessTypeEnum.DELETE)
@RequiresPermissions("cms:category:del")
public ResultData delete(@RequestBody List categorys) {
for (int i = 0; i < categorys.size(); i++) {
categoryBiz.delete(categorys.get(i).getId());
}
return ResultData.build().success();
}
/**
* 更新分类列表
* @param category 分类实体
*/
@ApiOperation(value = "更新分类列表接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryTitle", value = "栏目管理名称", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryDisplay", value = "栏目是否显示,enable:显示 disable:不显示", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryIsSearch", value = "栏目是否可搜索,enable:可搜索 disable:不可搜索", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryType", value = "栏目类型,1:列表,2:单篇,3:链接", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryId", value = "所属栏目", required = false, paramType = "query"),
@ApiImplicitParam(name = "categorySort", value = "自定义顺序", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryListUrl", value = "列表模板", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryUrl", value = "内容模板", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryKeyword", value = "栏目管理关键字", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryDescrip", value = "栏目管理描述", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryImg", value = "banner图", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryIco", value = "栏目小图", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryDiyUrl", value = "自定义链接", required = false, paramType = "query"),
@ApiImplicitParam(name = "mdiyModelId", value = "栏目管理的内容模型id", required = false, paramType = "query"),
@ApiImplicitParam(name = "dictId", value = "字典对应编号", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryFlag", value = "栏目属性", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryPath", value = "栏目路径", required = false, paramType = "query"),
@ApiImplicitParam(name = "categoryParentIds", value = "父类型编号", required = false, paramType = "query"),
})
@PostMapping("/update")
@ResponseBody
@LogAnn(title = "更新分类", businessType = BusinessTypeEnum.UPDATE)
@RequiresPermissions("cms:category:update")
public ResultData update(@ModelAttribute @ApiIgnore CategoryEntity category) {
//验证缩略图参数值是否合法
if (category.getCategoryImg() == null || !category.getCategoryImg().matches("^\\[.{1,}]$")) {
category.setCategoryImg("");
}
if (StringUtils.isEmpty(category.getId())){
return ResultData.build().error(getResString("err.empty", this.getResString("category.id")));
}
//验证栏目管理名称的值是否合法
if (StringUtil.isBlank(category.getCategoryTitle())) {
return ResultData.build().error(getResString("err.empty", this.getResString("category.title")));
}
if (!StringUtil.checkLength(category.getCategoryTitle() + "", 1, 100)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.title"), "1", "100"));
}
//验证栏目描述的值是否合法
if (!StringUtil.checkLength(category.getCategoryDescrip() + "", 0, 500)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.descrip"), "0", "500"));
}
//验证栏目关键字的值是否合法
if (!StringUtil.checkLength(category.getCategoryKeyword() + "", 0, 300)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.keyword"), "0", "300"));
}
//验证栏目自定义链接的值是否合法
if (!StringUtil.checkLength(category.getCategoryDiyUrl() + "", 0, 255)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.diy.url"), "0", "255"));
}
//验证栏目属性的值是否合法
if (!StringUtil.checkLength(category.getCategoryFlag() + "", 0, 20)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.flag"), "0", "20"));
}
//验证栏目列表模板的值是否合法
if (!StringUtil.checkLength(category.getCategoryListUrl() + "", 0, 50)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.list.url"), "0", "50"));
}
//验证栏目路径的值是否合法
if (!StringUtil.checkLength(category.getCategoryPath() + "", 0, 500)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.path"), "0", "500"));
}
//验证栏目类型的值是否合法
if (!StringUtil.checkLength(category.getCategoryType() + "", 0, 1)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.type"), "0", "1"));
}
//验证栏目内容模板的值是否合法
if (!StringUtil.checkLength(category.getCategoryUrl() + "", 0, 50)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.url"), "0", "50"));
}
//验证栏目内容模型id的值是否合法
if (!StringUtil.checkLength(category.getMdiyModelId() + "", 0, 50)) {
return ResultData.build().error(getResString("err.length", this.getResString("mdiy.model.id"), "0", "50"));
}
//验证栏目父ids值是否合法
if (!StringUtil.checkLength(category.getCategoryParentIds() + "", 0, 100)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.parent.id"), "1", "100"));
}
// 过滤非法路径
if (FileNameUtil.containsInvalid(category.getCategoryPinyin())) {
return ResultData.build().error(this.getResString("err.error",this.getResString("category.pinyin")));
}
//判断拼音是否重复并且是否和原拼音相同
if (StrUtil.isNotBlank(category.getCategoryPinyin()) && !categoryBiz.getById(category.getId()).getCategoryPinyin().equals(category.getCategoryPinyin())) {
if (!category.getCategoryPinyin().matches("^[a-zA-Z0-9]*$")){
return ResultData.build().error(this.getResString("err.error",this.getResString("category.pinyin")));
}
CategoryEntity _category = new CategoryEntity();
_category.setCategoryPinyin(category.getCategoryPinyin());
List query = categoryBiz.query(_category);
if (query.size() > 0) {
return ResultData.build().error(getResString("err.exist", this.getResString("category.pinyin")));
}
}
String pingYin = PinYinUtil.getPingYin(category.getCategoryTitle());
//如果用户填写了拼音则使用用户填写的
if (StrUtil.isNotBlank(category.getCategoryPinyin()) && StringUtil.checkLength(category.getCategoryPinyin() + "", 1, 100)) {
pingYin = category.getCategoryPinyin();
}
CategoryEntity categoryEntity = new CategoryEntity();
categoryEntity.setCategoryPinyin(pingYin);
CategoryEntity categoryBizEntity = categoryBiz.getEntity(categoryEntity);
category.setCategoryPinyin(pingYin);
//如果存在此拼音栏目则拼接上id
if (categoryBizEntity != null && !categoryBizEntity.getId().equals(category.getId())) {
category.setCategoryPinyin(pingYin + category.getId());
}
//判断是否选择子级为所属栏目
CategoryEntity _category = new CategoryEntity();
_category.setId(category.getId());
List categoryList = categoryBiz.queryChildren(_category);
if (categoryList.size() > 1) {
for (CategoryEntity item : categoryList) {
if (item.getId().equals(category.getCategoryId())) {
return ResultData.build().error(getResString("cannot.select.child"));
}
}
category.setLeaf(false);
} else {
category.setLeaf(true);
}
// 这里不能使用mybitsplus 存在业务
categoryBiz.updateEntity(category);
return ResultData.build().success(category);
}
/**
* 验证拼音
* @param category 栏目
* @return
*/
@ApiOperation(value = "验证拼音")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "query"),
@ApiImplicitParam(name = "categoryPinyin", value = "栏目管拼音", required = true, paramType = "query"),
})
@GetMapping("/verifyPingYin")
@ResponseBody
public ResultData verifyPingYin(@ModelAttribute @ApiIgnore CategoryEntity category) {
//验证id是否合法
if (StringUtils.isEmpty(category.getId())){
return ResultData.build().error(getResString("err.empty", this.getResString("id")));
}
if (!StringUtil.checkLength(category.getId() + "", 0, 20)) {
return ResultData.build().error(getResString("err.length", this.getResString("id"), "0", "20"));
}
//验证拼音是否合法
if (StringUtils.isEmpty(category.getCategoryPinyin())){
return ResultData.build().error(getResString("err.empty", this.getResString("category.pinyin")));
}
if (FileNameUtil.containsInvalid(category.getCategoryPinyin())) {
return ResultData.build().error(this.getResString("err.error",this.getResString("category.pinyin")));
}
if (!StringUtil.checkLength(category.getCategoryPinyin() + "", 0, 255)) {
return ResultData.build().error(getResString("err.length", this.getResString("category.pinyin"), "0", "255"));
}
long count = categoryBiz.count(Wrappers.lambdaQuery()
.ne(StrUtil.isNotBlank(category.getId()), CategoryEntity::getId, category.getId())
.eq(CategoryEntity::getCategoryPinyin, category.getCategoryPinyin()));
if (count > 0) {
return ResultData.build().error("存在相同拼音的栏目");
}
return ResultData.build().success();
}
/**
* 批量更新模板
* @param category 栏目实体
* @return
*/
@ApiOperation(value = "批量更新模板")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "query"),
})
@GetMapping("/updateTemplate")
@ResponseBody
@RequiresPermissions("cms:category:update")
public ResultData updateTemplate(@ModelAttribute @ApiIgnore CategoryEntity category) {
if (category == null || StringUtils.isEmpty(category.getId())) {
return ResultData.build().error(getResString("err.error", this.getResString("id")));
}
category = categoryBiz.getById(category.getId());
category.setCategoryParentIds(null);
List childs = categoryBiz.queryChildren(category);
//更新与父节点相同类型的子栏目的模板内容
for (int i = 0; i < childs.size(); i++) {
if (childs.get(i).getCategoryType().equals(category.getCategoryType())) {
childs.get(i).setCategoryUrl(category.getCategoryUrl());
childs.get(i).setCategoryListUrl(category.getCategoryListUrl());
categoryBiz.updateEntity(childs.get(i));
}
}
return ResultData.build().success();
}
/**
* 复制栏目
* @param category 栏目实体
* @return
*/
@ApiOperation(value = "复制栏目")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "query")
})
@GetMapping("/copyCategory")
@ResponseBody
@RequiresPermissions("cms:category:save")
public ResultData copyCategory(@ModelAttribute @ApiIgnore CategoryEntity category) {
if (category == null || StringUtils.isEmpty(category.getId())) {
return ResultData.build().error(getResString("err.error", this.getResString("id")));
}
categoryBiz.copyCategory(category);
return ResultData.build().success();
}
@ApiOperation(value = "强制转换类型接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "typeid", value = "编号", required =true,paramType="query"),
@ApiImplicitParam(name = "categoryType", value = "栏目类型,1:列表,2:单篇,3:链接", required =true,paramType="query")
})
@GetMapping("/changeType")
@ResponseBody
@RequiresPermissions("cms:category:update")
public ResultData changeType(){
String typeId = BasicUtil.getString(ParserUtil.TYPE_ID);
String categoryType = BasicUtil.getString("categoryType");
CategoryEntity category = categoryBiz.getById(typeId);
if (category == null){
return ResultData.build().error(getResString("err.error",getResString("id")));
}
if (CategoryTypeEnum.get(categoryType).equals(CategoryTypeEnum.UN_KNOW)){
return ResultData.build().error(getResString("err.error",getResString("category.type")));
}
categoryBiz.changeCategoryType(category,categoryType);
return ResultData.build().success(category);
}
}