passport-helper.js 3.13 KB
/**
 * Created by TaoHuang on 2016/6/20.
 */
'use strict';

const _ = require('lodash');

const helpers = global.yoho.helpers;
const UserService = require('./user-service');

const DEFAULT_HEAD_IMG_ICO = 'http://img10.static.yhbimg.com/headimg/2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100';

/**
 * 国家数据
 */
const getCountry = () => {
    return [
        {
            areaCode: '+61',
            selected: false,
            name: '澳大利亚'
        },
        {
            areaCode: '+82',
            selected: false,
            name: '韩国'
        },
        {
            areaCode: '+1',
            selected: false,
            name: '加拿大'
        },
        {
            areaCode: '+60',
            selected: false,
            name: '马来西亚'
        },
        {
            areaCode: '+1',
            selected: false,
            name: '美国'
        },
        {
            areaCode: '+81',
            selected: false,
            name: '日本'
        },
        {
            areaCode: '+65',
            selected: false,
            name: '新加坡'
        },
        {
            areaCode: '+44',
            selected: false,
            name: '英国'
        },
        {
            areaCode: '+86',
            selected: true,
            name: '中国'
        },
        {
            areaCode: '+853',
            selected: false,
            name: '中国澳门'
        },
        {
            areaCode: '+886',
            selected: false,
            name: '中国台湾'
        },
        {
            areaCode: '+852',
            selected: false,
            name: '中国香港'
        }
    ];
};

/**
 * 验证邮箱是否合法
 */
const verifyEmail = email => {
    if (!email) {
        return false;
    }

    const emailRegExp = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;

    return emailRegExp.test(email);
};

/**
 *  验证手机是否合法
 */
const verifyMobile = phone => {
    if (!phone) {
        return false;
    }

    return /^[0-9]+$/.test(phone);
};


/**
 * 密码是否合法
 */
const isPassword = pwd => {
    if (!pwd) {
        return false;
    }

    let pwdRegexp = /^([a-zA-Z0-9\-\+_!@\#$%\^&\*\(\)\:\;\.=\[\]\\\',\?]){6,20}$/;

    return pwdRegexp.test(_.trim(pwd));
};

/**
 * 第三方登录 根据手机号获取用户相关信息
 */
const getUserInfo = (area, mobile) => {
    return UserService.findByMobileAsync(area, mobile).then(user => {
        let profile = (user.profile_name || mobile).toString();

        if ((profile.length === 11 && profile.indexOf('*') < 0) || (profile.indexOf('-') >= 0 &&
            profile.indexOf('*') < 0)) {
            profile = profile.substring(0, 3) + '****' + profile.substring(7, 11);
        }

        return {
            username: profile,
            headImg: user.head_ico ? helpers.image(user.head_ico, 100, 100, 2) : DEFAULT_HEAD_IMG_ICO,
            bindLogin: helpers.urlFormat('/signin', { bindMobile: mobile, bindArea: area })
        };
    });
};

module.exports = {
    validator: {
        verifyMobile,
        verifyEmail,
        isPassword
    },
    getCountry,
    getUserInfo
};