dialog.vue 3.39 KB
<template>
  <van-popup round v-model="isShow" position="bottom" :style="{ height: '75%' }" class="popup" @close="onClose" :close-on-click-overlay="false">
    <div class="title">{{ title }}</div>
    <!-- <div v-if="loading"> -->
    <div class="content">
      <slot></slot>
    </div>
    <div class="btn-group">
      <div v-if="!hideCancelBtn" class="btn cancel" @click="handleCancel">取消</div>
      <div v-if="!hideConfirmBtn" class="btn confirm" @click="handleConfirm" :class="{ disabled: loading }">
        {{ loading ? '提交中...' : '确定' }}
      </div>
      <div v-if="showCloseBtn" class="close-btn" @click="handleCancel">关闭</div>
    </div>
    <!-- </div> -->

    <template v-if="loading">
      <div class="mask">
        <div class="loading-home">
          <van-loading size="50" color="#1989fa" />
        </div>
      </div>
    </template>
  </van-popup>
</template>

<script>
export default {
  data() {
    return {
      isShow: this.isVisible,
      loading: false,
      confirmPromise: null // 保存最近一次 confirm promise
    };
  },
  props: {
    title: {
      type: String,
      default: '驳回'
    },
    isVisible: {
      type: Boolean,
      default: true
    },
    hideCancelBtn: {
      type: Boolean,
      default: false
    },
    hideConfirmBtn: {
      type: Boolean,
      default: false
    },
    showCloseBtn: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isVisible(val) {
      this.isShow = val;
    }
  },
  methods: {
    async handleConfirm() {
      if (this.loading) return; // 避免重复点击

      this.$emit('confirm');

    },

    handleCancel() {
      this.$emit('cancel');
      this.isShow = false;
    },

    onClose() {
      this.$emit('update:visible', false);
    },

    /**
     * 公开方法:允许外部手动停止 loading 状态
     */
    stopLoading() {
      this.loading = false;
    },
    /**
     * 公开方法:允许外部手动开始 loading 状态
     */
    startLoading() {
      this.loading = true;
    }
  }
};
</script>

<style scoped lang="scss">
.popup {
  .title {
    height: 55px;
    line-height: 55px;
    box-shadow: inset 0 -0.5px 0 0 #e8e8ea;
    color: #333333;
    font-size: 16px;
    text-align: center;
    border-bottom: 1px solid #eff0f2;
  }

  .content {
    height: calc(100% - 111px);
    overflow-y: scroll;
  }

  .btn-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 16px;

    .btn {
      flex: 1;
      border-radius: 6px;
      height: 41px;
      line-height: 41px;
      font-size: 16px;
      cursor: pointer;

      &.disabled {
        opacity: 0.6;
        pointer-events: none;
      }
    }
    .close-btn {
      width: 100%;
      height: 40px;
      line-height: 40px;
      border-radius: 6px;
      opacity: 1;
      background: #f4f5f7;
      text-align: center;
      color: #333333;
      font-size: 16px;
    }

    .cancel {
      border: 1px solid #dee0e3;
      background: #f4f5f7;
      color: #292d33;
      text-align: center;
    }

    .confirm {
      background: #347bff;
      color: #ffffff;
      text-align: center;
      margin-left: 11px;
    }
  }
  .mask {
    position: absolute;
    height: 100%;
    width: 100%;
    background: #ffffff;
    opacity: 0.6;
    top: 0;
  }
  .loading-home {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}
</style>