Authored by htoooth

add api

@@ -9,6 +9,7 @@ const library = '../../../library'; @@ -9,6 +9,7 @@ const library = '../../../library';
9 const helpers = require(`${library}/helpers`); 9 const helpers = require(`${library}/helpers`);
10 10
11 const service = require('../models/back-service'); 11 const service = require('../models/back-service');
  12 +const sessionService = require('../models/session-service');
12 13
13 // 本地地址 localhost 14 // 本地地址 localhost
14 helpers.urlFormat = helpers.fakeUrlFormat; 15 helpers.urlFormat = helpers.fakeUrlFormat;
@@ -32,17 +33,33 @@ module.exports.indexPage = (req, res) => { @@ -32,17 +33,33 @@ module.exports.indexPage = (req, res) => {
32 /** 33 /**
33 * 校验用户输入信息,是否是已经注册的用户 34 * 校验用户输入信息,是否是已经注册的用户
34 */ 35 */
35 -module.exports.identifyUserByInputAPI = (req, res) => { 36 +module.exports.validateUserInputAPI = (req, res, next) => {
36 let userInput = req.body.phoneNum || ''; 37 let userInput = req.body.phoneNum || '';
37 38
38 let areaCode = req.body.area || '86'; 39 let areaCode = req.body.area || '86';
39 40
40 - service.identifyUserByEmailOrMobileAsync(userInput, areaCode) 41 + service.validateEmailOrMobileAsync(userInput, areaCode)
41 .then(result => { 42 .then(result => {
42 - res.json(result); 43 + req.inputInfo = result;
  44 + next();
  45 + })
  46 + .catch(err => {
  47 + res.json({
  48 + code: 400,
  49 + message: err
  50 + });
43 }); 51 });
44 }; 52 };
45 53
  54 +module.exports.getUserInfoAPI = (req, res, next) => {
  55 + let inputInfo = req.inputInfo;
  56 +
  57 + service.findUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
  58 + .then(result => {
  59 + res.json(result);
  60 + }).catch(next);
  61 +};
  62 +
46 module.exports.sendCodeAPI = (req, res, next) => { 63 module.exports.sendCodeAPI = (req, res, next) => {
47 let userInput = req.body.phoneNum || ''; 64 let userInput = req.body.phoneNum || '';
48 65
@@ -68,6 +85,60 @@ module.exports.sendCodeAPI = (req, res, next) => { @@ -68,6 +85,60 @@ module.exports.sendCodeAPI = (req, res, next) => {
68 }).catch(next); 85 }).catch(next);
69 }; 86 };
70 87
  88 +module.exports.sendBackMobileAPI = (req, res, next) => {
  89 + let mobile = req.param('mobile', '');
  90 +
  91 + let area = req.param('area', '86');
  92 +
  93 + service.sendCodeToMobileAsync(area, mobile)
  94 + .then(result => {
  95 + res.json(result);
  96 + }).catch(next);
  97 +};
  98 +
  99 +module.exports.sendEmailPage = (req, res, next) => {
  100 + service.getSendEmailPageDataAsync()
  101 + .then(result => {
  102 + res.render('back/send-email', Object.assign({
  103 + module: 'back',
  104 + page: 'send-email',
  105 + title: "邮件发送成功"
  106 + }, result
  107 + ));
  108 + }).catch(next);
  109 +
  110 +};
  111 +
  112 +module.exports.checkEmailCodePage = (req, res, next) => {
  113 + let code = req.param('code', '');
  114 +
  115 + service.checkEmailCodeAsync(code)
  116 + .then(result => {
  117 + if (result) {
  118 + next();
  119 + } else {
  120 + res.redirect('/passport/back/index.html');
  121 + }
  122 + }).catch(next);
  123 +};
  124 +
  125 +module.exports.resetPasswordByEmailPage = (req, res, next) => {
  126 + let code = res.param('code', '');
  127 +
  128 + service.getLeftBannerAsync()
  129 + .then(result => {
  130 + res.render('back/email-reset-pwd', Object.assign({
  131 + module: 'back',
  132 + page: 'email-reset-pwd'
  133 + }, {
  134 + code: code
  135 + }, result))
  136 + }).catch(next);
  137 +};
  138 +
  139 +/**
  140 + * 手机验证页面
  141 + */
71 module.exports.verifyCodeByMobilePage = (req, res, next) => { 142 module.exports.verifyCodeByMobilePage = (req, res, next) => {
72 143
73 service.getVerifyCodeByMobilePageDataAsync() 144 service.getVerifyCodeByMobilePageDataAsync()
@@ -87,3 +158,74 @@ module.exports.verifyCodeByMobilePage = (req, res, next) => { @@ -87,3 +158,74 @@ module.exports.verifyCodeByMobilePage = (req, res, next) => {
87 })); 158 }));
88 }).catch(next); 159 }).catch(next);
89 }; 160 };
  161 +
  162 +module.exports.verifyCodeByMobileAPI = (req, res, next) => {
  163 +
  164 +};
  165 +
  166 +module.exports.checkSuccessStatusPage = (req, res, next) => {
  167 + let successType = sessionService.get('successType', '');
  168 +
  169 + if (successType) {
  170 + sessionService.set('successType', '');
  171 + next();
  172 + } else {
  173 + res.redirect('/passport/back/index.html');
  174 + }
  175 +};
  176 +
  177 +module.exports.resetPwdSuccessPage = (req, res, next) => {
  178 + service.getLeftBannerAsync()
  179 + .then(result => {
  180 + res.render('back/email-reset-success', Object.assign({
  181 + module: 'back',
  182 + page: 'email-reset-success',
  183 + title: '重置密码成功'
  184 + }, result))
  185 + }).catch(next);
  186 +};
  187 +
  188 +module.exports.verifyCodyByMobileAPI = (req, res, next) => {
  189 +
  190 + let mobile = req.param('mobile', '');
  191 +
  192 + let area = req.param('aera', '86');
  193 +
  194 + let mobileCode = req.param('code', '');
  195 +
  196 + service.verifyCodyByMobileAsync(area, mobile, mobileCode)
  197 + .then(result => {
  198 + res.json(result);
  199 + }).catch(next);
  200 +
  201 +};
  202 +
  203 +module.exports.tokenCheckPage = (req, res, next) => {
  204 + let token = req.param('code', '');
  205 +
  206 + service.authRequest(token).then(result => {
  207 + if (result) {
  208 + req.authInfo = result;
  209 + next();
  210 + } else {
  211 + res.redirect('/passport/back/index');
  212 + }
  213 + })
  214 +};
  215 +
  216 +module.exports.validationPwdPage = (req, res, next) => {
  217 + let pwd = req.param('pwd', '');
  218 +
  219 + if (helpers.verifyPassword(pwd)) {
  220 + res.redirect('/passport/back/index.html');
  221 + } else {
  222 + next();
  223 + }
  224 +};
  225 +
  226 +module.exports.updatePwdPage = (req, res, next) => {
  227 +
  228 + let code = req.param('code', '');
  229 +
  230 + let auth = req.authInfo;
  231 +};
@@ -51,6 +51,23 @@ class Auth { @@ -51,6 +51,23 @@ class Auth {
51 }); // esline-disable-line 51 }); // esline-disable-line
52 }).catch(console.log); 52 }).catch(console.log);
53 } 53 }
  54 +
  55 + //
  56 +
  57 + // TODO:
  58 + static _auth(str, key, expiry, operation) {
  59 +
  60 + }
  61 +
  62 + // TODO:
  63 + static encode(str){
  64 + return '';
  65 + }
  66 +
  67 + // TODO:
  68 + static decode(str){
  69 + return ''
  70 + }
54 } 71 }
55 72
56 module.exports = Auth; 73 module.exports = Auth;
1 -/**  
2 - * Created by TaoHuang on 2016/6/20.  
3 - */  
4 -  
5 -  
6 -'use strict';  
7 -  
8 -  
9 -// TODO:  
10 -module.exports.getLeftBanner = (resourceCode) => {  
11 -  
12 -};  
@@ -9,43 +9,60 @@ const api = require('./back-api'); @@ -9,43 +9,60 @@ const api = require('./back-api');
9 const Promise = require('bluebird'); 9 const Promise = require('bluebird');
10 const co = Promise.coroutine; 10 const co = Promise.coroutine;
11 const _ = require('lodash'); 11 const _ = require('lodash');
  12 +const moment = require('moment');
12 13
13 const userService = require('./user-service'); 14 const userService = require('./user-service');
14 const sessionService = require('./session-service'); 15 const sessionService = require('./session-service');
15 -const api = require('./back-api'); 16 +const passportHelper = require('./passport-helper');
16 17
17 -module.exports.identifyUserByEmailOrMobileAsync = (userInput, areaCode) => {  
18 - return co(function * () { 18 +const BACK_LEFT_BANNER_CODE = '3bbaf502c447a2ddad60879042e286d8'; //找回密码左边的banner
  19 +
  20 +module.exports.validateEmailOrMobileAsync = (userInput, areaCode) => {
  21 + return new Promise(function (resolve, rejected) {
  22 + let result = {type: 'email', area: '', phone: ''};
  23 +
  24 + if (helpers.verifyEmail(userInput)) {
  25 +
  26 + result.type = 'email';
  27 + result.aera = '';
  28 + result.phone = userInput;
  29 +
  30 + resolve(result);
  31 + } else if (helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, userInput))) {
  32 + result.type = 'mobile';
  33 + result.area = areaCode;
  34 + result.phone = userInput;
  35 +
  36 + resolve(result);
  37 + } else {
  38 + rejected('输入信息出错!');
  39 + }
  40 + });
  41 +
  42 +};
19 43
  44 +module.exports.findUserAsync = (type, phone, area) => {
  45 + return co(function * () {
20 const MESSAGE = { 46 const MESSAGE = {
21 - errPhone: '您输入的手机号码尚未注册!',  
22 - errEmail: '您输入的邮件账户尚未注册!',  
23 - errUnknown: '服务器错误', 47 + mobile: '您输入的手机号码尚未注册!',
  48 + email: '您输入的邮件账户尚未注册!',
24 ok: '验证成功' 49 ok: '验证成功'
25 }; 50 };
26 51
27 - const OK = {code: 200, message: MESSAGE.ok};  
28 -  
29 - let status = {code: 400, message: MESSAGE.errUnknown}; 52 + const findBy = {
  53 + email: userService.findByEmailAsync,
  54 + mobile: userService.findByMobileAsync
  55 + };
30 56
31 - if (helpers.verifyEmail(userInput)) {  
32 - const user = yield userService.findByEmailAsync(userInput); 57 + const OK = {code: 200, message: MESSAGE.ok};
33 58
34 - if (_.isEmpty(user)) {  
35 - status.code = 402;  
36 - status.message = MESSAGE.errEmail;  
37 - return status;  
38 - }  
39 - } else if (helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, userInput))) {  
40 - const user = yield userService.findByMobileAsync(userInput, areaCode); 59 + const user = yield findBy[type](phone, area);
41 60
42 if (_.isEmpty(user)) { 61 if (_.isEmpty(user)) {
43 - status.code = 402;  
44 - status.message = MESSAGE.errPhone;  
45 - return status;  
46 - }  
47 - } else {  
48 - return status; 62 + return {
  63 + code: 402,
  64 + message: MESSAGE[type]
  65 + };
49 } 66 }
50 67
51 return OK; 68 return OK;
@@ -104,7 +121,6 @@ module.exports.sendCodeToUserAsync = (userInput, areaCode) => { @@ -104,7 +121,6 @@ module.exports.sendCodeToUserAsync = (userInput, areaCode) => {
104 })(); 121 })();
105 }; 122 };
106 123
107 -  
108 /** 124 /**
109 * 发送找回手机号短信 125 * 发送找回手机号短信
110 */ 126 */
@@ -125,11 +141,50 @@ module.exports.sendCodeToEmailAsync = (email) => { @@ -125,11 +141,50 @@ module.exports.sendCodeToEmailAsync = (email) => {
125 /** 141 /**
126 * 获得首页的数据 142 * 获得首页的数据
127 */ 143 */
128 -// TODO:  
129 module.exports.indexPageDataAsync = () => { 144 module.exports.indexPageDataAsync = () => {
130 - 145 + return co(function *() {
  146 + let banner = yield passportHelper.getLeftBannerAsync(BACK_LEFT_BANNER_CODE);
  147 + let countryList = passportHelper.getCountry();
  148 +
  149 + return {
  150 + back: {
  151 + coverHref: banner.url,
  152 + coverImg: banner.img,
  153 + countryCode: 86,
  154 + countryName: "中国",
  155 + captchaUrl: helpers.urlFormat('/passport/images', {t: moment().valueOf()}),
  156 + countryList: countryList
  157 + }
  158 + }
  159 + })();
131 }; 160 };
132 161
133 module.exports.getVerifyCodeByMobilePageDataAsync = () => { 162 module.exports.getVerifyCodeByMobilePageDataAsync = () => {
134 163
135 }; 164 };
  165 +
  166 +module.exports.getSendEmailPageDataAsync = () => {
  167 + return {
  168 + sendEmail: {
  169 + coverHref: '',
  170 + coverImg: '',
  171 + counttrys: '',
  172 + email: ''
  173 + }
  174 + };
  175 +};
  176 +
  177 +module.exports.verifyCodyByMobileAsync = (area, mobile, mobileCode) => {
  178 + const ERR = {
  179 + code: 400,
  180 + message: '验证码错误!',
  181 + data: helpers.urlFormat('/passport/back/index.html')
  182 + };
  183 +
  184 +
  185 +};
  186 +
  187 +module.exports.authRequest = (token) => {
  188 +
  189 +};
  190 +
  1 +/**
  2 + * Created by TaoHuang on 2016/6/21.
  3 + */
  4 +'use strict';
  5 +
  6 +const library = '../../../library';
  7 +var ServiceAPI = require(library + "/api").ServiceAPI;
  8 +const sign = require(`${library}/sign`);
  9 +const _ = require('lodash');
  10 +
  11 +var serviceAPI = new ServiceAPI();
  12 +
  13 +module.exports.getResourceAsync = (resourceCode) => {
  14 + return serviceAPI.get('/operations/api/v5/resource/get', sign.apiSign({
  15 + content_code: resourceCode
  16 + }));
  17 +};
  1 +/**
  2 + * Created by TaoHuang on 2016/6/21.
  3 + */
  4 +
  5 +const api = require('./index-api');
  6 +
  7 +module.exports.getResourceAsync = (resourceCode) => {
  8 + return api.getResourceAsync(resourceCode)
  9 + .then(result => {
  10 + if (result.code === 200) {
  11 + return result.data;
  12 + } else {
  13 + return {};
  14 + }
  15 + }).catch(() => {
  16 + return {};
  17 + });
  18 +};
  1 +/**
  2 + * Created by TaoHuang on 2016/6/20.
  3 + */
  4 +
  5 +
  6 +'use strict';
  7 +
  8 +const Cache = require(library + '/cache');
  9 +const helpers = require(library + '/helpers');
  10 +const Promise = require('bluebird');
  11 +const co = Promise.coroutine;
  12 +const _ = require('lodash');
  13 +
  14 +const indexService = require('./index-service');
  15 +
  16 +const KEY_WEB_LOGIN_LEFT_BANNER = 'key_web_login_left_banner'; // 登录页左侧的广告图
  17 +
  18 +module.exports.getLeftBannerAsync = (resourceCode) => {
  19 + const DEFAULT_VALUE = {
  20 + img: 'http://img12.static.yhbimg.com/' +
  21 + 'yhb-img01/2015/12/01/07/020a0b6e7ff908d0c2bc4045b4fef42b9f.png?imageView/2/w/252/h/190',
  22 + url: ''
  23 + };
  24 +
  25 + return co(function * () {
  26 + let key = KEY_WEB_LOGIN_LEFT_BANNER + "_" + resourceCode;
  27 +
  28 + let value = yield Cache.get(key);
  29 +
  30 + if (!_.isEmpty(value)) {
  31 + return value;
  32 + }
  33 +
  34 + let resource = yield indexService.getResourceAsync(resourceCode);
  35 +
  36 + if (_.isEmpty(resource)) {
  37 + return DEFAULT_VALUE;
  38 + }
  39 +
  40 + // 有点问题 // passport model 58
  41 + value.img = helpers.image(resource.data[0].data.src, 252, 190);
  42 + value.url = resource.data[0].data.url;
  43 +
  44 + Cache.set(key, value).then(()=> console.log('cache value ok')); // async
  45 + return value;
  46 + })();
  47 +};
  48 +
  49 +module.exports.getCountry = () => {
  50 + return [
  51 + {
  52 + areaCode: '+61',
  53 + selected: false,
  54 + name: '澳大利亚'
  55 + },
  56 + {
  57 + areaCode: '+82',
  58 + selected: false,
  59 + name: '韩国'
  60 + },
  61 + {
  62 + areaCode: '+1',
  63 + selected: false,
  64 + name: '加拿大'
  65 + },
  66 + {
  67 + areaCode: '+60',
  68 + selected: false,
  69 + name: '马来西亚'
  70 + },
  71 + {
  72 + areaCode: '+1',
  73 + selected: false,
  74 + name: '美国'
  75 + },
  76 + {
  77 + areaCode: '+81',
  78 + selected: false,
  79 + name: '日本'
  80 + },
  81 + {
  82 + areaCode: '+65',
  83 + selected: false,
  84 + name: '新加坡'
  85 + },
  86 + {
  87 + areaCode: '+44',
  88 + selected: false,
  89 + name: '英国'
  90 + },
  91 + {
  92 + areaCode: '+86',
  93 + selected: true,
  94 + name: '中国'
  95 + },
  96 + {
  97 + areaCode: '+853',
  98 + selected: false,
  99 + name: '中国澳门'
  100 + },
  101 + {
  102 + areaCode: '+886',
  103 + selected: false,
  104 + name: '中国台湾'
  105 + },
  106 + {
  107 + areaCode: '+852',
  108 + selected: false,
  109 + name: '中国香港'
  110 + }
  111 + ];
  112 +};
@@ -3,8 +3,8 @@ @@ -3,8 +3,8 @@
3 */ 3 */
4 'use strict'; 4 'use strict';
5 5
6 -var API = require('../../../library/api').API;  
7 const library = '../../../library'; 6 const library = '../../../library';
  7 +var API = require('../../../library/api').API;
8 const sign = require(`${library}/sign`); 8 const sign = require(`${library}/sign`);
9 const _ = require('lodash'); 9 const _ = require('lodash');
10 10
@@ -18,11 +18,26 @@ const router = express.Router(); // eslint-disable-line @@ -18,11 +18,26 @@ const router = express.Router(); // eslint-disable-line
18 router.get('/login/wechat', login.wechat.beforeLogin, login.wechat.login); // 登录 18 router.get('/login/wechat', login.wechat.beforeLogin, login.wechat.login); // 登录
19 router.get('/login/wechat/callback', login.wechat.callback); 19 router.get('/login/wechat/callback', login.wechat.callback);
20 20
21 - 21 +// 找回密码首页
22 router.get('/back/index.html'.back.indexPage); 22 router.get('/back/index.html'.back.indexPage);
23 -router.post('/back/authcode', captcha.checkAPI, back.identifyUserByInputAPI);  
24 23
25 -// 发送短信 24 +// 实时验证输入是否正确
  25 +router.post('/back/authcode', captcha.checkAPI, back.validateUserInputAPI, back.getUserInfoAPI);
26 router.post('/back/email', captcha.checkPage, back.sendCodeAPI); 26 router.post('/back/email', captcha.checkPage, back.sendCodeAPI);
27 27
  28 +
  29 +// 邮件重置密码
  30 +router.get('/back/backcode.html', back.authRequest, back.checkEmailCodePage, back.resetPasswordByEmailPage);
  31 +
  32 +// 邮件重置密码成功页面
  33 +router.get('/back/resetSuccess.html', back.checkSuccessStatusPage, back.resetPwdSuccessPage);
  34 +
  35 +// 重新发送短信
  36 +router.post('/back/sendBackMobile', captcha.checkAPI, back.sendBackMobileAPI);
  37 +router.get('/back/verification.html', back.verifyCodeByMobilePage);
  38 +router.post('/back/backMobile', captcha.checkAPI, back.verifyCodyByMobileAPI);
  39 +
  40 +// 手机更新密码页面
  41 +router.post('/back/update', back.authRequest, back.validationPwdPage, back.updatePwdPage);
  42 +
28 module.exports = router; 43 module.exports = router;