export function formatnumber(num, digits = 2, type = "", numstr = ",") {
// digits >>> 小数位取值数
// type >>> floor:向下取整;ceil:向上取整;0:取整数的部分;1:取小数的部分
// numstr >>> 千位分隔符
if (type == "floor") {
num = Math.floor(num)
} else if (type == "ceil") {
num = Math.ceil(num)
} else {
num = Number(num).toFixed(digits)
}
const numa = (+num || 0).toString().split('.')
let renum = (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, numstr))
var digNum = ""
if (numa.length > 1) {
digNum = numa[1]
if (numa[1].length < digits) {
renum += '0'
digNum += '0'
}
if (type === 1) {
return digNum
}
} else if (digits > 0) {
renum = renum + '.'
for (let i = 1; i <= digits; i++) {
renum += '0'
digNum += '0'
}
if (type === 1) {
return digNum
}
}
if (type === 0) {
return numa[0]
} else {
return renum
}
}
export function getPriceNumber(num, type = 0) {
if (!num) num = 0
num = Number(num).toFixed(2)
const numa = (+num || 0).toString().split('.')
let renum = (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ","))
if (numa.length > 1) {
if (numa[1].length < 2) {
renum = renum + '0'
}
} else {
renum = renum + '.'
for (let i = 1; i <= 2; i++) {
renum = renum + '0'
}
}
const arrnum = String(renum).split('.')
return arrnum[type]
}
export function formatDate(datestr, temp = "yyyy-MM-dd hh:mm:ss", type = false, dayCount = 30) {
var date
var time = new Date().getTime();
if (Object.prototype.toString.call(datestr) === '[object Date]') {
date = new Date(datestr);
} else if (datestr) {
if (Object.prototype.toString.call(datestr) === '[object Number]') {
date = new Date(parseInt(datestr))
} else {
var reg = new RegExp('-', "g");
datestr = String(datestr).replace(reg, '/');
date = new Date(datestr);
}
} else {
date = new Date();
}
if (temp == 'getDate') {
return date
} else if (temp == 'getTime') {
return date.getTime()
} else if (!type) {
// 返回标准日期格式
return _formatDate(date, temp);
} else {
// 返回距离当前间隔时间
var _date = date.getTime();
var _time = parseInt((time - _date) / 1000);
var s;
if (_time < 60 * 10) { //十分钟内
return '刚刚';
} else if ((_time < 60 * 60) && (_time >= 60 * 10)) {
//超过十分钟少于1小时
s = Math.floor(_time / 60);
return s + "分钟前";
} else if ((_time < 60 * 60 * 24) && (_time >= 60 * 60)) {
//超过1小时少于24小时
s = Math.floor(_time / 60 / 60);
return s + "小时前";
} else if ((_time < 60 * 60 * 24 * dayCount) && (_time >= 60 * 60 * 24)) {
//超过1天少于7天内
s = Math.floor(_time / 60 / 60 / 24);
return s + "天前";
} else {
//超过7天
return _formatDate(date, temp);
}
}
}
export function imgUrl(url) {
if (!url) {
return ""
}
url = String(url)
const appData = getApp().globalData
if (url.toLowerCase().indexOf('https://') == 0 || url.toLowerCase().indexOf('https://') == 0) {
return url
}
return appData.imgUrl + url;
}
export function statusName(state) {
if (state == -1) {
return '已驳回'
} else if (state == 0) {
return '禁用'
} else if (state == 1) {
return '启用'
} else if (state == 2) {
return '审核中'
}
}
export function newState(val) {
const state = ['待审核', '已审核', '已驳回']
return state[val]
}
export function activityState(val) {
if (val == -3) {
return '已驳回'
} else if (val == -2) {
return '待审核'
} else if (val == -1) {
return '暂存'
} else if (val == 1) {
return '已发布'
} else if (val == 2) {
return '活动结束'
} else if (val == 3) {
return '活动未开始'
}
}
export function orderState(state) {
// 0未支付 1待发货 3待收货 4已收货 5取消 6支付超时
switch (state) {
case 0:
return '待支付'
break;
case 1:
return '待发货'
break;
case 2:
return '待收货'
break;
case 3:
return '已收货'
break;
case -1:
return '已取消'
break;
case -2:
return '支付超时'
break;
case -3:
return '已退款'
break;
default:
return ""
break;
}
}
export function hidUserName(uname) {
if (!uname) {
return ""
}
uname = String(uname)
return uname.length > 2 ? uname.slice(0, 1) + "*" + uname.slice(-1) : uname.slice(0, 1) + "*";
}
export function getDateMath(datestr, type = "day", counts = 0, temp = "yyyy-MM-dd hh:mm:ss") {
var date
/*
type类型:
day:间隔天数
month: 间隔月数
year:间隔年份
*/
if (datestr) {
if (!isNaN(datestr)) {
datestr = new Date(parseInt(datestr))
}
var reg = new RegExp('-', "g");
datestr = String(datestr).replace(reg, '/');
date = new Date(datestr);
} else {
date = new Date();
}
switch (type) {
case "year":
date.setFullYear(date.getFullYear() + counts);
break;
case "month":
date.setMonth(date.getMonth() + counts);
break;
default:
date.setDate(date.getDate() + counts);
break;
}
return _formatDate(date, temp)
}
const _formatDate = function(_date, _temp = "yyyy-MM-dd hh:mm:ss") {
var o = {
"M+": _date.getMonth() + 1, //月份
"d+": _date.getDate(), //日
"h+": _date.getHours(), //小时
"m+": _date.getMinutes(), //分
"s+": _date.getSeconds(), //秒
"q+": Math.floor((_date.getMonth() + 3) / 3), //季度
"S": _date.getMilliseconds() //毫秒
};
if (/(y+)/.test(_temp)) {
_temp = _temp.replace(RegExp.$1, (_date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(_temp)) {
_temp = _temp.replace(
RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return _temp;
}
export function sizeDeal(size, idx = 2) {
const info = uni.getSystemInfoSync()
this.scale = 750 / info.windowWidth;
// 分离字体大小和单位,rpx 转 px
let s = Number.isNaN(parseFloat(size)) ? 0 : parseFloat(size)
let u = size.toString().replace(/[0-9]/g, '').replace('-', '')
if (u == 'rpx') {
s /= this.scale
u = 'px'
} else if (u == '') {
u = 'px'
} else if (u == 'vw') {
u = 'px'
s = s / 100 * 750 / this.scale
}
const data = [s, u, s + u]
return data[idx]
}
export function removeHTMLTag(str) {
str = str.replace(/<\/?[^>]*>/g, '') // 去除HTML tag
str = str.replace(/[ | ]*\n/g, '\n') // 去除行尾空白
str = str.replace(/\n[\s| | ]*\r/g, '\n'); //去除多余空行
str = str.replace(/ /ig, '') // 去掉
const arrEntities = {
'lt': '<',
'gt': '>',
'nbsp': ' ',
'amp': '&',
'quot': '"'
} // 转义符换成普通字符
str = str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
return arrEntities[t]
})
return str
}
//富文本转换
export function formatHtml(html, imgnum = -1) {
let newContent = html.replace(/]*>/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(/
]*\/>/gi, '');
newContent = newContent.replace(/\|\/>)/gi //匹配图片中的img标签
let arr = newContent.match(imgReg)
if (imgnum >= 0 && arr && arr.length > 0) {
// console.log(arr)
arr.forEach((item, index) => {
if (index >= imgnum) {
newContent = newContent.replace(item, '');
}
})
}
newContent = newContent.replace(/\