Authored by 李奇

点赞接口提交

... ... @@ -157,6 +157,34 @@ const article = {
message: GET_ARTICLE_SUCCESS
});
});
},
/**
* 文章点赞
* @param req
* @param res
*/
like(req, res) {
const actId = req.body.actId;
const articleId = req.body.articleId;
if (!actId || !articleId) {
return res.json({
code: 400,
message: MISS_PARAMS
});
}
req.ctx(ArticleModel).likeArticle(actId, articleId)
.then(() => {
req.ctx(ArticleModel).insertLikeDetail(actId, articleId)
.then(() => {
res.json({
code: 200,
message: '点赞成功'
});
});
});
}
};
... ...
... ... @@ -10,8 +10,7 @@ const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_USER = 'user';
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_ACT_ARTICLE_IMG = 'act_article_img';
// const TABLE_ACT_ARTICLE_GOOD = 'act_article_good';
const TABLE_ACT_ARTICLE_GOOD = 'act_article_good';
class ArticleModel extends global.yoho.BaseModel {
constructor(ctx) {
... ... @@ -126,6 +125,47 @@ class ArticleModel extends global.yoho.BaseModel {
}
);
}
/**
* 文章点赞
* @param actId
* @param articleId
* @returns {*}
*/
likeArticle(actId, articleId) {
return mysqlCli.update(
`UPDATE ${TABLE_ACT_ARTICLE} AA
SET good_count = good_count + 1
WHERE AA.act_id = :actId
AND AA.id = :articleId;`,
{
actId,
articleId
}
);
}
/**
* 插入点赞详情
* @param actId
* @param articleId
* @returns {*}
*/
insertLikeDetail(actId, articleId) {
const userId = _.get(this.ctx.req.session, 'user.id');
const ip = this.header['X-Forwarded-For'] || this.ctx.req.connection.remoteAddress;
return mysqlCli.insert(
`INSERT INTO ${TABLE_ACT_ARTICLE_GOOD} (act_id, article_id, user_id, ip)
VALUES (:actId, :articleId, :userId, :ip);`,
{
ip,
userId,
actId,
articleId
}
);
}
}
module.exports = ArticleModel;
... ...
... ... @@ -8,6 +8,7 @@ const router = express.Router(); // eslint-disable-line
const {auth} = require('../../middleware');
const article = require('./controllers/article');
router.post('/like', auth, article.like);
router.get('/list', article.list);
router.post('/publish', auth, article.publish);
router.get('/querySingle', article.querySingle);
... ...
... ... @@ -13,7 +13,7 @@ module.exports = (req, res, next) => {
if (!userId) {
return res.json({
code: 401,
message: '抱歉,您暂未登录!'
message: '抱歉,您暂未验证'
});
}
... ...
... ... @@ -7,6 +7,5 @@
const auth = require('./auth');
const error = require('./error');
const user = require('./user');
module.exports = {auth, user, error};
module.exports = {auth, error};
... ...
'use strict';
const _ = require('lodash');
module.exports = (req, res, next) => {
// 从 SESSION 中获取到当前登录用户的 UID
if (req.session && _.isNumber(req.session.LOGIN_UID)) {
req.user.uid = {
toString: () => {
return _.parseInt(req.session.LOGIN_UID);
},
sessionKey: req.session.SESSION_KEY
};
let userData = _.get(req.session, 'user', {});
_.merge(req.user, userData);
}
next();
};