123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- 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(/<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 class="rich-img" style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
- );
- let imgReg = /<img.*?(?:>|\/>)/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(/\<p/gi, '<P class="rich-p" '); //正则给p标签增加class
- return newContent;
- }
- //多行域文本转换
- export function formatText(val) {
- if (val) {
- let newString = val.replace(/\n/g, '_@').replace(/\r/g, '_#');
- newString = newString.replace(/_#_@/g, '<br/>');
- newString = newString.replace(/_@/g, '<br/>');
- newString = newString.replace(/\s/g, ' ');
- return newString;
- }
- return "";
- }
- /**
- * 数字转中文
- */
- export function numberToChinese(num) {
- if (isNaN(num)) return "Number is wrong!";
- let AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
- let BB = new Array("", "十", "百", "千", "万", "亿", "点", "");
- let a = ("" + num).replace(/(^0*)/g, "").split("."),
- k = 0,
- re = "";
- for (let i = a[0].length - 1; i >= 0; i--) {
- switch (k) {
- case 0:
- re = BB[7] + re;
- break;
- case 4:
- if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0]))
- re = BB[4] + re;
- break;
- case 8:
- re = BB[5] + re;
- BB[7] = BB[5];
- k = 0;
- break;
- }
- if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re;
- if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re;
- k++;
- }
- if (a.length > 1) //加上小数部分(如果有小数部分)
- {
- re += BB[6];
- for (let i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
- }
- return re;
- }
|