index.js 2.33 KB
Newer Older
zhanghaozhe committed
1
export { default as http } from './http'
zhanghaozhe committed
2
export { default as api } from './api'
zhanghaozhe committed
3
export { html, initCaptcha, validateTel, validateEmail,browser }
.  
baiguangyao committed
4

zhanghaozhe committed
5 6 7 8 9 10

export const getParam = (key, str) => {
    const _s = str ? str : location.href;
    const re = new RegExp(`(?:\\?|#|&)(${key})=([^=&#\\?]+)`, 'ig');
    let found;
    return (found = re.exec(_s)) ? found[2] : null;
11 12 13 14 15 16 17 18 19 20 21
}

const html = content => ({__html: htmlDecode(content)})

const htmlDecode = content => {
    let e = document.createElement('div');
    e.innerHTML = content;
    return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}


zhanghaozhe committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
//加载网易易盾辅助函数
function getTimestamp(msec) {
    msec = !msec && msec !== 0 ? msec : 1
    return parseInt((new Date()).valueOf() / msec, 10)
}

function loadScript(src, cb) {
    var head = document.head || document.getElementsByTagName('head')[0]
    var script = document.createElement('script')

    cb = cb || function () {
    }

    script.type = 'text/javascript'
    script.src = src

    if (!('onload' in script)) {
        script.onreadystatechange = function () {
            if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
            this.onreadystatechange = null
            cb(script)
        }
    }

    script.onload = function () {
        this.onload = null
        cb(script)
    }

    head.appendChild(script)
}

function initCaptcha(cb) {
    if (window.initNECaptcha) {
        cb()
    } else {
        const url = 'http://cstaticdun.126.net/load.min.js' + '?t=' + getTimestamp(1 * 60 * 1000)
        loadScript(url, cb)
    }

}


xuzhenghua committed
65 66 67 68 69 70 71
export const is_weixin = () => {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    }
    return false;
}
zhanghaozhe committed
72 73 74 75 76 77 78 79 80

function validateTel(tel) {
    return /^1[3-9](\d{9})$/.test(tel)
}

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(String(email).toLowerCase());
}
zhanghaozhe committed
81

zhanghaozhe committed
82 83 84 85 86 87 88 89 90
const browser = (function () {
    const ua = navigator.userAgent
    return {
        isWeixin: /MicroMessenger/i.test(ua),
        isAndroid: /Android/i.test(ua),
        isIOS: /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(ua),
        isIPad: /iPad/i.test(ua)
    }
})()