Authored by 李奇

短信验证和返回用户信息添加

... ... @@ -4,6 +4,7 @@
* @date: 28/06/2017
*/
const _ = require('lodash');
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_ARTICLE = 'act_article';
... ... @@ -23,15 +24,17 @@ class ArticleModel extends global.yoho.BaseModel {
/**
* 发表文章
* @param userName
* @param content
* @returns {*}
*/
createArticle({userName, content}) {
createArticle({content}) {
const session = this.ctx.req.session;
const userId = _.get(session, 'user.id');
return mysqlCli.insert(
`insert into ${TABLE_ACT_ARTICLE} (user_name, content) values (':userName', ':content');`,
`insert into ${TABLE_ACT_ARTICLE} (user_id, content) values (:userId, :content);`,
{
userName,
userId,
content
}
);
... ...
... ... @@ -6,23 +6,16 @@
'use strict';
const _ = require('lodash');
const EventEmitter = require('events');
const AuthHelper = require('../models/auth-helper');
const PhoneService = require('../models/phone-service');
const SmsModel = require('../models/sms');
const VERIFY_ERROR = '校验失败';
const LOGIN_SUCCESS = '登录成功';
const ANONYMOUS_USER = '匿名用户';
const CODE_REQUIRED = '请输入校验码';
const GET_USER_INFO_FAILED = '获取用户信息失败';
const sms = {
const smsController = {
/**
* 发送短信验证码
* @param req
* @param res
* @returns {*|boolean}
*/
send(req, res) {
sendCode(req, res) {
const em = new EventEmitter();
const area = (req.body.area || '').trim();
const mobile = (req.body.mobile || '').trim();
... ... @@ -32,7 +25,7 @@ const sms = {
_.set(req.session, 'smsLogin.area', area);
_.set(req.session, 'smsLogin.mobile', mobile);
PhoneService.sendSMS(mobile, area, 1)
req.ctx(SmsModel).sendSMS(mobile, area, 1)
.then(result => {
if (result.code === 200) {
return res.json({
... ... @@ -54,7 +47,8 @@ const sms = {
if (!inValid) {
return em.emit('resolve');
em.emit('resolve');
return;
}
em.emit('reject', {
... ... @@ -68,116 +62,28 @@ const sms = {
* @param req
* @param res
*/
check(req, res) {
console.log(req, res);
},
/**
* 验证码登录
* @param req
* @param res
* @param next
*/
login(req, res, next) {
checkCode(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
});
return;
}
Promise.all([
PhoneService.checkUserPhoneExist(mobile, area),
PhoneService.verifySMS(mobile, area, code, 1)
])
req.ctx(SmsModel).verifySMS(mobile, area, code, 1)
.then(result => {
const res0 = result[0] || {};
const res1 = result[1] || {};
if (res0.code !== 200) {
res.json(res0);
return;
}
if (res1.code !== 200) {
res.json(res1);
if (result.code !== 200) {
res.json(result);
return;
}
// 失败
if (res1.data.is_pass !== 'Y') {
if (result.data.is_pass !== 'Y') {
res.json({
code: 401,
message: VERIFY_ERROR
code: 501,
message: '验证码与手机号不匹配'
});
return;
}
// 未注册
if (res0.data.is_register !== 'Y') {
if (toSignUp) {
// TODO 对于想要注册的用户直接注册
} else {
res.json({
code: 200,
data: {
avatar: '',
name: `匿名用户${Math.random() * 100000}`,
},
message: ANONYMOUS_USER
next();
});
}
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;
module.exports = smsController;
... ...
/**
* 用户controller
* @author: leo <qi.li@yoho.cn>
* @date: 2017/06/26
*/
'use strict';
const _ = require('lodash');
const UserModel = require('../models/user');
const GET_USER_INFO_SUCCESS = '获取用户信息成功';
const userController = {
/**
* 获取用户信息
* @param req
* @param res
*/
userInfo(req, res) {
const mobile = _.get(req.session, 'smsLogin.mobile', '');
req.ctx(UserModel).getUser(mobile)
.then(result => {
let userInfo = {};
if (result.length) {
let user = result[0];
// 用户ID
_.set(req.session, 'user.id', user.id);
res.json({
code: 200,
data: {
name: user.user_name,
mobile: user.user_phone,
avatar: user.user_avatar
},
message: GET_USER_INFO_SUCCESS
});
} else {
let name = '用户_' + mobile.substring(5);
let avatar = 'http://img12.static.yhbimg.com/' +
'imserver/2016/11/17/11/02bc3c3de856432175c01d937342a1f2ce.jpg'; // TODO TO BE REPLACED
userInfo = {
name,
avatar,
mobile
};
req.ctx(UserModel).createUser(userInfo)
.then(() => {
res.json({
code: 200,
data: userInfo,
message: GET_USER_INFO_SUCCESS
});
});
}
});
}
};
module.exports = userController;
... ...
'use strict';
const API = global.yoho.API;
class PhoneService {
// 校验手机是否已注册
static checkUserPhoneExist(mobile, area) {
return API.get('', {
method: 'app.passport.checkUserExist',
mobile,
area
});
}
// 手机号自动登录
static autoSignin(param) {
return API.get('', {
method: 'app.passport.autoSignin',
profile: param.profile,
area: param.area,
code: param.code,
shopping_key: param.shopping_key
});
}
// 发送验证码
static sendSMS(mobile, area, type) {
return API.get('', {
method: 'app.message.sendSms',
mobile,
area,
type
});
}
// 校验 验证码
static verifySMS(mobile, area, code, type) {
return API.get('', {
method: 'app.message.verifySmsCode',
mobile,
area,
code,
type
});
}
}
module.exports = PhoneService;
'use strict';
const API = global.yoho.API;
class SmsModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 发送验证码
* @param mobile
* @param area
* @param type
*/
sendSMS(mobile, area, type) {
return API.get('', {
method: 'app.message.sendSms',
mobile,
area,
type
});
}
/**
* 校验验证码
* @param mobile
* @param area
* @param code
* @param type
*/
verifySMS(mobile, area, code, type) {
return API.get('', {
method: 'app.message.verifySmsCode',
mobile,
area,
code,
type
});
}
}
module.exports = SmsModel;
... ...
'use strict';
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_USER = 'user';
class UserModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
getUser(mobile) {
return mysqlCli.query(`select * from ${TABLE_USER}
where user_phone=:mobile`, {
mobile
});
}
createUser({name, mobile, avatar}) {
name = name || '';
mobile = mobile || '';
avatar = avatar || '';
return mysqlCli.insert(
`insert into ${TABLE_USER}
(user_phone, user_name, user_avatar) values (:mobile, :name, :avatar)`, {
name,
mobile,
avatar
});
}
}
module.exports = UserModel;
... ...
... ... @@ -8,12 +8,12 @@
const express = require('express');
const sms = require('./controllers/sms');
const user = require('./controllers/user');
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);
router.post('/sms/sendCode', sms.sendCode);
router.post('/sms/checkUser', sms.checkCode, user.userInfo);
module.exports = router;
... ...