This commit is contained in:
nepiedg
2026-01-25 11:45:16 +08:00
commit c883ae7b17
44 changed files with 5945 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
export function formatMoney(cent) {
if (!cent && cent !== 0) return '¥0'
const yuan = cent / 100
return `¥${yuan.toFixed(yuan % 1 === 0 ? 0 : 2)}`
}
export function formatPercent(value, decimals = 0) {
if (!value && value !== 0) return '0%'
return `${(value * 100).toFixed(decimals)}%`
}
export function formatNumber(num) {
if (!num && num !== 0) return '0'
return num.toLocaleString()
}
export function formatChange(current, previous) {
if (!previous) return { text: '', class: '' }
const diff = current - previous
const percent = Math.round((diff / previous) * 100)
if (diff < 0) {
return {
text: `较昨日 ${diff}`,
class: 'change-down',
percent: `${percent}%`
}
} else if (diff > 0) {
return {
text: `较昨日 +${diff}`,
class: 'change-up',
percent: `+${percent}%`
}
}
return {
text: '与昨日持平',
class: 'change-same',
percent: '0%'
}
}
+3
View File
@@ -0,0 +1,3 @@
export * from './storage'
export * from './time'
export * from './format'
+41
View File
@@ -0,0 +1,41 @@
const STORAGE_PREFIX = 'smt_'
export const storage = {
set(key, value) {
try {
uni.setStorageSync(STORAGE_PREFIX + key, JSON.stringify(value))
} catch (e) {
console.error('Storage set error:', e)
}
},
get(key, defaultValue = null) {
try {
const value = uni.getStorageSync(STORAGE_PREFIX + key)
return value ? JSON.parse(value) : defaultValue
} catch (e) {
console.error('Storage get error:', e)
return defaultValue
}
},
remove(key) {
try {
uni.removeStorageSync(STORAGE_PREFIX + key)
} catch (e) {
console.error('Storage remove error:', e)
}
},
clear() {
try {
uni.clearStorageSync()
} catch (e) {
console.error('Storage clear error:', e)
}
}
}
export const SESSION_KEY = 'session_key'
export const USER_KEY = 'user'
export const PROFILE_KEY = 'profile'
+83
View File
@@ -0,0 +1,83 @@
export function formatTime(date) {
if (!date) return ''
if (typeof date === 'string') {
date = new Date(date)
}
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
return `${hours}:${minutes}`
}
export function formatDate(date) {
if (!date) return ''
if (typeof date === 'string') {
date = new Date(date)
}
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
export function formatDateTime(date) {
if (!date) return ''
if (typeof date === 'string') {
date = new Date(date)
}
return `${formatDate(date)} ${formatTime(date)}:${String(date.getSeconds()).padStart(2, '0')}`
}
export function formatDuration(minutes) {
if (!minutes || minutes < 0) return '0分钟'
const hours = Math.floor(minutes / 60)
const mins = Math.round(minutes % 60)
if (hours === 0) {
return `${mins}分钟`
}
if (mins === 0) {
return `${hours}小时`
}
return `${hours}小时${mins}`
}
export function formatTimerDisplay(totalSeconds) {
const hours = Math.floor(totalSeconds / 3600)
const minutes = Math.floor((totalSeconds % 3600) / 60)
const seconds = totalSeconds % 60
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`
}
export function getGreeting() {
const hour = new Date().getHours()
if (hour < 6) return '凌晨好'
if (hour < 12) return '早上好'
if (hour < 14) return '中午好'
if (hour < 18) return '下午好'
return '晚上好'
}
export function isToday(dateStr) {
const today = new Date().toISOString().split('T')[0]
return dateStr === today
}
export function isYesterday(dateStr) {
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
return dateStr === yesterday.toISOString().split('T')[0]
}
export function daysBetween(date1, date2) {
const d1 = new Date(date1)
const d2 = new Date(date2)
const diffTime = Math.abs(d2 - d1)
return Math.ceil(diffTime / (1000 * 60 * 60 * 24))
}
export function getWeekday(dateStr) {
const weekdays = ['日', '一', '二', '三', '四', '五', '六']
const date = new Date(dateStr)
return weekdays[date.getDay()]
}