123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 'use strict';
- const ManagerController = require('./manager.js');
- /**
- * [文章分类管理控制器]
- * @type {[type]}
- */
- module.exports = class ArticlesCategoryController extends ManagerController {
- /**
- * [useModel 使用模型]
- * @author szjcomo
- * @date 2021-01-27
- * @return {[type]} [description]
- */
- get useModel() {
- let that = this;
- return that.app.model.ArticlesCategory;
- }
- /**
- * [createValidate 分类添加验证器]
- * @author szjcomo
- * @date 2021-01-27
- * @return {[type]} [description]
- */
- get createValidate() {
- let that = this;
- return {
- category_name:that.ctx.rules.name('分类名称').required().notEmpty().trim().maxLength(200),
- category_desc:that.ctx.rules.default('').required(),
- category_image:that.ctx.rules.default('').required(),
- pid:that.ctx.rules.default(0).number(),
- category_sort:that.ctx.rules.default(99).required().number(),
- category_status:that.ctx.rules.default(1).required().number(),
- category_level:that.ctx.rules.default(1).number().extend(async (field,value,row) => {
- if(row.pid > 0) {
- let info = await that.useModel.findOne({where:{category_id:row.pid},attributes:['category_level'],raw:true});
- if(!info) return '上级分类不存在,请检查';
- row[field] = (info.category_level+1);
- }
- }),
- category_recommend:that.ctx.rules.default(0).number(),
- admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number(),
- create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required()
- };
- }
- /**
- * [deleteValidate 删除文件分类验证器]
- * @author szjcomo
- * @date 2021-01-27
- * @return {[type]} [description]
- */
- get deleteValidate() {
- let that = this;
- return {
- category_id:that.ctx.rules.name('文章分类ID').required().number().extend(async (field,value,row) => {
- let info = await that.useModel.findOne({where:{pid:value},attributes:['category_id']});
- if(info) return '当前分类下还有子分类,无法删除';
- let articleInfo = await that.app.model.Articles.findOne({where:{category_id:value},attributes:['category_id']});
- if(articleInfo) return '当前分类下还有文章,无法删除';
- })
- };
- }
- /**
- * [select 查询文章分类列表]
- * @author szjcomo
- * @date 2021-01-27
- * @return {[type]} [description]
- */
- async select() {
- let that = this;
- try {
- let result = await that.service.articles.getArticlesCategoryAll({order:[['category_id','asc'],['category_sort','asc']]});
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
- } catch(err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [update 文章分类更新]
- * @author szjcomo
- * @date 2021-01-27
- * @return {[type]} [description]
- */
- async update() {
- let that = this;
- try {
- let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
- if(data.hasOwnProperty('pid')) {
- if(data.pid == 0) {
- data.category_level = 1;
- } else {
- let info = await that.useModel.findOne({where:{category_id:data.pid},attributes:['category_level'],raw:true});
- data.category_level = (info.category_level + 1);
- }
- }
- let updateBean = that.app.comoBean.instance(data,{where:{category_id:data.category_id},fields:Object.keys(data)});
- let result = await that.service.manager.update(updateBean,that.useModel,'文章分类更新失败,请重试');
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
- } catch(err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [delete 删除文章分类]
- * @author szjcomo
- * @date 2021-01-27
- * @return {[type]} [description]
- */
- async delete() {
- let that = this;
- try {
- let data = await that.ctx.validate(that.deleteValidate,await that.ctx.anyParse());
- let deleteBean = that.app.comoBean.instance(data,{where:{category_id:data.category_id}});
- let result = await that.service.manager.delete(deleteBean,that.useModel,'文章分类删除失败,请重试');
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
- } catch(err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- }
|