add get line msg log api

This commit is contained in:
Jay
2018-09-15 17:35:39 +08:00
parent 45271196bf
commit d4918b467f
2 changed files with 74 additions and 2 deletions
+41 -1
View File
@@ -134,11 +134,51 @@ const checkSession = async (c, n) => {
return n()
}
/**
* @typedef pageobj
* @prop {number} totalPage
* @prop {number} offset
* @prop {number} limit
* @prop {number} page
*/
/**
* calculate page object
* @param {number} count data row count
* @param {number} page page
* @param {number} maxItem page show item limit
* @return {pageobj}
*/
const calPage = function (count, page, maxItem = 10) {
if (!count || !isFinite(count) || count < 0) count = 0
if (!page || !isFinite(page) || page < 1) page = 1
if (!maxItem || !isFinite(maxItem) || maxItem < 1) maxItem = 10
let totalPage = Math.ceil(count / maxItem)
if (totalPage < 1) totalPage = 1
if (page > totalPage) page = totalPage
let offset = (page - 1) * maxItem
if (offset > count) offset = count
let limit = maxItem
totalPage = toInt(totalPage)
offset = toInt(offset)
limit = toInt(limit)
page = toInt(page)
return {
totalPage,
offset,
limit,
page
}
}
module.exports = {
chkObject,
resObject,
toInt,
APIError,
genError,
checkSession
checkSession,
calPage
}