Blame view

apps/passport/controllers/reg.js 6.22 KB
毕凯 authored
1
/**
htoooth authored
2 3
 * 注册控制器
 */
毕凯 authored
4
'use strict';
毕凯 authored
5
const _ = require('lodash');
毕凯 authored
6
const Promise = require('bluebird');
毕凯 authored
7
const passportHelper = require('../models/passport-helper');
htoooth authored
8 9
const RegService = require('../models/reg-service');
const LoginService = require('../models/login-service');
htoooth authored
10
11
const config = require('../../../config/common');
王水玲 authored
12
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
毕凯 authored
13
let helpers = global.yoho.helpers;
毕凯 authored
14
let cookie = global.yoho.cookie;
毕凯 authored
15
毕凯 authored
16
/**
毕凯 authored
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * 检查图形验证码
 */
let checkCode = (req, res, next) => {
    let verifyCode = _.trim(req.body.verifyCode);

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

/**
 * 检查手机格式
 */
let checkMobileMiddleware = (req, res, next) => {
htoooth authored
35 36
    let mobile = req.body.mobile;
    let area = req.body.area;
毕凯 authored
37
htoooth authored
38
    if (!(_.toNumber(mobile) && _.toNumber(area))) {
毕凯 authored
39 40 41 42 43 44 45 46 47
        return res.json({
            code: 400,
            message: '手机号码格式不正确'
        });
    }
    next();
};

/**
毕凯 authored
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
 * 检查密码格式
 */
let checkPassword = (req, res, next) => {
    /* 判断密码是否符合规则 */
    let password = req.body.password;

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

/**
毕凯 authored
64 65
 * 注册首页
 */
毕凯 authored
66 67
let index = (req, res, next) => {
    // 设置注册有效时间30分钟, 防机器刷
htoooth authored
68
    req.session.REG_EXPIRE_ = Date.now() + 1800000;
毕凯 authored
69 70

    let refer = req.query.refer;
htoooth authored
71 72
    let mobile = req.query.mobile;
    let area = req.query.area;
毕凯 authored
73 74

    refer && res.cookie('refer', encodeURI(refer), {
htoooth authored
75
        domain: config.cookieDomain
毕凯 authored
76 77
    });
htoooth authored
78
    req.ctx(RegService).getRegData().then((result) => {
毕凯 authored
79
        res.render('reg/index', {
王水玲 authored
80
            simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
毕凯 authored
81
            title: '新用户注册',
毕凯 authored
82
            passport: {
王水玲 authored
83
                region: req.ctx(passportHelper).getCountry(),
htoooth authored
84 85
                location: area || '+86',
                defaultMobile: mobile,
王水玲 authored
86
                captchaUrl: helpers.urlFormat('/passport/imagesNode', {t: Date.now()}),
yyq authored
87
                itemUrl: helpers.urlFormat('/help/detail', {id: 150, contId: 197}),
毕凯 authored
88
                referUrl: refer,
htoooth authored
89
                loginUrl: helpers.urlFormat('/signin.html', {refer: refer}),
毕凯 authored
90 91 92
                coverHref: result.url,
                coverImg: result.img,
                regBtnText: '立即注册'
王水玲 authored
93 94 95
            },
            module: 'passport',
            page: 'reg'
毕凯 authored
96 97 98 99
        });
    }).catch(next);
};
毕凯 authored
100
/**
htoooth authored
101 102 103 104 105 106 107 108 109
 * checkMobile的伪装,始终成功。
 */
let fakeCheckMobile = (req, res) => {
    return res.json({
        code: 200
    });
};

/**
毕凯 authored
110 111
 * 发送验证码
 */
毕凯 authored
112
let sendBindMsg = (req, res, next) => {
htoooth authored
113 114 115
    let mobile = req.body.mobile;
    let area = req.body.area;
    let sessionId = req.session.id;
htoooth authored
116
    let captcha = req.body.verifyCode;
毕凯 authored
117
htoooth authored
118 119
    /* 向手机发送注册验证码 */
    req.ctx(RegService).sendCodeToMobile(area, mobile, sessionId, captcha).then((result) => {
htoooth authored
120
        if (result.code === 200) {
htoooth authored
121
            req.session.type = 'register-step1';
毕凯 authored
122
        }
htoooth authored
123 124

        return res.json(result);
htoooth authored
125
    }).catch(next);
毕凯 authored
126 127
};
毕凯 authored
128 129 130
/**
 * 短信验证码校验
 */
毕凯 authored
131 132 133 134 135 136
let msgCaptcha = (req, res, next) => {
    let data = {
        code: 400,
        message: '',
        data: ''
    };
htoooth authored
137 138 139
    let area = req.body.area;
    let mobile = req.body.mobile;
    let code = req.body.code; // 短信验证码
毕凯 authored
140
htoooth authored
141
    req.ctx(RegService).validMobileCode(area, mobile, code).then((result) => {
毕凯 authored
142 143 144
        if (result.code) {
            return res.json(result);
        } else {
htoooth authored
145
            data.message = '短信验证码错误';
毕凯 authored
146 147 148 149 150
            return res.json(data);
        }
    }).catch(next);
};
毕凯 authored
151 152 153
/**
 * 注册接口
 */
毕凯 authored
154
let mobileRegister = (req, res, next) => {
htoooth authored
155
    Promise.coroutine(function* () {
毕凯 authored
156 157 158 159 160
        let data = {
            code: 400,
            message: '',
            data: ''
        };
毕凯 authored
161
htoooth authored
162 163 164 165
        if (req.session.type !== 'register-step1') {
            return res.json(data);
        }
htoooth authored
166 167 168
        let area = req.body.area;
        let mobile = req.body.mobile;
        let code = req.body.code; // 短信验证码
毕凯 authored
169
        let password = req.body.password;
htoooth authored
170
        let inviteCode = req.body.inviteCode;
htoooth authored
171
        let result = yield req.ctx(RegService).validMobileCode(area, mobile, code); // 验证注册的标识码是否有效
毕凯 authored
172 173

        if (!result.code || result.code !== 200) {
htoooth authored
174
            data.message = '短信验证码错误';
毕凯 authored
175 176 177 178
            return res.json(data);
        }

        /* 手机注册: 调用注册接口*/
htoooth authored
179
        let regResult = yield req.ctx(RegService).regMobileAes(
htoooth authored
180
            area, mobile, password, code, cookie.getShoppingKey(req), inviteCode, {udid: req.yoho.udid}
htoooth authored
181
        );
毕凯 authored
182 183 184 185 186

        if (!regResult.code || regResult.code !== 200) {
            data.message = '注册失败';
            return res.json(data);
        }
毕凯 authored
187
htoooth authored
188 189 190
        // 清除 session type
        req.session.type = '';
htoooth authored
191
        return req.ctx(LoginService).syncUserSession(regResult.data, req, res).then(() => {
毕凯 authored
192 193 194 195 196 197 198 199 200
            return res.json({
                code: 200,
                message: '注册成功',
                data: {
                    href: helpers.urlFormat('/passport/reg/success', {
                        next: cookie.getRefer(req, '/?go=1'),
                        goShoppingUrl: config.siteUrl
                    })
                }
毕凯 authored
201 202
            });
        });
毕凯 authored
203
    })().catch(next);
毕凯 authored
204 205
};
206 207
let success = (req, res, next) => {
    let goUrl = req.query.next || config.siteUrl;
毕凯 authored
208
    let goShoppingUrl = req.query.goShoppingUrl || config.siteUrl;
209
htoooth authored
210
    req.ctx(RegService).getRegData().then((result) => {
毕凯 authored
211 212
        res.render('reg/success', {
            title: '注册成功',
htoooth authored
213 214
            module: 'passport',
            page: 'reg-success',
王水玲 authored
215
            simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
毕凯 authored
216 217 218 219 220 221 222 223
            passport: {
                goUrl: goUrl,
                goShoppong: goShoppingUrl,
                coverHref: result.url,
                coverImg: result.img
            }
        });
    }).catch(next);
224 225
};
毕凯 authored
226
module.exports = {
毕凯 authored
227 228
    checkCode,
    checkMobileMiddleware,
毕凯 authored
229
    checkPassword,
230
    index,
毕凯 authored
231
    success,
毕凯 authored
232 233
    sendBindMsg,
    msgCaptcha,
htoooth authored
234 235
    mobileRegister,
    fakeCheckMobile
毕凯 authored
236
};