Authored by 李奇

点赞接口提交

@@ -157,6 +157,34 @@ const article = { @@ -157,6 +157,34 @@ const article = {
157 message: GET_ARTICLE_SUCCESS 157 message: GET_ARTICLE_SUCCESS
158 }); 158 });
159 }); 159 });
  160 + },
  161 +
  162 + /**
  163 + * 文章点赞
  164 + * @param req
  165 + * @param res
  166 + */
  167 + like(req, res) {
  168 + const actId = req.body.actId;
  169 + const articleId = req.body.articleId;
  170 +
  171 + if (!actId || !articleId) {
  172 + return res.json({
  173 + code: 400,
  174 + message: MISS_PARAMS
  175 + });
  176 + }
  177 +
  178 + req.ctx(ArticleModel).likeArticle(actId, articleId)
  179 + .then(() => {
  180 + req.ctx(ArticleModel).insertLikeDetail(actId, articleId)
  181 + .then(() => {
  182 + res.json({
  183 + code: 200,
  184 + message: '点赞成功'
  185 + });
  186 + });
  187 + });
160 } 188 }
161 }; 189 };
162 190
@@ -10,8 +10,7 @@ const mysqlCli = global.yoho.utils.mysqlCli; @@ -10,8 +10,7 @@ const mysqlCli = global.yoho.utils.mysqlCli;
10 const TABLE_USER = 'user'; 10 const TABLE_USER = 'user';
11 const TABLE_ACT_ARTICLE = 'act_article'; 11 const TABLE_ACT_ARTICLE = 'act_article';
12 const TABLE_ACT_ARTICLE_IMG = 'act_article_img'; 12 const TABLE_ACT_ARTICLE_IMG = 'act_article_img';
13 -  
14 -// const TABLE_ACT_ARTICLE_GOOD = 'act_article_good'; 13 +const TABLE_ACT_ARTICLE_GOOD = 'act_article_good';
15 14
16 class ArticleModel extends global.yoho.BaseModel { 15 class ArticleModel extends global.yoho.BaseModel {
17 constructor(ctx) { 16 constructor(ctx) {
@@ -126,6 +125,47 @@ class ArticleModel extends global.yoho.BaseModel { @@ -126,6 +125,47 @@ class ArticleModel extends global.yoho.BaseModel {
126 } 125 }
127 ); 126 );
128 } 127 }
  128 +
  129 + /**
  130 + * 文章点赞
  131 + * @param actId
  132 + * @param articleId
  133 + * @returns {*}
  134 + */
  135 + likeArticle(actId, articleId) {
  136 + return mysqlCli.update(
  137 + `UPDATE ${TABLE_ACT_ARTICLE} AA
  138 + SET good_count = good_count + 1
  139 + WHERE AA.act_id = :actId
  140 + AND AA.id = :articleId;`,
  141 + {
  142 + actId,
  143 + articleId
  144 + }
  145 + );
  146 + }
  147 +
  148 + /**
  149 + * 插入点赞详情
  150 + * @param actId
  151 + * @param articleId
  152 + * @returns {*}
  153 + */
  154 + insertLikeDetail(actId, articleId) {
  155 + const userId = _.get(this.ctx.req.session, 'user.id');
  156 + const ip = this.header['X-Forwarded-For'] || this.ctx.req.connection.remoteAddress;
  157 +
  158 + return mysqlCli.insert(
  159 + `INSERT INTO ${TABLE_ACT_ARTICLE_GOOD} (act_id, article_id, user_id, ip)
  160 + VALUES (:actId, :articleId, :userId, :ip);`,
  161 + {
  162 + ip,
  163 + userId,
  164 + actId,
  165 + articleId
  166 + }
  167 + );
  168 + }
129 } 169 }
130 170
131 module.exports = ArticleModel; 171 module.exports = ArticleModel;
@@ -8,6 +8,7 @@ const router = express.Router(); // eslint-disable-line @@ -8,6 +8,7 @@ const router = express.Router(); // eslint-disable-line
8 const {auth} = require('../../middleware'); 8 const {auth} = require('../../middleware');
9 const article = require('./controllers/article'); 9 const article = require('./controllers/article');
10 10
  11 +router.post('/like', auth, article.like);
11 router.get('/list', article.list); 12 router.get('/list', article.list);
12 router.post('/publish', auth, article.publish); 13 router.post('/publish', auth, article.publish);
13 router.get('/querySingle', article.querySingle); 14 router.get('/querySingle', article.querySingle);
@@ -13,7 +13,7 @@ module.exports = (req, res, next) => { @@ -13,7 +13,7 @@ module.exports = (req, res, next) => {
13 if (!userId) { 13 if (!userId) {
14 return res.json({ 14 return res.json({
15 code: 401, 15 code: 401,
16 - message: '抱歉,您暂未登录!' 16 + message: '抱歉,您暂未验证'
17 }); 17 });
18 } 18 }
19 19
@@ -7,6 +7,5 @@ @@ -7,6 +7,5 @@
7 7
8 const auth = require('./auth'); 8 const auth = require('./auth');
9 const error = require('./error'); 9 const error = require('./error');
10 -const user = require('./user');  
11 10
12 -module.exports = {auth, user, error}; 11 +module.exports = {auth, error};
1 -'use strict';  
2 -  
3 -const _ = require('lodash');  
4 -  
5 -module.exports = (req, res, next) => {  
6 -  
7 - // 从 SESSION 中获取到当前登录用户的 UID  
8 - if (req.session && _.isNumber(req.session.LOGIN_UID)) {  
9 - req.user.uid = {  
10 - toString: () => {  
11 - return _.parseInt(req.session.LOGIN_UID);  
12 - },  
13 - sessionKey: req.session.SESSION_KEY  
14 - };  
15 - let userData = _.get(req.session, 'user', {});  
16 -  
17 - _.merge(req.user, userData);  
18 - }  
19 -  
20 - next();  
21 -};