import { removeImgQuery } from './imgResize' /** * 将value保留到小数点后count位,并去除末尾多余的0 * @param {number} value 想要被截取的数字 * @param {number} count 想要被截取的小数点后的位数 */ const format = (value, count) => { var suffix = value.toFixed(count).split('.')[1]; let i = suffix.length - 1; let adjustedCount = count; while (i >= 0) { if (suffix[i] === '0') { adjustedCount--; } else { break; } i--; } return value.toFixed(adjustedCount); }; /** * 将数值格式化为交互约定的数值,999,1.5万,3.22亿等 * @param {number} num 想要被格式化的数值 */ export const formatNumber = (num) => { if (typeof num !== 'number'){ return 'NaN' } if (num < 10000) { return num.toString(); } let value; let baseValue; if (num < 100000000) { // 大于99999950显示一亿 if (num >= 99999950) { return '1亿'; } value = num / 10000; baseValue = Math.round(value); return (baseValue > value ? baseValue : format(value, 1)) + '万'; } value = num / 100000000; baseValue = Math.round(value); return (baseValue > value ? baseValue : format(value, 2)) + '亿'; }; export const padding = (value) => { value = value + ''; if (value.length === 1) { return '0' + value; } return value; } /** * 将时间戳格式化为交互规定的时间显示格式,1分钟内显示刚刚,1小时内多少分钟前等 * @param {number} timestamp 时间戳 */ export const formatTime = (timestamp) => { const nowDate = new Date(); const now = nowDate.getTime(); timestamp = /\d+/.test(timestamp) ? parseInt(timestamp, 10) : timestamp; const targetDate = new Date(timestamp); let diff = (now - timestamp) / 1000; if (diff < 60) { return '刚刚'; } diff /= 60; if (diff < 60) { return Math.round(diff) + '分钟前'; } diff /= 60; // diff /= 24; const hours = padding(targetDate.getHours()) + ':' + padding(targetDate.getMinutes()); if (diff < nowDate.getHours()) { return '今天 ' + hours; } else if (diff < nowDate.getHours() + 24) { return '昨天 ' + hours; } else if (diff < nowDate.getHours() + 48) { return '前天 ' + hours; } const days = (targetDate.getMonth() + 1) + '月' + targetDate.getDate() + '日'; if (targetDate.getUTCFullYear() === nowDate.getUTCFullYear()) { return days + ' ' + hours; } return targetDate.getUTCFullYear() + '年' + ' ' + days + ' ' + hours; } export const formatDate = timestamp => { if(!/^\d+$/.test(timestamp)){ return '格式错误' } const date = new Date(timestamp); return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}` } /** * 将时间戳格式化为2022年05月20日24:00这样的完整时间字符串 * @param timestamp 时间戳 * @returns 格式化后的时间字符串 */ export const formatFullDate = (timestamp: number, needSecond = true) => { if (!timestamp) { return '时间不能为空'; } if(!/^\d+$/.test(timestamp.toString())){ return '格式错误' } const time = new Date(timestamp); const month = time.getMonth() + 1; const date = time.getDate(); const hours = time.getHours(); const minutes = time.getMinutes(); const seconds = time.getSeconds(); return `${time.getFullYear()}年${padding(month)}月${padding(date)}日${padding(hours)}:${padding(minutes)}${needSecond ? ':' + padding(seconds) : ''}`; } /** * 根据时间戳返回月日字符 * @param timestamp 时间戳 * @returns x月x日 */ export const formatMonthDate = (timestamp: number) => { if (!timestamp) { return '时间参数为空' } if(!/^\d+$/.test(timestamp.toString())){ return '格式错误' } const date = new Date(timestamp); return `${date.getMonth() + 1}月${date.getDate()}日` } export const formatAvatar = (url: string) => { return url ? removeImgQuery(url) + '?imageView&thumbnail=200y200' : 'https://yuedust.yuedu.126.net/snail_st/static/images/share/account_avatar_big@2x.png' } /** * 将图片url转换为想要的宽高尺寸 * @param {string} url 图片url * @param {number} width 想要得到的图片宽度 * @param {number} height 想要得到的图片高度 */ export const formatImageSize = (url: string, width = 200, height = 200, type?: 'jpg' | 'png' | 'webp' | 'gif') => { if(!url) return url; if(url.indexOf('?') >= 0){ url = url.split('?')[0]; } if(typeof url === 'string'){ return url + `?imageView&thumbnail=${width}${width === 0 || height === 0 ? 'x' : 'y'}${height}&enlarge=1${type ? '&type=' + type : ''}`; }else{ throw new Error('Wrong url param.') } } export const formatPostUrlToMpPreviewUrl = (postUrl: string) => { // 由于小程序内只能打开 www.lofter.com 的链接,所以需要进行链接转换 // 针对例如这样的单日志文章链接 :http://loftermovie.lofter.com/post/1ddf0c55_2b9ca7477 // 转化为: https://www.lofter.com/front/blog/preview/post/1ddf0c55_2b9ca7477 if(!postUrl) return postUrl; // 去除 postUrl path 后面的参数 const url = new URL(postUrl); const path = url.pathname; const permalink = path.split('/').pop(); return `https://www.lofter.com/front/blog/preview/post/${permalink}` }