bind.js 3.79 KB
/**
 * 第三方登录后绑定
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 */

'use strict';

const _ = require('lodash');
const helpers = global.yoho.helpers;
const passportHelper = require('../models/passport-helper');
const BindService = require('../models/bind-service');
const LoginService = require('../models/login-service');

// const UserService = require('../models/user-service');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');

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

const bind = {
    indexPage: (req, res) => {
        let openId = req.query.openId;
        let sourceType = req.query.sourceType;
        let nickName = req.query.nickName;

        res.render('bind/index', {
            thirdLogin: true,
            openId: openId,
            sourceType: sourceType,
            nickName: nickName,
            region: passportHelper.getCountry(),
            serviceUrl: helpers.urlFormat('/help', {
                category_id: 9
            }),
            simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
            module: 'passport',
            page: 'bind',
            title: '联合登录补全信息'
        });
    },
    bindSuccess: (req, res) => {
        let sourceType = _.trim(req.query.sourceType);
        let sourceInfo = sourceType.split('_');
        let sourceName = Sources[sourceInfo[0]];
        let isRelate = (sourceInfo[1] === 'relate');
        let data = {
            thirdLogin: true,
            goShopping: helpers.urlFormat('/'),
            sourceName: sourceName,
            relate: isRelate,
            module: 'passport',
            page: 'bind-success',
            simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
            title: '绑定手机号'
        };

        req.session.type = '';

        res.render('bind/success', data);
    },
    sendBindMsg: (req, res, next) => {
        let mobile = req.body.mobile;
        let area = req.body.area;
        let sessionId = req.session.id;
        let captcha = req.body.verifyCode;

        req.ctx(BindService).sendBindMsg(area, mobile, sessionId, captcha).then(result => {
            if (result && result.code) {
                return res.json(result);
            } else {
                return res.json({code: 400, message: '', data: ''});
            }
        }).catch(next);
    },
    bindMobile: (req, res, next) => {
        let mobile = _.trim(req.body.mobile);
        let area = _.trim(req.body.area) || '86';
        let openId = _.trim(req.body.openId);
        let sourceType = _.trim(req.body.sourceType);
        let nickName = _.trim(req.body.nickName);
        let code = _.trim(req.body.code);

        try {
            nickName = decodeURIComponent(nickName);
        } catch (e) {
            nickName = '';
        }

        req.ctx(BindService).bindMobile(openId, sourceType, mobile, area, nickName, code).then(result => {
            if (result && result.code) {
                if (result.code === 200 && result.data && result.data.uid) {
                    let refer = helpers.urlFormat('/passport/thirdlogin/bindsuccess', {
                        sourceType: sourceType + '_bind'
                    });

                    return req.ctx(LoginService).syncUserSession(result.data, req, res).then(() => {
                        return {code: 200, message: result.message, data: {refer: refer}};
                    });
                } else {
                    return {code: result.code, message: result.message, data: {refer: ''}};
                }
            } else {
                return {code: 400, message: '', data: ''};
            }
        }).then(result => {
            return res.json(result);
        }).catch(next);
    },
};

module.exports = bind;