diff --git a/src/controller/category.js b/src/controller/category.js index 8c542f1..3f1abca 100644 --- a/src/controller/category.js +++ b/src/controller/category.js @@ -10,7 +10,7 @@ module.exports = class extends Base { const category = {name, news_id, remark}; const categoryModel = this.model('category'); - const insertId = await categoryModel.addCategory(category); + const insertId = await categoryModel.addOne(category); if (insertId) { this.success({message: '新闻分类添加成功!', id: insertId}); @@ -28,7 +28,7 @@ module.exports = class extends Base { const category = {id, name, news_id, remark}; const categoryModel = this.model('category'); - const affectedRows = await categoryModel.editCategoryById(id, category); + const affectedRows = await categoryModel.editById(id, category); if (affectedRows) { this.success({message: '新闻分类修改成功!', affectedRows: affectedRows}); } else { @@ -40,7 +40,7 @@ module.exports = class extends Base { const id = this.post('id'); const categoryModel = this.model('category'); - const affectedRows = await categoryModel.deleteCategoryById(id); + const affectedRows = await categoryModel.deleteById(id); if (affectedRows) { this.success({message: '新闻分类删除成功!', affectedRows: affectedRows}); } else { @@ -51,7 +51,7 @@ module.exports = class extends Base { async getCategoryByIdAction() { const id = this.post('id'); const categoryModel = this.model('category'); - const category = await categoryModel.getCategoryById(id); + const category = await categoryModel.getById(id); if (!think.isEmpty(category)) { this.success(category); @@ -62,7 +62,7 @@ module.exports = class extends Base { async getAllCategoriesAction() { const categoryModel = this.model('category'); - const categories = await categoryModel.getAllCategories(); + const categories = await categoryModel.getAll(); if (!think.isEmpty(categories)) { this.success(categories); @@ -74,7 +74,7 @@ module.exports = class extends Base { async getCategoriesByConditionAction() { const condition = this.post(); const categoryModel = this.model('category'); - const categories = await categoryModel.getCategoriesByCondition(condition); + const categories = await categoryModel.getByCondition(condition); if (!think.isEmpty(categories)) { this.success(categories); @@ -82,4 +82,13 @@ module.exports = class extends Base { this.success({message: '当前条件没有分类!'}); } } + + async listAction() { + const body = this.post(); + const page = body.page || 1; + const pageSize = body.pageSize || 10; + const categoryModel = this.model('category'); + const data = await categoryModel.getPageList(page, pageSize); + return this.success(data); + } }; diff --git a/src/model/base.js b/src/model/base.js index 096daaa..1b9ac4f 100644 --- a/src/model/base.js +++ b/src/model/base.js @@ -1,8 +1,33 @@ module.exports = class extends think.Model { - // 公共方法示例 - getDataById(id) { - return this.where({ id }).find(); + addOne(data) { + return this.add(data); } - // 其他公共方法和属性可以在这里定义 + editById(id, data) { + return this.where({id}).update(data); + } + + deleteById(id) { + const del = 1; + const data = {del}; + return this.where({id}).update(data); + } + + getById(id) { + const del = 0; + return this.where({id, del}).find(); + } + + getAll() { + const del = 0; + return this.where({del}).select(); + } + + getByCondition(condition) { + return this.where(condition).select(); + } + + getPageList(page, pageSize) { + return this.field('*').page(page, pageSize).select(); + } }; diff --git a/src/model/category.js b/src/model/category.js index 0809681..9e93780 100644 --- a/src/model/category.js +++ b/src/model/category.js @@ -1,32 +1,4 @@ const Base = require('./base.js'); module.exports = class extends Base { - addCategory(data) { - // 插入数据并返回插入的主键 - return this.add(data); - } - - editCategoryById(id, data) { - return this.where({id}).update(data); - } - - deleteCategoryById(id) { - const del = 1; - const data = {del}; - return this.where({id}).update(data); - } - - getCategoryById(id) { - const del = 0; - return this.where({id, del}).find(); - } - - getAllCategories() { - const del = 0; - return this.where({del}).select(); - } - - getCategoriesByCondition(condition) { - return this.where(condition).select(); - } };