index.js 3.41 KB
Newer Older
1
import jsCookie from "js-cookie";
zhanghaozhe committed
2 3 4 5
import {
  differenceInDays,
  differenceInHours,
  differenceInMinutes,
zhanghaozhe committed
6
  differenceInSeconds,
zhanghaozhe committed
7
} 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
const html = content => ({
zhanghaozhe committed
18
  __html: htmlDecode(content),
zhanghaozhe committed
19
})
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
  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'),
zhanghaozhe committed
105
    isIOSApp: /iPhone/i.test(ua) && getParam('version'),
zhanghaozhe committed
106
  }
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
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,
zhanghaozhe committed
122
    s,
zhanghaozhe committed
123 124
  }
}
125

zhanghaozhe committed
126 127 128 129
const isValidUrl = (str) => {
  return /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/.test(str)
}

zhanghaozhe committed
130 131

export {
zhanghaozhe committed
132
  default as http,
zhanghaozhe committed
133 134 135
}
  from './http'
export {
zhanghaozhe committed
136
  default as wxShare,
zhanghaozhe committed
137 138 139 140 141
}
  from './wechat/share'
export {
  html,
  initCaptcha,
zhanghaozhe committed
142
  initCaptchaNC,
zhanghaozhe committed
143 144 145 146
  validateTel,
  validateEmail,
  browser,
  isLogin,
zhanghaozhe committed
147
  dateCountDown,
zhanghaozhe committed
148
  isValidUrl,
zhanghaozhe committed
149 150
  loadScript,
  getTimestamp,
zhanghaozhe committed
151 152
}
export {
zhanghaozhe committed
153
  default as SendMessageToApp,
zhanghaozhe committed
154 155
}
  from './app'