Authored by 邱骏

Merge branch 'feature/platform-basic' of git.yoho.cn:fe/yoho-activity-platform i…

…nto feature/platform-basic
... ... @@ -7,6 +7,9 @@ const ArticleModel = require('../models/article');
const ADD_ARTICLE_SUCCESS = '文章发表成功';
const GET_ARTICLES_SUCCESS = '获取文章列表成功';
const INVALID_ACTIVITY_ID = '活动ID[actId]不能为空';
const INVALID_IMG = '图片[imgUrl]不能为空';
const INVALID_CONTENT = '内容[content]都不能为空';
const article = {
/**
... ... @@ -32,14 +35,38 @@ const article = {
* @param res
*/
publish(req, res) {
const params = req.body;
let errorMsg = '';
const actId = req.body.actId;
const imgUrl = req.body.imgUrl;
const content = req.body.content;
!imgUrl && (errorMsg = INVALID_IMG);
!content && (errorMsg = INVALID_CONTENT);
!actId && (errorMsg = INVALID_ACTIVITY_ID);
if (errorMsg) {
return res.json({
code: 400,
message: errorMsg
});
}
const params = {
actId,
imgUrl,
content
};
req.ctx(ArticleModel).createArticle(params)
.then(() => {
res.json({
code: 200,
message: ADD_ARTICLE_SUCCESS
});
.then((id) => {
req.ctx(ArticleModel).insertArticleImg(id, imgUrl)
.then(() => {
res.json({
code: 200,
message: ADD_ARTICLE_SUCCESS
});
});
});
}
};
... ...
... ... @@ -8,6 +8,7 @@ const _ = require('lodash');
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_ACT_ARTICLE_IMG = 'act_article_img';
class ArticleModel extends global.yoho.BaseModel {
constructor(ctx) {
... ... @@ -24,21 +25,41 @@ class ArticleModel extends global.yoho.BaseModel {
/**
* 发表文章
* @param content
* @param actId 活动ID
* @param content 文章内容
* @param imgUrl 图片链接
* @returns {*}
*/
createArticle({content}) {
createArticle({actId, content, imgUrl}) {
const session = this.ctx.req.session;
const userId = _.get(session, 'user.id');
return mysqlCli.insert(
`insert into ${TABLE_ACT_ARTICLE} (user_id, content) values (:userId, :content);`,
`insert into ${TABLE_ACT_ARTICLE} (act_id, user_id, content) values (:actId, :userId, :content);`,
{
actId,
userId,
imgUrl,
content
}
);
}
/**
* 文章图片插入
* @param articleId
* @param imgUrl
* @returns {*}
*/
insertArticleImg(articleId, imgUrl) {
return mysqlCli.insert(
`insert into ${TABLE_ACT_ARTICLE_IMG} (article_id, img_url) values (:articleId, :imgUrl);`,
{
imgUrl,
articleId
}
);
}
}
module.exports = ArticleModel;
... ...
... ... @@ -5,9 +5,10 @@
*/
const express = require('express');
const router = express.Router(); // eslint-disable-line
const {auth} = require('../../middleware');
const article = require('./controllers/article');
router.get('/all', article.all);
router.post('/publish', article.publish);
router.post('/publish', auth, article.publish);
module.exports = router;
... ...
... ... @@ -31,6 +31,7 @@ const userController = {
res.json({
code: 200,
data: {
id: user.id,
name: user.user_name,
mobile: user.user_phone,
avatar: user.user_avatar
... ... @@ -49,10 +50,10 @@ const userController = {
};
req.ctx(UserModel).createUser(userInfo)
.then(() => {
.then((id) => {
res.json({
code: 200,
data: userInfo,
data: _.assign(userInfo, {id}),
message: GET_USER_INFO_SUCCESS
});
});
... ...
/**
* index.js
* passport入口
* @author: leo
* @date: 23/06/2017
*/
... ...
/**
* 短信验证登录路由
* passport路由
* @author: leo <qi.li@yoho.cn>
* @date: 2017/06/26
*/
... ...
... ... @@ -74,7 +74,34 @@
### 四、图片列表
### 五、发布评论
### 五、发布文章
**请求路径**
> {host}/passport/article/publish
**请求方法**
> POST
**请求参数**
|名称 | 类型 |必填| 描述
|------|-------|-----|----
|actId|string |是|活动Id
|imgUrl|string |是|图片链接
|content | string |是|内容
### Request (JSON)
{
"actId": 1
"imgUrl": 'http://img12.static.yhbimg.com/imserver/2016/11/17/11/02bc3c3de856432175c01d937342a1f2ce.jpg'
"content":'测试评论内容'
}
### Result
{
"code": 200,
"message": "文章发表成功"
}
### 六、图片详情
... ...
... ... @@ -5,10 +5,14 @@
*/
'use strict';
const _ = require('lodash');
module.exports = (req, res, next) => {
if (!req.user.uid) {
const userId = _.get(req.session, 'user.id', 0);
if (!userId) {
return res.json({
code: 400,
code: 401,
message: '抱歉,您暂未登录!'
});
}
... ...