reset.js 1.63 KB
/*
 * @Author: Targaryen
 * @Date: 2017-04-13 10:21:07
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-18 09:55:57
 */

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

'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.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.uid;

    if (!uid || !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);
};

module.exports = {
    passwordResetPage,
    passwordReset
};