back.js 9.89 KB
/**
 * 找回密码
 * Created by Tao.Huang on 2016/6/12.
 */

'use strict';

const helpers        = global.yoho.helpers;
const service        = require('../models/back-service');
const passportHelper = require('../models/passport-helper');
const moment         = require('moment');

// 本地地址 localhost

/**
 * 找回密码主页面
 */
const index = (req, res, next) => {
    service.indexPageDataAsync()
        .then(result => {
            res.render('back/index', Object.assign({
                module: 'passport',
                page  : 'back-index',
                title : '找回密码'
            }, result));
        })
        .catch(next);
};

/**
 * 校验用户输入信息,是否是已经注册的用户
 */
const validateInputAPI = (req, res, next) => {
    let userInput = req.body.phoneNum || '';
    let areaCode  = (req.body.area || '86').replace('+', '');

    service.validateEmailOrMobileAsync(userInput, areaCode)
        .then(result => {
            req.inputInfo = result;
            console.log(result);
            next();
        })
        .catch(err => {
            res.json({
                code   : 400,
                message: err
            });
        });
};

/**
 * 校验用户输入信息,是否是已经注册的用户
 */
const validateUserPage = (req, res, next) => {
    let userInput = req.body.phoneNum || '';
    let areaCode  = (req.body.area || '86').replace('+', '');

    service.validateEmailOrMobileAsync(userInput, areaCode)
        .then(result => {
            req.inputInfo = result;
            next();
        })
        .catch(()=> {
            res.redirect(helpers.urlFormat('/passport/back/index.html'));
        });
};

const getUserInfoAPI = (req, res, next) => {
    let inputInfo = req.inputInfo;

    service.findUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
        .then(result => {
            res.json(result);
        })
        .catch(next);
};

const sendCodePage = (req, res, next) => {
    let inputInfo = req.inputInfo;

    service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
        .then(result => {
            if (_.isEmpty(result)) {
                res.redirect(helpers.urlFormat('/passport/back/index.html'));
            } else {
                next();
            }

        })
        .catch(() => {
            res.redirect(helpers.urlFormat('/passport/back/index.html'));
        });
};

const saveInSession = (req, res) => {
    switch (req.inputInfo.type) {
        case 'email':
        {
            req.session.email = req.inputInfo.phone;
            res.redirect(helpers.urlFormat('/passport/back/sendEmail.html'));
            break;
        }
        case 'mobile':
        {
            req.session.mobile = req.inputInfo.phone;
            req.session.area   = req.inputInfo.area;
            res.redirect(helpers.urlFormat('/passport/back/verification.html'));
            break;
        }
        default:
        {
            res.redirect(helpers.urlFormat('/passport/back/index.html'));
        }
    }
};

const sendBackMobileAPI = (req, res, next) => {
    service.sendCodeToMobileAsync(area, mobile)
        .then(result => {
            res.json(result);
        })
        .catch(next);
};

const validateMobileAPI = (req, res, next) => {
    let mobile = req.body.mobile || '';

    let area = req.body.area || '86';

    const ERR = {code: 400, message: '验证失败'};

    if (!helpers.verifyAreaMobile(helpers.makeAreaMobile(area, mobile))) {
        res.json(ERR);
    } else {
        next();
    }
};

const validateEmailInSession = (req, res, next) => {
    let email = req.session.email || '';

    if (!email) {
        res.redirect(helpers.urlFormat('/passport/back/index.html'));
    }

    const mapperEmailISP = {
        'yoho.cn': 'http://smail.yoho.cn'
    };

    let isp = email.split('@')[1];

    req.body.emailUrl = mapperEmailISP[isp] || `http://mail.${isp}`;
    next();
};

const sendEmailPage = (req, res, next) => {
    passportHelper.getLeftBannerAsync()
        .then(result => {
            res.render('back/send-email', Object.assign({
                module: 'passport',
                page  : 'back-send-email-ok',
                title : "邮件发送成功"
            }, {
                sendEmail: {
                    coverHref: result.url,
                    coverImg : result.img,
                    email    : req.body.emailUrl
                }
            }));
        })
        .catch(next);

};

const validateCodeByEmailPage = (req, res, next) => {
    let code = req.query.code || '';

    if (!_.isEmpty(req.mobileAuth)) {
        next();
        return;
    }

    service.checkEmailCodeAsync(code)
        .then(result => {
            if (result) {
                next();
            } else {
                res.redirect(helpers.urlFormat('/passport/back/index.html'));
            }
        })
        .catch(next);
};

const resetPasswordPage = (req, res, next) => {
    let code = req.query.code || '';

    passportHelper.getLeftBannerAsync()
        .then(result => {
            res.render('back/reset-pwd', Object.assign({
                module: 'passport',
                page  : 'back-reset-pwd',
                title : '重置密码'
            }, {
                resetPwd: Object.assign({
                    coverHref: result.url,
                    coverImg : result.img,
                    code     : code
                }, req.mobileAuth)
            }))
        })
        .catch(next);
};

/**
 * 手机验证页面
 */
const verifyCodeByMobilePage = (req, res, next) => {
    passportHelper.getLeftBannerAsync()
        .then(result => {
            res.render('back/verification', Object.assign({
                module: 'passport',
                page  : 'back-verify-mobile-code',
                title : '手机验证'
            }, {
                verification: {
                    coverHref : result.url,
                    coverImg  : result.img,
                    mobile    : req.body.mobile,
                    area      : req.body.area,
                    verifyCode: req.body.verifyCode
                }
            }));
        })
        .catch(next);
};

const checkSuccessStatusPage = (req, res, next) => {
    let successType = req.session.successType || '';

    if (successType) {
        delete req.session.successType;
        next();
    } else {
        res.redirect(helpers.urlFormat('/passport/back/index.html'));
    }
};

const resetPwdSuccessPage = (req, res, next) => {
    passportHelper.getLeftBannerAsync()
        .then(result => {
            res.render('back/reset-success', Object.assign({
                module: 'passport',
                page  : 'back-index',
                title : '重置密码成功'
            }, {
                resetSuccess: {
                    coverHref: result.url,
                    coverImg : result.img
                }
            }))
        })
        .catch(next);
};

const verifyCodeByMobileAPI = (req, res) => {
    let mobile     = req.param('mobile', '');
    let area       = req.param('area', '86');
    let mobileCode = req.param('code', '');
    const session  = req.session;

    const ERR = {
        code   : 400,
        message: '验证码错误!',
        data   : helpers.urlFormat('/passport/back/index.html')
    };

    if (!code || mobile !== session.mobile || area !== session.area) {
        res.json(ERR);
        return;
    }

    service.verifyCodyByMobileAsync(area, mobile, mobileCode)
        .then(result => {
            res.json(result);
        })
        .catch(()=> {
            res.json(ERR);
        });
};

const validateExistCodePage = (req, res, next) => {
    let code = req.param('code', '');

    if (!code) {
        return res.redirect(helpers.urlFormat('/passport/back/index.html'));
    }

    next();
};

const validateCodeByMobilePage = (req, res, next) => {
    let code   = req.param('code', '');
    let mobile = req.param('mobile', '');

    if (!mobile) {
        req.mobileAuth = {};
        next();
        return;
    }

    let data = {
        mobile     : mobile,
        area       : req.param('area', ''),
        token      : req.param('token', ''),
        create_time: req.param('create_time', 0)
    };

    code           = new Buffer(code, 'base64').toString();
    req.mobileAuth = service.authRequest(data, code);
    next();
};

const validatePwdPage = (req, res, next) => {
    let pwd = req.body.pwd || '';

    if (helpers.isPassword(pwd)) {
        next();
    } else {
        res.redirect(helpers.urlFormat('/passport/back/index.html'));
    }
};

const updatePwdAPI = (req, res, next) => {
    let code        = req.body.code || '';
    let mobileAuth  = req.mobileAuth;
    let newPassword = req.body.pwd;

    service.updatePwdAsync(code, mobileAuth, newPassword)
        .then(result => {
            if (result.status) {
                req.session.successType = result.type;
                res.redirect(helpers.urlFormat('/passport/back/resetSuccess.html'));
            } else {
                res.redirect(helpers.urlFormat('/passport/back/index.html'));
            }
        })
        .catch(next);
};

const validateMobileInSession = (req, res, next) => {
    req.body.mobile     = req.session.mobile || "";
    req.body.verifyCode = req.session.verifyCode || "";
    req.body.area       = req.session.area || "";

    if (req.body.mobile && req.body.verifyCode) {
        next()
    } else {
        res.redirect(helpers.urlFormat('/passport/back/index.html'));
    }
};

module.exports = {
    index,
    validateInputAPI,
    validateUserPage,
    getUserInfoAPI,
    sendCodePage,
    saveInSession,
    sendBackMobileAPI,
    validateMobileAPI,
    validateEmailInSession,
    sendEmailPage,
    validateCodeByEmailPage,
    resetPasswordPage,
    verifyCodeByMobilePage,
    checkSuccessStatusPage,
    resetPwdSuccessPage,
    verifyCodeByMobileAPI,
    validateExistCodePage,
    validateCodeByMobilePage,
    validatePwdPage,
    updatePwdAPI,
    validateMobileInSession
};