common.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import cacheManager from "./cacheManager.js"
  2. /**
  3. * 显示消息提示框
  4. * @param content 提示的标题
  5. */
  6. export function toast(content) {
  7. uni.showToast({
  8. icon: 'none',
  9. duration:4000,
  10. title: content
  11. })
  12. }
  13. /**
  14. * 显示模态弹窗
  15. * @param content 提示的标题
  16. */
  17. export function showConfirm(content) {
  18. return new Promise((resolve, reject) => {
  19. uni.showModal({
  20. title: '提示',
  21. content: content,
  22. cancelText: '取消',
  23. confirmText: '确定',
  24. success: function(res) {
  25. resolve(res)
  26. }
  27. })
  28. })
  29. }
  30. /**
  31. * 参数处理
  32. * @param params 参数
  33. */
  34. export function tansParams(params) {
  35. let result = ''
  36. // FIXME 拼接参数
  37. return result
  38. }
  39. /**
  40. * @summary 获取请求异常与正常返回
  41. * @param {Object} promise
  42. */
  43. export function catchError(promise) {
  44. return new Promise((resolve,reject) => {
  45. promise.then(data => {
  46. resolve([undefined, data.data])
  47. }).catch(err => {
  48. reject([err])
  49. })
  50. })
  51. }
  52. export function debounce(func, wait) {
  53. let timeout;
  54. return function(...args) {
  55. // 清除之前的定时器
  56. clearTimeout(timeout);
  57. // 设置新的定时器
  58. timeout = setTimeout(() => {
  59. func.apply(this, args);
  60. }, wait);
  61. };
  62. }
  63. export function formatDuration(duration = 0) {
  64. return Math.round(duration / 60);
  65. }
  66. export function getStaticUrl(url) {
  67. let result = '';
  68. // #ifdef H5
  69. result = `/mdist/${url}`
  70. // #endif
  71. // #ifdef APP-PLUS
  72. result = url
  73. // #endif
  74. return result;
  75. }
  76. export function formatSecondsToCnhms(value, isNoZero) {
  77. if (!value || value == 0) {
  78. return '';
  79. }
  80. let result = parseInt(value);
  81. let h = isNoZero ? Math.floor(result / 3600) : Math.floor(result / 3600) < 10
  82. ? '0' + Math.floor(result / 3600)
  83. : Math.floor(result / 3600);
  84. let m = isNoZero ? Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60)) < 10
  85. ? '0' + Math.floor((result / 60 % 60))
  86. : Math.floor((result / 60 % 60));
  87. let s = isNoZero ? Math.floor((result % 60)) : Math.floor((result % 60)) < 10
  88. ? '0' + Math.floor((result % 60))
  89. : Math.floor((result % 60));
  90. if (isNoZero) {
  91. result = '';
  92. if (h !== 0) {
  93. result += `${h}时`;
  94. // 判断如果有时有秒,分钟为0也显示
  95. if (s !== 0) {
  96. // 分为零也显示
  97. if (m === 0) {
  98. result += `${m}分`;
  99. }
  100. }
  101. }
  102. // 分钟不为零显示
  103. if (m !== 0) {
  104. result += `${m}分`;
  105. }
  106. // 秒不为零显示
  107. if (s !== 0) {
  108. result += `${s}秒`;
  109. }
  110. } else {
  111. result = `${h}时${m}分${s}秒`;
  112. }
  113. return result;
  114. }
  115. // utils/jsonp.js
  116. export function jsonp(url, params, callbackName = 'jsonp_callback') {
  117. return new Promise((resolve, reject) => {
  118. const queryString = new URLSearchParams(params).toString();
  119. const fullUrl = `${url}?${queryString}&callback=${callbackName}`;
  120. window[callbackName] = (data) => {
  121. resolve(data);
  122. delete window[callbackName];
  123. if (script.parentNode) document.body.removeChild(script);
  124. };
  125. const script = document.createElement('script');
  126. script.src = fullUrl;
  127. script.onerror = (error) => reject(error);
  128. document.body.appendChild(script);
  129. });
  130. }
  131. export function jsonp2(url, params, callbackName = 'jsonp_callback') {
  132. return new Promise((resolve, reject) => {
  133. // 创建一个全局回调函数
  134. const callbackName = `jsonp_callback_${Date.now()}`;
  135. window[callbackName] = function (data) {
  136. resolve(data); // 解析数据
  137. delete window[callbackName]; // 清理全局回调函数
  138. document.body.removeChild(script); // 移除 script 标签
  139. };
  140. // 创建 script 标签
  141. const script = document.createElement('script');
  142. script.src = `${url}&callback=${callbackName}`;
  143. script.onerror = () => {
  144. reject(new Error('JSONP 请求失败'));
  145. delete window[callbackName]; // 清理全局回调函数
  146. document.body.removeChild(script); // 移除 script 标签
  147. };
  148. // 将 script 标签添加到文档中
  149. document.body.appendChild(script);
  150. });
  151. }
  152. export function throttleAdvanced(func, delay = 5000) {
  153. let lastExecTime = 0; // 记录上次成功执行的时间戳
  154. return function(...args) {
  155. const currentTime = Date.now(); // 获取当前时间戳
  156. // 如果当前时间距离上次执行时间已经超过了设定的延迟时间
  157. if (currentTime - lastExecTime >= delay) {
  158. // 使用 apply 确保函数上下文和参数正确传递
  159. console.log(11111)
  160. func.apply(this, args);
  161. // 更新最后一次执行的时间戳为当前时间
  162. lastExecTime = currentTime;
  163. }
  164. // 如果时间间隔未到,则什么都不做,忽略此次调用
  165. };
  166. }