MessageBox.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <HModal
  3. :visible="visible"
  4. :show-close="showClose"
  5. :mask="mask"
  6. :close-on-click-mask="closeOnClickMask"
  7. :close-on-press-escape="closeOnPressEscape"
  8. @hide="handleAction('close')"
  9. >
  10. <template>
  11. <div class="h-message-box" v-if="type !== 'alert'">
  12. <header v-if="title" class="h-message-box__header">
  13. <HIcon v-if="icon" :type="icon" />
  14. {{ title }}
  15. </header>
  16. <main class="h-message-box__body">
  17. <div class="h-message-box__message">{{ message }}</div>
  18. </main>
  19. <footer class="h-message-box__footer">
  20. <div plain :class="cancelButtonClass" v-if="!hideCancleButton" @click="handleAction('cancel')">{{ cancelButtonText }}</div>
  21. <div ref="confirm" :class="confirmButtonClass" @click="handleAction('confirm')">{{ confirmButtonText }}</div>
  22. </footer>
  23. </div>
  24. <div class="h-alert-box" v-else>
  25. <div class="warn-icon">
  26. <h-icon :type="icon" style="font-size: 40px;line-height: 52px" />
  27. </div>
  28. <p class="message">{{ message }}</p>
  29. <div class="submit-btn" @click="handleAction('close')">确定</div>
  30. </div>
  31. </template>
  32. </HModal>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'HMessageBox',
  37. data () {
  38. return {
  39. visible: false,
  40. title: undefined,
  41. icon: undefined,
  42. message: '',
  43. action: '',
  44. showConfirmButton: true,
  45. showCancelButton: false,
  46. confirmButtonText: '',
  47. cancelButtonText: '',
  48. confirmButtonClass: '',
  49. cancelButtonClass: '',
  50. showClose: true,
  51. mask: true,
  52. closeOnClickMask: true,
  53. closeOnPressEscape: true,
  54. hideCancleButton: false,
  55. type: ''
  56. }
  57. },
  58. watch: {
  59. visible (val) {
  60. if (val) {
  61. this.uid++
  62. }
  63. }
  64. },
  65. methods: {
  66. getSafeClose() {
  67. const currentId = this.uid
  68. return () => {
  69. this.$nextTick(() => {
  70. if (currentId === this.uid) this.doClose()
  71. })
  72. }
  73. },
  74. doClose() {
  75. if (!this.visible) return
  76. this.visible = false
  77. this._closing = true
  78. this.onClose && this.onClose()
  79. setTimeout(() => {
  80. if (this.action) this.callback(this.action, this)
  81. })
  82. },
  83. handleAction (action) {
  84. this.action = action
  85. if (typeof this.beforeClose === 'function') {
  86. this.close = this.getSafeClose()
  87. this.beforeClose(action, this, this.close)
  88. } else {
  89. this.doClose()
  90. }
  91. }
  92. }
  93. }
  94. </script>
  95. <style lang="less">
  96. .h-message-box {
  97. width: 420px;
  98. padding: 10px 15px;
  99. }
  100. .h-message-box__header {
  101. padding: 5px 0;
  102. font-size: 18px;
  103. }
  104. .h-message-box__body {
  105. padding: 10px 0;
  106. color: #202020;
  107. line-height: 1.5;
  108. }
  109. .h-message-box__footer {
  110. padding-top: 10px;
  111. text-align: right;
  112. .h-button {
  113. font-size: 12px;
  114. }
  115. .h-button + .h-button {
  116. margin-left: 10px;
  117. }
  118. }
  119. .h-alert-box {
  120. width: 335px;
  121. height: 256px;
  122. padding: 48px 50px 30px;
  123. .warn-icon {
  124. width: 52px;
  125. height: 52px;
  126. border: 1px solid #ebebeb;
  127. border-radius: 50%;
  128. text-align: center;
  129. line-height: 52px;
  130. color: #1FE4DC;
  131. margin: 0 auto 10px;
  132. font-weight: bold;
  133. font-size: 40px;
  134. }
  135. .message {
  136. color: #909090;
  137. font-size: 16px;
  138. font-weight: 600;
  139. line-height: 20px;
  140. width: 194px;
  141. text-align: center;
  142. margin: 0 auto;
  143. }
  144. .submit-btn {
  145. width: 100%;
  146. background: #1FE4DC;
  147. line-height: 40px;
  148. text-align: center;
  149. font-weight: bold;
  150. margin-top: 30px;
  151. cursor: pointer;
  152. }
  153. }
  154. </style>