Authored by 邱骏

update

@@ -51,9 +51,6 @@ try { @@ -51,9 +51,6 @@ try {
51 origin: config.corsAllowOrigin 51 origin: config.corsAllowOrigin
52 })); 52 }));
53 53
54 - // 用户信息  
55 - app.use(middleware.user);  
56 -  
57 // 路由分发 54 // 路由分发
58 require('./dispatch')(app); 55 require('./dispatch')(app);
59 56
@@ -7,15 +7,9 @@ const _ = require('lodash'); @@ -7,15 +7,9 @@ const _ = require('lodash');
7 const camelcase = require('camelcase'); 7 const camelcase = require('camelcase');
8 const ArticleModel = require('../models/article'); 8 const ArticleModel = require('../models/article');
9 9
10 -const ADD_ARTICLE_SUCCESS = '文章发表成功';  
11 -const GET_ARTICLES_SUCCESS = '获取文章列表成功';  
12 -const GET_ARTICLE_SUCCESS = '获取文章详情成功';  
13 -const INVALID_ACTIVITY_ID = '活动ID[actId]不能为空';  
14 -const INVALID_IMG = '图片[imgUrl]不能为空';  
15 -const INVALID_CONTENT = '内容[content]都不能为空';  
16 -const MISS_PARAMS = '缺少参数';  
17 -const INVALID_ORDER = '排序[order]值为asc或desc';  
18 -const INVALID_ORDER_BY = '排序字段[orderBy]非法'; 10 +const POST_SUCCESS = '操作成功';
  11 +const GET_SUCCESS = '获取成功';
  12 +const INVALID_PARAMS = '参数错误';
19 13
20 const article = { 14 const article = {
21 /** 15 /**
@@ -42,14 +36,14 @@ const article = { @@ -42,14 +36,14 @@ const article = {
42 if (order !== 'asc' && order !== 'desc') { 36 if (order !== 'asc' && order !== 'desc') {
43 return res.json({ 37 return res.json({
44 code: 400, 38 code: 400,
45 - message: INVALID_ORDER 39 + message: INVALID_PARAMS
46 }); 40 });
47 } 41 }
48 42
49 if (orderByFields.indexOf(orderBy) === -1) { 43 if (orderByFields.indexOf(orderBy) === -1) {
50 return res.json({ 44 return res.json({
51 code: 400, 45 code: 400,
52 - message: INVALID_ORDER_BY 46 + message: INVALID_PARAMS
53 }); 47 });
54 } 48 }
55 49
@@ -81,7 +75,7 @@ const article = { @@ -81,7 +75,7 @@ const article = {
81 pageSize, 75 pageSize,
82 totalCount, 76 totalCount,
83 totalPage: Math.ceil(totalCount / pageSize), 77 totalPage: Math.ceil(totalCount / pageSize),
84 - message: GET_ARTICLES_SUCCESS 78 + message: GET_SUCCESS
85 }); 79 });
86 }); 80 });
87 }); 81 });
@@ -99,9 +93,9 @@ const article = { @@ -99,9 +93,9 @@ const article = {
99 const imgUrl = req.body.imgUrl; 93 const imgUrl = req.body.imgUrl;
100 const content = req.body.content; 94 const content = req.body.content;
101 95
102 - !imgUrl && (errorMsg = INVALID_IMG);  
103 - !content && (errorMsg = INVALID_CONTENT);  
104 - !actId && (errorMsg = INVALID_ACTIVITY_ID); 96 + if (!imgUrl || !content || !actId) {
  97 + errorMsg = INVALID_PARAMS;
  98 + }
105 99
106 if (errorMsg) { 100 if (errorMsg) {
107 return res.json({ 101 return res.json({
@@ -123,7 +117,7 @@ const article = { @@ -123,7 +117,7 @@ const article = {
123 res.json({ 117 res.json({
124 code: 200, 118 code: 200,
125 data: {id}, 119 data: {id},
126 - message: ADD_ARTICLE_SUCCESS 120 + message: POST_SUCCESS
127 }); 121 });
128 }); 122 });
129 }); 123 });
@@ -139,8 +133,9 @@ const article = { @@ -139,8 +133,9 @@ const article = {
139 const actId = req.query.actId; 133 const actId = req.query.actId;
140 const articleId = req.query.articleId; 134 const articleId = req.query.articleId;
141 135
142 - !actId && (errorMsg = MISS_PARAMS);  
143 - !articleId && (errorMsg = MISS_PARAMS); 136 + if (!actId || !articleId) {
  137 + errorMsg = INVALID_PARAMS;
  138 + }
144 139
145 if (errorMsg) { 140 if (errorMsg) {
146 return res.json({ 141 return res.json({
@@ -172,7 +167,35 @@ const article = { @@ -172,7 +167,35 @@ const article = {
172 res.json({ 167 res.json({
173 code: 200, 168 code: 200,
174 data: final_result, 169 data: final_result,
175 - message: GET_ARTICLE_SUCCESS 170 + message: GET_SUCCESS
  171 + });
  172 + });
  173 + },
  174 +
  175 + /**
  176 + * 文章点赞
  177 + * @param req
  178 + * @param res
  179 + */
  180 + like(req, res) {
  181 + const actId = req.body.actId;
  182 + const articleId = req.body.articleId;
  183 +
  184 + if (!actId || !articleId) {
  185 + return res.json({
  186 + code: 400,
  187 + message: INVALID_PARAMS
  188 + });
  189 + }
  190 +
  191 + req.ctx(ArticleModel).likeArticle(actId, articleId)
  192 + .then(() => {
  193 + req.ctx(ArticleModel).insertLikeDetail(actId, articleId)
  194 + .then(() => {
  195 + res.json({
  196 + code: 200,
  197 + message: POST_SUCCESS
  198 + });
176 }); 199 });
177 }); 200 });
178 } 201 }
@@ -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);
1 1
2 ## 基础服务 2 ## 基础服务
3 3
4 -## 登录(只支持短信验证登录)  
5 -[验证码登录](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录)  
6 -  
7 - [发送验证码](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/发送验证码.md) 4 - [发送验证码](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/发送验证码.md)
8 - - [手机号自动登录](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/手机号自动登录.md)  
9 - - [校验是否是注册用户](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/校验是否是注册用户.md)  
10 - [验证验证码](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/验证验证码.md) 5 - [验证验证码](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/验证验证码.md)
1 1
2 ### 一、点赞 2 ### 一、点赞
  3 +**请求路径**
  4 +> {host}/article/like
  5 +
  6 +**请求方法**
  7 +> POST
  8 +
  9 +**请求参数**
  10 +
  11 +| 名称 | 类型 | 描述 | 备注 |
  12 +| ---- | ----- |---- | --- |
  13 +| artId|string|活动ID|
  14 +| article | string|文章ID|
  15 +
  16 +
  17 +### Request (JSON)
  18 + {
  19 + "actId": 1,
  20 + "articleId": 1
  21 + }
  22 +### Result
  23 + {
  24 + "code": 200,
  25 + "message": "操作成功"
  26 + }
3 27
4 ### 二、发送验证码 28 ### 二、发送验证码
5 **请求路径** 29 **请求路径**
@@ -126,7 +150,7 @@ @@ -126,7 +150,7 @@
126 "pageSize": 10, 150 "pageSize": 10,
127 "totalCount": 21, 151 "totalCount": 21,
128 "totalPage": 3, 152 "totalPage": 3,
129 - "message": "获取文章列表成功" 153 + "message": "获取成功"
130 } 154 }
131 155
132 ### 五、发布文章 156 ### 五、发布文章
@@ -164,7 +188,7 @@ @@ -164,7 +188,7 @@
164 "data": { 188 "data": {
165 id: 3 189 id: 3
166 }, 190 },
167 - "message": "文章发表成功" 191 + "message": "操作成功"
168 } 192 }
169 193
170 194
@@ -207,5 +231,5 @@ @@ -207,5 +231,5 @@
207 "imgUrl": "http://img01.yohoboys.com/o_1bk3gc79c11p21c4l1e4ksk0vdl7.jpg?imageMogr2/auto-orient/thumbnail/626/strip/gravity/Center/quality/60/crop/626x530/format/jpg" 231 "imgUrl": "http://img01.yohoboys.com/o_1bk3gc79c11p21c4l1e4ksk0vdl7.jpg?imageMogr2/auto-orient/thumbnail/626/strip/gravity/Center/quality/60/crop/626x530/format/jpg"
208 } 232 }
209 ], 233 ],
210 - "message": "获取文章详情成功" 234 + "message": "获取成功"
211 } 235 }
@@ -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 -};