cl-confirm.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view class="cl-confirm">
  3. <cl-dialog :visible.sync="visible" :title="title" :width="width" @close="onClose">
  4. <view class="cl-confirm__message" v-if="message && !$slots.default">{{ message }}</view>
  5. <slot v-else></slot>
  6. <view class="cl-confirm__footer" slot="footer">
  7. <button
  8. v-if="showCancelButton"
  9. class="cl-confirm__button is-cancel"
  10. plain
  11. size="small"
  12. @tap="close('cancel')"
  13. >
  14. {{ cancelButtonText }}
  15. </button>
  16. <button
  17. v-if="showConfirmButton"
  18. class="cl-confirm__button is-confirm"
  19. plain
  20. size="small"
  21. @tap="close('confirm')"
  22. >
  23. {{ confirmButtonText }}
  24. </button>
  25. </view>
  26. </cl-dialog>
  27. </view>
  28. </template>
  29. <script>
  30. /**
  31. * confirm 确认框
  32. * @description 确认框
  33. * @tutorial https://docs.cool-js.com/uni/components/operate/dialog.html
  34. * @example <cl-confirm ref="confirm"></cl-confirm>
  35. */
  36. export default {
  37. name: "cl-confirm",
  38. data() {
  39. return {
  40. visible: false,
  41. closed: false,
  42. title: null,
  43. message: null,
  44. width: null,
  45. showCancelButton: null,
  46. cancelButtonText: null,
  47. showConfirmButton: null,
  48. confirmButtonText: null,
  49. closeOnClickModal: null,
  50. beforeClose: null,
  51. callback: null,
  52. duration: null,
  53. promise: {
  54. resolve: null,
  55. reject: null
  56. },
  57. timer: null
  58. };
  59. },
  60. methods: {
  61. open({
  62. title,
  63. message,
  64. width = "500rpx",
  65. showCancelButton = true,
  66. showConfirmButton = true,
  67. confirmButtonText = "确认",
  68. cancelButtonText = "取消",
  69. closeOnClickModal = true,
  70. callback,
  71. duration,
  72. beforeClose
  73. }) {
  74. return new Promise((resolve, reject) => {
  75. this.promise = {
  76. resolve,
  77. reject
  78. };
  79. this.visible = true;
  80. this.closed = false;
  81. // 设置选项
  82. this.$nextTick(() => {
  83. this.title = title;
  84. this.message = message;
  85. this.width = width;
  86. this.showConfirmButton = showConfirmButton;
  87. this.showCancelButton = showCancelButton;
  88. this.confirmButtonText = confirmButtonText;
  89. this.cancelButtonText = cancelButtonText;
  90. this.closeOnClickModal = closeOnClickModal;
  91. this.callback = callback;
  92. this.duration = duration;
  93. this.beforeClose = beforeClose;
  94. clearTimeout(this.timer);
  95. // 是否定时关闭
  96. if (duration) {
  97. this.timer = setTimeout(() => {
  98. this.close("close");
  99. }, duration);
  100. }
  101. });
  102. });
  103. },
  104. close(action = "close") {
  105. const done = () => {
  106. if (this.callback) {
  107. this.callback({ action });
  108. } else {
  109. if (action === "confirm") {
  110. this.promise.resolve();
  111. } else {
  112. this.promise.reject(action);
  113. }
  114. }
  115. this.$nextTick(() => {
  116. this.visible = false;
  117. this.closed = true;
  118. });
  119. };
  120. if (this.beforeClose) {
  121. this.beforeClose({ action, done });
  122. } else {
  123. done();
  124. }
  125. },
  126. onClose(action) {
  127. if (!this.closed) {
  128. this.close(action);
  129. }
  130. }
  131. }
  132. };
  133. </script>