MessageBox.uvue 6.77 KB
<template>
    <un-popup :mode="position"
          :show="visible"
          :z-index="zIndex"
          @close="handlePopupClose"
        >
        <view class="tips-wrapper"
              :class="[{'radius': position === 'center'}]"
              :style="popupStyle">
            <view class="tips-content">
                <view class="icon-close-box flex"
                      @click="handleHide">
                    <uni-icons class="icon-close"
                               color="#1A1A1A"
                               :size="18"
                               type="closeempty">
                    </uni-icons>
                </view>
                <view class="tips-title">
                    <template v-if="back">
                        <view class="icon-back"
                              :style="{ display: 'flex', alignItems: 'center', justifyContent: 'center' }"
                              @click="handleBack">
                            <un-icon name="arrow-left" size="20"></un-icon>
                        </view>
                    </template>
                    <text class="tips-title-text">{{ title }}</text>
                </view>
                <text v-if="content"
                      class="tips-text">
                    {{ content }}
                </text>
                <slot>
                </slot>
            </view>
            <view v-if="isFooter"
                  class="tips-footer">
                <view v-if="showCancelBtn"
                      class="tips-btn close"
                      @click="cancel">
                    <text class="tips-btn-text tips-btn-text-close">{{ cancelBtnText }}</text>
                </view>
                <view class="tips-btn preview"
                      @click="confirm">
                    <text class="tips-btn-text tips-btn-text-preview">{{ confirmBtnText }}</text>
                </view>
            </view>
            <template v-else>
                <slot name="footer">
                </slot>
            </template>
        </view>
    </un-popup>
</template>

<script setup lang="uts">

const props = defineProps({
	isAutoClose: {
		type: Boolean,
		default: true
	},
	position: {
		type: String,
		default: "center"
	},
	popupW: Number,
	popupH: {
		type: Number,
		default: 372
	},
	isFooter: {
		type: Boolean,
		default: true
	},
	title: {
		type: String,
		default: "提示"
	},
	content: {
		type: String,
		default: ""
	},
	confirmBtnText: {
		type: String,
		default: "确定"
	},
	cancelBtnText: {
		type: String,
		default: "取消"
	},
	maxHeight: Number,
	maxWidth: Number,
	back: Boolean,
	showCancelBtn: {
		type: Boolean,
		default: true
	},
	zIndex: {
		type: Number,
		default: 10_075
	}
});

const emit = defineEmits(["close",
	"cancel",
	"confirm"]);

const visible = ref<boolean>(false);
const popupRef= ref(null);
const popupStyle = computed(() => {
	const hasExplicitW = props.popupW != null && props.popupW > 0;
	const isCenter = props.position === "center";
	const style: UTSJSONObject = {
		minHeight: props.popupH != null && props.popupH > 0 ? `${props.popupH}px` : "100%",
		maxHeight: props.maxHeight != null && props.maxHeight > 0 ? `${props.maxHeight}px` : "100%"
	};
	if (hasExplicitW) {
		let wPx = props.popupW as number;
		if (isCenter) {
			const info = uni.getWindowInfo();
			const winW = info.windowWidth > 0 ? info.windowWidth : 375;
			const capPx = Math.floor(winW * 0.85);
			wPx = wPx < capPx ? wPx : capPx;
			style.alignSelf = "center";
		}
		if (props.maxWidth != null && props.maxWidth > 0) {
			wPx = wPx < props.maxWidth ? wPx : props.maxWidth;
		}
		style.width = `${wPx}px`;
	} else if (isCenter) {
		const info = uni.getWindowInfo();
		const winW = info.windowWidth > 0 ? info.windowWidth : 375;
		const capPx = Math.floor(winW * 0.85);
		const maxCap = props.maxWidth != null && props.maxWidth > 0 ? props.maxWidth : 360;
		const wPx = capPx < maxCap ? capPx : maxCap;
		style.width = `${wPx}px`;
		style.alignSelf = "center";
	} else {
		style.width = "100%";
		if (props.maxWidth != null && props.maxWidth > 0) {
			style.maxWidth = `${props.maxWidth}px`;
		}
	}
	return style;
});

function close(): void {
	if (props.isAutoClose) {
		visible.value = false;
	}
	emit("close");
}
function hide(): void {
	visible.value = false;
}
function handleBack(): void {
	emit("close");
}
function open(): void {
	visible.value = true;
}

function cancel(): void {
	if (props.isAutoClose) {
		visible.value = false;
	}
	emit("cancel");
}

function confirm(): void {
	if (props.isAutoClose) {
		visible.value = false;
	}
	emit("confirm");
}

function handlePopupClose(): void {
	visible.value = false;
	emit("close");
}

// 处理关闭按钮点击(不触发事件,只隐藏)
function handleHide(): void {
	hide();
}

defineExpose({ open, close, hide });
</script>

<style scoped lang="scss">
  .tips-wrapper {
    background: #ffffff;
    border-radius: 10px;
    height: 100%;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    .tips-content {
      width: 100%;
      padding: 24px;
      box-sizing: border-box;
      flex: 1;
      min-height: 0;
      position: relative;
      display: flex;
      flex-direction: column;
      .icon-close-box {
        position: absolute;
        right: 0;
        top: 0;
        width: 60px;
        height: 60px;
        justify-content: center;
        z-index: 2;
        align-items: center;
      }

      .tips-title {
        position: relative;
        width: 100%;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;

        .icon-back {
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }

    .tips-text {
      font-size: 15px;
      color: #5a5e66;
      letter-spacing: 0.5px;
      line-height: 20px;
      text-align: center;
      margin-top: 24px;
    }

    .tips-footer {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 16px 24px 24px;
      flex-shrink: 0;
      border-top: 1px solid #f1f3f5;
      flex-direction: row;
    }

    .tips-btn {
      flex: 1;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      height: 44px;
      border-radius: 8px;
    }

    .tips-btn.close {
      background-color: #f1f5f9;
      margin-right: 12px;
    }

    .tips-btn.preview {
      background-color: #3b82f6;
    }
  }

  .tips-title-text {
    font-weight: 700;
    font-size: 18px;
    color: #1e293b;
    text-align: center;
  }

  .tips-btn-text {
    text-align: center;
    font-size: 16px;
    font-weight: 700;
  }

  .tips-btn-text-close {
    color: #64748b;
  }

  .tips-btn-text-preview {
    color: #ffffff;
  }
  .radius{
    border-radius: 10px;

  }
</style>