reset.js 2.37 KB
/*
 * @Author: Targaryen
 * @Date: 2017-04-13 10:21:07
 * @Last Modified by: Targaryen
 */

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

'use strict';

const _ = require('lodash');
const config = global.yoho.config;
const utils = require(global.utils);
const ResetModel = 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;

    if (!uid && passwordWeakObj && passwordWeakObj.uid && passwordWeakObj.session_key) {
        uid = {
            toString: () => {
                return _.parseInt(passwordWeakObj.uid);
            },
            sessionKey: passwordWeakObj.session_key
        };
    }

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

    req.session.passwordWeak = null;

    req.ctx(ResetModel).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
};