推广小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

2 months ago
  1. export const Base64 = {
  2. _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  3. encode: function(input) {
  4. var output = "";
  5. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  6. var i = 0;
  7. input = Base64._utf8_encode(input);
  8. while (i < input.length) {
  9. chr1 = input.charCodeAt(i++);
  10. chr2 = input.charCodeAt(i++);
  11. chr3 = input.charCodeAt(i++);
  12. enc1 = chr1 >> 2;
  13. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  14. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  15. enc4 = chr3 & 63;
  16. if (isNaN(chr2)) {
  17. enc3 = enc4 = 64;
  18. } else if (isNaN(chr3)) {
  19. enc4 = 64;
  20. }
  21. output = output +
  22. this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  23. this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  24. }
  25. return output;
  26. },
  27. decode: function(input) {
  28. var output = "";
  29. var chr1, chr2, chr3;
  30. var enc1, enc2, enc3, enc4;
  31. var i = 0;
  32. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  33. while (i < input.length) {
  34. enc1 = this._keyStr.indexOf(input.charAt(i++));
  35. enc2 = this._keyStr.indexOf(input.charAt(i++));
  36. enc3 = this._keyStr.indexOf(input.charAt(i++));
  37. enc4 = this._keyStr.indexOf(input.charAt(i++));
  38. chr1 = (enc1 << 2) | (enc2 >> 4);
  39. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  40. chr3 = ((enc3 & 3) << 6) | enc4;
  41. output = output + String.fromCharCode(chr1);
  42. if (enc3 != 64) {
  43. output = output + String.fromCharCode(chr2);
  44. }
  45. if (enc4 != 64) {
  46. output = output + String.fromCharCode(chr3);
  47. }
  48. }
  49. output = Base64._utf8_decode(output);
  50. return output;
  51. },
  52. _utf8_encode: function(string) {
  53. string = string.replace(/\r\n/g, "\n");
  54. var utftext = "";
  55. for (var n = 0; n < string.length; n++) {
  56. var c = string.charCodeAt(n);
  57. if (c < 128) {
  58. utftext += String.fromCharCode(c);
  59. } else if ((c > 127) && (c < 2048)) {
  60. utftext += String.fromCharCode((c >> 6) | 192);
  61. utftext += String.fromCharCode((c & 63) | 128);
  62. } else {
  63. utftext += String.fromCharCode((c >> 12) | 224);
  64. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  65. utftext += String.fromCharCode((c & 63) | 128);
  66. }
  67. }
  68. return utftext;
  69. },
  70. _utf8_decode: function(utftext) {
  71. var string = "";
  72. var i = 0;
  73. var c = c1 = c2 = 0;
  74. while (i < utftext.length) {
  75. c = utftext.charCodeAt(i);
  76. if (c < 128) {
  77. string += String.fromCharCode(c);
  78. i++;
  79. } else if ((c > 191) && (c < 224)) {
  80. c2 = utftext.charCodeAt(i + 1);
  81. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  82. i += 2;
  83. } else {
  84. c2 = utftext.charCodeAt(i + 1);
  85. c3 = utftext.charCodeAt(i + 2);
  86. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  87. i += 3;
  88. }
  89. }
  90. return string;
  91. }
  92. }