diff --git a/pages/index/index.vue b/pages/index/index.vue index 67ee18e..85dac73 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -31,6 +31,7 @@ 常用功能 + 长按拖拽排序 @@ -48,22 +49,30 @@ - + 暂无可用功能 请联系仓库管理员检查授权 - + - + - + @@ -74,10 +83,21 @@ + class="grid-item grid-slot empty-item"> + + + + + + {{ dragState.item.name }} + @@ -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; + } + } } } diff --git a/pages/work/storage/receiving/components/info-container.vue b/pages/work/storage/receiving/components/info-container.vue index 5f424ed..035cccf 100644 --- a/pages/work/storage/receiving/components/info-container.vue +++ b/pages/work/storage/receiving/components/info-container.vue @@ -25,7 +25,12 @@ - + + + + + + @@ -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;