Authored by 李奇

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

@@ -23,12 +23,20 @@ const article = { @@ -23,12 +23,20 @@ const article = {
23 */ 23 */
24 list(req, res) { 24 list(req, res) {
25 const query = req.query; 25 const query = req.query;
  26 + const actId = query.actId;
26 const pageNo = query.pageNo || 1; 27 const pageNo = query.pageNo || 1;
27 const pageSize = query.pageSize || 10; 28 const pageSize = query.pageSize || 10;
28 const orderBy = query.orderBy || 'createTime'; 29 const orderBy = query.orderBy || 'createTime';
29 const order = ((query.order || 'desc') + '').toLowerCase(); 30 const order = ((query.order || 'desc') + '').toLowerCase();
30 const orderByFields = ['createTime', 'goodCount']; 31 const orderByFields = ['createTime', 'goodCount'];
31 32
  33 + if (!actId) {
  34 + return res.json({
  35 + code: 400,
  36 + message: '活动ID[actId]不能为空'
  37 + });
  38 + }
  39 +
32 if (order !== 'asc' && order !== 'desc') { 40 if (order !== 'asc' && order !== 'desc') {
33 return res.json({ 41 return res.json({
34 code: 400, 42 code: 400,
@@ -44,13 +52,14 @@ const article = { @@ -44,13 +52,14 @@ const article = {
44 } 52 }
45 53
46 req.ctx(ArticleModel).articleList({ 54 req.ctx(ArticleModel).articleList({
  55 + actId,
47 order, 56 order,
48 orderBy, 57 orderBy,
49 pageNo, 58 pageNo,
50 pageSize 59 pageSize
51 }) 60 })
52 .then(result => { 61 .then(result => {
53 - req.ctx(ArticleModel).allArticlesNum() 62 + req.ctx(ArticleModel).allArticlesNum(actId)
54 .then(totalCount => { 63 .then(totalCount => {
55 res.json({ 64 res.json({
56 code: 200, 65 code: 200,
@@ -21,7 +21,7 @@ class ArticleModel extends global.yoho.BaseModel { @@ -21,7 +21,7 @@ class ArticleModel extends global.yoho.BaseModel {
21 * 获取文章列表 21 * 获取文章列表
22 * @returns {*} 22 * @returns {*}
23 */ 23 */
24 - articleList({pageNo, pageSize, orderBy, order}) { 24 + articleList({actId, pageNo, pageSize, orderBy, order}) {
25 const orderMapping = { 25 const orderMapping = {
26 goodCount: 'good_count', 26 goodCount: 'good_count',
27 createTime: 'create_time' 27 createTime: 'create_time'
@@ -29,7 +29,9 @@ class ArticleModel extends global.yoho.BaseModel { @@ -29,7 +29,9 @@ class ArticleModel extends global.yoho.BaseModel {
29 29
30 let limitSql; 30 let limitSql;
31 let orderSql; 31 let orderSql;
  32 + let whereSql;
32 33
  34 + whereSql = `WHERE AA.act_id = ${actId}`;
33 orderSql = `ORDER BY AA.${orderMapping[orderBy]} ${order}`; 35 orderSql = `ORDER BY AA.${orderMapping[orderBy]} ${order}`;
34 limitSql = `LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}`; 36 limitSql = `LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}`;
35 37
@@ -39,7 +41,7 @@ class ArticleModel extends global.yoho.BaseModel { @@ -39,7 +41,7 @@ class ArticleModel extends global.yoho.BaseModel {
39 FROM ${TABLE_ACT_ARTICLE} AS AA 41 FROM ${TABLE_ACT_ARTICLE} AS AA
40 INNER JOIN ${TABLE_ACT_ARTICLE_IMG} AS AAI 42 INNER JOIN ${TABLE_ACT_ARTICLE_IMG} AS AAI
41 ON AA.id = AAI.article_id 43 ON AA.id = AAI.article_id
42 - ${orderSql} ${limitSql};` 44 + ${whereSql} ${orderSql} ${limitSql};`
43 ); 45 );
44 } 46 }
45 47
@@ -47,9 +49,9 @@ class ArticleModel extends global.yoho.BaseModel { @@ -47,9 +49,9 @@ class ArticleModel extends global.yoho.BaseModel {
47 * 获取文章总数 49 * 获取文章总数
48 * @returns {*} 50 * @returns {*}
49 */ 51 */
50 - allArticlesNum() { 52 + allArticlesNum(actId) {
51 return mysqlCli.query( 53 return mysqlCli.query(
52 - `SELECT COUNT(*) AS total FROM ${TABLE_ACT_ARTICLE};` 54 + `SELECT COUNT(*) AS total FROM ${TABLE_ACT_ARTICLE} WHERE act_id = ${actId};`
53 ).then(res => { 55 ).then(res => {
54 return res[0].total; 56 return res[0].total;
55 }); 57 });