refactor: 确立独立子工程架构 (backend, admin, mpuniapp) 并同步更新 AGENTS.md 规范
这个提交包含在:
@@ -0,0 +1,122 @@
|
||||
const PAGE_PATH = '/pages/template/banner-tabs-long-list-nested-scroll/banner-tabs-long-list-nested-scroll'
|
||||
const ACTIVE_COLORS = ['rgb(0, 122, 255)', '#007AFF', '#007AFFFF']
|
||||
const INACTIVE_COLORS = ['rgb(85, 85, 85)', '#555555', '#555555FF']
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isAndroid = platformInfo.startsWith('android')
|
||||
const isIos = platformInfo.startsWith('ios')
|
||||
const isHarmony = platformInfo.startsWith('harmony')
|
||||
const isApp = isAndroid || isIos || isHarmony
|
||||
|
||||
describe('template-banner-tabs-long-list-nested-scroll', () => {
|
||||
if (!isApp) {
|
||||
it('skip non-app platform', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let page
|
||||
|
||||
async function waitForSwiperCurrent(target, timeout = 4000) {
|
||||
const start = Date.now()
|
||||
await page.waitFor(async () => {
|
||||
const state = await page.callMethod('jest_getState')
|
||||
return state != null && state.swiperIndex == target && state.animationFinishIndex == target || Date.now() - start > timeout
|
||||
})
|
||||
|
||||
const state = await page.callMethod('jest_getState')
|
||||
expect(state.swiperIndex).toBe(target)
|
||||
expect(state.animationFinishIndex).toBe(target)
|
||||
}
|
||||
|
||||
async function waitForListState(index, matcher, timeout = 5000) {
|
||||
const start = Date.now()
|
||||
await page.waitFor(async () => {
|
||||
const state = await page.callMethod('jest_getListState', index)
|
||||
return matcher(state) || Date.now() - start > timeout
|
||||
})
|
||||
|
||||
return await page.callMethod('jest_getListState', index)
|
||||
}
|
||||
|
||||
async function waitForIndicatorChange(indicator, beforeTransform, beforeWidth, timeout = 4000) {
|
||||
const start = Date.now()
|
||||
await page.waitFor(async () => {
|
||||
const transform = await indicator.style('transform')
|
||||
const width = (await indicator.size()).width
|
||||
return transform !== beforeTransform || width !== beforeWidth || Date.now() - start > timeout
|
||||
})
|
||||
|
||||
const transform = await indicator.style('transform')
|
||||
const width = (await indicator.size()).width
|
||||
expect(transform !== beforeTransform || width !== beforeWidth).toBe(true)
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
await page.waitFor('view')
|
||||
await page.waitFor('scroll-view')
|
||||
await page.waitFor('swiper')
|
||||
await page.waitFor(800)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await page.callMethod('jest_setSwiperCurrent', 0)
|
||||
await page.callMethod('jest_prepareMockData')
|
||||
await waitForListState(0, (state) => state != null && state.renderedCount > 0)
|
||||
await page.waitFor(200)
|
||||
})
|
||||
|
||||
it('renders nested scroll banner tabs and initial list', async () => {
|
||||
const swipers = await page.$$('swiper')
|
||||
const tabs = await page.$$('.swiper-tabs-item')
|
||||
const firstListState = await page.callMethod('jest_getListState', 0)
|
||||
const secondListState = await page.callMethod('jest_getListState', 1)
|
||||
const pageState = await page.callMethod('jest_getState')
|
||||
|
||||
expect(swipers.length).toBe(2)
|
||||
expect(tabs.length).toBe(4)
|
||||
expect(await tabs[0].text()).toBe('最新上架')
|
||||
expect(await tabs[1].text()).toBe('免费热榜')
|
||||
expect(pageState.currentType).toBe('UpdatedDate')
|
||||
expect(pageState.nestedScrollChildId).toBe('list-id-1')
|
||||
expect(firstListState.renderedCount).toBe(10)
|
||||
expect(firstListState.firstTitle).toBe('UpdatedDate mock 1')
|
||||
expect(secondListState.renderedCount).toBe(0)
|
||||
expect(ACTIVE_COLORS).toContain(await tabs[0].style('color'))
|
||||
expect(INACTIVE_COLORS).toContain(await tabs[1].style('color'))
|
||||
})
|
||||
|
||||
it('switches tab state and nested child binding after tapping', async () => {
|
||||
const tabs = await page.$$('.swiper-tabs-item')
|
||||
const indicator = await page.$('.swiper-tabs-indicator')
|
||||
const beforeTransform = await indicator.style('transform')
|
||||
const beforeWidth = (await indicator.size()).width
|
||||
|
||||
await tabs[1].tap()
|
||||
await waitForSwiperCurrent(1)
|
||||
const secondListState = await waitForListState(1, (state) => state != null && state.renderedCount > 0)
|
||||
const pageState = await page.callMethod('jest_getState')
|
||||
|
||||
expect(pageState.currentType).toBe('FreeHot')
|
||||
expect(pageState.nestedScrollChildId).toBe('list-id-2')
|
||||
expect(secondListState.firstTitle).toBe('FreeHot mock 1')
|
||||
expect(ACTIVE_COLORS).toContain(await tabs[1].style('color'))
|
||||
expect(INACTIVE_COLORS).toContain(await tabs[0].style('color'))
|
||||
|
||||
await waitForIndicatorChange(indicator, beforeTransform, beforeWidth)
|
||||
})
|
||||
|
||||
it('supports programmatic switch to the last list tab', async () => {
|
||||
const changed = await page.callMethod('jest_setSwiperCurrent', 3)
|
||||
expect(changed).toBe(true)
|
||||
await waitForSwiperCurrent(3)
|
||||
|
||||
const fourthListState = await waitForListState(3, (state) => state != null && state.renderedCount > 0)
|
||||
const pageState = await page.callMethod('jest_getState')
|
||||
|
||||
expect(pageState.currentType).toBe('HotList')
|
||||
expect(pageState.nestedScrollChildId).toBe('list-id-4')
|
||||
expect(fourthListState.firstTitle).toBe('HotList mock 1')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,355 @@
|
||||
<template>
|
||||
<scroll-view ref="pageScrollView" class="page" :bounces="false" type="nested"
|
||||
:refresher-triggered="refresherTriggered" refresher-enabled="true" @refresherrefresh="onRefresherrefresh"
|
||||
>
|
||||
<nested-scroll-header>
|
||||
<swiper ref="header" indicator-dots="true" circular="true" style="height: 150px;">
|
||||
<swiper-item v-for="i in 3" :item-id="''+i" style="height: 100%;align-items: center;justify-content: center;">
|
||||
<text>{{i}}</text>
|
||||
<!-- 可以将上述text组件替换为image组件,实现轮播图效果 -->
|
||||
</swiper-item>
|
||||
</swiper>
|
||||
</nested-scroll-header>
|
||||
<nested-scroll-body style="height: 100%;">
|
||||
<view style="flex: 1;">
|
||||
<scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false">
|
||||
<view class="flex-row" style="align-self: flex-start;">
|
||||
<text ref="swipertab" class="swiper-tabs-item" :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''"
|
||||
v-for="(item, index) in swiperList" :key="index" @click="onTabClick(index)">
|
||||
{{item.name}}
|
||||
</text>
|
||||
</view>
|
||||
<view ref="indicatorNode" class="swiper-tabs-indicator"></view>
|
||||
</scroll-view>
|
||||
<swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
|
||||
@animationfinish="onSwiperAnimationfinish">
|
||||
<swiper-item class="swiper-item" v-for="(item, index) in swiperList" :key="index">
|
||||
<long-page ref="longPageRef" :id="item.id" :type="item.type" :preload="item.preload"></long-page>
|
||||
</swiper-item>
|
||||
</swiper>
|
||||
</view>
|
||||
</nested-scroll-body>
|
||||
</scroll-view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
/**
|
||||
* @description: 支持顶部 Banner、横向标签栏和长列表内容联动的嵌套滚动页面。
|
||||
* @logic: 顶部 Banner 先占据滚动头部区域,下方通过标签和 swiper 切换不同长列表,父子滚动容器共同完成嵌套滚动体验。
|
||||
* @optimization: 各标签列表按需预加载,并复用同一套标签尺寸缓存和指示器更新逻辑,减少嵌套切换时的重复计算。
|
||||
*/
|
||||
import longPage from '../search-header-long-list/long-list-page.uvue';
|
||||
|
||||
type SwiperTabsItem = {
|
||||
x : number,
|
||||
w : number
|
||||
}
|
||||
|
||||
type SwiperViewItem = {
|
||||
type : string,
|
||||
name : string,
|
||||
id : string,
|
||||
preload : boolean,
|
||||
}
|
||||
|
||||
type JestTabsPageState = {
|
||||
swiperIndex : number,
|
||||
animationFinishIndex : number,
|
||||
tabCount : number,
|
||||
currentType : string,
|
||||
currentName : string,
|
||||
currentPreload : boolean,
|
||||
nestedScrollChildId : string,
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据权重在两个值之间执行线性插值.
|
||||
* @constructor
|
||||
* @param {number} value1 - 第一个值,该值应为下限.
|
||||
* @param {number} value2 - 第二个值,该值应为上限.
|
||||
* @param {number} amount - 应介于 0 和 1 之间,指示内插的权重.
|
||||
* @returns {number} 内插值
|
||||
*/
|
||||
function lerpNumber(value1 : number, value2 : number, amount : number) : number {
|
||||
return (value1 + (value2 - value1) * amount)
|
||||
}
|
||||
|
||||
const swiperList = ref([
|
||||
{
|
||||
type: 'UpdatedDate',
|
||||
name: '最新上架',
|
||||
id: "list-id-1",
|
||||
preload: true
|
||||
} as SwiperViewItem,
|
||||
{
|
||||
type: 'FreeHot',
|
||||
name: '免费热榜',
|
||||
id: "list-id-2",
|
||||
preload: false
|
||||
} as SwiperViewItem,
|
||||
{
|
||||
type: 'PaymentHot',
|
||||
name: '付费热榜',
|
||||
id: "list-id-3",
|
||||
preload: false
|
||||
} as SwiperViewItem,
|
||||
{
|
||||
type: 'HotList',
|
||||
name: '热门总榜',
|
||||
id: "list-id-4",
|
||||
preload: false
|
||||
} as SwiperViewItem
|
||||
] as SwiperViewItem[]);
|
||||
const swiperIndex = ref(0);
|
||||
let headerHeight = 0;
|
||||
let animationFinishIndex = 0;
|
||||
const tabScrollView = ref<UniElement | null>(null);
|
||||
const indicatorNode = ref<UniElement | null>(null);
|
||||
let swiperWidth = 0;
|
||||
let swiperTabsRect: SwiperTabsItem[] = [];
|
||||
let nestedScrollChildId = "";
|
||||
// 自动化测试
|
||||
let jestStateSwiperIndex = -1;
|
||||
// 仅在自动化测试程序化切页时启用,避免晚到的旧 animationfinish 把目标索引回写成旧值。
|
||||
let isTestFinishGuardEnabled = false
|
||||
|
||||
const pageScrollView = ref<UniScrollViewElement | null>(null);
|
||||
const header = ref<UniSwiperElement | null>(null);
|
||||
const tabScroll = ref<UniScrollViewElement | null>(null);
|
||||
const swipertab = ref<UniElement[]>([]);
|
||||
const swiper = ref<UniSwiperElement | null>(null);
|
||||
const longPageRef = ref<ComponentPublicInstance[]>([]);
|
||||
|
||||
const refresherTriggered = ref(false);
|
||||
function onRefresherrefresh(e : UniRefresherEvent) {
|
||||
refresherTriggered.value = true;
|
||||
longPageRef.value[swiperIndex.value].$callMethod('refreshData', () => {
|
||||
refresherTriggered.value = false;
|
||||
});
|
||||
}
|
||||
|
||||
function initSwiperItemData(index : number) {
|
||||
if (!swiperList.value[index].preload) {
|
||||
swiperList.value[index].preload = true;
|
||||
longPageRef.value[index].$callMethod('loadData', null)
|
||||
}
|
||||
//swiper页面切换需要重新赋值嵌套滚动子元素的id
|
||||
nestedScrollChildId = swiperList.value[index].id
|
||||
}
|
||||
|
||||
function updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
|
||||
const current_size = swiperTabsRect[current_index]
|
||||
const move_to_size = swiperTabsRect[move_to_index]
|
||||
|
||||
// 计算指示线 左边距 和 宽度 在移动过程中的线性值
|
||||
const indicator_line_x = lerpNumber(current_size.x, move_to_size.x, percentage)
|
||||
const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage)
|
||||
|
||||
// 更新指示线
|
||||
// #ifdef APP
|
||||
const x = indicator_line_x + indicator_line_w / 2
|
||||
indicatorNode.value?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
|
||||
// #endif
|
||||
// #ifdef WEB
|
||||
// TODO chrome windows系统 transform scaleX渲染bug
|
||||
const x = indicator_line_x
|
||||
indicatorNode.value?.style?.setProperty('width', `${indicator_line_w}px`)
|
||||
indicatorNode.value?.style?.setProperty('transform', `translateX(${x}px)`)
|
||||
// #endif
|
||||
|
||||
// 滚动到水平中心位置
|
||||
const scroll_x = x - swiperWidth / 2
|
||||
if (tabScroll.value !== null) {
|
||||
tabScroll.value!.scrollLeft = scroll_x
|
||||
}
|
||||
}
|
||||
|
||||
function setSwiperIndex(index : number, updateIndicator : boolean) {
|
||||
if (swiperIndex.value === index) {
|
||||
return
|
||||
}
|
||||
|
||||
swiperIndex.value = index
|
||||
|
||||
initSwiperItemData(index)
|
||||
|
||||
if (updateIndicator) {
|
||||
updateTabIndicator(index, index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
function onTabClick(index : number) {
|
||||
jestStateSwiperIndex = -1
|
||||
isTestFinishGuardEnabled = false
|
||||
setSwiperIndex(index, false)
|
||||
}
|
||||
|
||||
function onSwiperTransition(e : SwiperTransitionEvent) {
|
||||
// 微信 skyline 每项完成触发 Animationfinish,偏移值重置
|
||||
// 微信 webview 全部完成触发 Animationfinish,偏移值累加
|
||||
// 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
|
||||
// uni-app-x 和微信 webview 行为一致
|
||||
|
||||
const offset_x = e.detail.dx
|
||||
|
||||
// 计算当前索引并重置差异
|
||||
const current_offset_x = offset_x % swiperWidth
|
||||
const current_offset_i = offset_x / swiperWidth
|
||||
const current_index = animationFinishIndex + parseInt(current_offset_i + '')
|
||||
|
||||
// 计算目标索引及边界检查
|
||||
let move_to_index = current_index
|
||||
if (current_offset_x > 0 && move_to_index < swiperList.value.length - 1) {
|
||||
move_to_index += 1
|
||||
} else if (current_offset_x < 0 && move_to_index > 0) {
|
||||
move_to_index -= 1
|
||||
}
|
||||
|
||||
// 计算偏移百分比
|
||||
const percentage = Math.abs(current_offset_x) / swiperWidth
|
||||
|
||||
// 通知更新指示线
|
||||
if (current_index != move_to_index) {
|
||||
updateTabIndicator(current_index, move_to_index, percentage)
|
||||
// 首次可见时初始化数据
|
||||
initSwiperItemData(move_to_index)
|
||||
}
|
||||
}
|
||||
|
||||
function onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
|
||||
// 自动化测试程序化切页时,忽略晚到的旧完成事件,避免把目标索引回写成旧值。
|
||||
if (isTestFinishGuardEnabled && jestStateSwiperIndex >= 0 && e.detail.current != jestStateSwiperIndex) {
|
||||
return
|
||||
}
|
||||
isTestFinishGuardEnabled = false
|
||||
jestStateSwiperIndex = -1
|
||||
setSwiperIndex(e.detail.current, true)
|
||||
animationFinishIndex = e.detail.current
|
||||
}
|
||||
|
||||
function cacheTabItemsSize() {
|
||||
swiperTabsRect.length = 0
|
||||
swipertab.value.forEach((node) => {
|
||||
swiperTabsRect.push({
|
||||
x: node.offsetLeft,
|
||||
w: node.offsetWidth
|
||||
} as SwiperTabsItem)
|
||||
})
|
||||
}
|
||||
|
||||
onReady(() => {
|
||||
headerHeight = header.value!.offsetHeight
|
||||
swiperWidth = swiper.value!.getBoundingClientRect().width
|
||||
cacheTabItemsSize()
|
||||
updateTabIndicator(swiperIndex.value, swiperIndex.value, 1)
|
||||
});
|
||||
|
||||
// 自动化测试辅助
|
||||
function jest_prepareMockData() : boolean {
|
||||
const pageRefs = longPageRef.value
|
||||
if (pageRefs.length == 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
for (let i = 0; i < pageRefs.length; i++) {
|
||||
pageRefs[i].$callMethod('jest_useMockData', i == swiperIndex.value)
|
||||
swiperList.value[i].preload = i == swiperIndex.value
|
||||
}
|
||||
nestedScrollChildId = swiperList.value[swiperIndex.value].id
|
||||
return true
|
||||
}
|
||||
|
||||
function jest_setSwiperCurrent(index : number) : boolean {
|
||||
if (index < 0 || index >= swiperList.value.length) {
|
||||
return false
|
||||
}
|
||||
isTestFinishGuardEnabled = true
|
||||
jestStateSwiperIndex = index
|
||||
swiperIndex.value = index
|
||||
initSwiperItemData(index)
|
||||
updateTabIndicator(index, index, 1)
|
||||
animationFinishIndex = index
|
||||
nestedScrollChildId = swiperList.value[index].id
|
||||
return true
|
||||
}
|
||||
|
||||
function jest_getState() : JestTabsPageState {
|
||||
const currentIndex = jestStateSwiperIndex >= 0 ? jestStateSwiperIndex : swiperIndex.value
|
||||
const currentItem = swiperList.value[currentIndex]
|
||||
return {
|
||||
swiperIndex: currentIndex,
|
||||
animationFinishIndex: animationFinishIndex,
|
||||
tabCount: swiperList.value.length,
|
||||
currentType: currentItem.type,
|
||||
currentName: currentItem.name,
|
||||
currentPreload: currentItem.preload,
|
||||
nestedScrollChildId: currentItem.id,
|
||||
} as JestTabsPageState
|
||||
}
|
||||
|
||||
function jest_getListState(index : number) : UTSJSONObject | null {
|
||||
if (index < 0 || index >= swiperList.value.length) {
|
||||
return null
|
||||
}
|
||||
return longPageRef.value[index].$callMethod('jest_getState', null) as UTSJSONObject | null
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
jest_prepareMockData,
|
||||
jest_setSwiperCurrent,
|
||||
jest_getState,
|
||||
jest_getListState,
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.flex-row {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.page {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.search-bar {
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.swiper-list {
|
||||
height: 100%;
|
||||
/* #ifdef WEB */
|
||||
flex: 1;
|
||||
/* #endif */
|
||||
}
|
||||
|
||||
.swiper-tabs {
|
||||
background-color: #ffffff;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.swiper-tabs-item {
|
||||
color: #555;
|
||||
font-size: 16px;
|
||||
padding: 12px 25px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.swiper-tabs-item-active {
|
||||
color: #007AFF;
|
||||
}
|
||||
|
||||
.swiper-tabs-indicator {
|
||||
width: 1px;
|
||||
height: 2px;
|
||||
background-color: #007AFF;
|
||||
}
|
||||
|
||||
.swiper-view {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.swiper-item {
|
||||
/* flex: 1; harmony 高度异常 */
|
||||
height: 100%;
|
||||
}
|
||||
</style>
|
||||
在新工单中引用
屏蔽一个用户