articles_category.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. const ManagerController = require('./manager.js');
  3. /**
  4. * [文章分类管理控制器]
  5. * @type {[type]}
  6. */
  7. module.exports = class ArticlesCategoryController extends ManagerController {
  8. /**
  9. * [useModel 使用模型]
  10. * @author szjcomo
  11. * @date 2021-01-27
  12. * @return {[type]} [description]
  13. */
  14. get useModel() {
  15. let that = this;
  16. return that.app.model.ArticlesCategory;
  17. }
  18. /**
  19. * [createValidate 分类添加验证器]
  20. * @author szjcomo
  21. * @date 2021-01-27
  22. * @return {[type]} [description]
  23. */
  24. get createValidate() {
  25. let that = this;
  26. return {
  27. category_name:that.ctx.rules.name('分类名称').required().notEmpty().trim().maxLength(200),
  28. category_desc:that.ctx.rules.default('').required(),
  29. category_image:that.ctx.rules.default('').required(),
  30. pid:that.ctx.rules.default(0).number(),
  31. category_sort:that.ctx.rules.default(99).required().number(),
  32. category_status:that.ctx.rules.default(1).required().number(),
  33. category_level:that.ctx.rules.default(1).number().extend(async (field,value,row) => {
  34. if(row.pid > 0) {
  35. let info = await that.useModel.findOne({where:{category_id:row.pid},attributes:['category_level'],raw:true});
  36. if(!info) return '上级分类不存在,请检查';
  37. row[field] = (info.category_level+1);
  38. }
  39. }),
  40. category_recommend:that.ctx.rules.default(0).number(),
  41. admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number(),
  42. create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required()
  43. };
  44. }
  45. /**
  46. * [deleteValidate 删除文件分类验证器]
  47. * @author szjcomo
  48. * @date 2021-01-27
  49. * @return {[type]} [description]
  50. */
  51. get deleteValidate() {
  52. let that = this;
  53. return {
  54. category_id:that.ctx.rules.name('文章分类ID').required().number().extend(async (field,value,row) => {
  55. let info = await that.useModel.findOne({where:{pid:value},attributes:['category_id']});
  56. if(info) return '当前分类下还有子分类,无法删除';
  57. let articleInfo = await that.app.model.Articles.findOne({where:{category_id:value},attributes:['category_id']});
  58. if(articleInfo) return '当前分类下还有文章,无法删除';
  59. })
  60. };
  61. }
  62. /**
  63. * [select 查询文章分类列表]
  64. * @author szjcomo
  65. * @date 2021-01-27
  66. * @return {[type]} [description]
  67. */
  68. async select() {
  69. let that = this;
  70. try {
  71. let result = await that.service.articles.getArticlesCategoryAll({order:[['category_id','asc'],['category_sort','asc']]});
  72. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  73. } catch(err) {
  74. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  75. }
  76. }
  77. /**
  78. * [update 文章分类更新]
  79. * @author szjcomo
  80. * @date 2021-01-27
  81. * @return {[type]} [description]
  82. */
  83. async update() {
  84. let that = this;
  85. try {
  86. let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
  87. if(data.hasOwnProperty('pid')) {
  88. if(data.pid == 0) {
  89. data.category_level = 1;
  90. } else {
  91. let info = await that.useModel.findOne({where:{category_id:data.pid},attributes:['category_level'],raw:true});
  92. data.category_level = (info.category_level + 1);
  93. }
  94. }
  95. let updateBean = that.app.comoBean.instance(data,{where:{category_id:data.category_id},fields:Object.keys(data)});
  96. let result = await that.service.manager.update(updateBean,that.useModel,'文章分类更新失败,请重试');
  97. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  98. } catch(err) {
  99. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  100. }
  101. }
  102. /**
  103. * [delete 删除文章分类]
  104. * @author szjcomo
  105. * @date 2021-01-27
  106. * @return {[type]} [description]
  107. */
  108. async delete() {
  109. let that = this;
  110. try {
  111. let data = await that.ctx.validate(that.deleteValidate,await that.ctx.anyParse());
  112. let deleteBean = that.app.comoBean.instance(data,{where:{category_id:data.category_id}});
  113. let result = await that.service.manager.delete(deleteBean,that.useModel,'文章分类删除失败,请重试');
  114. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  115. } catch(err) {
  116. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  117. }
  118. }
  119. }