reg.js 8.38 KB
/**
 * 注册控制器
 */
'use strict';
const _ = require('lodash');
const Promise = require('bluebird');
const passportHelper = require('../models/passport-helper');
const RegService = require('../models/reg-service');
const UserService = require('../models/user-service');
const LoginService = require('../models/login-service');
const config = require('../../../config/common');
const safeRedirect = require('../../../doraemon/middleware/safe-redirect').safeRedirect;
const captcha = require('./captcha');

let helpers = global.yoho.helpers;
let cache = global.yoho.cache;
let cookie = global.yoho.cookie;

/**
 * 检查图形验证码
 */
let checkCode = (req, res, next) => {
    let verifyCode = _.trim(req.body.verifyCode);

    if (verifyCode !== req.session.captcha) {
        return res.json({
            code: 400,
            message: '验证码错误'
        });
    }
    next();
};

/**
 * 检查密码格式
 */
let checkPassword = (req, res, next) => {
    /* 判断密码是否符合规则 */
    let password = req.body.password;

    if (!helpers.verifyPassword(password)) {
        return res.json({
            code: 400,
            message: '密码不正确'
        });
    }
    next();
};

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

    let refer = req.query.refer;
    let mobile = req.query.mobile;
    let area = req.query.area;

    refer && res.cookie('refer', encodeURI(refer), {
        domain: config.cookieDomain,
        httpOnly: true
    });

    res.display('reg/index', {
        title: '新用户注册',
        defaultHeader: false,
        passport: {
            country: {
                list: passportHelper.getCountry()
            },
            location: area || '+86',
            defaultMobile: mobile || '',
            countryName: {
                text: '中国'
            },
            captchaUrl: helpers.urlFormat('/passport/images', {
                t: Date.now()
            }),
            itemUrl: helpers.urlFormat('/help/detail', {
                id: 103,
                contId: 239
            }),
            referUrl: refer,
            loginUrl: helpers.urlFormat('/passport/login', {
                refer: refer
            }),
            regBtnText: '立即注册'
        }
    });
};

/**
 * 检查手机号码是否注册过
 */
let checkMobile = (req, res, next) => {
    let data = {
        code: 400
    };
    let mobile = req.body.mobile;
    let area = req.body.area;

    // 判断手机号是否检查超过指定次数
    let regCheckKey = 'regCheckMobileNum_' + mobile;

    cache.get(regCheckKey).then((checkNum) => {
        checkNum = +(checkNum || 0);

        return cache.set(regCheckKey, ++checkNum).then(() => {
            return checkNum;
        });
    }).then((checkNum) => {
        if (checkNum > 500) {
            data.message = '检查次数太多';
            return res.json(data);
        }

        // 判断用户是否存在
        return UserService.findByMobileAsync(area, mobile).then((user) => {
            if (!_.isEmpty(user)) {
                data.message = '手机号码已经存在';
                return res.json(data);
            }

            data.code = 200;
            return res.json(data);
        });
    }).catch(next);
};

/**
 * 图形验证码校验
 */
let picCaptcha = (req, res) => {
    // 图形验证码已经在中间件校验,如果走到这儿,说明通过校验
    return res.json({
        code: 200,
        message: '验证码正确'
    });
};

/**
 * 发送验证码
 */
let sendBindMsg = (req, res, next) => {
    Promise.coroutine(function*() {
        let data = {
            code: 400,
            message: '',
            data: ''
        };

        let mobile = req.body.mobile;
        let area = req.body.area;

        // 检查是否检查过
        // let checkNum = yield cache.get(`regCheckMobileNum_${mobile}`);

        //检查上次的发送短信号码
        if (captcha.checkSendMobile(req, res, true)) {
            req.session.sendMobile = '';
            data.message = '发送失败';
            return res.json(data);
        }

        // 校验是否发送过多
        let sendCodeKey = `send_code_${area}_${mobile}`;
        let sendCodeTimes = yield cache.get(sendCodeKey);

        sendCodeTimes = +(sendCodeTimes || 0);
        if (sendCodeTimes >= 5) {
            data.message = sendCodeTimes >= 10 ? '您已多次提交验证码,请尽快联系客服解决' : '您收到的验证码短信已超过本日限定最多次数,请您耐心等待';
            return res.json(data);
        }

        /* 向手机发送注册验证码 */
        let result = yield RegService.sendCodeToMobileAsync(area, mobile);

        return cache.set(sendCodeKey, sendCodeTimes + 1, 3600).then(() => {
            if (result.code) {
                //记录发送短信的号码
                req.session.sendMobile = mobile;
                return res.json(result);
            } else {
                data.message = '发送失败';
                return res.json(data);
            }
        });
    })().catch(next);
};

/**
 * 短信验证码校验
 */
let msgCaptcha = (req, res, next) => {
    let data = {
        code: 400,
        message: '',
        data: ''
    };
    let area = req.body.area;
    let mobile = req.body.mobile;
    let code = req.body.code; // 短信验证码

    RegService.validMobileCodeAsync(area, mobile, code).then((result) => {
        if (result.code) {
            return res.json(result);
        } else {
            data.message = '验证码错误';
            return res.json(data);
        }
    }).catch(next);
};

/**
 *  手机注册
 */
let mobileRegister = (req, res, next) => {
    Promise.coroutine(function*() {
        let data = {
            code: 400,
            message: '',
            data: ''
        };

        /* 判断是否是有效的注册方式,防注册机刷 */
        let regExpireTime = req.session._REG_EXPIRE;

        if (!regExpireTime || regExpireTime < Date.now()) {
            data.message = '注册超时';
            return res.json(data);
        }

        /* IP仅允许点击注册500次/时 */
        let ipKey = 'ip_register_' + req.ip;
        let ipTimes = yield cache.get(ipKey);

        if (!ipTimes) {
            ipTimes = 0;
        } else {
            ipTimes = +ipTimes;
        }

        if (ipTimes >= 500) {
            data.message = '由于你IP受限无法注册';
            return res.json(data);
        }

        yield cache.set(ipKey, ipTimes + 1, 3600);

        let area = req.body.area;
        let mobile = req.body.mobile;
        let code = req.body.code; // 短信验证码
        let password = req.body.password;
        let result = yield RegService.validMobileCodeAsync(area, mobile, code); // 验证注册的标识码是否有效

        if (!result.code || result.code !== 200) {
            data.message = '验证码错误';
            return res.json(data);
        }

        /* 手机注册: 调用注册接口*/
        let regResult = yield RegService.regMobileAsync(area, mobile, password, code, cookie.getShoppingKey(req));

        if (!regResult.code || regResult.code !== 200) {
            data.message = '注册失败';
            return res.json(data);
        }

        return LoginService.syncUserSession(regResult.data.uid, req, res).then(() => {
            return res.json({
                code: 200,
                message: '注册成功',
                data: {
                    href: safeRedirect(helpers.urlFormat('/passport/reg/success', {
                        next: cookie.getRefer(req, '/?go=1'),
                        goShoppingUrl: config.siteUrl
                    }))
                }
            });
        });
    })().catch(next);
};

/**
 * 成功页
 */
let success = (req, res) => {
    let goUrl = req.query.next || config.siteUrl;
    let goShoppingUrl = req.query.goShoppingUrl || config.siteUrl;
    let mobile = req.query.mobile || '用户';

    res.display('reg/success', {
        defaultHeader: false,
        title: '注册成功',
        passport: {
            goUrl: safeRedirect(goUrl),
            goShopping: safeRedirect(goShoppingUrl),
            mobile: mobile
        }
    });
};

module.exports = {
    checkCode,
    checkPassword,
    index,
    success,
    checkMobile,
    picCaptcha,
    sendBindMsg,
    msgCaptcha,
    mobileRegister
};