index.js 3.21 KB
Newer Older
1
import jsCookie from "js-cookie";
zhanghaozhe committed
2 3 4 5 6 7
import {
  differenceInDays,
  differenceInHours,
  differenceInMinutes,
  differenceInSeconds
} from 'date-fns'
.  
baiguangyao committed
8

zhanghaozhe committed
9 10

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

zhanghaozhe committed
17 18 19
const html = content => ({
  __html: htmlDecode(content)
})
20 21

const htmlDecode = content => {
zhanghaozhe committed
22 23 24
  let e = document.createElement('div');
  e.innerHTML = content;
  return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
25 26 27
}


zhanghaozhe committed
28 29
//加载网易易盾辅助函数
function getTimestamp(msec) {
zhanghaozhe committed
30 31
  msec = !msec && msec !== 0 ? msec : 1
  return parseInt((new Date()).valueOf() / msec, 10)
zhanghaozhe committed
32 33 34
}

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

zhanghaozhe committed
38 39
  cb = cb || function () {
  }
zhanghaozhe committed
40

zhanghaozhe committed
41 42
  script.type = 'text/javascript'
  script.src = src
zhanghaozhe committed
43

zhanghaozhe committed
44 45 46 47 48
  if (!('onload' in script)) {
    script.onreadystatechange = function () {
      if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
      this.onreadystatechange = null
      cb(script)
zhanghaozhe committed
49
    }
zhanghaozhe committed
50
  }
zhanghaozhe committed
51

zhanghaozhe committed
52 53 54 55
  script.onload = function () {
    this.onload = null
    cb(script)
  }
zhanghaozhe committed
56

zhanghaozhe committed
57
  head.appendChild(script)
zhanghaozhe committed
58 59 60
}

function initCaptcha(cb) {
zhanghaozhe committed
61 62 63 64 65 66
  if (window.initNECaptcha) {
    cb()
  } else {
    const url = '//cstaticdun.126.net/load.min.js' + '?t=' + getTimestamp(1 * 60 * 1000)
    loadScript(url, cb)
  }
zhanghaozhe committed
67 68 69

}

zhanghaozhe committed
70 71 72 73 74 75 76 77 78
function initCaptchaNC(cb) {
  if (window.NoCaptcha && typeof window.NoCaptcha.init === 'function') {
    cb()
  } else {
    const url = '//g.alicdn.com/sd/nch5/index.js' + '?t=' + getTimestamp(1 * 60 * 1000)
    loadScript(url, cb)
  }
}

zhanghaozhe committed
79

xuzhenghua committed
80
export const is_weixin = () => {
zhanghaozhe committed
81 82 83 84 85
  var ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    return true;
  }
  return false;
xuzhenghua committed
86
}
zhanghaozhe committed
87 88

function validateTel(tel) {
zhanghaozhe committed
89
  return /^1[3-9](\d{9})$/.test(tel)
zhanghaozhe committed
90 91 92
}

function validateEmail(email) {
zhanghaozhe committed
93 94
  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
95
}
zhanghaozhe committed
96

zhanghaozhe committed
97
const browser = (function () {
zhanghaozhe committed
98 99 100 101 102 103 104 105 106
  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),
    isAndroidApp: /Android/i.test(ua) && getParam('version'),
    isIOSApp: /iPhone/i.test(ua) && getParam('version')
  }
107 108 109
})()

const isLogin = (function () {
zhanghaozhe committed
110
  return jsCookie.get('uid') && jsCookie.get('token')
111 112
})()

zhanghaozhe committed
113 114 115 116 117 118 119 120 121 122 123 124
const dateCountDown = (later, earlier) => {
  const d = differenceInDays(later, earlier)
  const h = differenceInHours(later, earlier) % 24
  const m = differenceInMinutes(later, earlier) % 60
  const s = differenceInSeconds(later, earlier) % 60
  return {
    d,
    h,
    m,
    s
  }
}
125

zhanghaozhe committed
126 127 128 129 130 131 132 133 134 135 136 137

export {
  default as http
}
  from './http'
export {
  default as wxShare
}
  from './wechat/share'
export {
  html,
  initCaptcha,
zhanghaozhe committed
138
  initCaptchaNC,
zhanghaozhe committed
139 140 141 142
  validateTel,
  validateEmail,
  browser,
  isLogin,
zhanghaozhe committed
143
  dateCountDown,
zhanghaozhe committed
144 145 146 147 148
}
export {
  default as SendMessageToApp
}
  from './app'