refactor: 确立独立子工程架构 (backend, admin, mpuniapp) 并同步更新 AGENTS.md 规范

这个提交包含在:
wikiboss
2026-09-20 17:05:22 +08:00
父节点 de3cf8b870
当前提交 9fa1d1fbd0
共有 1679 个文件被更改,包括 204798 次插入 和 16817 次删除

查看文件

@@ -0,0 +1,295 @@
<template>
<list-view :id="id" class="list" :bounces="false" :scroll-y="true" :custom-nested-scroll="true"
@scrolltolower="loadData(null)" associative-container="nested-scroll-view">
<list-item class="list-item" v-for="(item, _) in dataList" :key="item.plugin_id" type="10">
<view class="list-item-icon">
<image class="list-item-icon-image" :src="item.plugin_img_link"></image>
</view>
<view class="list-item-fill">
<view class="flex-row">
<text class="title">{{item.plugin_name}}</text>
</view>
<view>
<text class="description-text">{{item.plugin_intro}}</text>
</view>
<uni-rate :value="item.score / 10" :full="true" :readonly="true"></uni-rate>
<view class="tag-list">
<text class="tag-item" v-for="(item2, index2) in item.tags" :key="index2">{{item2}}</text>
</view>
<view class="flex-row update-date">
<text class="update-date-text">更新日期</text>
<text class="update-date-value">{{item.update_date}}</text>
<text class="author">{{item.author_name}}</text>
</view>
</view>
</list-item type="20">
<list-item class="loading">
<!-- <uni-loading :loading="loading" color="#999" :text="loadingText"></uni-loading> -->
<loading v-if="loadingRef" style="color: #999"></loading>
<text v-if="isEnded" style="color: #999">到底了</text>
</list-item>
</list-view>
</template>
<script setup lang="uts">
/**
* @description: 作为长列表容器页,支持按分类加载插件列表并在触底时继续追加数据。
* @logic: 页面根据父级传入的分类参数请求列表接口,首次进入按需预加载,滚动到底部后继续分页拉取并更新列表内容。
* @optimization: 通过 `preload` 控制是否首屏加载,避免所有标签页同时请求数据;分页追加时直接复用同一列表实例,减少重复创建。
*/
const SERVER_URL = "https://unidemo.dcloud.net.cn/plugin/uniappx-plugin-list"
const PAGE_SIZE = 10
type ListItem = {
plugin_id : number,
plugin_img_link : string,
plugin_name : string,
plugin_intro : string,
score : number,
tags : Array<string>,
update_date : string,
author_name : string,
}
type ResponseDataType = {
code : number,
data : ListItem[]
}
const props = defineProps({
type: { type: String, default: '' },
preload: { type: Boolean, default: false },
id: { type: String, default: '' }
})
const loadingRef = ref(false)
const dataList = ref<ListItem[]>([])
const isEnded = ref(false)
const loadingError = ref('')
const currentPage = ref(1)
// 自动化测试辅助
const mockEnabled = ref(false)
function createMockItems(page : number) : ListItem[] {
if (page > 2) {
return []
}
const items : ListItem[] = []
const baseIndex = (page - 1) * PAGE_SIZE
for (let i = 0; i < PAGE_SIZE; i++) {
const itemIndex = baseIndex + i + 1
items.push({
plugin_id: itemIndex,
plugin_img_link: '/static/logo.png',
plugin_name: `${props.type} mock ${itemIndex}`,
plugin_intro: `${props.type} mock intro ${itemIndex}`,
score: 90,
tags: ['mock', props.type],
update_date: `2026-06-${(itemIndex < 10 ? '0' : '') + itemIndex}`,
author_name: 'uni-agent'
} as ListItem)
}
return items
}
function loadMockData(loadComplete : (() => void) | null) {
if (loadingRef.value || isEnded.value) {
return
}
loadingRef.value = true
const items = createMockItems(currentPage.value)
dataList.value.push(...items)
if (items.length == 0) {
isEnded.value = true
} else {
currentPage.value++
}
loadingRef.value = false
if (loadComplete != null) {
loadComplete()
}
}
/* const loadingText = computed((): string => {
if (loadingRef.value) {
return "加载中..."
} else if (isEnded.value) {
return "没有更多了"
} else if (loadingError.value.length > 0) {
return loadingError.value
} else {
return ""
}
}) */
function loadData(loadComplete : (() => void) | null) {
if (loadingRef.value || isEnded.value) {
return
}
if (mockEnabled.value) {
loadMockData(loadComplete)
return
}
loadingRef.value = true
uni.request<ResponseDataType>({
url: SERVER_URL,
data: {
type: props.type,
page: currentPage.value,
page_size: PAGE_SIZE
},
success: (res) => {
const responseData = res.data
// console.log('responseData',responseData)
if (responseData == null) {
return
}
dataList.value.push(...responseData.data)
if (responseData.data.length == 0) {
isEnded.value = true
} else {
currentPage.value++
}
},
fail: (err) => {
loadingError.value = err.errMsg
},
complete: () => {
loadingRef.value = false
if (loadComplete != null) {
loadComplete()
}
}
})
}
function refreshData(loadComplete : (() => void) | null) {
dataList.value.length = 0
currentPage.value = 1
isEnded.value = false
loadingError.value = ''
loadData(loadComplete)
}
onMounted(() => {
if (props.preload) {
loadData(null)
}
})
// 自动化测试辅助
function jest_useMockData(reload : boolean) {
mockEnabled.value = true
dataList.value.length = 0
currentPage.value = 1
isEnded.value = false
loadingError.value = ''
loadingRef.value = false
if (reload) {
loadData(null)
}
}
function jest_getState() : UTSJSONObject {
const firstItem = dataList.value.length > 0 ? dataList.value[0] : null
return {
type: props.type,
renderedCount: dataList.value.length,
currentPage: currentPage.value,
loading: loadingRef.value,
isEnded: isEnded.value,
firstTitle: firstItem != null ? firstItem.plugin_name : '',
mockEnabled: mockEnabled.value,
} as UTSJSONObject
}
defineExpose({
loadData,
refreshData,
jest_useMockData,
jest_getState
})
</script>
<style>
.list {
flex: 1;
background-color: #ffffff;
}
.list-item {
flex-direction: row;
margin-top: 10px;
padding: 10px;
}
.list-item-icon {
position: relative;
}
.list-item-icon-image {
width: 80px;
height: 80px;
}
.list-item-fill {
flex: 1;
margin-left: 15px;
}
.description-text {
font-size: 13px;
color: #666;
line-height: 19px;
}
.tag-list {
flex-direction: row;
margin-top: 5px;
}
.tag-item {
font-size: 12px;
background-color: #EFF9F0;
color: #639069;
border-radius: 20px;
margin-right: 5px;
padding: 2px 5px;
}
.update-date {
margin-top: 10px;
}
.update-date-text {
font-size: 12px;
color: #888888;
}
.update-date-value {
font-size: 12px;
color: #777777;
margin-left: 5px;
}
.author {
font-size: 12px;
color: #008000;
margin-left: auto;
}
.loading {
padding: 30px;
align-items: center;
}
.flex-row {
flex-direction: row;
}
</style>

查看文件

@@ -0,0 +1,115 @@
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
const isMP = platformInfo.startsWith('mp')
const PAGE_PATH = '/pages/template/search-header-long-list/search-header-long-list'
const ACTIVE_COLORS = ['rgb(0, 122, 255)', '#007AFF', '#007AFFFF']
const INACTIVE_COLORS = ['rgb(85, 85, 85)', '#555555', '#555555FF']
describe('template-search-header-long-list', () => {
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(3000)
})
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 search header tabs and initial mocked list', async () => {
const input = await page.$('input')
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(input).not.toBeNull()
expect(tabs.length).toBe(4)
expect(await tabs[0].text()).toBe('最新上架')
expect(await tabs[3].text()).toBe('热门总榜')
expect(pageState.tabCount).toBe(4)
expect(pageState.currentType).toBe('UpdatedDate')
expect(firstListState.renderedCount).toBe(10)
expect(firstListState.firstTitle).toBe('UpdatedDate mock 1')
expect(firstListState.mockEnabled).toBe(true)
expect(secondListState.renderedCount).toBe(0)
expect(ACTIVE_COLORS).toContain(await tabs[0].style('color'))
expect(INACTIVE_COLORS).toContain(await tabs[1].style('color'))
if (!isMP) {
const firstItem = await page.$('.list-item')
expect(firstItem).not.toBeNull()
}
})
it('switches to the second tab and lazy loads its list', 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.currentPreload).toBe(true)
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 swiper switching for web fallback', 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(fourthListState.firstTitle).toBe('HotList mock 1')
})
})

查看文件

@@ -0,0 +1,398 @@
<template>
<scroll-view ref="pageScrollView" class="page" :bounces="false"
@startnestedscroll="onStartNestedScroll" @nestedprescroll="onNestedPreScroll"
:refresher-triggered="refresherTriggered" refresher-enabled="true" @refresherrefresh="onRefresherrefresh">
<view ref="header" class="search-bar">
<input placeholder="搜索..." :maxlength="-1"/>
</view>
<view class="swiper-list">
<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="indicator" 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" :type="item.type" :preload="item.preload"></long-page>
</swiper-item>
</swiper>
</view>
</scroll-view>
</template>
<script setup lang="uts">
/**
* @description: 支持顶部搜索区域和横向分栏切换的长列表页面,列表内容随标签切换按需加载。
* @logic: 页面先展示搜索框和横向标签栏,再通过 swiper 切换不同分类的长列表,并联动更新下划线指示器位置。
* @optimization: 每个标签页只在首次切换到时触发数据加载,同时复用同一套指示器插值逻辑,减少初始渲染和切换开销。
*/
import longPage from './long-list-page.uvue';
type SwiperTabsItem = {
x : number,
w : number
}
type SwiperViewItem = {
id : string, // 唯一字符串
type : string,
name : string,
preload : boolean,
}
type JestTabsPageState = {
swiperIndex : number,
animationFinishIndex : number,
tabCount : number,
currentType : string,
currentName : string,
currentPreload : boolean,
}
/**
* 根据权重在两个值之间执行线性插值.
* @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<SwiperViewItem[]>([
{
id: 'updated', // 唯一字符串
type: 'UpdatedDate',
name: '最新上架',
preload: true
},
{
id: 'free',
type: 'FreeHot',
name: '免费热榜',
preload: false
},
{
id: 'payment',
type: 'PaymentHot',
name: '付费热榜',
preload: false
},
{
id: 'hot',
type: 'HotList',
name: '热门总榜',
preload: false
}
])
const swiperIndex = ref(0)
const pageScrollView = ref<UniScrollViewElement|null>(null)
const header = ref<UniElement|null>(null)
const tabScroll = ref<UniScrollViewElement|null>(null)
const indicator = ref<UniElement|null>(null)
const swiper = ref<UniSwiperElement|null>(null)
const swipertab = ref<UniTextElement[]|null>(null)
const longPageRef = ref<ComponentPublicInstance[]|null>(null)
let headerHeight = 0
let animationFinishIndex = 0
let swiperWidth = 0
let swiperTabsRect: SwiperTabsItem[] = []
// 自动化测试中记录程序化切页的目标索引,避免立即读状态时拿到旧值。
let jestTargetSwiperIndex = -1
// 仅在自动化测试程序化切页时启用旧 animationfinish 事件保护。
let isTestFinishGuardEnabled = false
function onStartNestedScroll() : boolean {
return true
}
function onNestedPreScroll(event : NestedPreScrollEvent) {
const deltaY = event.deltaY
const scrollTop = pageScrollView.value!.scrollTop
// 优先处理父容器滚动,父容器不能滚动时滚动子
if (deltaY > 0) {
// 向上滚动,如果父容器 header scrollTop < offsetHeight,先滚动父容器
if (scrollTop < headerHeight) {
const difference = headerHeight - scrollTop - deltaY
if (difference > 0) {
pageScrollView.value!.scrollBy(event.deltaX, deltaY)
event.consumed(event.deltaX, deltaY)
} else {
const top : number = deltaY + difference
event.consumed(event.deltaX, top.toFloat())
pageScrollView.value!.scrollBy(event.deltaX, top.toFloat())
}
}
} else if (deltaY < 0) {
// 向下滚动,如果父容器 scrollTop > 0,通知子被父容器消耗,然后滚动到 0
if (scrollTop > 0) {
event.consumed(event.deltaX, deltaY)
pageScrollView.value!.scrollBy(event.deltaX, deltaY)
}
}
}
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
indicator.value?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
// #endif
// #ifdef WEB || MP
// TODO chrome windows系统 transform scaleX渲染bug
const x = indicator_line_x
indicator.value?.style?.setProperty('width', `${indicator_line_w}px`)
indicator.value?.style?.setProperty('transform', `translateX(${x}px)`)
// #endif
// 滚动到水平中心位置
const scroll_x = x - swiperWidth / 2
// app 平台后续支持 scrollTo()
// #ifndef MP-WEIXIN
tabScroll.value!.scrollLeft = scroll_x
// #endif
// #ifdef MP-WEIXIN
tabScroll.value!.scrollTo({
left: scroll_x
})
// #endif
}
function initSwiperItemData(index : number) {
if (index < 0 || index >= swiperList.value.length) {
return
}
if (!swiperList.value[index].preload) {
swiperList.value[index].preload = true;
(longPageRef.value as ComponentPublicInstance[])[index].$callMethod('loadData', null)
}
}
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)
}
async function cacheTabItemsSize() {
swiperTabsRect.length = 0;
const tabs = swipertab.value as UniElement[]
for (let i = 0; i < tabs.length; i++) {
const element = tabs[i];
// #ifdef MP
const rect = await element.getBoundingClientRectAsync()!
const x = rect.left
const w = rect.width
// #endif
// #ifndef MP
const x = element.offsetLeft
const w = element.offsetWidth
// #endif
swiperTabsRect.push({
x,
w
} as SwiperTabsItem)
}
}
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) {
jestTargetSwiperIndex = -1
isTestFinishGuardEnabled = false
setSwiperIndex(index, false)
}
function onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
// 自动化测试程序化切页时,忽略晚到的旧完成事件,避免把目标索引回写成旧值。
if (isTestFinishGuardEnabled && jestTargetSwiperIndex >= 0 && e.detail.current != jestTargetSwiperIndex) {
return
}
isTestFinishGuardEnabled = false
jestTargetSwiperIndex = -1
setSwiperIndex(e.detail.current, true)
animationFinishIndex = e.detail.current
}
onReady(() => {
headerHeight = (header.value as UniElement).offsetHeight
swiper.value!.getBoundingClientRectAsync()!.then((res : DOMRect) : Promise<void> => {
swiperWidth = res.width
return cacheTabItemsSize()
}).then(() => {
updateTabIndicator(swiperIndex.value, swiperIndex.value, 1)
});
})
const refresherTriggered = ref(false);
function onRefresherrefresh(e : UniRefresherEvent) {
refresherTriggered.value = true;
(longPageRef.value as ComponentPublicInstance[])[swiperIndex.value].$callMethod('refreshData', () => {
refresherTriggered.value = false;
})
}
// 自动化测试辅助
function jest_prepareMockData() : boolean {
const pageRefs = longPageRef.value
if (pageRefs == null || 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
}
return true
}
function jest_setSwiperCurrent(index : number) : boolean {
if (index < 0 || index >= swiperList.value.length) {
return false
}
isTestFinishGuardEnabled = true
jestTargetSwiperIndex = index
swiperIndex.value = index
initSwiperItemData(index)
updateTabIndicator(index, index, 1)
animationFinishIndex = index
return true
}
function jest_getState() : JestTabsPageState {
const currentIndex = jestTargetSwiperIndex >= 0 ? jestTargetSwiperIndex : 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,
} as JestTabsPageState
}
function jest_getListState(index : number) : UTSJSONObject | null {
if (index < 0 || index >= swiperList.value.length || longPageRef.value == null) {
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>