setting.js 10.5 KB
/**
 * [个人中心]个人设置
 * @author: jiangmin
 * @date: 2016/07/13
 */

'use strict';
const mcHandler = require('../models/menu-crumb-handler');
const helpers = global.yoho.helpers;
const settingModel = require('../models/setting');
const accountModel = require('../models/account');
const passportHelper = require('../../passport/models/passport-helper');
const Promise = require('bluebird');
const co = Promise.coroutine;


const captchaUrl = helpers.urlFormat('/passport/images', {t: Date.now()});

// const captchaUrl = 'http://localhost:6003/passport/images?t=' + new Date().getTime();

// 根据type获取标题
const _getTitle = (type)=> {
    let typeName;
    let proName;
    let isModifyMobile = false;
    let isBindMobile = false;
    let isModifyEmail = false;
    let isBindEmail = false;
    let isShowPassword = false;

    switch (type) {
        case 'modifyPassword':
            typeName = '修改密码';
            proName = '输入新密码';
            isShowPassword = true;
            break;
        case 'modifyMobile':
            typeName = '修改手机号码';
            proName = '绑定手机';
            isModifyMobile = true;
            break;
        case 'bindMobile':
            typeName = '绑定手机号码';
            proName = '绑定手机';
            isBindMobile = true;
            break;
        case 'modifyEmail':
            typeName = '修改邮箱';
            proName = '绑定邮箱';
            isModifyEmail = true;
            break;
        case 'bindEmail':
            typeName = '绑定邮箱';
            proName = '绑定邮箱';
            isBindEmail = true;
            break;
        default:
            typeName = '';
            break;
    }
    return {
        typeName: typeName,
        proName: proName,
        isModifyMobile,
        isBindMobile,
        isModifyEmail,
        isBindEmail,
        isShowPassword
    };
};

/**
 * 个人设置页面加载
 */
const index = (req, res, next) => {
    let uid = req.user.uid;

    settingModel.getUserInfo(uid).then(result=> {
        result.info.gender ? result.genders[result.info.gender - 1].checked = true :
            result.genders[2].checked = true;
        result.info.head_ico = result.info.head_ico ? helpers.image(result.info.head_ico, 400, 300, 2) : '';
        result.info.mobile = result.info.mobile ?
        result.info.mobile.substring(0, 3) + '****' + result.info.mobile.substring(7, 11) : '';
        result.stepUrl = '/me/setting/step1';
        res.display('index', {
            module: 'me',
            page: 'setting',
            isMe: true,
            content: {
                nav: mcHandler.getMeCrumb('个人设置'),
                navigation: mcHandler.getSideMenu('个人设置'),
                banner: 'http://placehold.it/150x120',
                setting: true,
                title: '个人设置',
                userInfo: result
            }
        });
    }).catch(next);
};

/**
 * 编辑信息
 * @param req
 * @param res
 */
const editUserInfo = (req, res)=> {
    let uid = req.user.uid;
    let query = req.body;

    settingModel.editUserInfo(uid, query).then(result=> {
        res.send(result);
    });
};


/*
 *  step1
 * */
const bindMobile = (req, res, next) => {
    let uid = req.user.uid;
    let type = req.params.type;

    settingModel.getUserInfo(uid).then(result=> {
        if (result.info.verify_mobile !== '') {
            let info = result.info;

            info.ellipsisMobile = info.verify_mobile.substring(0, 3) + '****' + info.verify_mobile.substring(7, 11);
            info.checkCode = settingModel.cipheriv(info.uid + '.completeverify');

            res.display('index', {
                module: 'me',
                page: 'setting',
                isMe: true,
                content: {
                    region: passportHelper.getCountry(),
                    location: '+86',
                    nav: mcHandler.getMeCrumb('个人设置'),
                    navigation: mcHandler.getSideMenu('个人设置'),
                    banner: 'http://placehold.it/150x120',
                    validateStep: true,
                    title: _getTitle(type).typeName,
                    proTitle: _getTitle(type).proName,
                    captchaUrl: captchaUrl,
                    isShowMobile: true,
                    data: info
                }
            });
        }
        next();
    });
};

const bindEmail = (req, res, next) => {
    let uid = req.user.uid;
    let type = req.params.type;

    settingModel.getUserInfo(uid).then(result=> {
        if (result.info.verify_email !== '') {
            let info = result.info;

            info.ellipsisEmail = info.verify_email.substring(0, 3) + '****' + info.verify_email.substring(7, 11);
            info.checkCode = settingModel.cipheriv(info.uid + '.completeverify');
            res.display('index', {
                module: 'me',
                page: 'setting',
                isMe: true,
                content: {
                    nav: mcHandler.getMeCrumb('个人设置'),
                    navigation: mcHandler.getSideMenu('个人设置'),
                    banner: 'http://placehold.it/150x120',
                    validateStep: true,
                    title: _getTitle(type).typeName,
                    proTitle: _getTitle(type).proName,
                    captchaUrl: captchaUrl,
                    isShowEmail: true,
                    data: info
                }
            });
        }
        next();
    });
};

const modifyPassword = (req, res) => {
    let type = req.params.type;
    let checkCode = settingModel.cipheriv(req.user.uid + '.completeverify');

    res.display('index', {
        module: 'me',
        page: 'setting',
        isMe: true,
        content: {
            nav: mcHandler.getMeCrumb('个人设置'),
            navigation: mcHandler.getSideMenu('个人设置'),
            banner: 'http://placehold.it/150x120',
            validateStep: true,
            title: _getTitle(type).typeName,
            proTitle: _getTitle(type).proName,
            captchaUrl: captchaUrl,
            isShowPassword: true,
            checkCode: checkCode
        }
    });
};

/*
 * step2 渲染页面
 * */
const edit = (req, res)=> {
    let type = req.params.type;
    let code = settingModel.decipheriv(req.query.checkCode.split(' ').join('+'));
    let codes = code.split('.');

    if (parseInt(codes[0], 10) === req.user.uid && codes[1] === 'completeverify') {
        let result = {
            title: _getTitle(type).typeName,
            proTitle: _getTitle(type).proName,
            isShowMobile: _getTitle(type).isModifyMobile || _getTitle(type).isBindMobile,
            isShowEmail: _getTitle(type).isModifyEmail || _getTitle(type).isBindEmail,
            isShowPassword: _getTitle(type).isShowPassword
        };
        let checkCode = settingModel.cipheriv(req.user.uid + '.completeoperate');

        res.display('index', {
            module: 'me',
            page: 'setting',
            isMe: true,
            content: Object.assign({
                region: passportHelper.getCountry(),
                location: '+86',
                nav: mcHandler.getMeCrumb('个人设置'),
                navigation: mcHandler.getSideMenu('个人设置'),
                banner: 'http://placehold.it/150x120',
                operateStep: true,
                stepUrl: '/me/setting/step3/' + type,
                captchaUrl: captchaUrl,
                checkCode: checkCode
            }, result)
        });
    }


};

/*
 * step3
 * */
const success = (req, res)=> {
    let type = req.params.type;
    let code = settingModel.decipheriv(req.query.checkCode.split(' ').join('+'));
    let codes = code.split('.');

    if (parseInt(codes[0], 10) === req.user.uid && codes[1] === 'completeoperate') {
        let result = {
            title: _getTitle(type).typeName,
            proTitle: _getTitle(type).proName,
            isModifyMobile: _getTitle(type).isModifyMobile,
            isBindMobile: _getTitle(type).isBindMobile,
            isModifyEmail: _getTitle(type).isModifyEmail,
            isBindEmail: _getTitle(type).isBindEmail,
            isShowPassword: _getTitle(type).isShowPassword
        };

        res.display('index', {
            module: 'me',
            page: 'setting',
            isMe: true,
            content: Object.assign({
                region: passportHelper.getCountry(),
                location: '+86',
                nav: mcHandler.getMeCrumb('个人设置'),
                navigation: mcHandler.getSideMenu('个人设置'),
                banner: 'http://placehold.it/150x120',
                successStep: true,
                captchaUrl: captchaUrl
            }, result)
        });
    }

};

/*
 * post1
 * */
const validate1 = (req, res)=> {
    co(function *() {
        let type = req.params.type;
        let uid = req.user.uid;
        let body = req.body;

        if (type === 'password') {
            let a = yield accountModel.verifyPwd(uid, body.password);

            res.send(a);
        } else if (type === 'mobile') {
            let a = yield accountModel.checkVerifyMsg(body.code, body.mobile, body.area);

            res.send(a);
        } else if (type === 'email') {
            let a = yield accountModel.sendVerifyEmail(uid, body.email);

            res.send(a);
        }
    })();
};

/*
 * post2
 * */
const validate2 = (req, res)=> {
    co(function *() {
        let type = req.params.type;
        let uid = req.user.uid;
        let body = req.body;

        if (type === 'password') {
            let a = yield accountModel.changePwd(uid, body.password);

            res.send(a);
        } else if (type === 'mobile') {
            let a = yield accountModel.checkVerifyMsg(body.code, body.mobile, body.area);
            let b = yield accountModel.modifyVerifyMobile(uid, body.area, body.mobile);
            let msg = [];

            if (a.code === 200 && b.code === 200) {
                res.send({
                    code: 200,
                    data: {}
                });
            } else {
                if (a.code !== 200) {
                    msg.push('<p>图片验证:' + a.message + '</p>');
                }
                if (b.code !== 200) {
                    msg.push('<p>修改号码:' + a.message + '</p>');
                }
                res.send({
                    code: 500,
                    message: msg.join('')
                });
            }
        }
    })();
};

module.exports = {
    index,
    editUserInfo,
    bindMobile,
    bindEmail,
    modifyPassword,
    edit,
    success,
    validate1,
    validate2
};