bind.js 9.77 KB
/**
 * 手机号绑定功能
 *
 * @author JiangFeng<jeff.jiang@yoho.cn>
 * @date 2016/06/21
 */

'use strict';

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

const Sources = {
    qq: 'QQ',
    sina: '微博',
    alipay: '支付宝',
    wechat: '微信'
};

const bind = {
    indexPage: (req, res) => {
        let refer = req.get('Referer');

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

        let openId = req.query.openId;
        let sourceType = req.query.sourceType;

        res.render('bind/index', {
            bindIndex: true, // js标识
            backUrl: helpers.urlFormat('passport/login'), // 返回的URL链接
            showHeaderImg: true, // 控制显示头部图片
            isPassportPage: true, // 模板中模块标识
            sourceType: sourceType, // 第三方登录来源
            platform: Sources[sourceType],
            isWechatLogin: sourceType === 'wechat',
            openId: openId, // openId
            areaCode: '+86', // 默认区号
            countrys: RegService.getAreaData(), // 国别码
            serviceUrl: 'http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=620092&configID=149091&jid=8732423409&info=', // 在线客服
            module: 'passport',
            page: 'bind',
            title: '绑定手机号'
        });
    },

    codePage: (req, res) => {
        let openId = req.query.openId;
        let sourceType = req.query.sourceType;
        let areaCode = req.query.areaCode || '86';
        let isReg = req.query.isReg;
        let phoneNum = req.query.phoneNum;

        res.render('bind/code', {
            backUrl: helpers.urlFormat('passport/login'),
            showHeaderImg: true,
            isPassportPage: true,
            sourceType: sourceType,
            openId: openId,
            isReg: isReg,
            areaCode: areaCode,
            phoneNum: phoneNum,
            relateCode: isReg === 3,
            bindCode: isReg !== 3,
            module: 'passport',
            page: 'bind-code',
            title: '验证手机'
        });
    },

    bindCheck: (req, res, next) => {
        let phoneNum = req.body.phoneNum;
        let openId = req.body.openId;
        let areaCode = req.body.areaCode || '86';
        let sourceType = req.body.sourceType;

        if (_.isNumber(parseInt(phoneNum, 0)) && openId && areaCode && sourceType) {
            BindService.bindCheck(phoneNum, openId, sourceType, areaCode).then(result => {
                let data = {
                    code: result.code,
                    message: result.message,
                    data: {}
                };

                if (result.code === 200) {
                    let nextUrl = helpers.urlFormat('/passport/bind/code', {
                        isReg: result.data.is_register,  // esline-disable-line
                        openId: openId,
                        sourceType: sourceType,
                        areaCode: areaCode,
                        phoneNum: phoneNum
                    });

                    data.data.isReg = result.data.is_register;
                    data.data.next = nextUrl;
                } else {
                    data.data = result.data;
                }

                res.json(data);
            }).catch(next);
        } else {
            res.json({
                code: 400,
                message: '',
                data: ''
            });
        }
    },

    sendBindMsg: (req, res, next) => {
        let phoneNum = req.body.phoneNum;
        let areaCode = req.body.areaCode;

        if (req.xhr && _.isNumber(parseInt(phoneNum, 0)) && areaCode) {
            BindService.sendBindMsg(areaCode, phoneNum).then(result => {
                if (result && result.code) {
                    res.json(result);
                } else {
                    res.json({ code: 400, message: '', data: '' });
                }
            }).catch(next);
        } else {
            res.json({ code: 400, message: '', data: '' });
        }
    },

    checkBindMsg: (req, res, next) => {
        let phoneNum = req.body.phoneNum;
        let code = req.body.code;
        let areaCode = req.body.areaCode;

        if (_.isNumber(parseInt(phoneNum, 0)) && code && areaCode) {
            BindService.checkBindMsg(areaCode, phoneNum, code).then(result => {
                if (result && result.code) {
                    res.json(result);
                } else {
                    res.json({ code: 400, message: '', data: '' });
                }
            }).catch(next);
        } else {
            res.json({ code: 400, message: '', data: '' });
        }
    },

    bindMobile: (req, res, next) => {
        let phoneNum = req.body.phoneNum;
        let openId = req.body.openId;
        let areaCode = req.body.areaCode || '86';
        let sourceType = req.body.sourceType;
        let code = req.body.code;
        let password = req.body.password || '';

        if (_.isNumber(parseInt(phoneNum, 0)) && openId && sourceType && areaCode && code) {
            BindService.checkBindCode(areaCode, phoneNum, code).then(result => {
                if (result && result.code && result.code === 200) {
                    return BindService.bindMobile(openId, sourceType, phoneNum, areaCode, password);
                } else {
                    return { code: 400, message: '短信验证码错误', data: '' };
                }
            }).then(result => {
                let refer = req.cookies.refer;

                refer = refer ? decodeURI(refer) : helpers.urlFormat('/passport/bind/success');
                if (result && result.code && result.code === 200 && result.data.uid) {
                    AuthHelper.syncUserSession(result.data.uid, req, res);
                    result.data.refer = refer;
                }
                return result;
            }).then(result => {
                res.json(result);
            }).catch(next);
        } else {
            res.json({ code: 400, message: '', data: '' });
        }
    },

    relateMobile: (req, res, next) => {
        let phoneNum = req.body.phoneNum;
        let openId = req.body.openId;
        let areaCode = req.body.areaCode || '86';
        let sourceType = req.body.sourceType;
        let code = req.body.code;

        if (_.isNumber(parseInt(phoneNum, 0)) && openId && areaCode && sourceType && code) {
            BindService.checkBindCode(areaCode, phoneNum, code).then(result => {
                if (result && result.code && result.code === 200) {
                    return BindService.relateMobile(openId, sourceType, phoneNum, code);
                } else {
                    return { code: 400, message: '短信验证码错误', data: '' };
                }
            }).then(result => {
                let refer = helpers.urlFormat('/passport/bind/success', { sourceType: sourceType });

                if (result && result.code && result.code === 200 && result.data.uid) {
                    return AuthHelper.syncUserSession(result.data.uid, req, res).then(() => {
                        result.data.refer = refer;
                        return result;
                    });
                } else {
                    return { code: 400, message: '绑定失败', data: '' };
                }

            }).then(result => {
                res.json(result);
            }).catch(next);
        } else {
            res.json({ code: 400, message: '', data: '' });
        }
    },

    passwordPage: (req, res) => {
        let openId = req.query.openId;
        let sourceType = req.query.sourceType;
        let areaCode = req.query.areaCode || '86';
        let phoneNum = req.query.phoneNum;
        let code = req.query.code;

        res.render('bind/password', {
            module: 'passport',
            page: 'bind-password',
            bindPwd: true, // js标识
            backUrl: helpers.urlFormat('/passport/login'), // 返回的URL链接
            showHeaderImg: true, // 控制显示头部图片
            isPassportPage: true, // 模板中模块标识
            sourceType: sourceType, // 第三方登录来源
            openId: openId, // openId
            areaCode: areaCode, // 国别码
            phoneNum: phoneNum,   // 国别码
            code: code // 验证码
        });
    },

    successPage: (req, res) => {
        let refer = req.cookies.refer;

        refer = refer ? decodeURI(refer) : helpers.urlFormat('/?go=1');

        res.render('bind/success', {
            isPassportPage: true,
            successTip: '恭喜您,第三方账号关联手机号码成功!',
            goUrl: refer,
            module: 'passport',
            page: 'bind-success',
            title: '绑定手机号'
        });
    },

    changeCheck: (req, res, next) => {
        let phoneNum = req.body.phoneNum;
        let areaCode = req.body.areaCode;

        if (_.isNumber(parseInt(phoneNum, 0)) && areaCode) {
            BindService.changeCheck(phoneNum, areaCode).then(result => {
                res.json(result);
            }).catch(next);
        } else {
            res.json({ code: 400, message: '', data: '' });
        }
    },

    changeMobile: (req, res, next) => {
        let uid = req.user.uid;
        let phoneNum = req.body.phoneNum;
        let areaCode = req.body.areaCode;
        let code = req.body.code;

        if (_.isNumber(parseInt(phoneNum, 0)) && uid && areaCode && code) {
            BindService.changeMobile(uid, phoneNum, areaCode, code).then(result => {
                res.json(result);
            }).catch(next);
        } else {
            res.json({ code: 400, message: '', data: '' });
        }
    }
};

module.exports = bind;