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

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

查看文件

@@ -0,0 +1,271 @@
<template>
<view class="card" ref="card" @touchstart="touchstart($event as TouchEvent)"
@touchmove="touchmove($event as TouchEvent)" @touchend="touchend" @touchcancel="touchend">
<image class="card-img" ref="cardImg" :src="img"></image>
<view class="state">
<image class="state-icon like" ref="stateIconLike" src="/static/template/drop-card/like.png" mode="widthFix">
</image>
<image class="state-icon dislike" ref="stateIconDislike" src="/static/dislike.png"
mode="widthFix"></image>
<!-- cardIndex:{{cardIndex}} -->
</view>
</view>
</template>
<script setup lang="uts">
/**
* @description: 作为卡片堆叠中的单张卡片,支持拖拽跟手、状态提示和划走动画。
* @logic: 记录手势起点后实时更新卡片位移与旋转角度,松手时根据水平位移判断回弹或飞出,并通知父级更新堆叠顺序。
* @optimization: 通过缓存模板元素引用并直接更新 transform 和 opacity,降低拖拽过程中的样式计算与节点查询开销。
*/
// Template refs
const card = ref<UniElement | null>(null)
const cardImg = ref<UniElement | null>(null)
const stateIconLike = ref<UniElement | null>(null)
const stateIconDislike = ref<UniElement | null>(null)
// Props
const props = defineProps({
img: {
type: String,
default: "https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/drop-card-1.jpg"
},
cardIndex: {
type: Number,
default: 0
}
})
// Module-level variables
let sX : number = 0
let sY : number = 0
let screenWidth : number = 1
let screenHeight : number = 1
let floating : boolean = false
let touchstartAfter : boolean = false
// Data
const $elementMap = new Map<string, UniElement>()
const x = ref(0 as number)
const y = ref(0 as number)
let floatCount = 0
// Computed
const movePercent = computed(() : number => {
return Math.abs(x.value) / (screenWidth / 2 * 3)
})
const likeOpacity = computed(() : number => {
return x.value < 0 ? 0 : movePercent.value * 100
})
const dislikeOpacity = computed(() : number => {
return x.value > 0 ? 0 : movePercent.value * 100
})
// Methods
function setElementStyle(refName : string, propertyName : string, propertyStyle : any) : void {
let element : UniElement | null = $elementMap.get(refName)
if (element == null) {
// Map ref name to corresponding ref variable
if (refName == 'card') {
element = card.value
} else if (refName == 'card-img') {
element = cardImg.value
} else if (refName == 'state-icon-like') {
element = stateIconLike.value
} else if (refName == 'state-icon-dislike') {
element = stateIconDislike.value
}
if (element != null) {
$elementMap.set(refName, element)
}
}
if (element != null) {
element.style.setProperty(propertyName, propertyStyle.toString())
}
}
let _index = 0
function initCardStyle() {
_index = (props.cardIndex + floatCount) % 3
// TODO 临时处理 setProperty设置z-index无效的问题
setElementStyle('card', 'z-index', _index.toString())
setElementStyle('card', 'margin-top', screenHeight * 0.15 - 30 * _index + 'px')
setElementStyle('card', 'transform', 'scale(' + (0.9 + 0.05 * _index) + ')')
}
function moveCard() {
if (_index != 2){
// console.log('不是顶层卡片不允许移动')
return
}
setElementStyle("card",
'transform',
`translate(${x.value}px,${y.value}px) rotate(${x.value / -30}deg) scale(1)`
)
setElementStyle("state-icon-like", 'opacity', x.value < 0 ? 0 : movePercent.value * 10)
setElementStyle("state-icon-dislike", 'opacity', x.value > 0 ? 0 : movePercent.value * 10)
}
function touchstart(e : TouchEvent) {
e.preventDefault()
if (floating) {
return
}
sX = e.touches[0].screenX
sY = e.touches[0].screenY
x.value = 0
y.value = 0
touchstartAfter = true
}
function touchmove(e : TouchEvent) {
e.preventDefault()
if (!touchstartAfter || floating) {
return
}
x.value += e.touches[0].screenX - sX
y.value += e.touches[0].screenY - sY
sX = e.touches[0].screenX
sY = e.touches[0].screenY
moveCard()
}
function touchend() {
touchstartAfter = false
if (floating) {
return
}
floating = true
let k : number = 0
if (x.value > screenWidth / 10) {
k = 1
} else if (x.value < screenWidth * -1 / 10) {
k = -1
}
function cardTo(targetX : number, targetY : number, callback : () => void, speed : number = 10) {
let interval : number = 0
let acceleration : number = 1
interval = setInterval(() => {
acceleration += 0.2
const dx = targetX - x.value
if (Math.abs(dx) < 1) {
x.value = targetX
} else {
x.value += dx / speed * acceleration
}
const dy = targetY - y.value
if (Math.abs(dy) < 1) {
y.value = targetY
} else {
y.value += dy / speed * acceleration
}
moveCard()
if (x.value == targetX && y.value == targetY) {
clearInterval(interval)
callback()
}
}, 16)
}
if (Math.floor(k) != 0) {
cardTo(k * screenWidth * 1.3, y.value * 3, () => {
setElementStyle("state-icon-like", 'opacity', 0)
setElementStyle("state-icon-dislike", 'opacity', 0)
setElementStyle("card", 'opacity', 0)
setTimeout(() => {
setElementStyle("card", 'opacity', 1)
}, 300)
uni.$emit('uni-drop-card-float', null)
floating = false
}, 8)
} else {
const _x : number = x.value
const _y : number = y.value
cardTo(Math.floor(_x * -0.05), Math.floor(_y * -0.05), () => {
cardTo(0, 0, () => {
console.log('bounce')
floating = false
}, 30)
})
}
}
// Lifecycle
onMounted(() => {
screenWidth = uni.getWindowInfo().screenWidth
screenHeight = uni.getWindowInfo().screenHeight
setTimeout(() => {
setElementStyle('card', 'height', screenHeight * 0.7 + 'px')
setElementStyle('card-img', 'height', screenHeight * 0.7 + 'px')
initCardStyle()
}, 200)
uni.$on('uni-drop-card-float', () => {
floatCount++
initCardStyle()
})
})
onUnmounted(() => {
uni.$off('uni-drop-card-float')
})
defineOptions({
options: {
virtualHost: true
}
})
</script>
<style>
.card {
width:95%;
height: 0px;
position: absolute;
top: 0px;
/* left: 0px; */
margin: 0 12px;
margin-top: 50px;
border-radius: 10px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
background-color: #FFF;
transition: margin-top 300ms;
transition-timing-function: ease-in;
}
.card-img {
width: 100%;
height: 0px;
border-radius: 10px;
}
.state {
top: 10px;
left: 10px;
width: 86%;
padding: 4px;
position: absolute;
flex-direction: row;
justify-content: space-between;
}
.state-icon {
width: 30px;
height: 30px;
border: 1px solid #FFF;
background-color: #FFF;
padding: 3px;
border-radius: 100px;
box-shadow: 0 0 1px #EBEBEB;
opacity: 0;
}
</style>

查看文件

@@ -0,0 +1,269 @@
jest.setTimeout(60000)
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
const isWeb = platformInfo.startsWith('web') || platformInfo.startsWith('h5')
const isMP = platformInfo.startsWith('mp')
const isHarmony = platformInfo.startsWith('harmony')
const isPad = process.env.UNI_AUTOMATOR_IS_PAD == 'true'
const PAGE_PATH = '/pages/template/swipe-card-stack/swipe-card-stack'
const WAIT_FOR_CARD_INIT = 800
const WAIT_FOR_DRAG_RENDER = 120
const WAIT_FOR_RELEASE = 800
describe('template-swipe-card-stack', () => {
// TODO: 小程序端待补充适配后放开。
if (isMP || (isHarmony && isPad)) {
it('skip', () => {
expect(1).toBe(1)
})
return
}
let page
let windowInfo
async function openPage() {
try {
page = await program.reLaunch(PAGE_PATH)
} catch (error) {
if (!isWeb) {
throw error
}
page = await program.currentPage()
}
await page.waitFor('.card')
await page.waitFor(WAIT_FOR_CARD_INIT)
}
async function getCards() {
const cards = await page.$$('.card')
expect(cards.length).toBe(3)
return cards
}
async function getCardInfos() {
const cards = await getCards()
const infos = []
for (let i = 0; i < cards.length; i++) {
const rect = await cards[i].offset()
const size = await cards[i].size()
infos.push({
card: cards[i],
index: i,
top: rect.top,
left: rect.left,
width: size.width,
height: size.height
})
}
return infos
}
async function getTopCardInfo() {
const infos = await getCardInfos()
infos.sort((a, b) => a.top - b.top)
return infos[0]
}
async function getBackCardInfo() {
const infos = await getCardInfos()
infos.sort((a, b) => b.top - a.top)
return infos[0]
}
async function getTopCardIndex() {
return (await getTopCardInfo()).index
}
function createTouch(x, y) {
const pointX = Math.round(x)
const pointY = Math.round(y)
return {
identifier: 1,
pageX: pointX,
pageY: pointY,
clientX: pointX,
clientY: pointY,
screenX: pointX,
screenY: pointY
}
}
function createTouchEvent(x, y, isEnd = false) {
const touch = createTouch(x, y)
return {
touches: isEnd ? [] : [touch],
changedTouches: [touch]
}
}
async function dragCard(cardInfo, offsetX, offsetY = 0, options = {}) {
const startX = cardInfo.left + cardInfo.width / 2
const startY = cardInfo.top + Math.min(220, cardInfo.height / 2)
const steps = options.steps || 4
const interval = options.interval || 80
await cardInfo.card.touchstart(createTouchEvent(startX, startY))
for (let i = 1; i <= steps; i++) {
const nextX = startX + offsetX * i / steps
const nextY = startY + offsetY * i / steps
await cardInfo.card.touchmove(createTouchEvent(nextX, nextY))
await page.waitFor(interval)
}
if (options.release !== false) {
await cardInfo.card.touchend(createTouchEvent(startX + offsetX, startY + offsetY, true))
await page.waitFor(WAIT_FOR_RELEASE)
}
return cardInfo.card
}
async function dragTopCard(offsetX, offsetY = 0, options = {}) {
return await dragCard(await getTopCardInfo(), offsetX, offsetY, options)
}
function getTransformTranslate(transform) {
const translateMatched = transform.match(/translate(?:3d)?\(\s*([-0-9.]+)px(?:\s*,\s*([-0-9.]+)px)?/)
if (translateMatched != null) {
return {
x: parseFloat(translateMatched[1] || '0'),
y: parseFloat(translateMatched[2] || '0')
}
}
const matrix3dMatched = transform.match(/matrix3d\(([^)]+)\)/)
if (matrix3dMatched != null) {
const matrixValues = matrix3dMatched[1].split(',').map((value) => parseFloat(value.trim()))
return {
x: matrixValues[12] || 0,
y: matrixValues[13] || 0
}
}
const matrixMatched = transform.match(/matrix\(([^)]+)\)/)
expect(matrixMatched).not.toBeNull()
const matrixValues = matrixMatched[1].split(',').map((value) => parseFloat(value.trim()))
return {
x: matrixValues[4] || 0,
y: matrixValues[5] || 0
}
}
function expectTransformMoved(transform) {
const translate = getTransformTranslate(transform)
expect(Math.abs(translate.x) + Math.abs(translate.y)).toBeGreaterThan(0)
}
async function expectCardIsDragging(card) {
await page.waitFor(WAIT_FOR_DRAG_RENDER)
const transform = await card.style('transform')
expectTransformMoved(transform)
}
async function expectCardDraggedBeyondSwitchThreshold(card, direction) {
await page.waitFor(WAIT_FOR_DRAG_RENDER)
const transform = await card.style('transform')
const translateX = getTransformTranslate(transform).x
const threshold = windowInfo.screenWidth / 10
if (direction > 0) {
expect(translateX).toBeGreaterThan(threshold)
} else {
expect(translateX).toBeLessThan(-threshold)
}
}
async function screenShot() {
const image = await program.screenshot({
fullPage: true
})
expect(image).toSaveImageSnapshot()
}
beforeAll(async () => {
windowInfo = await program.callUniMethod('getWindowInfo')
})
beforeEach(async () => {
await openPage()
})
it('renders card stack', async () => {
const cards = await getCards()
const images = await page.$$('.card-img')
expect(images.length).toBe(3)
for (let i = 0; i < cards.length; i++) {
const rect = await cards[i].offset()
const size = await cards[i].size()
expect(rect.left).toBeGreaterThanOrEqual(0)
expect(rect.top).toBeGreaterThanOrEqual(0)
expect(size.width).toBeGreaterThan(0)
expect(size.height).toBeGreaterThan(0)
}
})
it('does not move a back card when dragging behind the top card', async () => {
const backCardInfo = await getBackCardInfo()
const beforeTransform = await backCardInfo.card.style('transform')
await dragCard(backCardInfo, Math.round(windowInfo.screenWidth * 0.35), 0, { release: false })
expect(await backCardInfo.card.style('transform')).toBe(beforeTransform)
await screenShot()
})
it('keeps current card after a short drag and release', async () => {
const topCardIndexBefore = await getTopCardIndex()
const draggedCard = await dragTopCard(20, 0, { release: false })
await expectCardIsDragging(draggedCard)
await screenShot()
await draggedCard.touchend(createTouchEvent(windowInfo.screenWidth / 2 + 20, windowInfo.screenHeight / 2, true))
await page.waitFor(WAIT_FOR_RELEASE)
expect(await getTopCardIndex()).toBe(topCardIndexBefore)
expect((await getCards()).length).toBe(3)
})
it('right drag feedback screenshot', async () => {
const topCardIndexBefore = await getTopCardIndex()
const draggedCard = await dragTopCard(Math.round(windowInfo.screenWidth * 0.45), 0, { release: false })
await expectCardIsDragging(draggedCard)
await screenShot()
expect(await getTopCardIndex()).toBe(topCardIndexBefore)
expect((await getCards()).length).toBe(3)
})
it('right drag reaches switch threshold', async () => {
const topCardIndexBefore = await getTopCardIndex()
const draggedCard = await dragTopCard(Math.round(windowInfo.screenWidth * 0.45), 0, { release: false })
await expectCardDraggedBeyondSwitchThreshold(draggedCard, 1)
expect(await getTopCardIndex()).toBe(topCardIndexBefore)
expect((await getCards()).length).toBe(3)
})
it('left drag feedback screenshot', async () => {
const topCardIndexBefore = await getTopCardIndex()
const draggedCard = await dragTopCard(Math.round(windowInfo.screenWidth * -0.45), 0, { release: false })
await expectCardIsDragging(draggedCard)
await screenShot()
expect(await getTopCardIndex()).toBe(topCardIndexBefore)
expect((await getCards()).length).toBe(3)
})
it('left drag reaches switch threshold', async () => {
const topCardIndexBefore = await getTopCardIndex()
const draggedCard = await dragTopCard(Math.round(windowInfo.screenWidth * -0.45), 0, { release: false })
await expectCardDraggedBeyondSwitchThreshold(draggedCard, -1)
expect(await getTopCardIndex()).toBe(topCardIndexBefore)
expect((await getCards()).length).toBe(3)
})
})

查看文件

@@ -0,0 +1,35 @@
<template>
<view class="root">
<template v-for="(item,index) in cardList" :key="index">
<card :img="item" :cardIndex="index"></card>
</template>
</view>
</template>
<script setup lang="uts">
/**
* @description: 支持在卡片堆叠中左右滑动当前卡片,并根据方向触发喜欢或不喜欢的划走反馈。
* @logic: 页面按顺序渲染多张卡片,用户拖拽顶部卡片时实时跟随手势移动,松手后根据位移决定回弹或飞出并切换到下一张卡片。
* @optimization: 卡片通过复用同一组位置层级和全局浮动事件维持堆叠顺序,减少每次划走后重新创建整组卡片的开销。
*/
import card from './card/card.uvue';
const cardList = ref([
'https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/drop-card-1.jpg',
'https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/drop-card-2.jpg',
'https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/drop-card-3.jpg'
] as string.ImageURIString[])
</script>
<style>
/* #ifdef MP */
page {
overflow: hidden
}
/* #endif */
.root {
flex: 1;
position: relative;
align-items: center;
}
</style>