12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- const Base = require('./base.js');
- const _ = require('lodash');
- module.exports = class extends Base {
- /**
- * index action
- * @return {Promise} []
- */
- async indexAction() {
- const model = this.model('category');
- const data = await model.where({is_show: 1}).order(['sort_order ASC']).select();
- const topCategory = data.filter((item) => {
- return item.parent_id === 0;
- });
- const categoryList = [];
- topCategory.map((item) => {
- item.level = 1;
- categoryList.push(item);
- data.map((child) => {
- if (child.parent_id === item.id) {
- child.level = 2;
- categoryList.push(child);
- }
- });
- });
- return this.success(categoryList);
- }
- async topCategoryAction() {
- const model = this.model('category');
- const data = await model.where({parent_id: 0}).order(['id ASC']).select();
- return this.success(data);
- }
- async infoAction() {
- const id = this.get('id');
- const model = this.model('category');
- const data = await model.where({id: id}).find();
- return this.success(data);
- }
- async storeAction() {
- if (!this.isPost) {
- return false;
- }
- const values = this.post();
- const id = this.post('id');
- const model = this.model('category');
- values.is_show = values.is_show ? 1 : 0;
- if (id > 0) {
- await model.where({id: id}).update(values);
- } else {
- delete values.id;
- await model.add(values);
- }
- return this.success(values);
- }
- async destoryAction() {
- const id = this.post('id');
- await this.model('category').where({id: id}).limit(1).delete();
- // TODO 删除图片
- return this.success();
- }
- };
|