back-new.js 2.95 KB
const _ = require('lodash');
const co = Promise.coroutine;
const helpers = global.yoho.helpers;
const backService = require('../models/back-service');

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

class BackNew {
    /**
     * 通过手机找回密码
     */
    backByMobile(req, res, next) {
        _.set(req.session, 'backupCaptch.verifyResult', false);

        if (req.session.captchaValidCount == null) { // eslint-disable-line
            req.session.captchaValidCount = 5;
        }

        co(function* () {
            let countrysResult = yield backService.getAreaDataAsync(); // 地区信息列表
            let countrys = _.get(countrysResult, 'data', []);

            res.render('back/mobile-new', {
                width750: true,
                localCss: true,
                module: 'passport',
                page: 'back-mobile-new',
                countrys: countrys,
            });
        })().catch(next);
    }

    /**
     * 提交表单数据处理,设置新的密码
     */
    setNewPasswordByMobileAPI(req, res, next) {
        let phoneNum = req.body.phoneNum || '';
        let code = req.body.code || '';
        let areaCode = req.body.areaCode || '86';
        let newPwd = req.body.password;

        co(function* () {
            let valiResult = yield backService.validateMobileCodeAsync(phoneNum, code, areaCode);

            if (valiResult.code === 200) {
                let token = _.get(valiResult, 'data.token', '');
                let setPwdResult = yield backService.modifyPasswordByMobileAsyncAes(phoneNum, token, newPwd, areaCode);

                if (setPwdResult.code === 200) {
                    res.json({
                        code: 200,
                        data: SIGN_IN
                    });
                } else {
                    res.json({
                        code: 400,
                        message: '修改密码失败'
                    });
                }
            } else {
                return res.json({
                    code: 400,
                    message: '验证码失败'
                });
            }
        })().catch(next);
    }

    /**
     * 通过邮箱找回密码
     */
    backByEmail(req, res) {
        res.render('back/email-new', {
            width750: true,
            localCss: true,
            module: 'passport',
            page: 'back-email-new'
        });
    }

    /**
     * 通过邮箱找回密码成功页
     */
    backByEmailSuccess(req, res, next) {
        let email = req.query.email || '';

        if (!helpers.verifyEmail(email)) {
            return next();
        }
        res.render('back/email-success-new', {
            width750: true,
            localCss: true,
            module: 'passport',
            page: 'back-email-success-new',
            doneUrl: helpers.urlFormat('/signin.html'),
            resendUrl: helpers.urlFormat('/passport/back/resendemail', { email: email })
        });
    }
}

module.exports = BackNew;