123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // 日期格式化
- export function parseTime(time, pattern) {
- if (arguments.length === 0 || !time) {
- return null
- }
- const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
- let date
- if (typeof time === 'object') {
- date = time
- } else {
- if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
- time = parseInt(time)
- } else if (typeof time === 'string') {
- time = time.replace(new RegExp(/-/gm), '/');
- }
- if ((typeof time === 'number') && (time.toString().length === 10)) {
- time = time * 1000
- }
- date = new Date(time)
- }
- const formatObj = {
- y: date.getFullYear(),
- m: date.getMonth() + 1,
- d: date.getDate(),
- h: date.getHours(),
- i: date.getMinutes(),
- s: date.getSeconds(),
- a: date.getDay()
- }
- const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
- let value = formatObj[key]
- // Note: getDay() returns 0 on Sunday
- if (key === 'a') {
- return ['日', '一', '二', '三', '四', '五', '六'][value]
- }
- if (result.length > 0 && value < 10) {
- value = '0' + value
- }
- return value || 0
- })
- return time_str
- }
- /**
- * 解决两个数相加精度丢失问题
- * @param a
- * @param b
- * @returns {Number}
- */
- export function floatAdd(a, b) {
- var c, d, e;
- if (undefined == a || null == a || "" == a || isNaN(a)) {
- a = 0;
- }
- if (undefined == b || null == b || "" == b || isNaN(b)) {
- b = 0;
- }
- try {
- c = a.toString().split(".")[1].length;
- } catch (f) {
- c = 0;
- }
- try {
- d = b.toString().split(".")[1].length;
- } catch (f) {
- d = 0;
- }
- e = Math.pow(10, Math.max(c, d));
- return (floatMul(a, e) + floatMul(b, e)) / e;
- }
- //除法去除精度损失
- export function accDiv(arg1, arg2) {
- if (!arg1 || !arg2) {
- if (arg2 == 0) {
- return Infinity
- }
- return arg1 / arg2
- }
- var t1 = 0,
- t2 = 0,
- r1, r2;
- try {
- t1 = arg1.toString().split(".")[1].length
- } catch (e) {}
- try {
- t2 = arg2.toString().split(".")[1].length
- } catch (e) {}
- r1 = Number(arg1.toString().replace(".", ""));
- r2 = Number(arg2.toString().replace(".", ""));
- if (r2 == 0) {
- return Infinity;
- } else {
- return (r1 / r2) * Math.pow(10, t2 - t1);
- }
- }
- export function numberFormat(number, decimals = 2, dec_point = '.', thousands_sep = ',') {
- /*
- * 参数说明:
- * number:要格式化的数字
- * decimals:保留几位小数
- * dec_point:小数点符号
- * thousands_sep:千分位符号
- * */
- number = accDiv(number, 100)
- number = (number + '').replace(/[^0-9+-Ee.]/g, '');
- var n = !isFinite(+number) ? 0 : +number,
- prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
- sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
- dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
- s = '',
- toFixedFix = function(n, prec) {
- var k = Math.pow(10, prec);
- return '' + Math.round(n * k) / k;
- };
- s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
- var re = /(-?\d+)(\d{3})/;
- while (re.test(s[0])) {
- s[0] = s[0].replace(re, "$1" + sep + "$2");
- }
- if ((s[1] || '').length < prec) {
- s[1] = s[1] || '';
- s[1] += new Array(prec - s[1].length + 1).join('0');
- }
- return s.join(dec);
- }
- /**
- * 处理富文本里的图片宽度自适应
- * 1.去掉img标签里的style、width、height属性
- * 2.img标签添加style属性:max-width:100%;height:auto
- * 3.修改所有style里的width属性为max-width:100%
- * 4.去掉<br/>标签
- * @param html
- * @returns {void|string|*}
- */
- export function formatRichText(html) { //控制小程序中图片大小
- let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
- match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
- match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
- match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
- return match;
- });
- newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
- match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
- 'max-width:100%;');
- return match;
- });
- newContent = newContent.replace(/<br[^>]*\/>/gi, '');
- newContent = newContent.replace(/\<img/gi,
- '<img style="max-width:100%;height:auto;font-size: 0;margin-top: -5px;"');
- return newContent;
- }
|