modal-popup.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <view class="modal-popup">
  3. <uni-popup ref="modalPopup" is-mask-click>
  4. <uni-popup-dialog :type="msgType" :cancelText="showDialogOptions.cancelText"
  5. :confirmText="showDialogOptions.confirmText" :title="showDialogOptions.title"
  6. :content="showDialogOptions.content" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
  7. </uni-popup>
  8. <uni-popup ref="popupRef" is-mask-click>
  9. <view class="popup-content column">
  10. <text class="bold colo0 mb10">权限说明</text>
  11. <text class="fs24 base-color-gray">{{showDialogOptions.content}}</text>
  12. </view>
  13. </uni-popup>
  14. </view>
  15. </template>
  16. <script setup>
  17. import {
  18. onMounted,
  19. ref
  20. } from "vue";
  21. name: 'modal-popup'
  22. const emits = defineEmits(['update:show', 'confirm', 'close'])
  23. const props = defineProps({
  24. msgType: {
  25. type: String,
  26. default: 'info'
  27. }
  28. })
  29. // 实例
  30. const modalPopup = ref(null)
  31. const popupRef = ref(null)
  32. // dialog对话框文本
  33. const showDialogOptions = ref({
  34. cancelText: '取消',
  35. confirmText: '确认',
  36. title: '提示',
  37. content: '',
  38. popType: 'dialog', //dialog:模态框,popup:无取消确认弹窗
  39. confirm: null,
  40. close: null,
  41. })
  42. /**
  43. * @param {String} cancelText:'取消'
  44. * @param {String} confirmText:'同意'
  45. * @param {String} title:'标题'
  46. * @param {String} content:'内容'
  47. * @param {String} popType:'dialog'|'popup'
  48. * @param {Function} confirm:()=>{}
  49. * @param {Function} close:()=>{}
  50. */
  51. const open = (options = {}, upType = 'centert') => {
  52. for (let key in showDialogOptions.value) {
  53. if (options[key]) showDialogOptions.value[key] = options[key]
  54. }
  55. if (options.popType == 'dialog') {
  56. popupRef.value.close()
  57. modalPopup.value.open()
  58. } else if (options.popType == 'popup') {
  59. modalPopup.value.close()
  60. popupRef.value.open(upType)
  61. }
  62. }
  63. // 确认
  64. const dialogConfirm = () => {
  65. emits('confirm')
  66. if (showDialogOptions.value.confirm) {
  67. showDialogOptions.value.confirm()
  68. } else {
  69. modalPopup.value.close()
  70. popupRef.value.close()
  71. }
  72. }
  73. // 取消
  74. const dialogClose = () => {
  75. emits('close')
  76. if (showDialogOptions.value.close) {
  77. showDialogOptions.value.close()
  78. } else {
  79. if (showDialogOptions.value.popType == 'dialog') {
  80. modalPopup.value.close()
  81. } else if (showDialogOptions.value.popType == 'popup') {
  82. popupRef.value.close()
  83. }
  84. }
  85. }
  86. defineExpose({
  87. open,
  88. dialogConfirm,
  89. dialogClose
  90. })
  91. onMounted(() => {
  92. })
  93. </script>
  94. <style lang="scss">
  95. .modal-popup {
  96. position: fixed;
  97. z-index: 10099;
  98. .popup-content {
  99. position: fixed;
  100. left: 0;
  101. top: 50rpx;
  102. width: calc(100% - 100rpx);
  103. background: #fff;
  104. padding: 20rpx;
  105. margin: 30rpx;
  106. border-radius: 30rpx;
  107. box-shadow: 0px 0px 10px 1px rgba(0, 0, 0, .3);
  108. }
  109. }
  110. </style>