| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import AES from 'crypto-js'
- //加密方法
- export function Encrypt(word) {
- //十六位十六进制数作为密钥
- const key = AES.enc.Utf8.parse('0123456789ASDFGH')
- //十六位十六进制数作为密钥偏移量
- const iv = AES.enc.Utf8.parse('ASDFGH0123456789')
- const src = AES.enc.Utf8.parse(word)
- const encrypted = AES.AES.encrypt(
- src,
- key,
- {
- iv: iv,
- mode: AES.mode.CBC,
- padding: AES.pad.Pkcs7
- })
- return encrypted.ciphertext.toString().toUpperCase()
- }
- //解密方法
- export function Decrypt(word) {
- const encryptedHexStr = AES.enc.Hex.parse(word)
- const src = AES.enc.Base64.stringify(encryptedHexStr)
- const decrypt = AES.AES.decrypt(
- src,
- key,
- {
- iv: iv,
- mode: AES.mode.CBC,
- padding: AES.pad.Pkcs7
- })
- const decryptedStr = decrypt.toString(AES.enc.Utf8)
- return decryptedStr.toString()
- }
- // 节流
- export function throttle(fn, time) {
- let timer = null
- time = time || 1000
- return function(...args) {
- if (timer) {
- return
- }
- const _this = this
- timer = setTimeout(() => {
- timer = null
- }, time)
- fn.apply(_this, args)
- }
- }
- // 防抖
- export function debounce(fn, time) {
- time = time || 200
- // 定时器
- let timer = null
- return function(...args) {
- var _this = this
- if (timer) {
- clearTimeout(timer)
- }
- timer = setTimeout(function() {
- timer = null
- fn.apply(_this, args)
- }, time)
- }
- }
- export function debounceMax(func, wait, immediate = false) {
- let timeout
- return function() {
- let context = this
- let args = arguments
- if (timeout) clearTimeout(timeout)
- if (immediate) {
- var callNow = !timeout
- timeout = setTimeout(() => {
- timeout = null
- }, wait)
- if (callNow) func.apply(context, args)
- } else {
- timeout = setTimeout(function() {
- func.apply(context, args)
- }, wait)
- }
- }
- }
- export function getClientIP(onNewIP) {
- let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection
- let pc = new MyPeerConnection({
- iceServers: []
- })
- let noop = () => {
- }
- let localIPs = {}
- let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g
- let iterateIP = (ip) => {
- if (!localIPs[ip]) onNewIP(ip)
- localIPs[ip] = true
- }
- pc.createDataChannel('')
- pc.createOffer().then((sdp) => {
- sdp.sdp.split('\n').forEach(function(line) {
- if (line.indexOf('candidate') < 0) return
- line.match(ipRegex).forEach(iterateIP)
- })
- pc.setLocalDescription(sdp, noop, noop)
- }).catch((reason) => {
- })
- pc.onicecandidate = (ice) => {
- if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return
- ice.candidate.candidate.match(ipRegex).forEach(iterateIP)
- }
- }
|