cert.js 6.46 KB
/**
 * 未认证手机号邮箱用户注册的引导手机号的绑定
 *
 * @author: lcy<chuanyang.liu@yoho.cn>
 */

'use strict';

const _ = require('lodash');
const helpers = global.yoho.helpers;
const passportHelper = require('../models/passport-helper');
const CertService = require('../models/cert-service');
const UserService = require('../models/user-service');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
const config = global.yoho.config;

const cert = {
    index: (req, res) => {
        let refer = req.query.refer || `${config.siteUrl}`;

        res.render('cert/index', {
            region: passportHelper.getCountry(),
            refer: refer,
            serviceUrl: helpers.urlFormat('/help', {
                category_id: 9
            }),
            simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
            module: 'passport',
            page: 'cert',
            title: '登录完善信息'
        });
    },
    cert: (req, res) => {
        let mobile = req.query.mobile;
        let area = req.query.area;
        let refer = req.query.refer;

        res.render('cert/cert', {
            enablePerfectInformation: true,
            mobile: mobile,
            area: area,
            refer: refer,
            prevUrl: helpers.urlFormat('/passport/cert/index', {
                refer: refer
            }),
            module: 'passport',
            page: 'cert-cert',
            simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
            title: '登录绑定'
        });
    },
    relate: (req, res, next) => {
        let mobile = req.body.mobile;
        let area = req.body.area;
        let refer = req.body.refer;

        req.ctx(UserService).getUserInfo(area, mobile).then(user => {
            let data = _.assign(user, {
                phoneNum: mobile,
                areaCode: area,
                refer: refer,
                changeHref: helpers.urlFormat('/passport/cert/index', {
                    refer: refer
                }),
                simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
                module: 'passport',
                page: 'cert-relate',
                title: '账号关联'
            });

            res.render('cert/relate', data);
        }).catch(next);
    },
    success: (req, res) => {
        let type = _.trim(req.query.type);
        let refer = req.query.refer || `${config.siteUrl}`;
        let isRelate = (type === 'relate');
        let data = {
            goShopping: refer,
            relate: isRelate,
            module: 'passport',
            page: 'cert-success',
            simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
            title: isRelate ? '绑定手机号' : '关联手机号'
        };

        res.render('cert/success', data);
    },
    check: (req, res, next) => {
        let mobile = req.body.mobile;
        let area = req.body.area || '86';
        let code = req.body.code;

        if (!mobile || !area || !code) {
            return res.json({code: 400, message: '', data: ''});
        }

        req.ctx(CertService).certCheck(mobile, area, code).then(({error, result}) => {
            if (error) {
                return res.json({code: 400, message: '验证错误,请重新登录', data: ''});
            }

            return res.json({code: 200, data: result});
        }).catch(next);
    },
    sendCertMsg: (req, res, next) => {
        let mobile = req.body.mobile;
        let area = req.body.area;

        req.ctx(CertService).sendCertMsg(mobile, area).then(result => {
            console.log(result);
            if (result && result.code) {
                return res.json(result);
            } else {
                return res.json({code: 400, message: '', data: ''});
            }
        }).catch(next);
    },
    certMobile: (req, res, next) => {
        let mobile = _.trim(req.body.mobile);
        let area = _.trim(req.body.area) || '86';
        let code = _.trim(req.body.code);

        if (!code) {
            return {
                code: 400,
                message: '短信验证码错误'
            };
        }

        if (!req.session.forceBind ||
            !req.session.forceBind.username ||
            !req.session.forceBind.password
        ) {
            return {
                code: 400,
                message: '请重新登录'
            };
        }

        req.ctx(CertService).certMobile(
            req.session.forceBind.username,
            req.session.forceBind.password,
            mobile, code, area
        ).then(result => {
            delete req.session.forceBind;

            if (!result || result.code !== 200) {
                return res.json({
                    code: 400,
                    message: _.get(result, 'message') || '操作错误,请稍后再试!'
                });
            }

            return res.json({
                code: 200
            });
        }).catch(() => {
            delete req.session.forceBind;
            next();
        });
    },
    relateMobile: (req, res, next) => {
        let uid = req.user.uid;
        let mobile = _.trim(req.body.mobile);
        let area = _.trim(req.body.areaCode) || '86';
        let code = _.trim(req.body.code);
        let refer = req.body.refer;
        let email;

        req.ctx(UserService).profile(uid).then(info => {
            if (info && info.code === 200) {
                email = _.get(info, 'data.email');
            } else {
                return res.json({
                    code: 400,
                    message: '系统错误,请稍后再试!'
                });
            }

            req.ctx(CertService).relateMobile(uid, email, code, mobile, area).then(result => {
                if (result && result.code === 200) {
                    let url = helpers.urlFormat('/passport/cert/success', {
                        type: 'relate',
                        refer: refer
                    });

                    return {
                        code: 200,
                        message: result.message,
                        data: {
                            nextUrl: url
                        }
                    };
                } else {
                    return {
                        code: _.get(result, 'code', 400),
                        message: _.get(result, 'message')
                    };
                }
            }).then(result => {
                res.json(result);
            }).catch(next);
        }).catch(next);
    }
};

module.exports = cert;