提取公共方法,并添加分页查询

This commit is contained in:
huangge1199 2024-08-09 11:54:08 +08:00
parent f5a2bd00a6
commit 88e965010a
3 changed files with 44 additions and 38 deletions

View File

@ -10,7 +10,7 @@ module.exports = class extends Base {
const category = {name, news_id, remark}; const category = {name, news_id, remark};
const categoryModel = this.model('category'); const categoryModel = this.model('category');
const insertId = await categoryModel.addCategory(category); const insertId = await categoryModel.addOne(category);
if (insertId) { if (insertId) {
this.success({message: '新闻分类添加成功!', id: insertId}); this.success({message: '新闻分类添加成功!', id: insertId});
@ -28,7 +28,7 @@ module.exports = class extends Base {
const category = {id, name, news_id, remark}; const category = {id, name, news_id, remark};
const categoryModel = this.model('category'); const categoryModel = this.model('category');
const affectedRows = await categoryModel.editCategoryById(id, category); const affectedRows = await categoryModel.editById(id, category);
if (affectedRows) { if (affectedRows) {
this.success({message: '新闻分类修改成功!', affectedRows: affectedRows}); this.success({message: '新闻分类修改成功!', affectedRows: affectedRows});
} else { } else {
@ -40,7 +40,7 @@ module.exports = class extends Base {
const id = this.post('id'); const id = this.post('id');
const categoryModel = this.model('category'); const categoryModel = this.model('category');
const affectedRows = await categoryModel.deleteCategoryById(id); const affectedRows = await categoryModel.deleteById(id);
if (affectedRows) { if (affectedRows) {
this.success({message: '新闻分类删除成功!', affectedRows: affectedRows}); this.success({message: '新闻分类删除成功!', affectedRows: affectedRows});
} else { } else {
@ -51,7 +51,7 @@ module.exports = class extends Base {
async getCategoryByIdAction() { async getCategoryByIdAction() {
const id = this.post('id'); const id = this.post('id');
const categoryModel = this.model('category'); const categoryModel = this.model('category');
const category = await categoryModel.getCategoryById(id); const category = await categoryModel.getById(id);
if (!think.isEmpty(category)) { if (!think.isEmpty(category)) {
this.success(category); this.success(category);
@ -62,7 +62,7 @@ module.exports = class extends Base {
async getAllCategoriesAction() { async getAllCategoriesAction() {
const categoryModel = this.model('category'); const categoryModel = this.model('category');
const categories = await categoryModel.getAllCategories(); const categories = await categoryModel.getAll();
if (!think.isEmpty(categories)) { if (!think.isEmpty(categories)) {
this.success(categories); this.success(categories);
@ -74,7 +74,7 @@ module.exports = class extends Base {
async getCategoriesByConditionAction() { async getCategoriesByConditionAction() {
const condition = this.post(); const condition = this.post();
const categoryModel = this.model('category'); const categoryModel = this.model('category');
const categories = await categoryModel.getCategoriesByCondition(condition); const categories = await categoryModel.getByCondition(condition);
if (!think.isEmpty(categories)) { if (!think.isEmpty(categories)) {
this.success(categories); this.success(categories);
@ -82,4 +82,13 @@ module.exports = class extends Base {
this.success({message: '当前条件没有分类!'}); 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);
}
}; };

View File

@ -1,8 +1,33 @@
module.exports = class extends think.Model { module.exports = class extends think.Model {
// 公共方法示例 addOne(data) {
getDataById(id) { return this.add(data);
return this.where({ id }).find();
} }
// 其他公共方法和属性可以在这里定义 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();
}
}; };

View File

@ -1,32 +1,4 @@
const Base = require('./base.js'); const Base = require('./base.js');
module.exports = class extends Base { 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();
}
}; };