reset.js 2.14 KB
/*
 * @Author: Targaryen
 * @Date: 2017-04-13 10:21:07
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-24 10:52:08
 */

/* ********************
 * 重置密码
 **********************/

'use strict';

const config = global.yoho.config;
const utils = require(global.utils);
const reset = require('../models/reset');

const passwordResetPage = (req, res) => {
    let passwordWeakObj = req.session.passwordWeak;
    let refer = req.cookies.refer;

    if (refer) {
        refer = decodeURI(req.cookies.refer);
    } else {
        refer = `${config.siteUrl}/home`;
    }

    if (/sign|login/.test(refer)) {
        refer = `${config.siteUrl}/home`;
    }

    refer = utils.refererLimit(refer);

    res.render('reset/password', {
        width750: true,
        module: 'passport',
        page: 'reset',
        pageStyle: 'passport-body',
        title: '重置密码',
        canSkip: (passwordWeakObj && passwordWeakObj.canSkip === 'Y') ? refer : false
    });
};

/**
 * 重置密码
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
const passwordReset = (req, res, next) => {
    let passwordWeakObj = req.session.passwordWeak;
    let uid = (req.user && req.user.uid) || (passwordWeakObj && passwordWeakObj.uid);

    if (!uid || !passwordWeakObj || !passwordWeakObj.token) {
        return res.json({
            code: 400,
            massage: '非法请求'
        });
    }

    req.session.passwordWeak = null;

    reset.resetPassword({
        uid: uid,
        oldPwd: req.body.oldPwd,
        newPwd: req.body.newPwd,
        token: passwordWeakObj.token
    }).then(result => {
        res.json(result);
    }).catch(next);
};


/**
 * 重置密码成功
 * @param {*} req
 * @param {*} res
 */
const passwordResetOkPage = (req, res) => {
    res.clearCookie('_SPK');
    res.cookie('_WX_PASS_LOGIN', true, {
        domain: 'm.yohobuy.com'
    });
    res.render('reset/reset-success', {
        width750: true,
        module: 'passport',
        page: 'reset-success',
        pageStyle: 'passport-body',
        title: '重置密码成功',
    });
};

module.exports = {
    passwordResetPage,
    passwordReset,
    passwordResetOkPage
};