Authored by 李奇

文章列表接口添加活动id参数

... ... @@ -23,12 +23,20 @@ const article = {
*/
list(req, res) {
const query = req.query;
const actId = query.actId;
const pageNo = query.pageNo || 1;
const pageSize = query.pageSize || 10;
const orderBy = query.orderBy || 'createTime';
const order = ((query.order || 'desc') + '').toLowerCase();
const orderByFields = ['createTime', 'goodCount'];
if (!actId) {
return res.json({
code: 400,
message: '活动ID[actId]不能为空'
});
}
if (order !== 'asc' && order !== 'desc') {
return res.json({
code: 400,
... ... @@ -44,13 +52,14 @@ const article = {
}
req.ctx(ArticleModel).articleList({
actId,
order,
orderBy,
pageNo,
pageSize
})
.then(result => {
req.ctx(ArticleModel).allArticlesNum()
req.ctx(ArticleModel).allArticlesNum(actId)
.then(totalCount => {
res.json({
code: 200,
... ...
... ... @@ -21,7 +21,7 @@ class ArticleModel extends global.yoho.BaseModel {
* 获取文章列表
* @returns {*}
*/
articleList({pageNo, pageSize, orderBy, order}) {
articleList({actId, pageNo, pageSize, orderBy, order}) {
const orderMapping = {
goodCount: 'good_count',
createTime: 'create_time'
... ... @@ -29,7 +29,9 @@ class ArticleModel extends global.yoho.BaseModel {
let limitSql;
let orderSql;
let whereSql;
whereSql = `WHERE AA.act_id = ${actId}`;
orderSql = `ORDER BY AA.${orderMapping[orderBy]} ${order}`;
limitSql = `LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}`;
... ... @@ -39,7 +41,7 @@ class ArticleModel extends global.yoho.BaseModel {
FROM ${TABLE_ACT_ARTICLE} AS AA
INNER JOIN ${TABLE_ACT_ARTICLE_IMG} AS AAI
ON AA.id = AAI.article_id
${orderSql} ${limitSql};`
${whereSql} ${orderSql} ${limitSql};`
);
}
... ... @@ -47,9 +49,9 @@ class ArticleModel extends global.yoho.BaseModel {
* 获取文章总数
* @returns {*}
*/
allArticlesNum() {
allArticlesNum(actId) {
return mysqlCli.query(
`SELECT COUNT(*) AS total FROM ${TABLE_ACT_ARTICLE};`
`SELECT COUNT(*) AS total FROM ${TABLE_ACT_ARTICLE} WHERE act_id = ${actId};`
).then(res => {
return res[0].total;
});
... ...