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.

193 lines
4.3 KiB

3 years ago
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. * @param {Function} succ 点确认执行函数
  35. * @return {Promise}
  36. */
  37. showModal(msg,succ){
  38. let that = this
  39. return new Promise((rs,rj)=>{
  40. uni.showModal({
  41. title: '温馨提示',
  42. content: msg,
  43. success: function (res) {
  44. if (res.confirm) {
  45. succ()
  46. rs(true)
  47. } else if (res.cancel) {
  48. that.showNone("已取消")
  49. rj(false)
  50. }
  51. }
  52. });
  53. })
  54. },
  55. /**
  56. * @description: 弹窗封装
  57. * @param {String} msg
  58. * @return: {*}
  59. */
  60. showNone(msg){
  61. uni.showToast({
  62. title: msg,
  63. icon: 'none',
  64. duration:2000
  65. });
  66. },
  67. /**
  68. * @description: 自动封装promise
  69. * @param {Function} api
  70. * @return {Promise API}
  71. */
  72. promisify(api){
  73. return (options, ...params) => {
  74. return new Promise((resolve, reject) => {
  75. api(Object.assign({}, options, { success: resolve, fail: reject }), ...params);
  76. });
  77. }
  78. },
  79. /**
  80. * @description: 获取URL拼接
  81. * @param {JSON Object} data
  82. * @param {String} url
  83. * @return {String}
  84. */
  85. getWebURL(data,url){
  86. let result = ""
  87. for(var i in data){
  88. result+=`&${i}=${data[i]}`
  89. }
  90. return url+"?"+result.slice(1)
  91. },
  92. /**
  93. * @description: 休眠指定时间
  94. * @param {Int} time
  95. * @return: {*}
  96. */
  97. async sleep(time){
  98. const res = await new Promise(resolve => {
  99. setTimeout(() => resolve("asyncSetTimeOut"), time||1000);
  100. });
  101. return res
  102. },
  103. /**
  104. * @description: 解析onload传递参数,兼容多种传参方式
  105. * @param {String ,JSON Object } param
  106. * @return: {*}
  107. */
  108. getDecodeObj(param) { //解析传递参数 - 兼容开发者工具快速调试参数,并做promise封装
  109. return new Promise((rs, rj) => {
  110. let res = decodeURIComponent(param)
  111. try {
  112. res = JSON.parse(res)
  113. } catch (e) {
  114. //TODO handle the exception
  115. res = JSON.parse(decodeURIComponent(res)) || res
  116. } finally {
  117. if (typeof(res) == 'object') rs(res)
  118. rj(res)
  119. }
  120. })
  121. },
  122. /**
  123. * @description: 防抖函数封装
  124. * @param {Function} func
  125. * @param {Int} wait
  126. * @param {Bool} immediate
  127. * @return {debounce Function}
  128. */
  129. debounce(func, wait, immediate) {
  130. let timeout, args, context, timestamp, result;
  131. const later = function() {
  132. // 据上一次触发时间间隔
  133. const last = +new Date() - timestamp;
  134. // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
  135. if (last < wait && last > 0) {
  136. timeout = setTimeout(later, wait - last);
  137. } else {
  138. timeout = null;
  139. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  140. if (!immediate) {
  141. result = func.apply(context, args);
  142. if (!timeout) context = args = null;
  143. }
  144. }
  145. }
  146. return function(...args) {
  147. context = this;
  148. timestamp = +new Date();
  149. const callNow = immediate && !timeout;
  150. // 如果延时不存在,重新设定延时
  151. if (!timeout) timeout = setTimeout(later, wait);
  152. if (callNow) {
  153. result = func.apply(context, args);
  154. context = args = null;
  155. }
  156. return result;
  157. }
  158. },
  159. /**
  160. * @description: 路由跳转,uni版(未兼容路由)
  161. * @param {String Path} url
  162. * @param {String } type
  163. * @return: {*}
  164. */
  165. routeTo(url,type){
  166. switch(type){
  167. case 'nT': uni.navigateTo({url});
  168. break
  169. case 'rT': uni.redirectTo({url});
  170. break
  171. case 'rL': uni.reLaunch({url});
  172. break
  173. case 'sT': uni.switchTab({url});
  174. break
  175. default: uni.navigateBack({delta: 1})
  176. break
  177. }
  178. },
  179. route(url,type){
  180. // 无再次封装必要,参考
  181. // https://hhyang.cn/v2/api/routerInsatll.html#router-aftereach
  182. },
  183. }