DialogAction.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { type App } from "vue";
  2. /**
  3. * 说明:对话框相关的封装
  4. */
  5. /**
  6. * 显示一个文本提示框
  7. * @param content - 要显示的提示内容
  8. */
  9. function toast(content: string) {
  10. if (typeof content!=='string')
  11. content = '' + content;
  12. uni.showToast({
  13. title: content,
  14. icon: 'none',
  15. })
  16. }
  17. /**
  18. * 显示一个确认对话框
  19. * @param option - 对话框的配置选项
  20. * @param option.title - 对话框的标题,可选
  21. * @param option.content - 对话框的内容,可选
  22. */
  23. function alert(option: {
  24. title?: string,
  25. content?: string,
  26. }) {
  27. uni.showModal({
  28. title: option.title,
  29. content: option.content,
  30. showCancel: false,
  31. })
  32. };
  33. /**
  34. * 显示一个确认对话框
  35. * @param option - 对话框的配置选项
  36. * @param option.title - 对话框的标题,可选
  37. * @param option.content - 对话框的内容,可选
  38. * @param option.cancelText - 取消按钮的文本,可选
  39. * @param option.confirmText - 确认按钮的文本,可选
  40. * @returns - 返回一个 Promise 对象,当用户点击确定按钮时,Promise 的结果为 true,否则为 false
  41. */
  42. function confirm(option: {
  43. title?: string,
  44. content?: string,
  45. cancelText?: string,
  46. confirmText?: string,
  47. }) {
  48. return new Promise<boolean>((resolve, reject) => {
  49. uni.showModal({
  50. title: option.title || '',
  51. content: option.content || '',
  52. showCancel: true,
  53. cancelText: option.cancelText || '取消',
  54. confirmText: option.confirmText || '确定',
  55. success(res) {
  56. resolve(res.confirm);
  57. },
  58. fail(res) {
  59. reject(res);
  60. }
  61. })
  62. })
  63. }
  64. export {
  65. toast,
  66. alert,
  67. confirm,
  68. }
  69. export default {
  70. install(app: App<Element>) : void {
  71. app.config.globalProperties.$toast = toast;
  72. app.config.globalProperties.$alert = alert;
  73. app.config.globalProperties.$confirm = confirm;
  74. },
  75. }