utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. export default {
  2. /**
  3. * @method guid() 生成uuid
  4. */
  5. guid: () => {
  6. function s4() {
  7. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  8. }
  9. return (s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4())
  10. },
  11. // 节流函数
  12. throttle(fn, delay) {
  13. let timer = null;
  14. return function () {
  15. if (timer) {
  16. return;
  17. }
  18. timer = setTimeout(() => {
  19. fn.apply(this, arguments);
  20. timer = null;
  21. }, delay);
  22. };
  23. },
  24. HTMLDecode(text) {
  25. // let obj = {
  26. // '&lt;': '<',
  27. // '&gt;': '>',
  28. // '&quot;': '"',
  29. // '&amp;': "&",
  30. // "&#39;": "'"
  31. // }
  32. // for(let [key,value] of Object.entries(obj)) {
  33. // text = text.replace(new RegExp(key,'g'), value)
  34. // }
  35. // return text
  36. let temp = document.createElement("div")
  37. temp.innerHTML = text
  38. let output = temp.innerText || temp.textContent
  39. temp = null
  40. return output
  41. },
  42. //AES encode
  43. rEncode(key, str) {
  44. if (!str) return ''
  45. let key1 = CryptoJS.enc.Utf8.parse(key)
  46. let e = CryptoJS.AES.encrypt(str, key1, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  47. return e.ciphertext.toString()
  48. },
  49. //AES decode
  50. rDecode(key, rStr) {
  51. if (!rStr) return ''
  52. let key1 = CryptoJS.enc.Utf8.parse(key)
  53. let a = CryptoJS.enc.Hex.parse(rStr)
  54. let b = CryptoJS.enc.Base64.stringify(a)
  55. let c = CryptoJS.AES.decrypt(b, key1, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  56. return c.toString(CryptoJS.enc.Utf8)
  57. },
  58. //des encode
  59. rEncodeDes(key, str) {
  60. if (!str) return ''
  61. let key1 = CryptoJS.enc.Utf8.parse(key)
  62. let e = CryptoJS.DES.encrypt(str, key1, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  63. return e.ciphertext.toString()
  64. },
  65. //DES decode
  66. rDecodeDes(key, rStr) {
  67. if (!rStr) return ''
  68. let key1 = CryptoJS.enc.Utf8.parse(key)
  69. let a = CryptoJS.enc.Hex.parse(rStr)
  70. let b = CryptoJS.enc.Base64.stringify(a)
  71. let c = CryptoJS.DES.decrypt(b, key1, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  72. return c.toString(CryptoJS.enc.Utf8)
  73. },
  74. /**
  75. * @method decodeObj 对编码后的特殊字符进行解码
  76. */
  77. decodeObj(obj) {
  78. if (typeof obj == 'string') {
  79. return this.HTMLDecode(obj)
  80. } else if (typeof obj == 'object' && !(obj instanceof Array) && obj != null) {
  81. //object
  82. let temp = {}
  83. for (let [key, value] of Object.entries(obj)) {
  84. temp[key] = this.decodeObj(value)
  85. }
  86. return temp
  87. } else if (typeof obj == 'object' && obj instanceof Array) {
  88. // array
  89. return obj.map(item => {
  90. return this.decodeObj(item)
  91. })
  92. } else {
  93. return obj
  94. }
  95. },
  96. }