articles.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. /**
  4. * 文章操作服务
  5. */
  6. class ArticlesService extends Service {
  7. /**
  8. * [getArticlesCategoryAllChild 查询分类下所有子分类]
  9. * @author szjcomo
  10. * @date 2021-01-27
  11. * @param {[type]} category_id [description]
  12. * @return {[type]} [description]
  13. */
  14. async getArticlesCategoryAllChild(category_id = 0) {
  15. let that = this;
  16. let categorys = await that.getArticlesCategoryAll({attributes:['category_id','category_name','pid'],raw:true});
  17. let tmp = that.app.szjcomo.arrayRecursion(categorys,category_id,'pid','category_id');
  18. let data = that.app.szjcomo.arrayRecursiveBack(tmp);
  19. let arr = [category_id];
  20. data.forEach(item => {
  21. arr.push(item.category_id);
  22. })
  23. return arr;
  24. }
  25. /**
  26. * [getArticlesCategoryAll 查询所有分类]
  27. * @author szjcomo
  28. * @date 2021-01-27
  29. * @return {[type]} [description]
  30. */
  31. async getArticlesCategoryAll(options = {}) {
  32. let that = this;
  33. let seq = that.app.Sequelize;
  34. let default_options = {
  35. include:[{model:that.app.model.AdminUser,as:'admin_user',attributes:[]}],
  36. attributes:{
  37. include:[[seq.col('admin_user.username'),'username']],
  38. exclude:['admin_id']
  39. }
  40. };
  41. let finalOptions = Object.assign(default_options,options)
  42. let result = await that.app.model.ArticlesCategory.findAll(finalOptions);
  43. return result;
  44. }
  45. /**
  46. * [getArticlesCategoryAllParents 查询分类的所有父级分类]
  47. * @author szjcomo
  48. * @date 2021-01-27
  49. * @param {[type]} category_id [description]
  50. * @return {[type]} [description]
  51. */
  52. async getArticlesCategoryAllParents(category_id = 0) {
  53. let that = this;
  54. if(category_id == 0) return [];
  55. let categorys = await that.getArticlesCategoryAll({attributes:['category_id','category_name','pid'],raw:true});
  56. //将一维数组转换成递归的多维数组
  57. let tmp = that.app.szjcomo.arrayRecursion(categorys,0,'pid','category_id');
  58. //将多维数组转换成一维数组并按照层级进行有序排列
  59. let data = that.app.szjcomo.arrayRecursiveBack(tmp);
  60. //当前分类总父级有多少个
  61. let currCategoryTotalParents = 0;
  62. //当前分类在数组中的下标
  63. let currCategoryIndex = -1;
  64. for(let i = 0;i < data.length;i++) {
  65. let item = data[i];
  66. if(item.pid == 0) {
  67. currCategoryTotalParents = 0;
  68. } else {
  69. currCategoryTotalParents += 1;
  70. }
  71. if(item.category_id == category_id) {
  72. currCategoryIndex = i;
  73. break;
  74. }
  75. }
  76. if(currCategoryIndex == -1) return [];
  77. if(currCategoryTotalParents == 0) {
  78. let item = data[currCategoryIndex];
  79. delete item.child;
  80. return [item];
  81. }
  82. let result = [];
  83. for(let i = currCategoryTotalParents;i >= 0;i--,currCategoryIndex--) {
  84. let item = data[currCategoryIndex];
  85. delete item.child;
  86. result.unshift(item);
  87. }
  88. return result;
  89. }
  90. }
  91. module.exports = ArticlesService;