back-service.js 3.47 KB
/**
 * Created by TaoHuang on 2016/6/14.
 */

'use strict';

const helpers = require(`${library}/helpers`);
const api = require('./back-api');
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');

const userService = require('./user-service');
const sessionService = require('./session-service');
const api = require('./back-api');

module.exports.identifyUserByEmailOrMobileAsync = (userInput, areaCode) => {
    return co(function * () {

        const MESSAGE = {
            errPhone: '您输入的手机号码尚未注册!',
            errEmail: '您输入的邮件账户尚未注册!',
            errUnknown: '服务器错误',
            ok: '验证成功'
        };

        const OK = {code: 200, message: MESSAGE.ok};

        let status = {code: 400, message: MESSAGE.errUnknown};

        if (helpers.verifyEmail(userInput)) {
            const user = yield userService.findByEmailAsync(userInput);

            if (_.isEmpty(user)) {
                status.code = 402;
                status.message = MESSAGE.errEmail;
                return status;
            }
        } else if (helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, userInput))) {
            const user = yield userService.findByMobileAsync(userInput, areaCode);

            if (_.isEmpty(user)) {
                status.code = 402;
                status.message = MESSAGE.errPhone;
                return status;
            }
        } else {
            return status;
        }

        return OK;
    })();
};

function sendCodeToEmailAsync(input) {
    return api.sendCodeToEmailAsync(input).then(result => {
        if (!result.code || result.code !== 200) {
            return Promise.reject('request error!');
        }

        return result.data;
    }).catch(() => {
        return {};
    });
}

function sendCodeToMobileAsync(areaCode, userInput) {
    return api.sendCodeToMobileAsync(areaCode, userInput).then(result => {
        if (!result.code || result.code !== 200) {
            return Promise.reject('request error');
        }

        return result.data;
    }).catch(() => {
        return {};
    });
}

module.exports.sendCodeToUserAsync = (userInput, areaCode) => {
    return co(function * () {
        if (helpers.verifyEmail(userInput)) {
            const result = yield sendCodeToEmailAsync(userInput);

            if (!_.isEmpty(result)) {
                sessionService.set('email', userInput);
                return 'email;';
            }

            return 'unknown';
        } else if (helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, userInput))) {
            const result = yield sendCodeToMobileAsync(userInput, areaCode);

            if (!_.isEmpty(result)) {
                sessionService.set('mobile', userInput);
                sessionService.set('area', areaCode);
                return 'mobile';
            }

            return 'unknown';
        } else {
            return 'unknown';
        }

    })();
};


/**
 * 发送找回手机号短信
 */
module.exports.sendCodeToMobileAsync = (areaCode, mobile) => {
    const ERR = {code: 400, message: '验证失败'};

    if (!helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, mobile))) {
        return ERR;
    }

    return api.sendCodeToMobileAsync(mobile, areaCode);
};

module.exports.sendCodeToEmailAsync = (email) => {

};

/**
 * 获得首页的数据
 */
// TODO:
module.exports.indexPageDataAsync = () => {

};

module.exports.getVerifyCodeByMobilePageDataAsync = () => {

};