Authored by htoooth

add api

... ... @@ -9,6 +9,7 @@ const library = '../../../library';
const helpers = require(`${library}/helpers`);
const service = require('../models/back-service');
const sessionService = require('../models/session-service');
// 本地地址 localhost
helpers.urlFormat = helpers.fakeUrlFormat;
... ... @@ -32,17 +33,33 @@ module.exports.indexPage = (req, res) => {
/**
* 校验用户输入信息,是否是已经注册的用户
*/
module.exports.identifyUserByInputAPI = (req, res) => {
module.exports.validateUserInputAPI = (req, res, next) => {
let userInput = req.body.phoneNum || '';
let areaCode = req.body.area || '86';
service.identifyUserByEmailOrMobileAsync(userInput, areaCode)
service.validateEmailOrMobileAsync(userInput, areaCode)
.then(result => {
res.json(result);
req.inputInfo = result;
next();
})
.catch(err => {
res.json({
code: 400,
message: err
});
});
};
module.exports.getUserInfoAPI = (req, res, next) => {
let inputInfo = req.inputInfo;
service.findUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
.then(result => {
res.json(result);
}).catch(next);
};
module.exports.sendCodeAPI = (req, res, next) => {
let userInput = req.body.phoneNum || '';
... ... @@ -51,23 +68,77 @@ module.exports.sendCodeAPI = (req, res, next) => {
service.sendCodeToUserAsync(userInput, areaCode).then(result => {
switch (result) {
case 'mobile':
{
res.redirect('/passport/back/sendemail.html');
break;
}
{
res.redirect('/passport/back/sendemail.html');
break;
}
case 'email':
{
res.redirect('/passport/back/verification.html');
break;
}
{
res.redirect('/passport/back/verification.html');
break;
}
default:
{
res.redirect('./passport/back/index.html');
}
{
res.redirect('./passport/back/index.html');
}
}
}).catch(next);
};
module.exports.sendBackMobileAPI = (req, res, next) => {
let mobile = req.param('mobile', '');
let area = req.param('area', '86');
service.sendCodeToMobileAsync(area, mobile)
.then(result => {
res.json(result);
}).catch(next);
};
module.exports.sendEmailPage = (req, res, next) => {
service.getSendEmailPageDataAsync()
.then(result => {
res.render('back/send-email', Object.assign({
module: 'back',
page: 'send-email',
title: "邮件发送成功"
}, result
));
}).catch(next);
};
module.exports.checkEmailCodePage = (req, res, next) => {
let code = req.param('code', '');
service.checkEmailCodeAsync(code)
.then(result => {
if (result) {
next();
} else {
res.redirect('/passport/back/index.html');
}
}).catch(next);
};
module.exports.resetPasswordByEmailPage = (req, res, next) => {
let code = res.param('code', '');
service.getLeftBannerAsync()
.then(result => {
res.render('back/email-reset-pwd', Object.assign({
module: 'back',
page: 'email-reset-pwd'
}, {
code: code
}, result))
}).catch(next);
};
/**
* 手机验证页面
*/
module.exports.verifyCodeByMobilePage = (req, res, next) => {
service.getVerifyCodeByMobilePageDataAsync()
... ... @@ -87,3 +158,74 @@ module.exports.verifyCodeByMobilePage = (req, res, next) => {
}));
}).catch(next);
};
module.exports.verifyCodeByMobileAPI = (req, res, next) => {
};
module.exports.checkSuccessStatusPage = (req, res, next) => {
let successType = sessionService.get('successType', '');
if (successType) {
sessionService.set('successType', '');
next();
} else {
res.redirect('/passport/back/index.html');
}
};
module.exports.resetPwdSuccessPage = (req, res, next) => {
service.getLeftBannerAsync()
.then(result => {
res.render('back/email-reset-success', Object.assign({
module: 'back',
page: 'email-reset-success',
title: '重置密码成功'
}, result))
}).catch(next);
};
module.exports.verifyCodyByMobileAPI = (req, res, next) => {
let mobile = req.param('mobile', '');
let area = req.param('aera', '86');
let mobileCode = req.param('code', '');
service.verifyCodyByMobileAsync(area, mobile, mobileCode)
.then(result => {
res.json(result);
}).catch(next);
};
module.exports.tokenCheckPage = (req, res, next) => {
let token = req.param('code', '');
service.authRequest(token).then(result => {
if (result) {
req.authInfo = result;
next();
} else {
res.redirect('/passport/back/index');
}
})
};
module.exports.validationPwdPage = (req, res, next) => {
let pwd = req.param('pwd', '');
if (helpers.verifyPassword(pwd)) {
res.redirect('/passport/back/index.html');
} else {
next();
}
};
module.exports.updatePwdPage = (req, res, next) => {
let code = req.param('code', '');
let auth = req.authInfo;
};
... ...
... ... @@ -51,6 +51,23 @@ class Auth {
}); // esline-disable-line
}).catch(console.log);
}
//
// TODO:
static _auth(str, key, expiry, operation) {
}
// TODO:
static encode(str){
return '';
}
// TODO:
static decode(str){
return ''
}
}
module.exports = Auth;
... ...
/**
* Created by TaoHuang on 2016/6/20.
*/
'use strict';
// TODO:
module.exports.getLeftBanner = (resourceCode) => {
};
... ... @@ -9,43 +9,60 @@ const api = require('./back-api');
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const moment = require('moment');
const userService = require('./user-service');
const sessionService = require('./session-service');
const api = require('./back-api');
const passportHelper = require('./passport-helper');
module.exports.identifyUserByEmailOrMobileAsync = (userInput, areaCode) => {
return co(function * () {
const BACK_LEFT_BANNER_CODE = '3bbaf502c447a2ddad60879042e286d8'; //找回密码左边的banner
module.exports.validateEmailOrMobileAsync = (userInput, areaCode) => {
return new Promise(function (resolve, rejected) {
let result = {type: 'email', area: '', phone: ''};
if (helpers.verifyEmail(userInput)) {
result.type = 'email';
result.aera = '';
result.phone = userInput;
resolve(result);
} else if (helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, userInput))) {
result.type = 'mobile';
result.area = areaCode;
result.phone = userInput;
resolve(result);
} else {
rejected('输入信息出错!');
}
});
};
module.exports.findUserAsync = (type, phone, area) => {
return co(function * () {
const MESSAGE = {
errPhone: '您输入的手机号码尚未注册!',
errEmail: '您输入的邮件账户尚未注册!',
errUnknown: '服务器错误',
mobile: '您输入的手机号码尚未注册!',
email: '您输入的邮件账户尚未注册!',
ok: '验证成功'
};
const OK = {code: 200, message: MESSAGE.ok};
let status = {code: 400, message: MESSAGE.errUnknown};
const findBy = {
email: userService.findByEmailAsync,
mobile: userService.findByMobileAsync
};
if (helpers.verifyEmail(userInput)) {
const user = yield userService.findByEmailAsync(userInput);
const OK = {code: 200, message: MESSAGE.ok};
if (_.isEmpty(user)) {
status.code = 402;
status.message = MESSAGE.errEmail;
return status;
}
} else if (helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, userInput))) {
const user = yield userService.findByMobileAsync(userInput, areaCode);
const user = yield findBy[type](phone, area);
if (_.isEmpty(user)) {
status.code = 402;
status.message = MESSAGE.errPhone;
return status;
}
} else {
return status;
if (_.isEmpty(user)) {
return {
code: 402,
message: MESSAGE[type]
};
}
return OK;
... ... @@ -104,7 +121,6 @@ module.exports.sendCodeToUserAsync = (userInput, areaCode) => {
})();
};
/**
* 发送找回手机号短信
*/
... ... @@ -125,11 +141,50 @@ module.exports.sendCodeToEmailAsync = (email) => {
/**
* 获得首页的数据
*/
// TODO:
module.exports.indexPageDataAsync = () => {
return co(function *() {
let banner = yield passportHelper.getLeftBannerAsync(BACK_LEFT_BANNER_CODE);
let countryList = passportHelper.getCountry();
return {
back: {
coverHref: banner.url,
coverImg: banner.img,
countryCode: 86,
countryName: "中国",
captchaUrl: helpers.urlFormat('/passport/images', {t: moment().valueOf()}),
countryList: countryList
}
}
})();
};
module.exports.getVerifyCodeByMobilePageDataAsync = () => {
};
module.exports.getSendEmailPageDataAsync = () => {
return {
sendEmail: {
coverHref: '',
coverImg: '',
counttrys: '',
email: ''
}
};
};
module.exports.verifyCodyByMobileAsync = (area, mobile, mobileCode) => {
const ERR = {
code: 400,
message: '验证码错误!',
data: helpers.urlFormat('/passport/back/index.html')
};
};
module.exports.authRequest = (token) => {
};
... ...
/**
* Created by TaoHuang on 2016/6/21.
*/
'use strict';
const library = '../../../library';
var ServiceAPI = require(library + "/api").ServiceAPI;
const sign = require(`${library}/sign`);
const _ = require('lodash');
var serviceAPI = new ServiceAPI();
module.exports.getResourceAsync = (resourceCode) => {
return serviceAPI.get('/operations/api/v5/resource/get', sign.apiSign({
content_code: resourceCode
}));
};
... ...
/**
* Created by TaoHuang on 2016/6/21.
*/
const api = require('./index-api');
module.exports.getResourceAsync = (resourceCode) => {
return api.getResourceAsync(resourceCode)
.then(result => {
if (result.code === 200) {
return result.data;
} else {
return {};
}
}).catch(() => {
return {};
});
};
... ...
/**
* Created by TaoHuang on 2016/6/20.
*/
'use strict';
const Cache = require(library + '/cache');
const helpers = require(library + '/helpers');
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const indexService = require('./index-service');
const KEY_WEB_LOGIN_LEFT_BANNER = 'key_web_login_left_banner'; // 登录页左侧的广告图
module.exports.getLeftBannerAsync = (resourceCode) => {
const DEFAULT_VALUE = {
img: 'http://img12.static.yhbimg.com/' +
'yhb-img01/2015/12/01/07/020a0b6e7ff908d0c2bc4045b4fef42b9f.png?imageView/2/w/252/h/190',
url: ''
};
return co(function * () {
let key = KEY_WEB_LOGIN_LEFT_BANNER + "_" + resourceCode;
let value = yield Cache.get(key);
if (!_.isEmpty(value)) {
return value;
}
let resource = yield indexService.getResourceAsync(resourceCode);
if (_.isEmpty(resource)) {
return DEFAULT_VALUE;
}
// 有点问题 // passport model 58
value.img = helpers.image(resource.data[0].data.src, 252, 190);
value.url = resource.data[0].data.url;
Cache.set(key, value).then(()=> console.log('cache value ok')); // async
return value;
})();
};
module.exports.getCountry = () => {
return [
{
areaCode: '+61',
selected: false,
name: '澳大利亚'
},
{
areaCode: '+82',
selected: false,
name: '韩国'
},
{
areaCode: '+1',
selected: false,
name: '加拿大'
},
{
areaCode: '+60',
selected: false,
name: '马来西亚'
},
{
areaCode: '+1',
selected: false,
name: '美国'
},
{
areaCode: '+81',
selected: false,
name: '日本'
},
{
areaCode: '+65',
selected: false,
name: '新加坡'
},
{
areaCode: '+44',
selected: false,
name: '英国'
},
{
areaCode: '+86',
selected: true,
name: '中国'
},
{
areaCode: '+853',
selected: false,
name: '中国澳门'
},
{
areaCode: '+886',
selected: false,
name: '中国台湾'
},
{
areaCode: '+852',
selected: false,
name: '中国香港'
}
];
};
... ...
... ... @@ -3,8 +3,8 @@
*/
'use strict';
var API = require('../../../library/api').API;
const library = '../../../library';
var API = require('../../../library/api').API;
const sign = require(`${library}/sign`);
const _ = require('lodash');
... ...
... ... @@ -18,11 +18,26 @@ const router = express.Router(); // eslint-disable-line
router.get('/login/wechat', login.wechat.beforeLogin, login.wechat.login); // 登录
router.get('/login/wechat/callback', login.wechat.callback);
// 找回密码首页
router.get('/back/index.html'.back.indexPage);
router.post('/back/authcode', captcha.checkAPI, back.identifyUserByInputAPI);
// 发送短信
// 实时验证输入是否正确
router.post('/back/authcode', captcha.checkAPI, back.validateUserInputAPI, back.getUserInfoAPI);
router.post('/back/email', captcha.checkPage, back.sendCodeAPI);
// 邮件重置密码
router.get('/back/backcode.html', back.authRequest, back.checkEmailCodePage, back.resetPasswordByEmailPage);
// 邮件重置密码成功页面
router.get('/back/resetSuccess.html', back.checkSuccessStatusPage, back.resetPwdSuccessPage);
// 重新发送短信
router.post('/back/sendBackMobile', captcha.checkAPI, back.sendBackMobileAPI);
router.get('/back/verification.html', back.verifyCodeByMobilePage);
router.post('/back/backMobile', captcha.checkAPI, back.verifyCodyByMobileAPI);
// 手机更新密码页面
router.post('/back/update', back.authRequest, back.validationPwdPage, back.updatePwdPage);
module.exports = router;
... ...