commonUtils.js 1.6 KB

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