utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. function formatTime(time, format) {
  2. let temp = '0000000000' + time
  3. let len = format.length
  4. return temp.substr(-len)
  5. }
  6. module.exports = {
  7. formatTime: formatTime
  8. }
  9. //判断字符是否为空的方法
  10. function isEmpty(obj) {
  11. return !obj || obj === "null" || obj === "undefined" || obj === "";
  12. }
  13. module.exports.isEmpty = isEmpty
  14. /*
  15. * base64编码(编码,配合encodeURIComponent使用)
  16. * @parm : str 传入的字符串
  17. * 使用:
  18. 1、引入util.js(路径更改) :const util = require('../../utils/util.js');
  19. 2、util.base64_encode(util.utf16to8('base64 编码'));
  20. */
  21. function base64_encode(str) {
  22. //下面是64个基本的编码
  23. var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  24. var out, i, len;
  25. var c1, c2, c3;
  26. len = str.length;
  27. i = 0;
  28. out = "";
  29. while (i < len) {
  30. c1 = str.charCodeAt(i++) & 0xff;
  31. if (i == len) {
  32. out += base64EncodeChars.charAt(c1 >> 2);
  33. out += base64EncodeChars.charAt((c1 & 0x3) << 4);
  34. out += "==";
  35. break;
  36. }
  37. c2 = str.charCodeAt(i++);
  38. if (i == len) {
  39. out += base64EncodeChars.charAt(c1 >> 2);
  40. out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  41. out += base64EncodeChars.charAt((c2 & 0xF) << 2);
  42. out += "=";
  43. break;
  44. }
  45. c3 = str.charCodeAt(i++);
  46. out += base64EncodeChars.charAt(c1 >> 2);
  47. out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  48. out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  49. out += base64EncodeChars.charAt(c3 & 0x3F);
  50. }
  51. return out;
  52. }
  53. /*
  54. * base64编码(编码,配合encodeURIComponent使用)
  55. * @parm : str 传入的字符串
  56. */
  57. function utf16to8(str) {
  58. var out, i, len, c;
  59. out = "";
  60. len = str.length;
  61. for (i = 0; i < len; i++) {
  62. c = str.charCodeAt(i);
  63. if ((c >= 0x0001) && (c <= 0x007F)) {
  64. out += str.charAt(i);
  65. } else if (c > 0x07FF) {
  66. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  67. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  68. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  69. } else {
  70. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  71. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  72. }
  73. }
  74. return out;
  75. }
  76. /*
  77. * base64解码(配合decodeURIComponent使用)
  78. * @parm : input 传入的字符串
  79. * 使用:
  80. 1、引入util.js(路径更改) :const util = require('../../utils/util.js');
  81. 2、util.base64_decode('YmFzZTY0IOe8lueggQ==');
  82. */
  83. function base64_decode(input) {
  84. var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  85. var output = "";
  86. var chr1, chr2, chr3;
  87. var enc1, enc2, enc3, enc4;
  88. var i = 0;
  89. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  90. while (i < input.length) {
  91. enc1 = base64EncodeChars.indexOf(input.charAt(i++));
  92. enc2 = base64EncodeChars.indexOf(input.charAt(i++));
  93. enc3 = base64EncodeChars.indexOf(input.charAt(i++));
  94. enc4 = base64EncodeChars.indexOf(input.charAt(i++));
  95. chr1 = (enc1 << 2) | (enc2 >> 4);
  96. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  97. chr3 = ((enc3 & 3) << 6) | enc4;
  98. output = output + String.fromCharCode(chr1);
  99. if (enc3 != 64) {
  100. output = output + String.fromCharCode(chr2);
  101. }
  102. if (enc4 != 64) {
  103. output = output + String.fromCharCode(chr3);
  104. }
  105. }
  106. return utf8_decode(output);
  107. }
  108. /*
  109. * utf-8解码
  110. * @parm : utftext 传入的字符串
  111. */
  112. function utf8_decode(utftext) {
  113. var string = '';
  114. let i = 0;
  115. let c = 0;
  116. let c1 = 0;
  117. let c2 = 0;
  118. while (i < utftext.length) {
  119. c = utftext.charCodeAt(i);
  120. if (c < 128) {
  121. string += String.fromCharCode(c);
  122. i++;
  123. } else if ((c > 191) && (c < 224)) {
  124. c1 = utftext.charCodeAt(i + 1);
  125. string += String.fromCharCode(((c & 31) << 6) | (c1 & 63));
  126. i += 2;
  127. } else {
  128. c1 = utftext.charCodeAt(i + 1);
  129. c2 = utftext.charCodeAt(i + 2);
  130. string += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63));
  131. i += 3;
  132. }
  133. }
  134. return string;
  135. }
  136. /*
  137. * base64编码函数封装
  138. * @parm: str(传入要编成base64的内容)
  139. * 使用:
  140. 1、引入util.js(路径更改) :const util = require('../../utils/util.js');
  141. 2、util.baseEncode('base64 编码');
  142. */
  143. function baseEncode(str) {
  144. return base64_encode(utf16to8(str));
  145. }
  146. /*
  147. * base64解码函数封装
  148. * @parm: str(传入要解为正常字体)
  149. * 使用:
  150. 1、引入util.js(路径更改) :const util = require('../../utils/util.js');
  151. 2、util.baseDecode(util.baseEncode('base64 编码'))
  152. */
  153. function baseDecode(str) {
  154. return base64_decode(str);
  155. }// 抛出函数使用
  156. module.exports.baseEncode = baseEncode
  157. module.exports.baseDecode = baseDecode