Blame view

apps/passport/controllers/bind.js 10.7 KB
htoooth authored
1 2 3
/**
 * 第三方登录后绑定
 *
htoooth authored
4
 * @author: TaoHuang
htoooth authored
5 6 7 8
 */

'use strict';
htoooth authored
9 10 11
const _ = require('lodash');
const helpers = global.yoho.helpers;
const PassportHelper = require('../models/passport-helper');
htoooth authored
12
const UserService = require('../models/user-service');
htoooth authored
13 14
const BindService = require('../models/bind-service');
const LoginService = require('../models/login-service');
htoooth authored
15 16 17 18 19

const Sources = {
    qq: 'QQ',
    sina: '微博',
    alipay: '支付宝',
htoooth authored
20
    wechat: '微信'
htoooth authored
21 22
};
23 24
const DEFAULT_URL = 'http://img10.static.yhbimg.com/headimg/2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100';
htoooth authored
25 26 27
/**
 * 首页
 */
htoooth authored
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
const indexPage = (req, res) => {
    let openId = req.query.openId;
    let sourceType = req.query.sourceType;

    res.display('bind/index', Object.assign({
        module: 'passport',
        page: 'bind',
        title: '联合登录补全信息',
        defaultHeader: false
    }, {
        openId: openId,
        sourceType: sourceType,
        country: {list: PassportHelper.getCountry()},
        local: '+86',
        countryName: {text: '中国'},
        imgCaptcha: helpers.urlFormat('/passport/images', {t: Date.now()})
    }));
};
htoooth authored
47 48 49
/**
 * 设置密码
 */
htoooth authored
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
const bindSetPwdPage = (req, res) => {
    let mobile = req.query.thirdPart.mobile;
    let sourceType = req.query.thirdPart.sourceType;
    let openId = req.query.thirdPart.openId;
    let area = req.query.thirdPart.area;

    res.display('bind/bind-set-pwd', Object.assign({
        module: 'passport',
        page: 'bind-set-pwd',
        title: '登录绑定',
        defaultHeader: false
    }, {
        mobile: mobile,
        sourceType: sourceType,
        openId: openId,
        area: area
    }));

};
htoooth authored
70 71 72
/**
 * 绑定确认页面
 */
htoooth authored
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
const bindConfirmPage = (req, res) => {
    let thirdPart = req.query.thirdPart;
    let user = req.query.user;
    let avatar = user.headImg || DEFAULT_URL;

    res.display('bind/bind-confirm', Object.assign({
        module: 'passport',
        page: 'bind-confirm',
        title: '绑定确认',
        defaultHeader: false
    }, {
        avatar: avatar,
        name: user.username || '咸鸭蛋',
        bindSuccess: helpers.urlFormat('/thirdlogin/bindSuccess', {
            sourceType: thirdPart.sourceType
        }),
        bindUrl: helpers.urlFormat('/passport/thirdlogin/index', {
htoooth authored
90
            openId: thirdPart.openId,
htoooth authored
91
            sourceType: thirdPart.sourceType
htoooth authored
92 93 94 95 96 97 98 99 100 101
        }),
        loginUrl: helpers.urlFormat('/passport/login'),
        mobile: thirdPart.mobile,
        openId: thirdPart.openId,
        area: thirdPart.area,
        code: thirdPart.code,
        sourceType: thirdPart.sourceType
    }));
};
htoooth authored
102 103 104
/**
 * 绑定成功页面
 */
htoooth authored
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
const bindSuccessPage = (req, res) => {
    let sourceType = _.trim(req.query.sourceType);
    let sourceInfo = sourceType.split('_');
    let sourceName = Sources[sourceInfo[0]];

    res.display('bind/bind-success', Object.assign({
        module: 'passport',
        page: 'bind-success',
        title: '绑定手机号',
        defaultHeader: false
    }, {
        goShopping: helpers.urlFormat('/'),
        sourceName: sourceName
    }));
};
htoooth authored
121 122 123
/**
 * 已绑定过手机页面
 */
htoooth authored
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
const bindedPage = (req, res) => {
    let thirdPart = req.query.thirdPart;
    let user = req.query.user;
    let avatar = user.headImg || DEFAULT_URL;

    res.display('bind/bind-done', Object.assign({
        module: 'passport',
        page: 'bind-success',
        title: '绑定手机号',
        defaultHeader: false
    }, {
        avatar: avatar,
        name: user.username || '咸鸭蛋',
        loginUrl: helpers.urlFormat('/passport/login'),
        bindUrl: helpers.urlFormat('/passport/thirdlogin/index', {
            openId: thirdPart.openId,
            sourceType: thirdPart.sourceType
        })
    }));
};
htoooth authored
145 146 147
/**
 * 关联成功页面
 */
htoooth authored
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
const relateSuccessPage = (req, res) => {
    let sourceType = _.trim(req.query.sourceType);
    let sourceInfo = sourceType.split('_');
    let sourceName = Sources[sourceInfo[0]];

    res.display('bind/relate-success', Object.assign({
        module: 'passport',
        page: 'relate-success',
        title: '关联手机号',
        defaultHeader: false
    }, {
        goShopping: helpers.urlFormat('/'),
        sourceName: sourceName
    }));
};
htoooth authored
164 165 166
/**
 * 关联手机号确定页面
 */
htoooth authored
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
const relateConfirmPage = (req, res) => {
    let thirdPart = req.query.thirdPart;
    let user = req.query.user;
    let avatar = user.headImg || DEFAULT_URL;

    res.display('bind/relate-confirm', Object.assign({
        module: 'passport',
        page: 'relate-confirm',
        title: '关联确认',
        defaultHeader: false
    }, {
        avatar: avatar,
        name: user.username || '咸鸭蛋',
        relateUrl: helpers.urlFormat('/passport/thirdlogin/index', {
            openId: thirdPart.openId,
            sourceType: thirdPart.sourceType
        }),
        signinUrl: helpers.urlFormat('/passport/login'),
        mobile: thirdPart.mobile,
        area: thirdPart.area,
        sourceType: thirdPart.sourceType,
        openId: thirdPart.openId
    }));
};
htoooth authored
192 193 194
/**
 * 检查手机号和第三方绑定情况
 */
htoooth authored
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
const bindCheck = (req, res, next) => {
    let mobile = req.body.mobile;
    let openId = req.body.openId;
    let area = req.body.area || '86';
    let sourceType = req.body.sourceType;

    if (mobile && openId && area && sourceType) {
        /**
         * 接口绑定返回值:
         * code:200,is_register=0     // 绑定流程:未注册,可绑定
         * code:200,is_register=1     // 绑定流程:已注册绑定过其他第三方
         * code:200:is_register=3     // 关联流程
         * code:505                   // 手机号码注册过,而且该第三方也已经绑定过手机号
         * code:506                   // 手机号码注册过,而且该手机号码也已经绑定过该类型第三方
         */
        BindService.bindCheckAsync(mobile, openId, sourceType, area).then(result => {
            if (!result || !result.code) {
htoooth authored
212
                return {code: 400, message: '', data: ''};
htoooth authored
213 214 215 216 217 218
            } else if (result.code === 200 && result.data.is_register === 0) {
                let nextUrl = helpers.urlFormat('/passport/thirdlogin/bindSetPwd');

                // 绑定流程:code=200 未注册,可绑定
                return {code: 200, message: result.message, data: {next: nextUrl}};
            } else if (result.code === 200 && result.data.is_register === 1) {
htoooth authored
219
                return UserService.getUserInfoAsync(area, mobile).then(user => {
htoooth authored
220 221 222 223 224
                    // 绑定流程:code=201 已注册 绑定过其他第三方
                    return {code: 201, message: result.message, data: {user: user}};
                });
            } else if (result.code === 200 && result.data.is_register === 3) {
                // 关联流程
htoooth authored
225
                return UserService.getUserInfoAsync(area, mobile).then(user => {
htoooth authored
226 227 228
                    return {code: 203, message: result.message, data: {user: user}};
                });
            } else if (result.code === 506 || result.code === 505) {
htoooth authored
229
                return UserService.getUserInfoAsync(area, mobile).then(user => {
htoooth authored
230 231 232
                    // 绑定流程:code=201 已注册 绑定过其他第三方
                    return {code: 205, message: result.message, data: {user: user}};
                });
htoooth authored
233
            } else {
htoooth authored
234
                return {code: result.code, message: result.message, data: result.data ? result.data : ''};
htoooth authored
235 236 237 238
            }
        }).then(result => {
            res.json(result);
        }).catch(next);
htoooth authored
239 240
    } else {
        res.json({code: 400, message: '', data: ''});
htoooth authored
241
    }
htoooth authored
242 243
};
htoooth authored
244 245 246
/**
 * 发送绑定短信页面
 */
htoooth authored
247 248 249 250 251 252 253 254 255 256 257 258
const sendBindMsg = (req, res, next) => {
    let mobile = req.body.mobile;
    let area = req.body.area;

    BindService.sendBindMsgAsync(area, mobile).then(result => {
        if (result && result.code) {
            res.json(result);
        } else {
            res.json({code: 400, message: '', data: ''});
        }
    }).catch(next);
};
htoooth authored
259 260 261 262

/**
 * 验证绑定短信
 */
htoooth authored
263 264 265 266 267 268 269 270 271 272 273 274 275 276
const checkBindMsg = (req, res, next) => {
    let mobile = req.body.mobile;
    let area = req.body.area;
    let code = req.body.code;

    BindService.checkBindCodeAsync(area, mobile, code).then(result => {
        if (result && result.code) {
            res.json(result);
        } else {
            res.json({code: 400, message: '', data: ''});
        }
    }).catch(next);
};
htoooth authored
277 278 279
/**
 * 绑定第三方到手机号
 */
htoooth authored
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
const 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 password = _.trim(req.body.password) || '';

    BindService.bindMobileAsync(openId, sourceType, mobile, area, password).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 LoginService.syncUserSession(result.data.uid, 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 => {
        res.json(result);
    }).catch(next);
};
htoooth authored
308 309 310
/**
 * 关联第三方到手机号
 */
htoooth authored
311 312 313 314 315 316
const relateMobile = (req, res, next) => {
    let mobile = req.body.mobile;
    let openId = req.body.openId;
    let areaCode = req.body.areaCode || '86';
    let sourceType = req.body.sourceType;
htoooth authored
317
    BindService.relateMobileAsync(openId, sourceType, mobile, areaCode).then(result => {
htoooth authored
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
        if (result && result.code) {
            if (result.code === 200 && result.data && result.data.uid) {
                let refer = helpers.urlFormat('/passport/thirdlogin/relatesuccess', {
                    sourceType: sourceType + '_bind'
                });

                return LoginService.syncUserSession(result.data.uid, 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 => {
        res.json(result);
    }).catch(next);
};

module.exports = {
htoooth authored
339 340 341 342 343 344 345 346 347 348 349 350
    indexPage,
    bindSetPwdPage,
    bindConfirmPage,
    bindSuccessPage,
    bindedPage,
    relateConfirmPage,
    relateSuccessPage,
    bindCheck,
    sendBindMsg,
    checkBindMsg,
    bindMobile,
    relateMobile
htoooth authored
351
};