articles.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. 'use strict';
  2. const ManagerController = require('./manager.js');
  3. /**
  4. * [文章管理控制器]
  5. * @type {[type]}
  6. */
  7. module.exports = class ArticlesController 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.Articles;
  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. article_title:that.ctx.rules.name('文章标题').required().notEmpty().trim().maxLength(200),
  28. article_content:that.ctx.rules.name('文章内容').required().notEmpty().extend(async (field,value,row) => {
  29. row[field] = await that.ctx.postParse(field,'',(val) => {return val;});
  30. }),
  31. category_id:that.ctx.rules.name('文章分类ID').required().number(),
  32. article_image:that.ctx.rules.default('').required(),
  33. article_type:that.ctx.rules.default(0).number(),
  34. article_desc:that.ctx.rules.default('').required(),
  35. article_tags:that.ctx.rules.default('').required(),
  36. view_auth:that.ctx.rules.default(0).required().number(),
  37. article_status:that.ctx.rules.default(1).number(),
  38. article_author:that.ctx.rules.default('').required(),
  39. article_views:that.ctx.rules.default(10).number(),
  40. article_template_info:that.ctx.rules.default('').required(),
  41. article_recommend:that.ctx.rules.default(0).number(),
  42. article_template_list:that.ctx.rules.default('').required(),
  43. article_sort:that.ctx.rules.default(99).number(),
  44. admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number(),
  45. create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required()
  46. };
  47. }
  48. /**
  49. * [selectValidate 文章查询验证器]
  50. * @author szjcomo
  51. * @date 2021-01-27
  52. * @return {[type]} [description]
  53. */
  54. get selectValidate() {
  55. let that = this;
  56. return {
  57. page:that.ctx.rules.default(1).number(),
  58. limit:that.ctx.rules.default(20).number(),
  59. article_title:that.ctx.rules.default('').required(),
  60. category_id:that.ctx.rules.default(0).number(),
  61. article_id:that.ctx.rules.default(0).number()
  62. };
  63. }
  64. /**
  65. * [pkValidate 主建验证]
  66. * @author szjcomo
  67. * @date 2021-01-27
  68. * @return {[type]} [description]
  69. */
  70. get pkValidate() {
  71. let that = this;
  72. return {
  73. article_id:that.ctx.rules.name('文章ID').required().number(),
  74. admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number()
  75. };
  76. }
  77. /**
  78. * [videoValidate 管理员获取播放列表]
  79. * @author szjcomo
  80. * @date 2021-04-12
  81. * @return {[type]} [description]
  82. */
  83. get videoValidate() {
  84. let that = this;
  85. return {
  86. admin_user:that.ctx.rules.default(that.service.manager.ActionAdminUser()).required()
  87. };
  88. }
  89. /**
  90. * [select 查看文章列表]
  91. * @author szjcomo
  92. * @date 2021-01-27
  93. * @return {[type]} [description]
  94. */
  95. async select() {
  96. let that = this;
  97. try {
  98. let data = await that.ctx.validate(that.selectValidate,await that.ctx.getParse());
  99. if(data.article_id > 0) {
  100. let info = await that.selectInfo(data.article_id);
  101. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',info,false));
  102. }
  103. let seq = that.app.Sequelize;
  104. let options = {
  105. offset:(data.page - 1) * data.limit,
  106. limit:data.limit,where:{},
  107. order:[['article_id','desc']],
  108. include:[
  109. {model:that.app.model.AdminUser,as:'admin_user',attributes:[]},
  110. {model:that.app.model.ArticlesCategory,as:'category',attributes:[]}
  111. ],
  112. attributes:{
  113. include:[[seq.col('admin_user.username'),'username'],[seq.col('category.category_name'),'category_name']],
  114. exclude:['article_content','article_desc']
  115. }
  116. };
  117. if(data.article_title.length > 0) options.where.article_title = {[seq.Op.like]:`%${data.article_title}%`};
  118. if(data.category_id > 0) {
  119. let allChildCategorys = await that.service.articles.getArticlesCategoryAllChild(data.category_id);
  120. options.where.category_id = {[seq.Op.in]:allChildCategorys};
  121. }
  122. let selectBean = that.app.comoBean.instance(data,options);
  123. let result = await that.service.manager.select(selectBean,that.useModel,'文章列表查询失败,请重试',true);
  124. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  125. } catch(err) {
  126. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  127. }
  128. }
  129. /**
  130. * [selectInfo 查询文章详情]
  131. * @author szjcomo
  132. * @date 2021-01-27
  133. * @param {[type]} article_id [description]
  134. * @return {[type]} [description]
  135. */
  136. async selectInfo(article_id) {
  137. let that = this;
  138. let selectBean = that.app.comoBean.instance({},{where:{article_id:article_id}});
  139. let info = await that.service.manager.select(selectBean,that.useModel,'文章详情查询失败,请重试',false);
  140. return info;
  141. }
  142. /**
  143. * [update 文章更新]
  144. * @author szjcomo
  145. * @date 2021-01-27
  146. * @return {[type]} [description]
  147. */
  148. async update() {
  149. let that = this;
  150. try {
  151. let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
  152. if(data.article_content) {
  153. data.article_content = await that.ctx.anyParse('article_content','',(val) => {return val;});
  154. }
  155. let updateBean = that.app.comoBean.instance(data,{where:{article_id:data.article_id},fields:Object.keys(data)});
  156. let result = await that.service.manager.update(updateBean,that.useModel,'文章更新失败,请重试');
  157. // await that.updateBeanAfter(data);
  158. await that.deleteArticleCache(data.article_id);
  159. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  160. } catch(err) {
  161. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  162. }
  163. }
  164. /**
  165. * [updateBeanAfter 置顶文章操作]
  166. * @author szjcomo
  167. * @date 2021-04-08
  168. * @param {[type]} app [description]
  169. * @return {[type]} [description]
  170. */
  171. async updateBeanAfter(data = {}) {
  172. let that = this;
  173. if(data.hasOwnProperty('article_recommend')) {
  174. let value = Number(data.article_recommend);
  175. let website_notice_list = await that.service.configs.getConfigValue('website_notice_list');
  176. let tmpvalue;
  177. if(website_notice_list || false) {
  178. tmpvalue = that.app.szjcomo.json_decode(website_notice_list);
  179. } else {
  180. tmpvalue = [];
  181. }
  182. let result = [];
  183. tmpvalue.forEach(item => {
  184. if(item.type == 'article') {
  185. if(item.id != data.article_id) result.push(item);
  186. } else {
  187. result.push(item);
  188. }
  189. })
  190. if(value) {
  191. let info = await that.useModel.findOne({where:{article_id:data.article_id},attributes:['article_title']});
  192. result.push({type:'article',id:data.article_id,text:info.dataValues.article_title});
  193. }
  194. await that.app.model.Configs.update({field_value:that.app.szjcomo.json_encode(result)},{
  195. where:{field_index:'website_notice_list'}
  196. });
  197. await that.service.configs.clear();
  198. }
  199. }
  200. /**
  201. * [delete 文章删除]
  202. * @author szjcomo
  203. * @date 2021-01-27
  204. * @return {[type]} [description]
  205. */
  206. async delete() {
  207. let that = this;
  208. try {
  209. let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
  210. let deleteBean = that.app.comoBean.instance(data,{where:{article_id:data.article_id}});
  211. let result = await that.service.manager.delete(deleteBean,that.useModel,'文章删除失败,请稍候重试');
  212. await that.deleteArticleCache(data.article_id);
  213. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  214. } catch(err) {
  215. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  216. }
  217. }
  218. /**
  219. * [deleteArticleCache 删除文章缓存]
  220. * @author szjcomo
  221. * @date 2021-04-03
  222. * @return {[type]} [description]
  223. */
  224. async deleteArticleCache(article_id) {
  225. let that = this;
  226. //let cache_key = `article_id_${article_id}`;
  227. //await that.service.redis.del(cache_key);
  228. }
  229. /**
  230. * [videoList 获取视频列表]
  231. * @author szjcomo
  232. * @date 2021-04-12
  233. * @return {[type]} [description]
  234. */
  235. async videoList() {
  236. let that = this;
  237. try {
  238. let data = await that.ctx.validate(that.videoValidate,await that.ctx.getParse());
  239. let tmp = await that.service.configs.getConfigValue('article_category_insinuate');
  240. let tmpdata = that.app.szjcomo.json_decode(tmp);
  241. let seq = that.app.Sequelize;
  242. let options = {where:{article_type:1,article_status:1,category_id:tmpdata['manager_video']},attributes:['article_title','article_image','article_desc']};
  243. if(data.admin_user.role_flag) options.where.view_auth = {[seq.Op.in]:[0,1]};
  244. let result = await that.useModel.findAll(options);
  245. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  246. } catch(err) {
  247. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  248. }
  249. }
  250. }