common.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import cacheManager from "./cacheManager.js"
  2. /**
  3. * 显示消息提示框
  4. * @param content 提示的标题
  5. */
  6. export function toast(content) {
  7. uni.showToast({
  8. icon: 'none',
  9. title: content
  10. })
  11. }
  12. /**
  13. * 显示模态弹窗
  14. * @param content 提示的标题
  15. */
  16. export function showConfirm(content) {
  17. return new Promise((resolve, reject) => {
  18. uni.showModal({
  19. title: '提示',
  20. content: content,
  21. cancelText: '取消',
  22. confirmText: '确定',
  23. success: function(res) {
  24. resolve(res)
  25. }
  26. })
  27. })
  28. }
  29. /**
  30. * 参数处理
  31. * @param params 参数
  32. */
  33. export function tansParams(params) {
  34. let result = ''
  35. // FIXME 拼接参数
  36. return result
  37. }
  38. /**
  39. * @summary 获取请求异常与正常返回
  40. * @param {Object} promise
  41. */
  42. export function catchError(promise) {
  43. return new Promise((resolve,reject) => {
  44. promise.then(data => {
  45. resolve([undefined, data.data])
  46. }).catch(err => {
  47. reject([err])
  48. })
  49. })
  50. }
  51. export function debounce(func, wait) {
  52. let timeout;
  53. return function(...args) {
  54. // 清除之前的定时器
  55. clearTimeout(timeout);
  56. // 设置新的定时器
  57. timeout = setTimeout(() => {
  58. func.apply(this, args);
  59. }, wait);
  60. };
  61. }
  62. export function formatDuration(duration = 0) {
  63. return Math.round(duration / 60);
  64. }
  65. export function getStaticUrl(url) {
  66. let result = '';
  67. // #ifdef H5
  68. result = `/mdist/${url}`
  69. // #endif
  70. // #ifdef APP-PLUS
  71. result = url
  72. // #endif
  73. return result;
  74. }
  75. export function formatSecondsToCnhms(value, isNoZero) {
  76. if (!value || value == 0) {
  77. return '';
  78. }
  79. let result = parseInt(value);
  80. let h = isNoZero ? Math.floor(result / 3600) : Math.floor(result / 3600) < 10
  81. ? '0' + Math.floor(result / 3600)
  82. : Math.floor(result / 3600);
  83. let m = isNoZero ? Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60)) < 10
  84. ? '0' + Math.floor((result / 60 % 60))
  85. : Math.floor((result / 60 % 60));
  86. let s = isNoZero ? Math.floor((result % 60)) : Math.floor((result % 60)) < 10
  87. ? '0' + Math.floor((result % 60))
  88. : Math.floor((result % 60));
  89. if (isNoZero) {
  90. result = '';
  91. if (h !== 0) {
  92. result += `${h}时`;
  93. // 判断如果有时有秒,分钟为0也显示
  94. if (s !== 0) {
  95. // 分为零也显示
  96. if (m === 0) {
  97. result += `${m}分`;
  98. }
  99. }
  100. }
  101. // 分钟不为零显示
  102. if (m !== 0) {
  103. result += `${m}分`;
  104. }
  105. // 秒不为零显示
  106. if (s !== 0) {
  107. result += `${s}秒`;
  108. }
  109. } else {
  110. result = `${h}时${m}分${s}秒`;
  111. }
  112. return result;
  113. }