common.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * 通用工具函数
  3. */
  4. /**
  5. * 格式化金额(后端返回单位:元)
  6. */
  7. export function formatMoney(amount: number | string): string {
  8. if (amount === null || amount === undefined || amount === '') return '0.00';
  9. const num = typeof amount === 'string' ? parseFloat(amount) : amount;
  10. return num.toFixed(2);
  11. }
  12. /**
  13. * 格式化日期时间
  14. */
  15. export function formatDateTime(dateStr: string | number | Date): string {
  16. const date = new Date(dateStr);
  17. const year = date.getFullYear();
  18. const month = String(date.getMonth() + 1).padStart(2, '0');
  19. const day = String(date.getDate()).padStart(2, '0');
  20. const hour = String(date.getHours()).padStart(2, '0');
  21. const minute = String(date.getMinutes()).padStart(2, '0');
  22. const second = String(date.getSeconds()).padStart(2, '0');
  23. return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  24. }
  25. /**
  26. * 格式化日期
  27. */
  28. export function formatDate(dateStr: string | number | Date): string {
  29. const date = new Date(dateStr);
  30. const year = date.getFullYear();
  31. const month = String(date.getMonth() + 1).padStart(2, '0');
  32. const day = String(date.getDate()).padStart(2, '0');
  33. return `${year}-${month}-${day}`;
  34. }
  35. /**
  36. * 格式化时间
  37. */
  38. export function formatTime(dateStr: string | number | Date): string {
  39. const date = new Date(dateStr);
  40. const hour = String(date.getHours()).padStart(2, '0');
  41. const minute = String(date.getMinutes()).padStart(2, '0');
  42. return `${hour}:${minute}`;
  43. }
  44. /**
  45. * 相对时间
  46. */
  47. export function formatRelativeTime(dateStr: string | number | Date): string {
  48. const date = new Date(dateStr);
  49. const now = new Date();
  50. const diff = now.getTime() - date.getTime();
  51. const minute = 60 * 1000;
  52. const hour = 60 * minute;
  53. const day = 24 * hour;
  54. const week = 7 * day;
  55. const month = 30 * day;
  56. if (diff < minute) {
  57. return '刚刚';
  58. } else if (diff < hour) {
  59. return `${Math.floor(diff / minute)}分钟前`;
  60. } else if (diff < day) {
  61. return `${Math.floor(diff / hour)}小时前`;
  62. } else if (diff < week) {
  63. return `${Math.floor(diff / day)}天前`;
  64. } else if (diff < month) {
  65. return `${Math.floor(diff / week)}周前`;
  66. } else {
  67. return formatDate(dateStr);
  68. }
  69. }
  70. /**
  71. * 格式化手机号(隐藏中间4位)
  72. */
  73. export function formatPhone(phone: string): string {
  74. if (!phone || phone.length !== 11) return phone;
  75. return `${phone.slice(0, 3)}****${phone.slice(7)}`;
  76. }
  77. /**
  78. * 防抖函数
  79. */
  80. export function debounce<T extends (...args: any[]) => any>(
  81. fn: T,
  82. delay: number
  83. ): (...args: Parameters<T>) => void {
  84. let timer: ReturnType<typeof setTimeout> | null = null;
  85. return function (this: any, ...args: Parameters<T>) {
  86. if (timer) clearTimeout(timer);
  87. timer = setTimeout(() => {
  88. fn.apply(this, args);
  89. }, delay);
  90. };
  91. }
  92. /**
  93. * 节流函数
  94. */
  95. export function throttle<T extends (...args: any[]) => any>(
  96. fn: T,
  97. delay: number
  98. ): (...args: Parameters<T>) => void {
  99. let lastTime = 0;
  100. return function (this: any, ...args: Parameters<T>) {
  101. const now = Date.now();
  102. if (now - lastTime >= delay) {
  103. lastTime = now;
  104. fn.apply(this, args);
  105. }
  106. };
  107. }
  108. /**
  109. * 显示加载中
  110. */
  111. export function showLoading(title = '加载中...'): void {
  112. uni.showLoading({ title, mask: true });
  113. }
  114. /**
  115. * 隐藏加载中
  116. */
  117. export function hideLoading(): void {
  118. uni.hideLoading();
  119. }
  120. /**
  121. * 显示提示
  122. */
  123. export function showToast(title: string, icon: 'success' | 'error' | 'none' = 'none'): void {
  124. uni.showToast({ title, icon });
  125. }
  126. /**
  127. * 显示确认对话框
  128. */
  129. export function showConfirm(content: string, title = '提示'): Promise<boolean> {
  130. return new Promise((resolve) => {
  131. uni.showModal({
  132. title,
  133. content,
  134. success: (res) => {
  135. resolve(res.confirm);
  136. },
  137. fail: () => {
  138. resolve(false);
  139. }
  140. });
  141. });
  142. }
  143. /**
  144. * 页面跳转
  145. */
  146. export function navigateTo(url: string): void {
  147. uni.navigateTo({ url });
  148. }
  149. /**
  150. * 页面重定向
  151. */
  152. export function redirectTo(url: string): void {
  153. uni.redirectTo({ url });
  154. }
  155. /**
  156. * 返回上一页
  157. */
  158. export function navigateBack(delta = 1): void {
  159. uni.navigateBack({ delta });
  160. }