uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.8 KiB

3 years ago
3 years ago
  1. export default {
  2. /**
  3. * 图片处理-预览图片
  4. * @param {Array} urls - 图片列表
  5. * @param {Number} current - 首个预览下标
  6. */
  7. previewImage(urls = [], current = 0) {
  8. uni.previewImage({
  9. urls: urls,
  10. current: current,
  11. indicator: 'default',
  12. loop: true,
  13. fail(err) {
  14. console.log('previewImage出错', urls, err)
  15. },
  16. })
  17. },
  18. /**
  19. * 打电话
  20. * @param {String<Number>} phoneNumber - 数字字符串
  21. */
  22. callPhone(phoneNumber = '') {
  23. let num = phoneNumber.toString()
  24. uni.makePhoneCall({
  25. phoneNumber: num,
  26. fail(err) {
  27. console.log('makePhoneCall出错', err)
  28. },
  29. });
  30. },
  31. /**
  32. * @description: 弹窗封装
  33. * @param {String} msg
  34. * @return: {*}
  35. */
  36. showNone(msg){
  37. uni.showToast({
  38. title: msg,
  39. icon: 'none',
  40. duration:2000
  41. });
  42. },
  43. /**
  44. * @description: 自动封装promise
  45. * @param {Function} api
  46. * @return {Promise API}
  47. */
  48. promisify(api){
  49. return (options, ...params) => {
  50. return new Promise((resolve, reject) => {
  51. api(Object.assign({}, options, { success: resolve, fail: reject }), ...params);
  52. });
  53. }
  54. },
  55. /**
  56. * @description: 获取URL拼接
  57. * @param {JSON Object} data
  58. * @param {String} url
  59. * @return {String}
  60. */
  61. getWebURL(data,url){
  62. let result = ""
  63. for(var i in data){
  64. result+=`&${i}=${data[i]}`
  65. }
  66. return url+"?"+result.slice(1)
  67. },
  68. /**
  69. * @description: 休眠指定时间
  70. * @param {Int} time
  71. * @return: {*}
  72. */
  73. async sleep(time){
  74. const res = await new Promise(resolve => {
  75. setTimeout(() => resolve("asyncSetTimeOut"), time||1000);
  76. });
  77. return res
  78. },
  79. /**
  80. * @description: 解析onload传递参数,兼容多种传参方式
  81. * @param {String ,JSON Object } param
  82. * @return: {*}
  83. */
  84. getDecodeObj(param) { //解析传递参数 - 兼容开发者工具快速调试参数,并做promise封装
  85. return new Promise((rs, rj) => {
  86. let res = decodeURIComponent(param)
  87. try {
  88. res = JSON.parse(res)
  89. } catch (e) {
  90. //TODO handle the exception
  91. res = JSON.parse(decodeURIComponent(res)) || res
  92. } finally {
  93. if (typeof(res) == 'object') rs(res)
  94. rj(res)
  95. }
  96. })
  97. },
  98. /**
  99. * @description: 防抖函数封装
  100. * @param {Function} func
  101. * @param {Int} wait
  102. * @param {Bool} immediate
  103. * @return {debounce Function}
  104. */
  105. debounce(func, wait, immediate) {
  106. let timeout, args, context, timestamp, result;
  107. const later = function() {
  108. // 据上一次触发时间间隔
  109. const last = +new Date() - timestamp;
  110. // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
  111. if (last < wait && last > 0) {
  112. timeout = setTimeout(later, wait - last);
  113. } else {
  114. timeout = null;
  115. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  116. if (!immediate) {
  117. result = func.apply(context, args);
  118. if (!timeout) context = args = null;
  119. }
  120. }
  121. }
  122. return function(...args) {
  123. context = this;
  124. timestamp = +new Date();
  125. const callNow = immediate && !timeout;
  126. // 如果延时不存在,重新设定延时
  127. if (!timeout) timeout = setTimeout(later, wait);
  128. if (callNow) {
  129. result = func.apply(context, args);
  130. context = args = null;
  131. }
  132. return result;
  133. }
  134. },
  135. /**
  136. * @description: 路由跳转,uni版(未兼容路由)
  137. * @param {String Path} url
  138. * @param {String } type
  139. * @return: {*}
  140. */
  141. routeTo(url,type){
  142. switch(type){
  143. case 'nT': uni.navigateTo({url});
  144. break
  145. case 'rT': uni.redirectTo({url});
  146. break
  147. case 'rL': uni.reLaunch({url});
  148. break
  149. case 'sT': uni.switchTab({url});
  150. break
  151. default: uni.navigateBack({delta: 1})
  152. break
  153. }
  154. },
  155. route(url,type){
  156. // 无再次封装必要,参考
  157. // https://hhyang.cn/v2/api/routerInsatll.html#router-aftereach
  158. },
  159. }