Authored by 陈峰

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

…tform into feature/platform-basic
... ... @@ -4,6 +4,7 @@
* @date: 2017/6/23
*/
'use strict';
const cors = require('cors');
const express = require('express');
const config = require('./config');
const bluebird = require('bluebird');
... ... @@ -34,7 +35,6 @@ app.use(bodyParser.urlencoded({
}));
app.use(cookieParser());
app.use(global.yoho.httpCtx());
app.use(session({
proxy: true,
resave: false,
... ... @@ -64,6 +64,9 @@ const middleware = require('./middleware');
try {
// 允许跨域
app.use(cors());
// 用户信息
app.use(middleware.user);
... ...
... ... @@ -5,34 +5,44 @@
*/
const ArticleModel = require('../models/article');
const GET_ARTICLES_SUCCESS = '获取文章列表成功';
const ADD_ARTICLE_SUCCESS = '文章发表成功';
const GET_ARTICLES_SUCCESS = '获取文章列表成功';
function all(req, res) {
req.ctx(ArticleModel).allArticles()
.then(result => {
res.json({
code: 200,
data: result,
message: GET_ARTICLES_SUCCESS
const article = {
/**
* 所有文章
* @param req
* @param res
*/
all(req, res) {
req.ctx(ArticleModel).allArticles()
.then(result => {
res.json({
code: 200,
data: result,
message: GET_ARTICLES_SUCCESS
});
});
});
}
},
function add(req, res) {
const params = req.body;
/**
* 创建文章
* @param req
* @param res
*/
add(req, res) {
const params = req.body;
req.ctx(ArticleModel).createArticle(params)
.then(() => {
res.json({
code: 200,
message: ADD_ARTICLE_SUCCESS
req.ctx(ArticleModel).createArticle(params)
.then(() => {
res.json({
code: 200,
message: ADD_ARTICLE_SUCCESS
});
});
});
}
module.exports = {
all,
add
}
};
module.exports = article;
... ...
/**
* 短信controller
* @author: leo <qi.li@yoho.cn>
* @date: 2017/06/26
*/
'use strict';
const _ = require('lodash');
const EventEmitter = require('events');
... ... @@ -10,154 +15,169 @@ const ANONYMOUS_USER = '匿名用户';
const CODE_REQUIRED = '请输入校验码';
const GET_USER_INFO_FAILED = '获取用户信息失败';
// const BAD_PASSWORD = '密码格式不正确';
// const PASSWORD_REQUIRED = '请输入密码';
// const PASSWORD_LENGTH_ERROR = '密码6-20位,请重新输入';
/**
* 发送短信验证码
* @param req
* @param res
* @returns {*|boolean}
*/
exports.send = (req, res) => {
const em = new EventEmitter();
const area = (req.body.area || '').trim();
const mobile = (req.body.mobile || '').trim();
const inValid = [area, mobile].some(v => v === '');
em.on('resolve', () => {
_.set(req.session, 'smsLogin.area', area);
_.set(req.session, 'smsLogin.mobile', mobile);
PhoneService.sendSMS(mobile, area, 1)
.then(result => {
if (result.code === 200) {
return res.json({
code: 200,
message: '验证码发送成功'
const sms = {
/**
* 发送短信验证码
* @param req
* @param res
* @returns {*|boolean}
*/
send(req, res) {
const em = new EventEmitter();
const area = (req.body.area || '').trim();
const mobile = (req.body.mobile || '').trim();
const inValid = [area, mobile].some(v => v === '');
em.on('resolve', () => {
_.set(req.session, 'smsLogin.area', area);
_.set(req.session, 'smsLogin.mobile', mobile);
PhoneService.sendSMS(mobile, area, 1)
.then(result => {
if (result.code === 200) {
return res.json({
code: 200,
message: '验证码发送成功'
});
}
res.json({
code: 500,
message: '验证码发送失败'
});
}
res.json({
code: 500,
message: '验证码发送失败'
});
});
});
em.on('reject', error => {
res.json(error);
});
if (!inValid) {
return em.emit('resolve');
}
em.emit('reject', {
code: 400,
message: '请填写手机号'
});
};
exports.login = (req, res, next) => {
const code = (req.body.code || '').trim();
const area = _.get(req.session, 'smsLogin.area', '');
const mobile = _.get(req.session, 'smsLogin.mobile', '');
const toSignUp = (req.body.toSignUp || '').trim() || false;
if (!code) {
res.json({
code: 401,
message: CODE_REQUIRED
});
return;
}
if (!mobile || !area) {
res.json({
code: 401,
message: VERIFY_ERROR
em.on('reject', error => {
res.json(error);
});
return;
}
Promise.all([
PhoneService.checkUserPhoneExist(mobile, area),
PhoneService.verifySMS(mobile, area, code, 1)
])
.then(result => {
const res0 = result[0] || {};
const res1 = result[1] || {};
if (res0.code !== 200) {
res.json(res0);
return;
if (!inValid) {
return em.emit('resolve');
}
if (res1.code !== 200) {
res.json(res1);
em.emit('reject', {
code: 400,
message: '请填写手机号'
});
},
/**
* 验证码校验
* @param req
* @param res
*/
check(req, res) {
console.log(req, res);
},
/**
* 验证码登录
* @param req
* @param res
* @param next
*/
login(req, res, next) {
const code = (req.body.code || '').trim();
const area = _.get(req.session, 'smsLogin.area', '');
const mobile = _.get(req.session, 'smsLogin.mobile', '');
const toSignUp = (req.body.toSignUp || '').trim() || false;
if (!code) {
res.json({
code: 401,
message: CODE_REQUIRED
});
return;
}
// 失败
if (res1.data.is_pass !== 'Y') {
if (!mobile || !area) {
res.json({
code: 401,
message: VERIFY_ERROR
});
return;
}
// 未注册
if (res0.data.is_register !== 'Y') {
if (toSignUp) {
// TODO 对于想要注册的用户直接注册
} else {
res.json({
code: 200,
data: {
avatar: '',
name: `匿名用户${Math.random() * 100000}`,
},
message: ANONYMOUS_USER
});
}
return;
}
Promise.all([
PhoneService.checkUserPhoneExist(mobile, area),
PhoneService.verifySMS(mobile, area, code, 1)
])
.then(result => {
const res0 = result[0] || {};
const res1 = result[1] || {};
// 已注册
PhoneService.autoSignin({
profile: mobile,
code: res1.data.code,
area
})
.then(info => {
if (info.code !== 200) {
return res.json({
code: info.code,
data: GET_USER_INFO_FAILED
});
}
if (res0.code !== 200) {
res.json(res0);
return;
}
const uid = info.data.uid;
const sessionKey = info.data.session_key;
if (res1.code !== 200) {
res.json(res1);
return;
}
return AuthHelper.syncUserSession(uid, req, res, sessionKey);
})
.then(() => {
res.json({
code: 200,
data: req.session.user,
message: LOGIN_SUCCESS
});
})
.catch(error => {
res.json(error);
});
// 失败
if (res1.data.is_pass !== 'Y') {
res.json({
code: 401,
message: VERIFY_ERROR
});
return;
}
})
.catch(next);
// 未注册
if (res0.data.is_register !== 'Y') {
if (toSignUp) {
// TODO 对于想要注册的用户直接注册
} else {
res.json({
code: 200,
data: {
avatar: '',
name: `匿名用户${Math.random() * 100000}`,
},
message: ANONYMOUS_USER
});
}
return;
}
// 已注册
PhoneService.autoSignin({
profile: mobile,
code: res1.data.code,
area
})
.then(info => {
if (info.code !== 200) {
return res.json({
code: info.code,
data: GET_USER_INFO_FAILED
});
}
const uid = info.data.uid;
const sessionKey = info.data.session_key;
return AuthHelper.syncUserSession(uid, req, res, sessionKey);
})
.then(() => {
res.json({
code: 200,
data: req.session.user,
message: LOGIN_SUCCESS
});
})
.catch(error => {
res.json(error);
});
})
.catch(next);
}
};
module.exports = sms;
... ...
... ... @@ -14,5 +14,6 @@ const router = express.Router(); // eslint-disable-line
// SMS 短信
router.post('/sms/login', sms.login);
router.post('/sms/sendCode', sms.send);
router.post('/sms/checkCode', sms.check);
module.exports = router;
... ...
... ... @@ -40,6 +40,7 @@
"connect-memcached": "^0.2.0",
"connect-multiparty": "^2.0.0",
"cookie-parser": "^1.4.3",
"cors": "^2.8.3",
"cssnano": "^3.10.0",
"express": "^4.15.3",
"fast-safe-stringify": "^1.2.0",
... ...
... ... @@ -1599,6 +1599,13 @@ core-util-is@~1.0.0:
version "1.0.2"
resolved "http://npm.yoho.cn/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
cors@^2.8.3:
version "2.8.3"
resolved "http://npm.yoho.cn/cors/-/cors-2.8.3.tgz#4cf78e1d23329a7496b2fc2225b77ca5bb5eb802"
dependencies:
object-assign "^4"
vary "^1"
cosmiconfig@^2.1.0, cosmiconfig@^2.1.1:
version "2.1.3"
resolved "http://npm.yoho.cn/cosmiconfig/-/cosmiconfig-2.1.3.tgz#952771eb0dddc1cb3fa2f6fbe51a522e93b3ee0a"
... ... @@ -4943,7 +4950,7 @@ object-assign@^3.0.0:
version "3.0.0"
resolved "http://npm.yoho.cn/object-assign/-/object-assign-3.0.0.tgz#9bedd5ca0897949bca47e7ff408062d549f587f2"
object-assign@^4.0.1, object-assign@^4.1.0:
object-assign@^4, object-assign@^4.0.1, object-assign@^4.1.0:
version "4.1.1"
resolved "http://npm.yoho.cn/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
... ... @@ -7869,7 +7876,7 @@ validate-npm-package-name@^3.0.0, validate-npm-package-name@~3.0.0:
dependencies:
builtins "^1.0.3"
vary@~1.1.0, vary@~1.1.1:
vary@^1, vary@~1.1.0, vary@~1.1.1:
version "1.1.1"
resolved "http://npm.yoho.cn/vary/-/vary-1.1.1.tgz#67535ebb694c1d52257457984665323f587e8d37"
... ...