| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- /**
- * 通用工具函数
- */
- /**
- * 格式化金额(后端返回单位:元)
- */
- export function formatMoney(amount: number | string): string {
- if (amount === null || amount === undefined || amount === '') return '0.00';
- const num = typeof amount === 'string' ? parseFloat(amount) : amount;
- return num.toFixed(2);
- }
- /**
- * 格式化日期时间
- */
- export function formatDateTime(dateStr: string | number | Date): string {
- const date = new Date(dateStr);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0');
- const day = String(date.getDate()).padStart(2, '0');
- const hour = String(date.getHours()).padStart(2, '0');
- const minute = String(date.getMinutes()).padStart(2, '0');
- const second = String(date.getSeconds()).padStart(2, '0');
- return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
- }
- /**
- * 格式化日期
- */
- export function formatDate(dateStr: string | number | Date): string {
- const date = new Date(dateStr);
- 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 formatTime(dateStr: string | number | Date): string {
- const date = new Date(dateStr);
- const hour = String(date.getHours()).padStart(2, '0');
- const minute = String(date.getMinutes()).padStart(2, '0');
- return `${hour}:${minute}`;
- }
- /**
- * 相对时间
- */
- export function formatRelativeTime(dateStr: string | number | Date): string {
- const date = new Date(dateStr);
- const now = new Date();
- const diff = now.getTime() - date.getTime();
-
- const minute = 60 * 1000;
- const hour = 60 * minute;
- const day = 24 * hour;
- const week = 7 * day;
- const month = 30 * day;
-
- if (diff < minute) {
- return '刚刚';
- } else if (diff < hour) {
- return `${Math.floor(diff / minute)}分钟前`;
- } else if (diff < day) {
- return `${Math.floor(diff / hour)}小时前`;
- } else if (diff < week) {
- return `${Math.floor(diff / day)}天前`;
- } else if (diff < month) {
- return `${Math.floor(diff / week)}周前`;
- } else {
- return formatDate(dateStr);
- }
- }
- /**
- * 格式化手机号(隐藏中间4位)
- */
- export function formatPhone(phone: string): string {
- if (!phone || phone.length !== 11) return phone;
- return `${phone.slice(0, 3)}****${phone.slice(7)}`;
- }
- /**
- * 防抖函数
- */
- export function debounce<T extends (...args: any[]) => any>(
- fn: T,
- delay: number
- ): (...args: Parameters<T>) => void {
- let timer: ReturnType<typeof setTimeout> | null = null;
- return function (this: any, ...args: Parameters<T>) {
- if (timer) clearTimeout(timer);
- timer = setTimeout(() => {
- fn.apply(this, args);
- }, delay);
- };
- }
- /**
- * 节流函数
- */
- export function throttle<T extends (...args: any[]) => any>(
- fn: T,
- delay: number
- ): (...args: Parameters<T>) => void {
- let lastTime = 0;
- return function (this: any, ...args: Parameters<T>) {
- const now = Date.now();
- if (now - lastTime >= delay) {
- lastTime = now;
- fn.apply(this, args);
- }
- };
- }
- /**
- * 显示加载中
- */
- export function showLoading(title = '加载中...'): void {
- uni.showLoading({ title, mask: true });
- }
- /**
- * 隐藏加载中
- */
- export function hideLoading(): void {
- uni.hideLoading();
- }
- /**
- * 显示提示
- */
- export function showToast(title: string, icon: 'success' | 'error' | 'none' = 'none'): void {
- uni.showToast({ title, icon });
- }
- /**
- * 显示确认对话框
- */
- export function showConfirm(content: string, title = '提示'): Promise<boolean> {
- return new Promise((resolve) => {
- uni.showModal({
- title,
- content,
- success: (res) => {
- resolve(res.confirm);
- },
- fail: () => {
- resolve(false);
- }
- });
- });
- }
- /**
- * 页面跳转
- */
- export function navigateTo(url: string): void {
- uni.navigateTo({ url });
- }
- /**
- * 页面重定向
- */
- export function redirectTo(url: string): void {
- uni.redirectTo({ url });
- }
- /**
- * 返回上一页
- */
- export function navigateBack(delta = 1): void {
- uni.navigateBack({ delta });
- }
|