ts

  1. /***
  2. * @Description:
  3. * @Author: Harry
  4. * @Date: 2021-12-03 20:01:23
  5. * @Url: https://u.mr90.top
  6. * @github: https://github.com/rr210
  7. * @LastEditTime: 2021-12-03 20:01:23
  8. * @LastEditors: Harry
  9. */
  10. class Base64 {
  11. _keyStr: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  12. constructor() {
  13. }
  14. encode = (input: string) => {
  15. var output = "";
  16. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  17. var i = 0;
  18. input = this._utf8_encode(input);
  19. while (i < input.length) {
  20. chr1 = input.charCodeAt(i++);
  21. chr2 = input.charCodeAt(i++);
  22. chr3 = input.charCodeAt(i++);
  23. enc1 = chr1 >> 2;
  24. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  25. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  26. enc4 = chr3 & 63;
  27. if (isNaN(chr2)) {
  28. enc3 = enc4 = 64;
  29. } else if (isNaN(chr3)) {
  30. enc4 = 64;
  31. }
  32. output = output +
  33. this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  34. this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  35. }
  36. return output;
  37. }
  38. decode = (input: string) => {
  39. var output = "";
  40. var chr1, chr2, chr3;
  41. var enc1, enc2, enc3, enc4;
  42. var i = 0;
  43. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  44. while (i < input.length) {
  45. enc1 = this._keyStr.indexOf(input.charAt(i++));
  46. enc2 = this._keyStr.indexOf(input.charAt(i++));
  47. enc3 = this._keyStr.indexOf(input.charAt(i++));
  48. enc4 = this._keyStr.indexOf(input.charAt(i++));
  49. chr1 = (enc1 << 2) | (enc2 >> 4);
  50. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  51. chr3 = ((enc3 & 3) << 6) | enc4;
  52. output = output + String.fromCharCode(chr1);
  53. if (enc3 != 64) {
  54. output = output + String.fromCharCode(chr2);
  55. }
  56. if (enc4 != 64) {
  57. output = output + String.fromCharCode(chr3);
  58. }
  59. }
  60. output = this._utf8_decode(output);
  61. return output;
  62. }
  63. _utf8_encode = function (string: string) {
  64. string = string.replace(/\r\n/g, "\n");
  65. var utftext = "";
  66. for (var n = 0; n < string.length; n++) {
  67. var c = string.charCodeAt(n);
  68. if (c < 128) {
  69. utftext += String.fromCharCode(c);
  70. } else if ((c > 127) && (c < 2048)) {
  71. utftext += String.fromCharCode((c >> 6) | 192);
  72. utftext += String.fromCharCode((c & 63) | 128);
  73. } else {
  74. utftext += String.fromCharCode((c >> 12) | 224);
  75. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  76. utftext += String.fromCharCode((c & 63) | 128);
  77. }
  78. }
  79. return utftext;
  80. }
  81. _utf8_decode = function (utftext: string) {
  82. var string = "";
  83. var i = 0;
  84. let c = 0
  85. let c1 = 0;
  86. let c2 = 0;
  87. // console.log(object);
  88. while (i < utftext.length) {
  89. c = utftext.charCodeAt(i);
  90. if (c < 128) {
  91. string += String.fromCharCode(c);
  92. i++;
  93. } else if ((c > 191) && (c < 224)) {
  94. c2 = utftext.charCodeAt(i + 1);
  95. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  96. i += 2;
  97. } else {
  98. c2 = utftext.charCodeAt(i + 1);
  99. let c3 = utftext.charCodeAt(i + 2);
  100. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  101. i += 3;
  102. }
  103. }
  104. return string;
  105. }
  106. }
  107. export default Base64
  1. /*
  2. * @Description:
  3. * @Author: Harry
  4. * @Date: 2021-12-02 22:33:52
  5. * @Url: https://u.mr90.top
  6. * @github: https://github.com/rr210
  7. * @LastEditTime: 2021-12-18 20:53:35
  8. * @LastEditors: Harry
  9. */
  10. const setCookie: Function = function (name: string, value: string) {
  11. let expdate = new Date(); //初始化时间
  12. expdate.setTime(expdate.getTime() + 30 * 60 * 1000); //时间单位毫秒
  13. document.cookie = name + "=" + escape(value) + ";expires=" + expdate.toUTCString() + ";path=/";
  14. // document.cookie = name + "=" + value + ";path=/"; // 时间默认为当前会话可以不要,但路径要填写,因为JS的默认路径是当前页,如果不填,此cookie只在当前页面生效!
  15. }
  16. const getCookie: Function = function (a: string) {
  17. // console.log(a)
  18. let d;
  19. let b = document.cookie;
  20. let c = b.split(";");
  21. // console.log(c);
  22. // console.log(escape('YWRtaW4='));
  23. for (let e = 0; e < c.length; e++) {
  24. let f = c[e].split("=");
  25. if (a == f[0].toString().trim()) {
  26. d = f[1];
  27. break;
  28. }
  29. } if (void 0 == d || null == d) {
  30. return "";
  31. }
  32. else {
  33. let g = unescape(d.trim());
  34. return g;
  35. }
  36. }
  37. const delCookie: Function = function (a: string) {
  38. let b = new Date(0).toUTCString();
  39. document.cookie = a + "=;expires=" + b + ";path=/";
  40. }
  41. export default {
  42. setCookie,
  43. getCookie,
  44. delCookie
  45. }

js

  1. /***
  2. * @Description:
  3. * @Author: Harry
  4. * @Date: 2021-12-03 20:01:23
  5. * @Url: https://u.mr90.top
  6. * @github: https://github.com/rr210
  7. * @LastEditTime: 2021-12-27 16:47:53
  8. * @LastEditors: Harry
  9. */
  10. class Base64 {
  11. _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  12. constructor(name) {
  13. this._keyStr = name
  14. }
  15. encode = (input) => {
  16. var output = ''
  17. var chr1, chr2, chr3, enc1, enc2, enc3, enc4
  18. var i = 0
  19. input = this._utf8_encode(input)
  20. while (i < input.length) {
  21. chr1 = input.charCodeAt(i++)
  22. chr2 = input.charCodeAt(i++)
  23. chr3 = input.charCodeAt(i++)
  24. enc1 = chr1 >> 2
  25. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
  26. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
  27. enc4 = chr3 & 63
  28. if (isNaN(chr2)) {
  29. enc3 = enc4 = 64
  30. } else if (isNaN(chr3)) {
  31. enc4 = 64
  32. }
  33. output = output +
  34. this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  35. this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4)
  36. }
  37. return output
  38. }
  39. decode = (input) => {
  40. var output = ''
  41. var chr1, chr2, chr3
  42. var enc1, enc2, enc3, enc4
  43. var i = 0
  44. // eslint-disable-next-line no-useless-escape
  45. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')
  46. while (i < input.length) {
  47. enc1 = this._keyStr.indexOf(input.charAt(i++))
  48. enc2 = this._keyStr.indexOf(input.charAt(i++))
  49. enc3 = this._keyStr.indexOf(input.charAt(i++))
  50. enc4 = this._keyStr.indexOf(input.charAt(i++))
  51. chr1 = (enc1 << 2) | (enc2 >> 4)
  52. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
  53. chr3 = ((enc3 & 3) << 6) | enc4
  54. output = output + String.fromCharCode(chr1)
  55. if (enc3 !== 64) {
  56. output = output + String.fromCharCode(chr2)
  57. }
  58. if (enc4 !== 64) {
  59. output = output + String.fromCharCode(chr3)
  60. }
  61. }
  62. output = this._utf8_decode(output)
  63. return output
  64. }
  65. // eslint-disable-next-line camelcase
  66. _utf8_encode = function (string) {
  67. string = string.replace(/\r\n/g, '\n')
  68. var utftext = ''
  69. for (var n = 0; n < string.length; n++) {
  70. var c = string.charCodeAt(n)
  71. if (c < 128) {
  72. utftext += String.fromCharCode(c)
  73. } else if ((c > 127) && (c < 2048)) {
  74. utftext += String.fromCharCode((c >> 6) | 192)
  75. utftext += String.fromCharCode((c & 63) | 128)
  76. } else {
  77. utftext += String.fromCharCode((c >> 12) | 224)
  78. utftext += String.fromCharCode(((c >> 6) & 63) | 128)
  79. utftext += String.fromCharCode((c & 63) | 128)
  80. }
  81. }
  82. return utftext
  83. }
  84. // eslint-disable-next-line camelcase
  85. _utf8_decode = function (utftext) {
  86. var string = ''
  87. var i = 0
  88. let c = 0
  89. let c2 = 0
  90. // console.log(object);
  91. while (i < utftext.length) {
  92. c = utftext.charCodeAt(i)
  93. if (c < 128) {
  94. string += String.fromCharCode(c)
  95. i++
  96. } else if ((c > 191) && (c < 224)) {
  97. c2 = utftext.charCodeAt(i + 1)
  98. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63))
  99. i += 2
  100. } else {
  101. c2 = utftext.charCodeAt(i + 1)
  102. const c3 = utftext.charCodeAt(i + 2)
  103. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
  104. i += 3
  105. }
  106. }
  107. return string
  108. }
  109. }
  110. export default Base64
  1. /*
  2. * @Description:
  3. * @Author: Harry
  4. * @Date: 2021-12-02 22:33:52
  5. * @Url: https://u.mr90.top
  6. * @github: https://github.com/rr210
  7. * @LastEditTime: 2021-12-18 20:53:35
  8. * @LastEditors: Harry
  9. */
  10. const setCookie = function (name, value) {
  11. const expdate = new Date() // 初始化时间
  12. expdate.setTime(expdate.getTime() + 30 * 60 * 1000) // 时间单位毫秒
  13. document.cookie = name + '=' + escape(value) + ';expires=' + expdate.toUTCString() + ';path=/'
  14. // document.cookie = name + "=" + value + ";path=/"; // 时间默认为当前会话可以不要,但路径要填写,因为JS的默认路径是当前页,如果不填,此cookie只在当前页面生效!
  15. }
  16. const getCookie = function (a) {
  17. // console.log(a)
  18. let d
  19. const b = document.cookie
  20. const c = b.split(';')
  21. // console.log(c);
  22. // console.log(escape('YWRtaW4='));
  23. for (let e = 0; e < c.length; e++) {
  24. const f = c[e].split('=')
  25. if (a === f[0].toString().trim()) {
  26. d = f[1]
  27. break
  28. }
  29. // eslint-disable-next-line no-void
  30. } if (void 0 === d || d == null) {
  31. return ''
  32. } else {
  33. const g = unescape(d.trim())
  34. return g
  35. }
  36. }
  37. const delCookie = function (a) {
  38. const b = new Date(0).toUTCString()
  39. document.cookie = a + '=;expires=' + b + ';path=/'
  40. }
  41. export default {
  42. setCookie,
  43. getCookie,
  44. delCookie
  45. }