setting.js 12.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
/**
 * [个人中心]个人设置
 * @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;
var fs = require('fs');
var path = require('path');
const uuid = require('uuid');
const os = require('os');
const _ = require('lodash');
const regMobile = /(\d{3})\d{4}(\d{4})/;// 正则匹配替换手机号码中间4位
const regEmail = /(\d{3})\d{4}/;// 正则匹配替换邮箱中间4位

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

// 根据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;

    Promise.all([settingModel.getUserInfo(uid), mcHandler.getMeThumb()]).then(result => {
        const thumb = result[1];
        let user = result[0];

        user.info.gender ? user.genders[user.info.gender - 1].checked = true :
            user.genders[2].checked = true;
        user.info.head_ico = user.info.head_ico ? helpers.image(user.info.head_ico, 400, 300, 2) : '';
        user.info.mobile = user.info.mobile ? user.info.mobile.replace(regMobile, '$1****$2') : '';
        user.stepUrl = '/me/setting/step1';
        if (user.info.birthday) {
            let a = user.info.birthday.split('-');

            _.forEach(user.date.selectYear, function(x) {
                if (x.value === a[0]) {
                    x.isSelected = 'selected';
                }
            });
            _.forEach(user.date.selectMonth, function(x) {
                if (parseInt(x.value, 10) === parseInt(a[1], 10)) {
                    x.isSelected = 'selected';
                }
            });
            _.forEach(user.date.selectDay, function(x) {
                if (parseInt(x.value, 10) === parseInt(a[2], 10)) {
                    x.isSelected = 'selected';
                }
            });
        }

        res.display('index', {
            module: 'me',
            page: 'setting',
            isMe: true,
            content: {
                nav: mcHandler.getMeCrumb('个人设置'),
                navigation: mcHandler.getSideMenu('个人设置'),
                banner: thumb,
                setting: true,
                title: '个人设置',
                userInfo: user,
                _token: req.csrfToken()
            }
        });
    }).catch(next);
};

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

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


/*
 *  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 !== '') {
            mcHandler.getMeThumb().then((thumb) => {
                let info = result.info;

                info.ellipsisMobile = info.verify_mobile.replace(regMobile, '$1****$2');
                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: thumb,
                        validateStep: true,
                        title: _getTitle(type).typeName,
                        proTitle: _getTitle(type).proName,
                        captchaUrl: captchaUrl,
                        isShowMobile: true,
                        data: info
                    }
                });
            });
        }
        next();
    });
};

/**
 * step1 绑定的邮箱进行身份验证
 */
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.replace(regEmail, '$1****');
            info.checkCode = settingModel.cipheriv(info.uid + '.completeverify');

            mcHandler.getMeThumb().then((thumb) => {
                res.display('index', {
                    module: 'me',
                    page: 'setting',
                    isMe: true,
                    content: {
                        nav: mcHandler.getMeCrumb('个人设置'),
                        navigation: mcHandler.getSideMenu('个人设置'),
                        banner: thumb,
                        validateStep: true,
                        title: _getTitle(type).typeName,
                        proTitle: _getTitle(type).proName,
                        captchaUrl: captchaUrl,
                        isShowEmail: true,
                        data: info
                    }
                });
            });
        }
        next();
    });
};

/**
 *step1 登录密码进行身份验证
 */
const modifyPassword = (req, res) => {
    let type = req.params.type;
    let checkCode = settingModel.cipheriv(req.user.uid + '.completeverify');

    mcHandler.getMeThumb().then((thumb) => {
        res.display('index', {
            module: 'me',
            page: 'setting',
            isMe: true,
            content: {
                nav: mcHandler.getMeCrumb('个人设置'),
                navigation: mcHandler.getSideMenu('个人设置'),
                banner: thumb,
                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');

        mcHandler.getMeThumb().then((thumb) => {
            res.display('index', {
                module: 'me',
                page: 'setting',
                isMe: true,
                content: Object.assign({
                    region: passportHelper.getCountry(),
                    location: '+86',
                    nav: mcHandler.getMeCrumb('个人设置'),
                    navigation: mcHandler.getSideMenu('个人设置'),
                    banner: thumb,
                    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
        };

        mcHandler.getMeThumb().then((thumb) => {
            res.display('index', {
                module: 'me',
                page: 'setting',
                isMe: true,
                content: Object.assign({
                    region: passportHelper.getCountry(),
                    location: '+86',
                    nav: mcHandler.getMeCrumb('个人设置'),
                    navigation: mcHandler.getSideMenu('个人设置'),
                    banner: thumb,
                    successStep: true,
                    captchaUrl: captchaUrl
                }, result)
            });
        });
    }

};

/*
 * post1 第一步身份验证的post请求
 * */
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 第二步具体操作的post请求
 * */
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('')
                });
            }
        }
    })();
};


/**
 * 文件处理中间件
 * @param req
 * @param res
 * @param next
 */
function getfilePath(req, res, next) {
    const fid = uuid.v4();
    const filePath = path.join(os.tmpdir(), fid);
    const uploadStream = fs.createWriteStream(filePath);

    req.filePath = filePath;
    req.pipe(uploadStream);
    uploadStream.on('finish', function() {
        next();
    });
}

/**
 * 修改头像
 * @param req
 * @param res
 */
const modifyHead = (req, res)=> {
    let uid = req.user.uid;
    let bucket = 'yhb-head';
    let filePath = req.filePath;

    settingModel.modifyHead(uid, bucket, filePath).then(result=> {
        res.send(result);
    });
};

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