commonUtils.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // commonUtils.js
  2. const http = uni.$u.http
  3. // 判断数据是否为空
  4. function isDataEmpty(data) {
  5. if (data === null || data === undefined) {
  6. return true;
  7. }
  8. if (typeof data === 'string' && data.trim() === '') {
  9. return true;
  10. }
  11. if (Array.isArray(data) && data.length === 0) {
  12. return true;
  13. }
  14. if (typeof data === 'object' && Object.keys(data).length === 0) {
  15. return true;
  16. }
  17. return false;
  18. }
  19. // 日期转换
  20. function formatDate(timestamp) {
  21. const date = new Date(timestamp);
  22. const year = date.getFullYear();
  23. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  24. const day = date.getDate().toString().padStart(2, '0');
  25. const hour = date.getHours().toString().padStart(2,'0');
  26. const minutes = date.getMinutes().toString().padStart(2,'0');
  27. const secoonds = date.getSeconds().toString().padStart(2,'0');
  28. return `${year}-${month}-${day} ${hour}:${minutes}:${secoonds}`;
  29. }
  30. // 日期转换
  31. function dateFormatYmd(dateStr) {
  32. const timestamp = new Date(dateStr).getTime();
  33. const date = new Date(timestamp);
  34. const year = date.getFullYear();
  35. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  36. const day = date.getDate().toString().padStart(2, '0');
  37. return `${year}-${month}-${day}`;
  38. }
  39. // 导出工具类方法
  40. export default {
  41. isDataEmpty,
  42. formatDate,
  43. dateFormatYmd,
  44. };