You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
826 B
JavaScript
30 lines
826 B
JavaScript
10 months ago
|
class BaseController {
|
||
|
response = (code = 200, msg = '', data = {}, page = 1, limit = 10) => {
|
||
|
const res = this.getPagingData(data, page, limit);
|
||
|
return {
|
||
|
'errcode': code,
|
||
|
'msg': msg,
|
||
|
'title': msg,
|
||
|
// 'data': data,
|
||
|
...res,
|
||
|
};
|
||
|
};
|
||
|
getPagination = (page, size = 10) => {
|
||
|
const limit = size ? +size : 3;
|
||
|
const offset = page ? (page-1) * limit : 0;
|
||
|
|
||
|
return { limit, offset };
|
||
|
};
|
||
|
getPagingData = (_data, page, limit) => {
|
||
|
const { count: totalCount, rows: data } = _data;
|
||
|
if ( !data || !totalCount) {
|
||
|
return { data: _data };
|
||
|
}
|
||
|
const currentPage = page ? +page : 0;
|
||
|
const totalPages = Math.ceil(totalCount / limit);
|
||
|
|
||
|
return { totalCount, data, totalPages, currentPage };
|
||
|
};
|
||
|
}
|
||
|
module.exports = BaseController;
|