123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- import {
- formatTimestamp
- } from '@/common/util/utils'
- // 检测10位补0后转换,13位直接转换
- export function dateFormatterNew(timestamp, fmt) {
- if (!timestamp) return ""
- if (timestamp.toString().length == 10) {
- timestamp = timestamp * 1000
- }
- timestamp = typeof timestamp === 'string' ? timestamp.replace(/-/g, '/') : timestamp
- fmt = fmt || "yyyy-MM-dd";
- const $this = new Date(timestamp);
- const o = {
- "M+": $this.getMonth() + 1,
- "d+": $this.getDate(),
- "h+": $this.getHours(),
- "m+": $this.getMinutes(),
- "s+": $this.getSeconds(),
- "q+": Math.floor(($this.getMonth() + 3) / 3),
- S: $this.getMilliseconds()
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- ($this.getFullYear() + "").substr(4 - RegExp.$1.length)
- );
- }
- for (var k in o) {
- if (new RegExp("(" + k + ")").test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- RegExp.$1.length === 1 ?
- o[k] :
- ("00" + o[k]).substr(("" + o[k]).length)
- );
- }
- }
- return fmt;
- }
- export const getYear = value => { //时间戳转年份过滤器
- return formatTimestamp(+value)[0]
- }
- export const getMonth = value => { //时间戳转月份过滤器
- const month = formatTimestamp(+value)[1] + ''
- return month.length === 1 ? '0' + month : month //月份只有一位数字时补零
- }
- export const getDay = value => { //时间戳转周过滤器
- return formatTimestamp(+value)[2]
- }
- export const getDate = value => { //时间戳转 日 过滤器
- const date = formatTimestamp(+value)[3] + ''
- return date.length === 1 ? '0' + date : date //日期份只有一位数字时补零
- }
- export const getHousr = value => { //时间戳转 时 过滤器
- const hour = formatTimestamp(+value)[4] + ''
- return hour.length === 1 ? '0' + hour : hour // 小时只有一位时补零
- }
- export const getMinute = value => { //时间戳转 分 过滤器
- const minute = formatTimestamp(+value)[5] + ''
- return minute.length === 1 ? '0' + minute : minute // 分钟只有一位时补零
- }
- export const getSecond = value => { //时间戳转 秒 过滤器
- return formatTimestamp(+value)[6]
- }
- export const formatTime = value => { //格式化时间
- return `${getYear(value)}-${getMonth(value)}-${getDate(value)} ${getHousr(value)}:${getMinute(value)}`
- }
- export const formatDate = value => { //格式化日期
- return `${getYear(value)}-${getMonth(value)}-${getDate(value)}`
- }
- export const formatHourMin = value => { //格式化时分
- return `${getHousr(value)}:${getMinute(value)}`
- }
- export const formatActivity = value => { //格式化时分
- return `${getMonth(value)}月${getDate(value)}日 ${getHousr(value)}:${getMinute(value)}`
- }
- export const numDot = num => {
- if (!num) return 0
- return (num + '').replace(/(?=(?:\d{3})+(?!\d))/g, ',').replace(/^,/, '') || ''
- }
- export const getOrderStatus = val => {
- if (+val === 0) {
- return '待付款'
- } else if (+val === 1) {
- return '待发货'
- } else if (+val === 2) {
- return '配送中'
- } else if (+val === 3) {
- return '已完成'
- } else if (+val === 4) {
- return '已取消'
- } else {
- return ''
- }
- }
- export const willDeg = (value) => {
- if (value <= 200) {
- return '大学毕业'
- }
- if (value >= 201 && value <= 500) {
- return '学士毕业'
- } else if (value >= 501 && value <= 1000) {
- return '硕士毕业'
- } else if (value >= 1001 && value <= 1500) {
- return '博士毕业'
- } else if (value >= 1501) {
- return '成为学霸'
- }
- return ''
- }
- export const lessScore = (value) => {
- if (value <= 500) {
- return 500 - value
- } else if (value >= 501 && value <= 1000) {
- return 1000 - value
- } else if (value >= 1001 && value <= 1500) {
- return 1500 - value
- } else if (value >= 1501 && value <= 2000) {
- return 2000 - value
- } else if (value >= 2001) {
- return ''
- }
- return ''
- }
- export const getAddressString = val => {
- return val ? `${val.province ? val.province : val.provice}-${val.city}-${val.area}-${val.address}` : ''
- }
- export const DistanceNow = t => { // 计算动态距离现在的发布时间
- t = (t + '').length === 10 ? `${t}000` : t
- const d = Date.now() - Number(t)
- if (d >= 0 && d < 10800000) {
- return '刚刚发表'
- } else if (d >= 10800000 && d < 86400000) {
- return `${Math.floor(d / 3600000)}小时前`
- } else if (d >= 86400000) {
- return Math.floor(d / 86400000) > 9 ? formatDate(t) : `${Math.floor(d / 86400000)}天前`
- }
- }
- export function GetQueryString(name) {
- var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
- var r = window.location.search.substr(1).match(reg);
- if (r != null)
- return decodeURI(r[2]);
- return null;
- }
- export function dateFormatter(timestamp, fmt) {
- if (!timestamp) return ""
- timestamp = typeof timestamp === 'string' ? timestamp.replace(/-/g, '/') : timestamp
- fmt = fmt || "yyyy-MM-dd";
- const $this = new Date(timestamp);
- const o = {
- "M+": $this.getMonth() + 1,
- "d+": $this.getDate(),
- "h+": $this.getHours(),
- "m+": $this.getMinutes(),
- "s+": $this.getSeconds(),
- "q+": Math.floor(($this.getMonth() + 3) / 3),
- S: $this.getMilliseconds()
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- ($this.getFullYear() + "").substr(4 - RegExp.$1.length)
- );
- }
- for (var k in o) {
- if (new RegExp("(" + k + ")").test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- RegExp.$1.length === 1 ?
- o[k] :
- ("00" + o[k]).substr(("" + o[k]).length)
- );
- }
- }
- return fmt;
- }
- export const remove = val => {
- if (!val) {
- return false;
- }
- return val.replace(/ /ig, '').replace(/—/ig, '').replace(/“/ig, '').replace(/‘/ig, '')
- .replace(/”/ig, '');
- }
- /*店铺使用*/
- export const blanceFmt = val => { //分转化为元
- var num = Number(val);
- if (!num) { //等于0
- return num + '.00';
- } else { //不等于0
- num = Math.round((num) * 100) / 10000;
- num = num.toFixed(2);
- num += ''; //转成字符串
- var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g; //千分符的正则
- return num.replace(reg, '$1,') //千分位格式化
- }
- };
- export const getName = (val, num) => { //店铺名字截取
- if (!val) return '';
- if (num) {
- return val.length > num ? val.slice(0, num) + '...' : val;
- } else {
- return val.length > 10 ? val.slice(0, 10) + '...' : val;
- }
- }
- export const hideMiddle = (val, num) => { //隐藏号码
- if (val === null || !val) {
- return '';
- } else {
- return `${val.substring(0, num)}************${val.substring(val.length - num)}`;
- }
- }
- export const getStatus = val => { //显示订单的状态
- switch (val) {
- case 0:
- return '待付款';
- break;
- case 1:
- return '待发货';
- break;
- case 2:
- return '待收货';
- break;
- case 3:
- return '待评价';
- default:
- break;
- }
- }
- export const formatRichText = html => {
- //显示富文本信息控制小程序中图片大小
- if (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;display:inline-block;margin:10rpx auto;"');
- return newContent;
- }
- }
- export const sendDay = times => {
- //截取字符串年月日
- let timestr = '';
- var timearr = times
- .replace(' ', ':')
- .replace(/\:/g, '-')
- .split('-');
- var date = new Date();
- let month = date.getMonth() + 1;
- if (month != timearr[1]) { //如果不是当前月份,显示年月
- timestr = timearr[0] + '-' + timearr[1]
- } else {
- timestr = date.getDate() - Number(timearr[2]) > 0 ? date.getDate() - Number(timearr[2]) + '天前' : '今天'
- }
- return timestr;
- }
- //总金额
- export const totalPrice = data => {
- let totalPrice = 0;
- data.map(i => {
- i.sku.map(j => {
- totalPrice += j.num * j.price;
- });
- });
- return totalPrice;
- }
- //总数量
- export const totalNum = data => {
- let totalNum = 0;
- data.map(i => {
- i.sku.map(j => {
- totalNum += j.num;
- });
- });
- return totalNum;
- }
- //小计数量
- export const subNum = data => {
- let subNum = 0;
- data.map(i => {
- subNum += i.num;
- });
- return subNum;
- }
- //小计金额
- export const subtotal = data => {
- let subtotal = 0;
- data.map(j => {
- subtotal += j.num * j.price;
- });
- return subtotal;
- }
- /*店铺使用end*/
- export const format = num => {
- if (!num) return '';
- return '0'.repeat(2 - String(Math.floor(num / 60)).length) + Math.floor(num / 60) + ':' + '0'.repeat(2 -
- String(Math.floor(num % 60)).length) + Math.floor(num % 60)
- }
- export const moreNum = value => { //超出9999显示9999+
- if (!value) return 0
- if (value >= 9999) {
- return '9999+'
- } else {
- return value
- }
- }
|