'use strict'; const ManagerController = require('./manager.js'); /** * [文章管理控制器] * @type {[type]} */ module.exports = class ArticlesController extends ManagerController { /** * [useModel 使用模型] * @author szjcomo * @date 2021-01-27 * @return {[type]} [description] */ get useModel() { let that = this; return that.app.model.Articles; } /** * [createValidate 添加文章] * @author szjcomo * @date 2021-01-27 * @return {[type]} [description] */ get createValidate() { let that = this; return { article_title:that.ctx.rules.name('文章标题').required().notEmpty().trim().maxLength(200), article_content:that.ctx.rules.name('文章内容').required().notEmpty().extend(async (field,value,row) => { row[field] = await that.ctx.postParse(field,'',(val) => {return val;}); }), category_id:that.ctx.rules.name('文章分类ID').required().number(), article_image:that.ctx.rules.default('').required(), article_type:that.ctx.rules.default(0).number(), article_desc:that.ctx.rules.default('').required(), article_tags:that.ctx.rules.default('').required(), view_auth:that.ctx.rules.default(0).required().number(), article_status:that.ctx.rules.default(1).number(), article_author:that.ctx.rules.default('').required(), article_views:that.ctx.rules.default(10).number(), article_template_info:that.ctx.rules.default('').required(), article_recommend:that.ctx.rules.default(0).number(), article_template_list:that.ctx.rules.default('').required(), article_sort:that.ctx.rules.default(99).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() }; } /** * [selectValidate 文章查询验证器] * @author szjcomo * @date 2021-01-27 * @return {[type]} [description] */ get selectValidate() { let that = this; return { page:that.ctx.rules.default(1).number(), limit:that.ctx.rules.default(20).number(), article_title:that.ctx.rules.default('').required(), category_id:that.ctx.rules.default(0).number(), article_id:that.ctx.rules.default(0).number() }; } /** * [pkValidate 主建验证] * @author szjcomo * @date 2021-01-27 * @return {[type]} [description] */ get pkValidate() { let that = this; return { article_id:that.ctx.rules.name('文章ID').required().number(), admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number() }; } /** * [videoValidate 管理员获取播放列表] * @author szjcomo * @date 2021-04-12 * @return {[type]} [description] */ get videoValidate() { let that = this; return { admin_user:that.ctx.rules.default(that.service.manager.ActionAdminUser()).required() }; } /** * [select 查看文章列表] * @author szjcomo * @date 2021-01-27 * @return {[type]} [description] */ async select() { let that = this; try { let data = await that.ctx.validate(that.selectValidate,await that.ctx.getParse()); if(data.article_id > 0) { let info = await that.selectInfo(data.article_id); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',info,false)); } let seq = that.app.Sequelize; let options = { offset:(data.page - 1) * data.limit, limit:data.limit,where:{}, order:[['article_id','desc']], include:[ {model:that.app.model.AdminUser,as:'admin_user',attributes:[]}, {model:that.app.model.ArticlesCategory,as:'category',attributes:[]} ], attributes:{ include:[[seq.col('admin_user.username'),'username'],[seq.col('category.category_name'),'category_name']], exclude:['article_content','article_desc'] } }; if(data.article_title.length > 0) options.where.article_title = {[seq.Op.like]:`%${data.article_title}%`}; if(data.category_id > 0) { let allChildCategorys = await that.service.articles.getArticlesCategoryAllChild(data.category_id); options.where.category_id = {[seq.Op.in]:allChildCategorys}; } let selectBean = that.app.comoBean.instance(data,options); let result = await that.service.manager.select(selectBean,that.useModel,'文章列表查询失败,请重试',true); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [selectInfo 查询文章详情] * @author szjcomo * @date 2021-01-27 * @param {[type]} article_id [description] * @return {[type]} [description] */ async selectInfo(article_id) { let that = this; let selectBean = that.app.comoBean.instance({},{where:{article_id:article_id}}); let info = await that.service.manager.select(selectBean,that.useModel,'文章详情查询失败,请重试',false); return info; } /** * [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.article_content) { data.article_content = await that.ctx.anyParse('article_content','',(val) => {return val;}); } let updateBean = that.app.comoBean.instance(data,{where:{article_id:data.article_id},fields:Object.keys(data)}); let result = await that.service.manager.update(updateBean,that.useModel,'文章更新失败,请重试'); // await that.updateBeanAfter(data); await that.deleteArticleCache(data.article_id); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [updateBeanAfter 置顶文章操作] * @author szjcomo * @date 2021-04-08 * @param {[type]} app [description] * @return {[type]} [description] */ async updateBeanAfter(data = {}) { let that = this; if(data.hasOwnProperty('article_recommend')) { let value = Number(data.article_recommend); let website_notice_list = await that.service.configs.getConfigValue('website_notice_list'); let tmpvalue; if(website_notice_list || false) { tmpvalue = that.app.szjcomo.json_decode(website_notice_list); } else { tmpvalue = []; } let result = []; tmpvalue.forEach(item => { if(item.type == 'article') { if(item.id != data.article_id) result.push(item); } else { result.push(item); } }) if(value) { let info = await that.useModel.findOne({where:{article_id:data.article_id},attributes:['article_title']}); result.push({type:'article',id:data.article_id,text:info.dataValues.article_title}); } await that.app.model.Configs.update({field_value:that.app.szjcomo.json_encode(result)},{ where:{field_index:'website_notice_list'} }); await that.service.configs.clear(); } } /** * [delete 文章删除] * @author szjcomo * @date 2021-01-27 * @return {[type]} [description] */ async delete() { let that = this; try { let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse()); let deleteBean = that.app.comoBean.instance(data,{where:{article_id:data.article_id}}); let result = await that.service.manager.delete(deleteBean,that.useModel,'文章删除失败,请稍候重试'); await that.deleteArticleCache(data.article_id); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [deleteArticleCache 删除文章缓存] * @author szjcomo * @date 2021-04-03 * @return {[type]} [description] */ async deleteArticleCache(article_id) { let that = this; //let cache_key = `article_id_${article_id}`; //await that.service.redis.del(cache_key); } /** * [videoList 获取视频列表] * @author szjcomo * @date 2021-04-12 * @return {[type]} [description] */ async videoList() { let that = this; try { let data = await that.ctx.validate(that.videoValidate,await that.ctx.getParse()); let tmp = await that.service.configs.getConfigValue('article_category_insinuate'); let tmpdata = that.app.szjcomo.json_decode(tmp); let seq = that.app.Sequelize; let options = {where:{article_type:1,article_status:1,category_id:tmpdata['manager_video']},attributes:['article_title','article_image','article_desc']}; if(data.admin_user.role_flag) options.where.view_auth = {[seq.Op.in]:[0,1]}; let result = await that.useModel.findAll(options); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } }