category.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const Base = require('./base.js');
  2. const _ = require('lodash');
  3. module.exports = class extends Base {
  4. /**
  5. * index action
  6. * @return {Promise} []
  7. */
  8. async indexAction() {
  9. const model = this.model('category');
  10. const data = await model.where({is_show: 1}).order(['sort_order ASC']).select();
  11. const topCategory = data.filter((item) => {
  12. return item.parent_id === 0;
  13. });
  14. const categoryList = [];
  15. topCategory.map((item) => {
  16. item.level = 1;
  17. categoryList.push(item);
  18. data.map((child) => {
  19. if (child.parent_id === item.id) {
  20. child.level = 2;
  21. categoryList.push(child);
  22. }
  23. });
  24. });
  25. return this.success(categoryList);
  26. }
  27. async topCategoryAction() {
  28. const model = this.model('category');
  29. const data = await model.where({parent_id: 0}).order(['id ASC']).select();
  30. return this.success(data);
  31. }
  32. async infoAction() {
  33. const id = this.get('id');
  34. const model = this.model('category');
  35. const data = await model.where({id: id}).find();
  36. return this.success(data);
  37. }
  38. async storeAction() {
  39. if (!this.isPost) {
  40. return false;
  41. }
  42. const values = this.post();
  43. const id = this.post('id');
  44. const model = this.model('category');
  45. values.is_show = values.is_show ? 1 : 0;
  46. if (id > 0) {
  47. await model.where({id: id}).update(values);
  48. } else {
  49. delete values.id;
  50. await model.add(values);
  51. }
  52. return this.success(values);
  53. }
  54. async destoryAction() {
  55. const id = this.post('id');
  56. await this.model('category').where({id: id}).limit(1).delete();
  57. // TODO 删除图片
  58. return this.success();
  59. }
  60. };