reg.js 7.72 KB
/**
 * 注册
 *
 * @author Bi Kai<kai.bi@yoho.cn>
 * @date 2016/06/23
 */

'use strict';

const _ = require('lodash');
const helpers = global.yoho.helpers;
const sign = global.yoho.sign;
const cookie = global.yoho.cookie;
const RegService = require('../models/reg-service');
const AuthHelper = require('../models/auth-helper');

let index = (req, res) => {
    // 设置注册有效时间30分钟, 防机器刷
    req.session._REG_EXPIRE = Date.now() + 1800000;

    let refer = req.query.refer;

    refer && res.cookie('refer', encodeURI(refer), {
        domain: 'yohobuy.com'
    });

    res.render('reg/index', {
        title: '注册',
        backUrl: 'javascript:history.go(-1)', // eslint-disable-line
        headerText: '注册', // 头部信息
        isPassportPage: true, // 模板中模块标识
        areaCode: '+86', // 默认的区号
        countrys: RegService.getAreaData() // 地区信息列表
    });
};
let verifyMobile = (req, res, next) => {
    let data = {
        code: 400,
        message: '手机号已存在',
        data: ''
    };

    let mobile = +req.body.phoneNum;
    let area = +(req.body.areaCode || 86);

    // 判断参数是否合法
    if (!_.isNumber(mobile) || !_.isNumber(area)) {
        data.message = '手机号错误';
        return res.json(data);
    }

    // 设置注册有效时间30分钟, 防机器刷
    let expire = req.session._REG_EXPIRE;

    if (!expire || expire < Date.now()) {
        data.message = '非法请求';
        return res.json(data);
    }

    // 向手机发送注册验证码
    RegService.sendCodeToMobile(area, mobile).then((result) => {
        if (!result.code) {
            return res.json(data);
        }

        // 返回跳转到验证页面的链接
        if (result.code === 200) {
            let token = sign.makeToken(mobile);

            result.data = helpers.urlFormat('/passport/reg/code', {
                token: token,
                phoneNum: mobile,
                areaCode: area
            });
        }

        return res.json(result);
    }).catch(next);
};
let codeAction = (req, res, next) => {
    let token = req.query.token;
    let mobile = +req.query.phoneNum;
    let area = +(req.query.areaCode || 86);

    // 判断是否允许访问, 不允许则跳转到错误页面
    if (!_.isString(token) || !_.isNumber(mobile) || !sign.verifyToken(mobile, token)) {
        return next({
            code: 403,
            message: 'error token or mobile'
        });
    }

    res.render('reg/code', {
        page: 'code',
        title: '注册-验证码',
            backUrl: '/?go=1', // eslint-disable-line
        headerText: '注册', // 头部信息
        isPassportPage: true, // 模板中模块标识
        areaCode: area, // 默认的区号
        phoneNum: mobile, // 手机号
        token: token, // 访问令牌
        serviceUrl: 'http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=620092&configID=149091&jid=8732423409&info=' // 在线客服
    });
};
let sendCode = (req, res, next) => {
    let data = {
        code: 400,
        message: '发送验证码失败',
        data: ''
    };

    let mobile = +req.body.phoneNum;
    let area = +(req.body.areaCode || 86);

    // 判断参数是否合法
    if (!_.isNumber(mobile) || !_.isNumber(area)) {
        data.message = '手机号错误';
        return res.json(data);
    }

    // 设置注册有效时间30分钟, 防机器刷
    let expire = req.session._REG_EXPIRE;

    if (!expire || expire < Date.now()) {
        data.message = '非法请求';
        return res.json(data);
    }

    // 向手机发送注册验证码
    RegService.sendCodeToMobile(area, mobile).then((result) => {
        return result.code ? res.json(result) : res.json(data);
    }).catch(next);
};
let verifyCode = (req, res, next) => {
    let data = {
        code: 400,
        message: '验证码错误',
        data: ''
    };

    let mobile = +req.body.phoneNum;
    let area = +(req.body.areaCode || 86);
    let code = +req.body.code;

        /* 判断参数是否合法 */
    if (!_.isNumber(mobile) || !_.isNumber(area) || !_.isNumber(code)) {
        data.message = '手机号错误';
        return res.json(data);
    }

    // 设置注册有效时间30分钟, 防机器刷
    let expire = req.session._REG_EXPIRE;

    if (!expire || expire < Date.now()) {
        data.message = '非法请求';
        return res.json(data);
    }

    // 验证注册的标识码是否有效
    RegService.validMobileCode(area, mobile, code).then((result) => {
        if (!result.code) {
            return res.json(data);
        }

        // 返回跳转到设置密码的链接
        if (result.code === 200) {
            let token = sign.makeToken(mobile);

            result.data = helpers.urlFormat('/passport/reg/password', {
                token: token,
                phoneNum: mobile,
                areaCode: area
            });
        } else if (result.code === 404) {
            result.message = '验证码错误'; // 统一验证提示
        }

        return res.json(result);
    }).catch(next);
};
let passwordAction = (req, res, next) => {
    let token = req.query.token;
    let mobile = +req.query.phoneNum;
    let area = +(req.query.areaCode || 86);

    // 判断是否允许访问, 不允许则跳转到错误页面
    if (!_.isString(token) || !_.isNumber(mobile) || !_.isNumber(area) || !sign.verifyToken(mobile, token)) {
        return next({
            code: 403,
            message: 'error token or mobile'
        });
    }

    res.render('reg/password', {
        page: 'password',
        title: '注册-设置密码',
            backUrl: '/?go=1', // eslint-disable-line
        headerText: '注册', // 头部信息
        isPassportPage: true, // 模板中模块标识
        areaCode: area, // 默认的区号
        phoneNum: mobile, // 手机号
        token: token // 访问令牌
    });
};
let setPassword = (req, res, next) => {
    let data = {
        code: 400,
        message: '密码格式不正确',
        data: ''
    };

    let mobile = +req.body.phoneNum;
    let area = +(req.body.areaCode || 86);
    let password = req.body.password;
    let token = req.body.token;

    // 判断参数是否合法
    if (!_.isString(token) || !_.isNumber(mobile) || !_.isNumber(area) || !password) {
        data.message = '请求参数不合法';
        return res.json(data);
    }

    // 判断是否允许访问
    if (!sign.verifyToken(mobile, token)) {
        data.message = '非法 token';
        return res.json(data);
    }

    // 判断密码是否符合规则
    if (!helpers.verifyPassword(password)) {
        return res.json(data);
    }

    // 购物车key
    let shoppingKey = cookie.getShoppingKey(req);

    // 验证注册的标识码是否有效
    RegService.regMobileAes(area, mobile, password, shoppingKey).then((result) => {
        if (!result.code || result.code !== 200) {
            return Promise.reject(result);
        }
        if (!result.data || !result.data.uid) {
            return Promise.reject(result);
        }
        return AuthHelper.syncUserSession(result.data.uid, req, res);
    }).then(() => {
        // 返回跳转到来源页面
        let refer = req.cookies.refer;

        if (refer) {
            refer = decodeURI(req.cookies.refer);
        } else {
            refer = '/home';
        }

        if (/sign|login/.test(refer)) {
            refer = '/home';
        }

        return res.json({
            code: 200,
            message: '注册成功',
            data: {
                session: refer,
                href: refer
            }
        });
    }).catch(next);
};

module.exports = {
    index,
    verifyMobile,
    code: codeAction,
    sendCode,
    verifyCode,
    password: passwordAction,
    setPassword
};