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

'use strict';

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;

const SIGN_IN_URL = helpers.urlFormat('/signin.html');


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

/**
 * 校验用户输入信息,是否是已经注册的用户
 */
module.exports.validateUserInputAPI = (req, res, next) => {
    let userInput = req.body.phoneNum || '';

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

    service.validateEmailOrMobileAsync(userInput, areaCode)
        .then(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 || '';

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

    service.sendCodeToUserAsync(userInput, areaCode).then(result => {
        switch (result) {
            case 'mobile':
            {
                res.redirect('/passport/back/sendemail.html');
                break;
            }
            case 'email':
            {
                res.redirect('/passport/back/verification.html');
                break;
            }
            default:
            {
                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()
        .then(result => {
            res.render('back/verification', Object.assign({
                module: 'back',
                page: 'verification',
                title: '手机验证'
            }, {
                verification: {
                    coverHref: result.url,
                    coverImg: result.img,
                    mobile: result.mobile,
                    area: result.area,
                    verifyCode: result.verifyCode
                }
            }));
        }).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;
};