feat: optimize quick actions drag sorting and scan input

This commit is contained in:
zm2000 2026-04-04 18:28:30 +08:00
parent 4ba720b4c7
commit 446ae7d273
2 changed files with 437 additions and 9 deletions

View File

@ -31,6 +31,7 @@
<view class="section-title-bar">
<view class="title-left">
<text class="section-title">常用功能</text>
<text class="drag-tip" v-if="quickActions.length > 1">长按拖拽排序</text>
</view>
<view class="title-right">
<u-icon name="grid" color="#2979ff" size="20"></u-icon>
@ -48,22 +49,30 @@
</view>
<!-- 网格内容 -->
<view class="quick-grid-wrapper">
<view class="quick-grid-wrapper" @touchmove="onDragMove" @touchend="endDrag" @touchcancel="endDrag">
<!-- 空状态提示 -->
<view class="empty-state" v-if="!isLoading && quickActions.length === 0">
<u-icon name="grid" size="80" color="#ccc"></u-icon>
<text class="empty-text">暂无可用功能</text>
<text class="empty-hint">请联系仓库管理员检查授权</text>
</view>
<swiper v-else class="quick-swiper" :current="currentPage - 1" @change="onSwiperChange"
:duration="300">
<swiper v-else class="quick-swiper" :current="currentPage - 1" :disable-touch="dragState.active" @change="onSwiperChange"
:duration="swiperDuration">
<swiper-item v-for="(pageItems, pageIndex) in paginatedActions" :key="pageIndex">
<view class="grid-page">
<view class="grid-page" :id="`grid-page-${pageIndex}`">
<!-- 3行4列布局,共12个 -->
<view class="grid-item" v-for="(item, index) in pageItems" :key="index"
@click="toPath(item)">
<view
class="grid-item grid-slot"
v-for="(item, index) in pageItems"
:key="getMenuKey(item)"
:class="{
'drag-placeholder': isDraggingItem(item),
'drag-disabled': dragState.active
}"
@longpress.stop="startDrag($event, item, pageIndex, index)"
@click="handleGridItemClick(item)">
<view class="item-icon-box" :style="{ background: item.color + '15' }">
<u-icon :name="item.icon" size="26" :color="item.color"></u-icon>
</view>
@ -74,10 +83,21 @@
<!-- 填充空白项 -->
<view v-for="n in (12 - pageItems.length)" :key="'empty-' + n"
class="grid-item empty-item"></view>
class="grid-item grid-slot empty-item"></view>
</view>
</swiper-item>
</swiper>
<view
v-if="dragState.active && dragState.item"
class="drag-preview"
:style="dragPreviewStyle"
>
<view class="item-icon-box" :style="{ background: dragState.item.color + '15' }">
<u-icon :name="dragState.item.icon" size="26" :color="dragState.item.color"></u-icon>
</view>
<text class="item-name">{{ dragState.item.name }}</text>
</view>
</view>
<!-- 下一页按钮 -->
@ -119,7 +139,22 @@
unreadCount: 0,
quickActions: [],
pageSize: 12,
FIXED_MENU_KEY: 'fixed_home_menus'
FIXED_MENU_KEY: 'fixed_home_menus',
dragTouchStart: null,
dragState: {
active: false,
item: null,
key: '',
pageIndex: -1,
hoverIndex: -1,
previewX: 0,
previewY: 0,
slotRects: [],
pageRect: null
},
isSavingOrder: false,
suppressItemClick: false,
dragPageSwitching: false
}
},
@ -135,6 +170,17 @@
totalPages() {
return Math.ceil(this.quickActions.length / this.pageSize) || 1;
},
swiperDuration() {
return this.dragState.active ? 0 : 300;
},
dragPreviewStyle() {
return {
left: `${this.dragState.previewX}px`,
top: `${this.dragState.previewY}px`
};
}
},
@ -241,6 +287,246 @@
''
);
},
getTouchPoint(event) {
const fallbackPoint = event?.detail?.x || event?.detail?.y
? {
pageX: event.detail.x,
pageY: event.detail.y
}
: null;
const touch = event?.touches?.[0] || event?.changedTouches?.[0] || fallbackPoint;
return {
x: Number(touch?.pageX || touch?.clientX || 0),
y: Number(touch?.pageY || touch?.clientY || 0)
};
},
isDraggingItem(item) {
return this.dragState.active && this.getMenuKey(item) === this.dragState.key;
},
async captureCurrentPageSlots(pageIndex) {
return new Promise((resolve) => {
const query = uni.createSelectorQuery().in(this);
query.select(`#grid-page-${pageIndex}`).boundingClientRect();
query.selectAll(`#grid-page-${pageIndex} .grid-slot`).boundingClientRect();
query.exec((res) => {
resolve({
pageRect: res?.[0] || null,
slotRects: res?.[1] || []
});
});
});
},
async startDrag(event, item, pageIndex, index) {
if (!item || this.dragState.active || this.quickActions.length <= 1) {
return;
}
const { pageRect, slotRects } = await this.captureCurrentPageSlots(pageIndex);
if (!pageRect || !slotRects.length) {
return;
}
const point = this.getTouchPoint(event);
const startPoint = (point.x || point.y)
? point
: {
x: (slotRects[index]?.left || 0) + ((slotRects[index]?.width || 0) / 2),
y: (slotRects[index]?.top || 0) + ((slotRects[index]?.height || 0) / 2)
};
const slotRect = slotRects[index] || slotRects[0];
this.suppressItemClick = true;
this.dragState = {
active: true,
item,
key: this.getMenuKey(item),
pageIndex,
hoverIndex: pageIndex * this.pageSize + index,
previewX: startPoint.x - ((slotRect?.width || 0) / 2),
previewY: startPoint.y - ((slotRect?.height || 0) / 2),
slotRects,
pageRect
};
},
async switchDragPage(targetPageIndex) {
if (!this.dragState.active || this.dragPageSwitching) {
return false;
}
if (targetPageIndex < 0 || targetPageIndex >= this.totalPages || targetPageIndex === this.dragState.pageIndex) {
return false;
}
this.dragPageSwitching = true;
try {
const targetIndex = targetPageIndex < this.dragState.pageIndex
? Math.min(((targetPageIndex + 1) * this.pageSize) - 1, this.quickActions.length - 1)
: Math.min(targetPageIndex * this.pageSize, this.quickActions.length - 1);
this.reorderQuickActions(this.dragState.key, targetIndex);
this.currentPage = targetPageIndex + 1;
await this.$nextTick();
await new Promise(resolve => setTimeout(resolve, 16));
const { pageRect, slotRects } = await this.captureCurrentPageSlots(targetPageIndex);
if (!pageRect || !slotRects.length) {
return false;
}
this.dragState = {
...this.dragState,
pageIndex: targetPageIndex,
hoverIndex: targetIndex,
slotRects,
pageRect
};
return true;
} finally {
this.dragPageSwitching = false;
}
},
async maybeSwitchDragPage(point) {
const pageRect = this.dragState.pageRect;
if (!pageRect) {
return false;
}
const edgeThreshold = 50;
if (point.x <= pageRect.left + edgeThreshold && this.dragState.pageIndex > 0) {
return this.switchDragPage(this.dragState.pageIndex - 1);
}
if (point.x >= pageRect.right - edgeThreshold && this.dragState.pageIndex < this.totalPages - 1) {
return this.switchDragPage(this.dragState.pageIndex + 1);
}
return false;
},
findClosestSlotIndex(point) {
const { slotRects } = this.dragState;
if (!Array.isArray(slotRects) || !slotRects.length) {
return -1;
}
let closestIndex = -1;
let minDistance = Infinity;
slotRects.forEach((rect, index) => {
if (!rect) {
return;
}
const inside = point.x >= rect.left && point.x <= rect.right && point.y >= rect.top && point.y <= rect.bottom;
if (inside) {
closestIndex = index;
minDistance = 0;
return;
}
if (minDistance === 0) {
return;
}
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const distance = Math.pow(point.x - centerX, 2) + Math.pow(point.y - centerY, 2);
if (distance < minDistance) {
minDistance = distance;
closestIndex = index;
}
});
return closestIndex;
},
reorderQuickActions(dragKey, targetIndex) {
const fromIndex = this.quickActions.findIndex(item => this.getMenuKey(item) === dragKey);
if (fromIndex < 0) {
return;
}
const maxIndex = Math.max(this.quickActions.length - 1, 0);
const safeTargetIndex = Math.min(Math.max(targetIndex, 0), maxIndex);
if (fromIndex === safeTargetIndex) {
return;
}
const nextActions = [...this.quickActions];
const [movedItem] = nextActions.splice(fromIndex, 1);
nextActions.splice(safeTargetIndex, 0, movedItem);
this.quickActions = nextActions;
this.dragState.hoverIndex = safeTargetIndex;
this.currentPage = Math.floor(safeTargetIndex / this.pageSize) + 1;
},
async onDragMove(event) {
if (!this.dragState.active || this.dragPageSwitching) {
return;
}
const point = this.getTouchPoint(event);
this.dragState.previewX = point.x - 60;
this.dragState.previewY = point.y - 60;
await this.maybeSwitchDragPage(point);
const slotIndex = this.findClosestSlotIndex(point);
if (slotIndex < 0) {
return;
}
const pageStart = this.dragState.pageIndex * this.pageSize;
const targetIndex = Math.min(pageStart + slotIndex, this.quickActions.length - 1);
this.reorderQuickActions(this.dragState.key, targetIndex);
},
async saveQuickActionsOrder() {
if (this.isSavingOrder) {
return;
}
this.isSavingOrder = true;
try {
await this.$store.dispatch('setting/setValue', {
key: this.FIXED_MENU_KEY,
value: this.quickActions
});
} catch (error) {
console.error('保存常用功能排序失败:', error);
uni.showToast({
title: '排序保存失败',
icon: 'none'
});
} finally {
this.isSavingOrder = false;
}
},
async endDrag() {
if (!this.dragState.active) {
return;
}
this.dragState = {
active: false,
item: null,
key: '',
pageIndex: -1,
hoverIndex: -1,
previewX: 0,
previewY: 0,
slotRects: [],
pageRect: null
};
this.dragTouchStart = null;
this.dragPageSwitching = false;
setTimeout(() => {
this.suppressItemClick = false;
}, 120);
await this.saveQuickActionsOrder();
},
handleGridItemClick(item) {
if (this.dragState.active || this.suppressItemClick) {
return;
}
this.toPath(item);
},
// 获取用户菜单
getUserMenus() {
@ -498,6 +784,12 @@
color: #333;
margin-left: 10rpx;
}
.drag-tip {
margin-left: 16rpx;
font-size: 22rpx;
color: #909399;
}
}
.title-right {
@ -557,6 +849,7 @@
display: flex;
flex-direction: column;
align-items: center;
position: relative;
.quick-swiper {
width: 100%;
@ -578,6 +871,7 @@
justify-content: center;
position: relative;
border-radius: 12rpx;
transition: transform 0.18s ease, opacity 0.18s ease, background 0.18s ease;
.item-icon-box {
width: 100rpx;
@ -608,11 +902,55 @@
background: #f8f9fa;
}
&.drag-placeholder {
opacity: 0.2;
transform: scale(0.92);
}
&.drag-disabled {
transition: none;
}
&.empty-item {
opacity: 0;
}
}
}
.drag-preview {
position: fixed;
z-index: 999;
width: 120rpx;
height: 170rpx;
border-radius: 12rpx;
background: rgba(255, 255, 255, 0.96);
box-shadow: 0 12rpx 32rpx rgba(0, 0, 0, 0.16);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
pointer-events: none;
transform: scale(1.05);
.item-icon-box {
width: 100rpx;
height: 100rpx;
border-radius: 20rpx;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 12rpx;
}
.item-name {
font-size: 26rpx;
font-weight: bold;
color: #333;
text-align: center;
line-height: 1.3;
padding: 0 6rpx;
}
}
}
}

View File

@ -25,7 +25,12 @@
</u-form-item>
<u-form-item v-else-if="receivingInfo.delivery_method !== 'other'" label="物流单号" prop="tracking_number">
<u-input v-model="receivingInfo.tracking_number" placeholder="请输入物流单号" />
<view class="tracking-input-wrap">
<u-input v-model="receivingInfo.tracking_number" placeholder="请输入物流单号" />
<view class="scan-camera-btn" @click="handleTrackingScan">
<u-icon name="camera" size="34rpx" color="#2979ff"></u-icon>
</view>
</view>
</u-form-item>
</view>
<view class="section" v-if="receivingInfo.id">
@ -156,6 +161,64 @@
}
},
methods: {
normalizeUniScanResult(scanResult) {
if (!scanResult) {
return '';
}
if (typeof scanResult === 'string') {
return scanResult.trim();
}
if (typeof scanResult.result === 'string') {
return scanResult.result.trim();
}
if (Array.isArray(scanResult.result)) {
const firstResult = scanResult.result.find(item => item);
if (typeof firstResult === 'string') {
return firstResult.trim();
}
if (firstResult && typeof firstResult.data === 'string') {
return firstResult.data.trim();
}
}
if (typeof scanResult.data === 'string') {
return scanResult.data.trim();
}
return '';
},
handleTrackingScan() {
uni.scanCode({
onlyFromCamera: false,
scanType: ['qrCode', 'barCode', 'datamatrix', 'pdf417'],
success: (res) => {
const rawCode = this.normalizeUniScanResult(res);
if (!rawCode) {
uni.showToast({
title: '未识别到有效内容',
icon: 'none'
});
return;
}
this.receivingInfo.tracking_number = rawCode;
},
fail: (error) => {
const errMsg = String(error?.errMsg || '').toLowerCase();
if (errMsg.includes('cancel')) {
return;
}
uni.showToast({
title: '扫码失败,请重试',
icon: 'none'
});
}
});
},
qrcode(data){
var qr = new UQRCode();
qr.data = data;
@ -213,6 +276,33 @@
height: 100%;
}
.tracking-input-wrap {
width: 100%;
display: flex;
align-items: center;
gap: 16rpx;
}
.tracking-input-wrap :deep(.u-input) {
flex: 1;
}
.scan-camera-btn {
width: 76rpx;
height: 76rpx;
border-radius: 38rpx;
background: #eef4ff;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
box-shadow: 0 4rpx 12rpx rgba(41, 121, 255, 0.12);
}
.scan-camera-btn:active {
opacity: 0.85;
}
.section {
background-color: #fff;
border-radius: 16rpx;