ProductIntroSwiper.uvue 10.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
<template>
    <!-- <view v-if="!loadComplete" class="intro-swiper-loading">
        <BannerSkeleton />
    </view> -->
    <view class="intro-swiper-custom-wrap fade-in">
        <view class="intro-swiper-custom-content">
            <view class="intro-hero-head">
                <view class="intro-hero-title-wrap flex-row items-center">
                    <text class="intro-hero-eyebrow">{{ eyebrow }}</text>
                    <text class="intro-hero-title">{{ heroTitle }}</text>
                </view>
                <view v-if="showSwitchProduct"
                      class="intro-switch-btn"
                      hover-class="intro-switch-btn-hover"
                      @click="onSwitchProductClick">
                    <view class="intro-switch-icon-box">
                        <un-icon name="reload" size="14" color="#2c66f7"></un-icon>
                    </view>
                    <text class="intro-switch-text">切换产品</text>
                </view>
                <view v-else-if="modules.length > 0" class="intro-hero-indicator-pill">
                    <text class="intro-hero-indicator-text">{{ currentSwiperIndex + 1 }}/{{ modules.length }}</text>
                </view>
            </view>
            <view class="intro-swiper-stack" v-if="modules.length > 0">
                <un-swiper class="intro-swiper"
                           :list="swiperList"
                           :autoplay="modules.length > 1"
                           :circular="modules.length > 1"
                           :indicator="false"
                           :height="swiperHeight"
                           image-mode="aspectFill"
                           @change="(e: any) => onSwiperChangeFromEvent(e)"
                           @click="(idx: any) => onSlideTapFromEvent(idx)"></un-swiper>
                <view v-if="hasDotsOverlay" class="intro-swiper-overlay">
                    <view class="intro-swiper-dots">
                        <view v-for="(_, i) in modules"
                              :key="i"
                              class="intro-dot"
                              :class="[{ active: i == currentSwiperIndex }]" />
                    </view>
                </view>
            </view>
            <view v-else class="intro-empty-un-wrap">
                <un-empty mode="data"
                          :text="emptyDisplayText" />
            </view>
        </view>
    </view>
</template>

<script setup lang="uts">
import { computed, ref, watch, withDefaults } from "vue";

import BannerSkeleton from "@/components/Skeleton/BannerSkeleton.uvue";
// ✅ 从 uview-unix 模块导入 UnSwiperItem 类型
import type { UnSwiperItem } from "@/uni_modules/uview-unix";
import { useScreenVars } from "@/services/adapter/hooks/index.uts";
import homeFramePageVars from "./styles/index.vars.uts";  // ✅ 直接导入

const props = withDefaults(defineProps<{
	loadComplete: boolean
	swiperList: UnSwiperItem[]
	modules: any[]
	eyebrow: string
	emptyTitle: string
	emptyText: string
	/** 与 simgle_app HomeSlot「切换产品」一致:展示按钮并向外触发选择流程 */
	showSwitchProduct?: boolean
}>(), {
	showSwitchProduct: false,
});

const emit = defineEmits<{
	swiperChange: [index: number]
	slideTap: [index: number]
	switchProduct: []
}>();

const currentSwiperIndex = ref<number>(0);

/** 轮播图高度:从 useScreenVars 中获取 --swiper-height 变量 */
const screenVars = useScreenVars(homeFramePageVars);

const swiperHeight = computed((): number => {
	// 从 CSS 变量中获取轮播图高度
	const heightStr = screenVars.value["--swiper-height"] as string | null;
	if (heightStr != null && heightStr !== "") {
		// 如果是 calc() 表达式,无法直接解析,返回默认值
		if (heightStr.indexOf("calc") >= 0) {
			// calc(var(--module-box-height) - 40px) 的情况
			// 尝试提取 module-box-height 的值
			const moduleBoxHeightStr = screenVars.value["--module-box-height"] as string | null;
			if (moduleBoxHeightStr != null && moduleBoxHeightStr.indexOf("px") > 0) {
				const moduleBoxHeight = parseInt(moduleBoxHeightStr.replace("px", ""));
				return moduleBoxHeight - 40; // 减去头部高度 40px
			}
		}
		// 如果是直接的 px 值
		if (heightStr.indexOf("px") > 0) {
			return parseInt(heightStr.replace("px", ""));
		}
	}
	// 默认高度(竖屏)
	return 380;
});

/** 无列表数据时 un-empty 主文案(仍可用父级 intro-empty-text) */
const emptyDisplayText = computed((): string => {
	const t = props.emptyText;
	if (t != null && t !== "") {
		return t;
	}
	return "暂无数据";
});

watch((): number => props.modules.length, (_len: number) => {
	currentSwiperIndex.value = 0;
});

/** 切换业务类型后列表可能条数不变但 URL 已变,需回到第一张 */
watch((): string => {
	const list = props.swiperList;
	const n = list.length;
	if (n == 0) {
		return "0|";
	}
	// ✅ 使用导入的 UnSwiperItem 类型
	const firstItem = list[0] as UnSwiperItem;
	const u = firstItem.url != null ? firstItem.url : "";
	const uStr = u !== "" ? `${u}` : "";
	return `${n}|${uStr}`;
}, (_sig: string) => {
	currentSwiperIndex.value = 0;
});

function moduleTitleAt(item: any): string {
	if (item == null) {
		return "";
	}
	const obj = item as UTSJSONObject;
	const value =
		(obj.detailTitle as null | string)
		?? (obj.materialTitle as null | string)
		?? (obj.shortTitle as null | string)
		?? (obj.materialName as null | string)
		?? (obj.title as null | string)
		?? (obj.name as null | string)
		?? "";
	return value;
}

const heroTitle = computed((): string => {
	const list = props.modules;
	if (list.length == 0) {
		return "";
	}
	const idx = currentSwiperIndex.value;
	if (idx < 0 || idx >= list.length) {
		return "";
	}
	return moduleTitleAt(list[idx]);
});

const hasDotsOverlay = computed((): boolean => {
	const list = props.modules;
	const idx = currentSwiperIndex.value;
	return list.length > 0 && idx >= 0 && idx < list.length;
});

function parseSwiperEventIndex(e: number | UniSwiperChangeEvent | null): number {
	if (e == null) {
		return 0;
	}
	if (typeof e === "number") {
		return e as number;
	}
	return e.detail.current;
}

function onSwiperChange(e: number | UniSwiperChangeEvent): void {
	const idx = parseSwiperEventIndex(e);
	currentSwiperIndex.value = idx;
	emit("swiperChange", idx);
}

function onSlideTap(index: number): void {
	emit("slideTap", index);
}

/** 模板里必须写成函数调用;事件类型用 any 收口再收窄 */
function onSwiperChangeFromEvent(e: any): void {
	onSwiperChange(e as number | UniSwiperChangeEvent);
}

function onSlideTapFromEvent(idx: any): void {
	const n = typeof idx === "number" ? (idx as number) : 0;
	onSlideTap(n);
}

function onSwitchProductClick(): void {
	emit("switchProduct");
}
</script>

<style scoped lang="scss">
.intro-swiper-loading {
  width: 100%;
  height: 100%;
  padding: 12px;
  box-sizing: border-box;
}

.intro-swiper-custom-wrap {
  width: 100%;
  height: 100%;
  position: relative;
  padding: 0;
  box-sizing: border-box;
}

.intro-swiper-custom-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  border-radius: 0;
  background: rgba(255, 255, 255, 0.86);
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.10);
  overflow: hidden;
}

.intro-hero-head {
  width: 100%;
  height: 40px;
  padding: 0 10px;
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(226, 236, 248, 0.8);
  background-color: var(--bg-card, #FFFFFF);
}

.intro-hero-title-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  font-size: 14px;
}

/* 对齐 simgle_app `HomeSlot.vue` .title-text .highlight-type */
.intro-hero-eyebrow {
  flex-shrink: 0;
  color: #2c66f7;
  font-weight: 700;
  margin-right: 8px;
  font-size: 18px;  // ✅ UVUE 不支持 em 单位,1.1em ≈ 18px
  line-height: 1.2;
  // text-shadow: 0 4px 8px rgba(44, 102, 247, 0.15);
}

.intro-hero-title {
  flex: 1;
  min-width: 0;
  margin-top: 0;
  font-size: 14px;
  font-weight: 700;
  color: #1e293b;
  line-height: 18px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.intro-hero-indicator-pill {
  height: 22px;
  min-width: 44px;
  padding: 0 8px;
  border-radius: 13px;
  background: #e9f1ff;
  border: 1px solid #cfe0f8;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.intro-hero-indicator-text {
  font-size: 11px;
  font-weight: 600;
  color: #2a5ecf;
}

/* 对齐 simgle_app `HomeSlot.vue` .switch-btn */
.intro-switch-btn {
  flex-shrink: 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 4px 10px 4px 6px;
  margin-left: 8px;
  background: rgba(44, 102, 247, 0.08);
  border-radius: 16px;
  border-width: 1px;
  border-style: solid;
  border-color: rgba(44, 102, 247, 0.15);
  box-sizing: border-box;
}

.intro-switch-btn-hover {
  opacity: 0.92;
  background: rgba(44, 102, 247, 0.14);
}

.intro-switch-icon-box {
  width: 18px;
  height: 18px;
  border-radius: 9px;
  background: #ffffff;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  margin-right: 6px;
  box-shadow: 0 2px 4px rgba(44, 102, 247, 0.12);
}

.intro-switch-text {
  font-size: 12px;
  color: #2c66f7;
  font-weight: 500;
  line-height: 14px;
}

.intro-swiper-stack {
  flex: 1;
  width: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
}

/* 无轮播数据:un-empty 在卡片内容区(标题下)水平垂直居中 */
.intro-empty-un-wrap {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  padding: 16px;
}

.intro-swiper {
  width: 100%;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.10);
}


.intro-swiper-overlay {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  z-index: 10;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  /* uni-app x 仅支持 background-image 的 linear-gradient */
  background-image: linear-gradient(to bottom, rgba(30, 58, 138, 0), rgba(30, 58, 138, 0.35));
  border-radius: 10px;
}

.intro-swiper-dots {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 2px;
  display: flex;
  flex-direction: row;
  justify-content: center;
  padding: 0;
}

.intro-dot {
  width: 5px;
  height: 5px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.42);
  margin: 0 3px;
  transition: all 0.3s ease;
}

.intro-dot.active {
  width: 14px;
  background: rgba(255, 255, 255, 0.95);
}

</style>