Authored by 邱骏

update

... ... @@ -51,9 +51,6 @@ try {
origin: config.corsAllowOrigin
}));
// 用户信息
app.use(middleware.user);
// 路由分发
require('./dispatch')(app);
... ...
... ... @@ -7,15 +7,9 @@ const _ = require('lodash');
const camelcase = require('camelcase');
const ArticleModel = require('../models/article');
const ADD_ARTICLE_SUCCESS = '文章发表成功';
const GET_ARTICLES_SUCCESS = '获取文章列表成功';
const GET_ARTICLE_SUCCESS = '获取文章详情成功';
const INVALID_ACTIVITY_ID = '活动ID[actId]不能为空';
const INVALID_IMG = '图片[imgUrl]不能为空';
const INVALID_CONTENT = '内容[content]都不能为空';
const MISS_PARAMS = '缺少参数';
const INVALID_ORDER = '排序[order]值为asc或desc';
const INVALID_ORDER_BY = '排序字段[orderBy]非法';
const POST_SUCCESS = '操作成功';
const GET_SUCCESS = '获取成功';
const INVALID_PARAMS = '参数错误';
const article = {
/**
... ... @@ -42,14 +36,14 @@ const article = {
if (order !== 'asc' && order !== 'desc') {
return res.json({
code: 400,
message: INVALID_ORDER
message: INVALID_PARAMS
});
}
if (orderByFields.indexOf(orderBy) === -1) {
return res.json({
code: 400,
message: INVALID_ORDER_BY
message: INVALID_PARAMS
});
}
... ... @@ -81,7 +75,7 @@ const article = {
pageSize,
totalCount,
totalPage: Math.ceil(totalCount / pageSize),
message: GET_ARTICLES_SUCCESS
message: GET_SUCCESS
});
});
});
... ... @@ -99,9 +93,9 @@ const article = {
const imgUrl = req.body.imgUrl;
const content = req.body.content;
!imgUrl && (errorMsg = INVALID_IMG);
!content && (errorMsg = INVALID_CONTENT);
!actId && (errorMsg = INVALID_ACTIVITY_ID);
if (!imgUrl || !content || !actId) {
errorMsg = INVALID_PARAMS;
}
if (errorMsg) {
return res.json({
... ... @@ -123,7 +117,7 @@ const article = {
res.json({
code: 200,
data: {id},
message: ADD_ARTICLE_SUCCESS
message: POST_SUCCESS
});
});
});
... ... @@ -139,8 +133,9 @@ const article = {
const actId = req.query.actId;
const articleId = req.query.articleId;
!actId && (errorMsg = MISS_PARAMS);
!articleId && (errorMsg = MISS_PARAMS);
if (!actId || !articleId) {
errorMsg = INVALID_PARAMS;
}
if (errorMsg) {
return res.json({
... ... @@ -172,9 +167,37 @@ const article = {
res.json({
code: 200,
data: final_result,
message: GET_ARTICLE_SUCCESS
message: GET_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: INVALID_PARAMS
});
}
req.ctx(ArticleModel).likeArticle(actId, articleId)
.then(() => {
req.ctx(ArticleModel).insertLikeDetail(actId, articleId)
.then(() => {
res.json({
code: 200,
message: POST_SUCCESS
});
});
});
}
};
... ...
... ... @@ -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);
... ...
## 基础服务
## 登录(只支持短信验证登录)
[验证码登录](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录)
- [发送验证码](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/发送验证码.md)
- [手机号自动登录](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/手机号自动登录.md)
- [校验是否是注册用户](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/校验是否是注册用户.md)
- [验证验证码](http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/个人中心/验证码登录/验证验证码.md)
... ...
### 一、点赞
**请求路径**
> {host}/article/like
**请求方法**
> POST
**请求参数**
| 名称 | 类型 | 描述 | 备注 |
| ---- | ----- |---- | --- |
| artId|string|活动ID|
| article | string|文章ID|
### Request (JSON)
{
"actId": 1,
"articleId": 1
}
### Result
{
"code": 200,
"message": "操作成功"
}
### 二、发送验证码
**请求路径**
> {host}/passport/sms/sendCOde
... ... @@ -126,7 +150,7 @@
"pageSize": 10,
"totalCount": 21,
"totalPage": 3,
"message": "获取文章列表成功"
"message": "获取成功"
}
### 五、发布文章
... ... @@ -164,7 +188,7 @@
"data": {
id: 3
},
"message": "文章发表成功"
"message": "操作成功"
}
... ... @@ -207,5 +231,5 @@
"imgUrl": "http://img01.yohoboys.com/o_1bk3gc79c11p21c4l1e4ksk0vdl7.jpg?imageMogr2/auto-orient/thumbnail/626/strip/gravity/Center/quality/60/crop/626x530/format/jpg"
}
],
"message": "获取文章详情成功"
"message": "获取成功"
}
... ...
... ... @@ -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();
};