LatestCouponActivityPopup.vue 5.83 KB
<template>
    <view class="latest-activity-popup-host">
        <uni-popup ref="popupRef" class="latest-activity-popup" type="center" background-color="#fff"
            border-radius="16px" :is-mask-click="true" @maskClick="onMaskDismiss">
            <view v-if="activity" class="latest-activity-card">
                <view class="latest-activity-close" @click.stop="onHeaderCloseTap">
                    <uni-icons type="closeempty" size="20" color="#999999" />
                </view>
                <image class="latest-activity-illust" :src="illustSrc" mode="widthFix" />
                <view class="latest-activity-title">{{ activity.activityName }}</view>
                <scroll-view scroll-y class="latest-activity-scroll" :show-scrollbar="true">
                    <view class="latest-activity-body">
                        <rich-text class="latest-activity-body-rich"
                            :nodes="formatLatestActivityRichText(activity.agreementContent)"></rich-text>
                    </view>
                </scroll-view>
                <van-button class="latest-activity-action" round block color="#FF5000"
                    @click="onParticipate">立即参拍</van-button>
            </view>
        </uni-popup>
    </view>
</template>

<script lang="ts">
import { Component, Prop, Ref, Vue, Watch } from "vue-facing-decorator";
import { couponImageUrl } from "@/common/couponImageUrl";

export interface LatestActivityPopupData {
    activityName: string;
    agreementContent: string;
}

@Component({
    emits: ["close"],
})
export default class LatestCouponActivityPopup extends Vue {
    @Prop({ type: Boolean, default: false })
    show!: boolean;

    @Prop({ type: Object, default: null })
    activity!: LatestActivityPopupData | null;

    @Ref popupRef: any;

    @Watch("show")
    onShowPropChanged(): void {
        if (!this.show) {
            this.dismissPopupShell();
            return;
        }
        if (this.activity != null) {
            this.presentWhenShown();
        }
    }

    @Watch("activity")
    onActivityPropChanged(): void {
        if (this.show && this.activity != null) {
            this.presentWhenShown();
        }
    }

    get illustSrc(): string {
        return couponImageUrl("Promotion.png");
    }

    /**
     * 与 AgreementPage 一致:弱化编辑器内联样式,便于小程序 rich-text 排版
     */
    formatLatestActivityRichText(html: string): string {
        if (!html) {
            return "";
        }
        let s = html;
        s = s.replace(/class="mce-.*?"/g, "");
        s = s.replace(/width:\s*\d+px;/g, "");
        s = s.replace(/height:\s*\d+px;/g, "height: auto;");
        s = s.replace(/<(\/)?pre[^>]*>/gi, "");
        return s;
    }

    private dismissPopupShell(): void {
        this.popupRef?.close?.("center");
    }

    private presentWhenShown(): void {
        this.scheduleUniPopupOpen();
    }

    onMaskDismiss(): void {
        if (!this.activity) {
            return;
        }
        this.$emit("close");
    }

    onHeaderCloseTap(): void {
        if (!this.activity) {
            return;
        }
        this.popupRef?.close?.("center");
        this.$emit("close");
    }

    onParticipate(): void {
        if (!this.activity) {
            return;
        }
        this.popupRef?.close?.("center");
        uni.switchTab({ url: "/pages/tabbar/AuctionPage" });
        this.$emit("close");
    }

    private scheduleUniPopupOpen(): void {
        const popupInstance = (): any =>
            this.popupRef ?? (this.$refs as { popupRef?: unknown }).popupRef;
        const tryOpenCenter = (): boolean => {
            const ref = popupInstance();
            if (ref && typeof ref.open === "function") {
                ref.open("center");
                return true;
            }
            return false;
        };
        this.$nextTick(() => {
            this.$nextTick(() => {
                if (tryOpenCenter()) {
                    return;
                }
                setTimeout(() => {
                    if (tryOpenCenter()) {
                        return;
                    }
                    setTimeout(() => {
                        if (!tryOpenCenter()) {
                            console.warn(
                                "[LatestCouponActivityPopup] uni-popup.open 不可用,popupRef=",
                                popupInstance(),
                            );
                        }
                    }, 280);
                }, 80);
            });
        });
    }
}
</script>

<style scoped lang="scss">
.latest-activity-popup-host {
    position: relative;
    z-index: 20000;
}

.latest-activity-popup-host :deep(.uni-popup) {
    z-index: 20000 !important;
}

.latest-activity-card {
    position: relative;
    width: 300px;
    max-width: 86vw;
    padding: 24px 20px 20px;
    box-sizing: border-box;
}

.latest-activity-close {
    position: absolute;
    right: 12px;
    top: 12px;
    z-index: 2;
    padding: 4px;
}

.latest-activity-illust {
    display: block;
    width: 120px;
    margin: 0px auto 0px;
}

.latest-activity-title {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-word;
    font-size: 20px;
    font-weight: 600;
    color: #323233;
    line-height: 28px;
    text-align: center;
    margin-bottom: 12px;
}

.latest-activity-scroll {
    max-height: 220px;
    margin-bottom: 16px;
}

.latest-activity-body {
    background: #f7f8fa;
    border-radius: 8px;
    padding: 12px;
}

.latest-activity-body-rich {
    font-size: 13px;
    color: #646566;
    line-height: 20px;
    white-space: normal !important;
    word-wrap: break-word !important;
    word-break: break-all !important;
    width: 100% !important;
    max-width: 100% !important;
}

.latest-activity-action {
    margin-top: 4px;
}
</style>