Authored by 邱骏

articleListWithOutImg

... ... @@ -87,6 +87,82 @@ const article = {
},
/**
* 文章列表
* @param req
* @param res
* @param next
*/
list2(req, res, next) {
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', 'id'];
console.log('query', query);
if (!actId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
if (order !== 'asc' && order !== 'desc') {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
if (orderByFields.indexOf(orderBy) === -1) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
req.ctx(ArticleModel).articleListWithOutImg({
actId,
order,
orderBy,
pageNo,
pageSize
})
.then(result => {
console.log('result', result);
let list = [];
_.each(result, item => {
let data = {};
_.each(item, (val, key) => {
data[camelcase(key)] = val;
});
list.push(data);
});
return list;
})
.then(list => {
req.ctx(ArticleModel).allArticlesNum(actId)
.then(totalCount => {
res.json({
code: 200,
data: list,
pageNo,
pageSize,
totalCount,
totalPage: Math.ceil(totalCount / pageSize),
message: GET_SUCCESS
});
});
})
.catch(next);
},
/**
* 发布文章
* @param req
* @param res
... ...
... ... @@ -52,6 +52,36 @@ class ArticleModel extends global.yoho.BaseModel {
}
/**
* 获取文章列表无图
* @returns {*}
*/
articleListWithOutImg({actId, pageNo, pageSize, orderBy, order}) {
const orderMapping = {
goodCount: 'good_count',
createTime: 'create_time',
id: 'id'
};
let limitSql;
let orderSql;
let whereSql;
whereSql = `WHERE act_id = ${actId}`;
orderSql = `ORDER BY ${orderMapping[orderBy]} ${order}`;
limitSql = `LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}`;
console.log('sql', orderSql);
return mysqlCli.query(
`SELECT
id,
good_count,
content,
create_time
FROM ${TABLE_ACT_ARTICLE}
${whereSql} ${orderSql} ${limitSql};`
);
}
/**
* 获取文章总数
* @returns {*}
*/
... ...
... ... @@ -10,6 +10,7 @@ const article = require('./controllers/article');
router.post('/like', article.like);
router.get('/list', article.list);
router.get('/list2', article.list2);
router.post('/publish', auth, article.publish);
router.get('/querySingle', article.querySingle);
... ...