123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- export default {
- /**
- * @method guid() 生成uuid
- */
- guid: () => {
- function s4() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
- }
- return (s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4())
- },
- // 节流函数
- throttle(fn, delay) {
- let timer = null;
- return function () {
- if (timer) {
- return;
- }
- timer = setTimeout(() => {
- fn.apply(this, arguments);
- timer = null;
- }, delay);
- };
- },
- HTMLDecode(text) {
- // let obj = {
- // '<': '<',
- // '>': '>',
- // '"': '"',
- // '&': "&",
- // "'": "'"
- // }
- // for(let [key,value] of Object.entries(obj)) {
- // text = text.replace(new RegExp(key,'g'), value)
- // }
- // return text
- let temp = document.createElement("div")
- temp.innerHTML = text
- let output = temp.innerText || temp.textContent
- temp = null
- return output
- },
- //AES encode
- rEncode(key, str) {
- if (!str) return ''
- let key1 = CryptoJS.enc.Utf8.parse(key)
- let e = CryptoJS.AES.encrypt(str, key1, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
- return e.ciphertext.toString()
- },
- //AES decode
- rDecode(key, rStr) {
- if (!rStr) return ''
- let key1 = CryptoJS.enc.Utf8.parse(key)
- let a = CryptoJS.enc.Hex.parse(rStr)
- let b = CryptoJS.enc.Base64.stringify(a)
- let c = CryptoJS.AES.decrypt(b, key1, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
- return c.toString(CryptoJS.enc.Utf8)
- },
- //des encode
- rEncodeDes(key, str) {
- if (!str) return ''
- let key1 = CryptoJS.enc.Utf8.parse(key)
- let e = CryptoJS.DES.encrypt(str, key1, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
- return e.ciphertext.toString()
- },
- //DES decode
- rDecodeDes(key, rStr) {
- if (!rStr) return ''
- let key1 = CryptoJS.enc.Utf8.parse(key)
- let a = CryptoJS.enc.Hex.parse(rStr)
- let b = CryptoJS.enc.Base64.stringify(a)
- let c = CryptoJS.DES.decrypt(b, key1, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
- return c.toString(CryptoJS.enc.Utf8)
- },
- /**
- * @method decodeObj 对编码后的特殊字符进行解码
- */
- decodeObj(obj) {
- if (typeof obj == 'string') {
- return this.HTMLDecode(obj)
- } else if (typeof obj == 'object' && !(obj instanceof Array) && obj != null) {
- //object
- let temp = {}
- for (let [key, value] of Object.entries(obj)) {
- temp[key] = this.decodeObj(value)
- }
- return temp
- } else if (typeof obj == 'object' && obj instanceof Array) {
- // array
- return obj.map(item => {
- return this.decodeObj(item)
- })
- } else {
- return obj
- }
- },
- }
|