passport-helper.js 5.22 KB
/**
 * Created by TaoHuang on 2016/6/20.
 */


'use strict';

const helpers = global.yoho.helpers;
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');

const indexService = require('./index-service');

const getLeftBannerAsync = (resourceCode) => {
    const DEFAULT_VALUE = {
        img: 'http://img12.static.yhbimg.com/' +
        'yhb-img01/2015/12/01/07/020a0b6e7ff908d0c2bc4045b4fef42b9f.png?imageView/2/w/252/h/190',
        url: ''
    };

    return co(function * () {
        let resource = yield indexService.getResourceAsync(resourceCode);

        if (_.isEmpty(resource)) {
            return DEFAULT_VALUE;
        }

        let value = {};

        // 有点问题 // passport model 58
        value.img = helpers.image(resource[0].data[0].src, 252, 190);
        value.url = resource[0].data[0].url;

        return value;
    })();
};

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: '中国香港'
        }
    ];
};

/**
 * 验证邮箱是否合法
 *
 * @param string email
 * @return boolean
 */
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);
};

/**
 * 各国手机号规则
 */
function _areaMobileVerify(phone, area) {
    area = area || '86';
    phone = phone.trim();

    let verify = {
        86: {
            name: '中国',
            match: /^1[3|4|5|8|7][0-9]{9}$/.test(phone)
        },
        852: {
            name: '中国香港',
            match: /^[9|6|5][0-9]{7}$/.test(phone)
        },
        853: {
            name: '中国澳门',
            match: /^[0-9]{8}$/.test(phone)
        },
        886: {
            name: '中国台湾',
            match: /^[0-9]{10}$/.test(phone)
        },
        65: {
            name: '新加坡',
            match: /^[9|8][0-9]{7}$/.test(phone)
        },
        60: {
            name: '马来西亚',
            match: /^1[1|2|3|4|6|7|9][0-9]{8}$/.test(phone)
        },
        1: {
            name: '加拿大&美国',
            match: /^[0-9]{10}$/.test(phone)
        },
        82: {
            name: '韩国',
            match: /^01[0-9]{9}$/.test(phone)
        },
        44: {
            name: '英国',
            match: /^7[7|8|9][0-9]{8}$/.test(phone)
        },
        81: {
            name: '日本',
            match: /^0[9|8|7][0-9]{9}$/.test(phone)
        },
        61: {
            name: '澳大利亚',
            match: /^[0-9]{11}$/.test(phone)
        }
    };

    if (verify[area]) {
        return verify[area].match;
    } else {
        return false;
    }
}

/**
 * 验证国际手机号是否合法
 */
const isAreaMobile = areaMobile => {
    if (!areaMobile) {
        return false;
    }

    let mobile = {
        area: '86',
        phone: ''
    };

    let splitMobile = areaMobile.split('-');

    if (splitMobile.length === 2) {
        mobile.area = splitMobile[0];
        mobile.phone = splitMobile[1];
    } else {
        mobile.phone = splitMobile[0];
    }

    return _areaMobileVerify(mobile.phone, mobile.area);
};

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

    return /^1[3|4|5|8|7][0-9]{9}$/.test(phone);
};

const verifyPassword = password => {
    if (!password) {
        return false;
    }

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

const makeAreaMobile = (area, mobile) => {
    if (!area || area === '86') {
        return mobile;
    }

    return `${area}-${mobile}`;
};

const isPassword = pwd => {
    if (!pwd) {
        return false;
    }

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

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

module.exports = {
    validator: {
        verifyPassword,
        verifyMobile,
        isAreaMobile,
        verifyEmail,
        isPassword
    },
    makeAreaMobile,
    getCountry,
    getLeftBannerAsync
};