index.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. type Icon = 'success' | 'error' | 'loading' | 'none'
  2. const duration = 3000
  3. /**
  4. * 轻提示
  5. * @param text 提示文本
  6. * @param icon 提示图标
  7. */
  8. export function showToast(text = '网络异常,请稍后再试', icon: Icon = 'none'): void {
  9. wx.showToast({
  10. title: text,
  11. icon: icon,
  12. duration: duration
  13. })
  14. }
  15. /**
  16. * 显示 loading 提示框
  17. * @param fn 回调函数
  18. * @param text 提示文本
  19. */
  20. export function showLoading(fn?: () => void, text = '正在加载...'): void {
  21. wx.showLoading({
  22. title: text,
  23. mask: true,
  24. success: () => {
  25. if (fn) fn()
  26. }
  27. })
  28. }
  29. /**
  30. * 隐藏 loading 提示框
  31. * @param fn 回调函数
  32. * @param text 提示文本
  33. */
  34. export function hideLoading(fn?: () => void, text?: string, icon: Icon = 'none'): void {
  35. wx.hideLoading({
  36. success: () => {},
  37. complete:() => {
  38. if (text) showToast(text, icon)
  39. if (fn) fn()
  40. }
  41. })
  42. }
  43. /**
  44. * 隐藏 model 提示框
  45. * @param fn 回调函数
  46. * @param title 提示文本
  47. * @param content 提示内容
  48. */
  49. export function showModel(fn?: () => void, title = '提示', content?: string, showCancel = true): void {
  50. wx.showModal({
  51. title: title,
  52. content: content,
  53. showCancel: showCancel,
  54. success: (res) => {
  55. if (res.confirm) {
  56. if (fn) fn()
  57. }
  58. }
  59. })
  60. }