util.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // 日期格式化
  2. export function parseTime(time, pattern) {
  3. if (arguments.length === 0 || !time) {
  4. return null
  5. }
  6. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  7. let date
  8. if (typeof time === 'object') {
  9. date = time
  10. } else {
  11. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  12. time = parseInt(time)
  13. } else if (typeof time === 'string') {
  14. time = time.replace(new RegExp(/-/gm), '/');
  15. }
  16. if ((typeof time === 'number') && (time.toString().length === 10)) {
  17. time = time * 1000
  18. }
  19. date = new Date(time)
  20. }
  21. const formatObj = {
  22. y: date.getFullYear(),
  23. m: date.getMonth() + 1,
  24. d: date.getDate(),
  25. h: date.getHours(),
  26. i: date.getMinutes(),
  27. s: date.getSeconds(),
  28. a: date.getDay()
  29. }
  30. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  31. let value = formatObj[key]
  32. // Note: getDay() returns 0 on Sunday
  33. if (key === 'a') {
  34. return ['日', '一', '二', '三', '四', '五', '六'][value]
  35. }
  36. if (result.length > 0 && value < 10) {
  37. value = '0' + value
  38. }
  39. return value || 0
  40. })
  41. return time_str
  42. }
  43. /**
  44. * 解决两个数相加精度丢失问题
  45. * @param a
  46. * @param b
  47. * @returns {Number}
  48. */
  49. export function floatAdd(a, b) {
  50. var c, d, e;
  51. if (undefined == a || null == a || "" == a || isNaN(a)) {
  52. a = 0;
  53. }
  54. if (undefined == b || null == b || "" == b || isNaN(b)) {
  55. b = 0;
  56. }
  57. try {
  58. c = a.toString().split(".")[1].length;
  59. } catch (f) {
  60. c = 0;
  61. }
  62. try {
  63. d = b.toString().split(".")[1].length;
  64. } catch (f) {
  65. d = 0;
  66. }
  67. e = Math.pow(10, Math.max(c, d));
  68. return (floatMul(a, e) + floatMul(b, e)) / e;
  69. }
  70. //除法去除精度损失
  71. export function accDiv(arg1, arg2) {
  72. if (!arg1 || !arg2) {
  73. if (arg2 == 0) {
  74. return Infinity
  75. }
  76. return arg1 / arg2
  77. }
  78. var t1 = 0,
  79. t2 = 0,
  80. r1, r2;
  81. try {
  82. t1 = arg1.toString().split(".")[1].length
  83. } catch (e) {}
  84. try {
  85. t2 = arg2.toString().split(".")[1].length
  86. } catch (e) {}
  87. r1 = Number(arg1.toString().replace(".", ""));
  88. r2 = Number(arg2.toString().replace(".", ""));
  89. if (r2 == 0) {
  90. return Infinity;
  91. } else {
  92. return (r1 / r2) * Math.pow(10, t2 - t1);
  93. }
  94. }
  95. export function numberFormat(number, decimals = 2, dec_point = '.', thousands_sep = ',') {
  96. /*
  97. * 参数说明:
  98. * number:要格式化的数字
  99. * decimals:保留几位小数
  100. * dec_point:小数点符号
  101. * thousands_sep:千分位符号
  102. * */
  103. number = accDiv(number, 100)
  104. number = (number + '').replace(/[^0-9+-Ee.]/g, '');
  105. var n = !isFinite(+number) ? 0 : +number,
  106. prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
  107. sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
  108. dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
  109. s = '',
  110. toFixedFix = function(n, prec) {
  111. var k = Math.pow(10, prec);
  112. return '' + Math.round(n * k) / k;
  113. };
  114. s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  115. var re = /(-?\d+)(\d{3})/;
  116. while (re.test(s[0])) {
  117. s[0] = s[0].replace(re, "$1" + sep + "$2");
  118. }
  119. if ((s[1] || '').length < prec) {
  120. s[1] = s[1] || '';
  121. s[1] += new Array(prec - s[1].length + 1).join('0');
  122. }
  123. return s.join(dec);
  124. }
  125. /**
  126. * 处理富文本里的图片宽度自适应
  127. * 1.去掉img标签里的style、width、height属性
  128. * 2.img标签添加style属性:max-width:100%;height:auto
  129. * 3.修改所有style里的width属性为max-width:100%
  130. * 4.去掉<br/>标签
  131. * @param html
  132. * @returns {void|string|*}
  133. */
  134. export function formatRichText(html) { //控制小程序中图片大小
  135. let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
  136. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  137. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  138. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  139. return match;
  140. });
  141. newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
  142. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
  143. 'max-width:100%;');
  144. return match;
  145. });
  146. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  147. newContent = newContent.replace(/\<img/gi,
  148. '<img style="max-width:100%;height:auto;font-size: 0;margin-top: -5px;"');
  149. return newContent;
  150. }