index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. export class Prompt {
  2. constructor() {}
  3. /**
  4. * 显示消息提示框。
  5. */
  6. showToast(
  7. options = {
  8. title: '', // String 是 提示的内容,长度与 icon 取值有关。
  9. icon: 'none', // String 否 图标,有效值详见下方说明。
  10. image: '', // String 否 自定义图标的本地路径 5+App、H5、微信小程序、百度小程序
  11. mask: true, // Boolean 否 是否显示透明蒙层,防止触摸穿透,默认:false 5+App、微信小程序
  12. duration: 1500, // Number 否 提示的延迟时间,单位毫秒,默认:1500
  13. position: '', // String 否 纯文本轻提示显示位置,填写有效值后只有 title 属性生效, 有效值详见下方说明。 5+App
  14. success: () => {}, // Function 否 接口调用成功的回调函数
  15. fail: () => {}, // Function 否 接口调用失败的回调函数
  16. complete: () => {} // Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
  17. }
  18. ) {
  19. uni.showToast({
  20. title: options.title || '提示',
  21. icon: options.icon || 'none',
  22. image: options.image || '',
  23. mask: options.mask || true,
  24. duration: options.duration || 1500,
  25. position: options.position || '',
  26. success: options.success || (() => {}),
  27. fail: options.fail || (() => {}),
  28. complete: options.complete || (() => {})
  29. })
  30. }
  31. /**
  32. * 隐藏消息提示框。
  33. */
  34. hideToast() {
  35. uni.hideToast()
  36. }
  37. /**
  38. * 显示 loading 提示框, 需主动调用 hideLoading 才能关闭提示框。
  39. * @param {*} options
  40. */
  41. showLoading(
  42. options = {
  43. title: '', // String 是 提示的内容,长度与 icon 取值有关。
  44. mask: true, // Boolean 否 是否显示透明蒙层,防止触摸穿透,默认:false 5+App、微信小程序
  45. success: () => {}, // Function 否 接口调用成功的回调函数
  46. fail: () => {}, // Function 否 接口调用失败的回调函数
  47. complete: () => {} // Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
  48. }
  49. ) {
  50. uni.showLoading({
  51. title: options.title || '',
  52. mask: options.mask || true,
  53. success: options.success || (() => {}),
  54. fail: options.fail || (() => {}),
  55. complete: options.complete || (() => {})
  56. })
  57. }
  58. /**
  59. * 隐藏 loading 提示框。
  60. */
  61. hideLoading() {
  62. uni.hideLoading()
  63. }
  64. /**
  65. * 显示操作菜单
  66. * @param {*} options
  67. */
  68. showActionSheet(
  69. options = {
  70. itemList: [], // Array<String>
  71. itemColor: '#000000',
  72. success: () => {}, // Function 否 接口调用成功的回调函数
  73. fail: () => {}, // Function 否 接口调用失败的回调函数
  74. complete: () => {} // Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
  75. }
  76. ) {
  77. uni.showActionSheet({
  78. itemList: options.itemList || [],
  79. itemColor: options.itemColor || '#000000',
  80. success: options.success || (() => {}),
  81. fail: options.fail || (() => {}),
  82. complete: options.complete || (() => {})
  83. })
  84. }
  85. /**
  86. * 显示模态弹窗,类似于标准 html 的消息框:alert、confirm。
  87. * @param {*} options
  88. */
  89. showModal(
  90. options = {
  91. title: '', // String 是 提示的标题
  92. content: '', // String 是 提示的内容
  93. showCancel: true, // Boolean 否 是否显示取消按钮,默认为 true
  94. cancelText: '取消', // String 否 取消按钮的文字,默认为"取消",最多 4 个字符
  95. cancelColor: '#000000', // HexColor 否 取消按钮的文字颜色,默认为"#000000" H5、微信小程序、百度小程序
  96. confirmText: '确定', // String 否 确定按钮的文字,默认为"确定",最多 4 个字符
  97. confirmColor: '#007aff', // HexColor 否 确定按钮的文字颜色,H5平台默认为"#007aff",微信小程序平台默认为"#3CC51F",百度小程序平台默认为"#3c76ff" H5、微信小程序、百度小程序
  98. success: () => {}, // Function 否 接口调用成功的回调函数
  99. fail: () => {}, // Function 否 接口调用失败的回调函数
  100. complete: () => {} // Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
  101. }
  102. ) {
  103. uni.showModal({
  104. title: options.title || '提示',
  105. content: options.content || '',
  106. showCancel: options.showCancel || true,
  107. cancelText: options.cancelText || '取消',
  108. cancelColor: options.cancelColor || '#000000',
  109. confirmText: options.confirmText || '确定',
  110. confirmColor: options.confirmColor || '#007aff',
  111. success: options.success || (() => {}),
  112. fail: options.fail || (() => {}),
  113. complete: options.complete || (() => {})
  114. })
  115. }
  116. }