index.js 2.47 KB
Newer Older
1
import jsCookie from "js-cookie";
.  
baiguangyao committed
2

zhanghaozhe committed
3 4 5 6 7 8

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;
9 10 11 12 13 14 15 16 17 18 19
}

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
20 21 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
//加载网易易盾辅助函数
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 {
zhanghaozhe committed
56
        const url = '//cstaticdun.126.net/load.min.js' + '?t=' + getTimestamp(1 * 60 * 1000)
zhanghaozhe committed
57 58 59 60 61 62
        loadScript(url, cb)
    }

}


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

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
79

zhanghaozhe committed
80 81 82 83 84 85 86 87
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)
    }
88 89 90 91 92 93 94 95 96 97
})()

const isLogin = (function () {
    return jsCookie.get('uid') && jsCookie.get('token')
})()


export { default as http } from './http'
export { default as api } from './api'
export { html, initCaptcha, validateTel, validateEmail, browser, isLogin }