WMS_PDA/pages/index/index.vue

1014 lines
25 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="pda-home">
<view class="status_bar">
</view>
<view class="compact-header">
<!-- 主体切换 -->
<view class="org-switcher">
<view class="org-info" @click="showStoreSelector">
<text class="org-name">{{ userInfo.store_name || '选择主体' }}</text>
<text class="org-id">{{ userInfo.employees_name || '---' }}</text>
</view>
<u-icon @click="showStoreSelector" name="arrow-down" size="16" color="#666" v-if="userInfo.employees && userInfo.employees.length > 1"></u-icon>
<view class="news-wrapper" @click="toNewsPage">
<u-notice-bar :text="newsList[0]" mode="horizontal" bg-color="transparent" color="#666"
:font-size="12" :padding="0" :radius="0" :volume-icon="false" :more-icon="false" :speed="60"
:height="30" :autoplay="true"></u-notice-bar>
</view>
</view>
<!-- 消息区域 -->
<view class="header-actions">
</view>
</view>
<!-- 主内容区 -->
<scroll-view class="main-scroll" scroll-y :scroll-top="scrollTop" :show-scrollbar="false">
<!-- 快捷入口区域 -->
<view class="quick-section">
<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>
<text class="edit-btn" @click="toEditPage">全部功能</text>
<text class="divider">|</text>
<text class="page-indicator">{{ currentPage }}/{{ totalPages }}</text>
</view>
</view>
<!-- 3行4列的快捷入口网格 -->
<view class="quick-grid-container">
<!-- 上一页按钮 -->
<view class="nav-arrow left-arrow" @click="prevPage" :class="{ disabled: currentPage === 1 }">
<u-icon name="arrow-left" size="20" color="#666"></u-icon>
</view>
<!-- 网格内容 -->
<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" :disable-touch="dragState.active" @change="onSwiperChange"
:duration="swiperDuration">
<swiper-item v-for="(pageItems, pageIndex) in paginatedActions" :key="pageIndex">
<view class="grid-page" :id="`grid-page-${pageIndex}`">
<!-- 3行4列布局,共12个 -->
<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>
<text class="item-name">{{ item.name }}</text>
<u-badge v-if="item.badge && item.badge > 0" :count="item.badge" size="mini"
type="error" :offset="[-3, 3]" class="item-badge"></u-badge>
</view>
<!-- 填充空白项 -->
<view v-for="n in (12 - pageItems.length)" :key="'empty-' + n"
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>
<!-- 下一页按钮 -->
<view class="nav-arrow right-arrow" @click="nextPage"
:class="{ disabled: currentPage === totalPages }">
<u-icon name="arrow-right" size="20" color="#666"></u-icon>
</view>
</view>
<!-- 分页指示器 -->
<view class="pagination-dots">
<view v-for="n in totalPages" :key="n" class="pagination-dot"
:class="{ active: n === currentPage }"></view>
</view>
</view>
<!-- 加载状态 -->
<view class="loading-state" v-if="isLoading">
<text class="loading-text">加载中...</text>
</view>
</scroll-view>
</view>
</template>
<script>
export default {
data() {
return {
isLoading: true,
currentPage: 1,
scrollTop: 0,
userInfo: {},
newsList: [
'欢迎使用PDA仓库管理系统',
'请及时处理待办任务',
'祝您工作愉快'
],
unreadCount: 0,
quickActions: [],
pageSize: 12,
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
}
},
computed: {
// 分页后的快捷入口
paginatedActions() {
const pages = [];
for (let i = 0; i < this.quickActions.length; i += this.pageSize) {
pages.push(this.quickActions.slice(i, i + this.pageSize));
}
return pages;
},
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`
};
}
},
async onLoad() {
await this.loadUserInfo();
},
onShow() {
this.loadUserInfo();
},
methods: {
async loadUserInfo() {
// 等待用户信息加载
await new Promise(resolve => setTimeout(resolve, 100));
if (!this.$store.getters.user) {
console.log('等待用户信息...');
setTimeout(() => this.loadUserInfo(), 100);
return;
}
const userData = this.$store.getters.user;
if (userData) {
this.isLoading = false;
this.userInfo = userData;
await this.refreshQuickActions();
}
},
// 刷新快捷入口
async refreshQuickActions() {
this.isLoading = true;
try {
// 1. 获取用户当前可用的菜单
const userMenus = this.getUserMenus();
const latestActions = this.processMenus(userMenus, null);
// 2. 从Vuex获取已固定的菜单
const savedFixedMenus = await this.$store.dispatch('setting/getValue', this.FIXED_MENU_KEY) || [];
const hasSavedConfig = Array.isArray(savedFixedMenus);
// 3. 如果存在已保存的常用配置(包括空数组),优先按配置展示
if (hasSavedConfig) {
const validFixedMenus = this.validateAndFilterMenus(savedFixedMenus, latestActions);
console.log('按已保存常用菜单展示:', validFixedMenus);
this.quickActions = validFixedMenus;
return;
}
// 4. 如果没有有效的固定菜单,检查用户菜单
if (userMenus && userMenus.length > 0) {
console.log('从用户菜单生成快捷入口:', userMenus);
// 根据用户菜单生成快捷入口
this.quickActions = this.processMenus(userMenus, 24);
} else {
// 5. 如果还是没有菜单,返回空数组
console.log('用户没有可用菜单');
this.quickActions = [];
}
} catch (error) {
console.error('刷新快捷入口失败:', error);
this.quickActions = [];
} finally {
this.isLoading = false;
}
},
// 辅助方法:验证和过滤菜单权限
validateAndFilterMenus(fixedMenus, latestActions) {
if (!Array.isArray(fixedMenus) || !Array.isArray(latestActions)) return [];
const actionMap = new Map();
latestActions.forEach(action => {
actionMap.set(this.getMenuKey(action), action);
});
return fixedMenus
.map(fixedMenu => {
const matched = actionMap.get(this.getMenuKey(fixedMenu));
if (!matched) return null;
return {
...matched,
...fixedMenu,
name: matched.name,
path: matched.path,
component: matched.component,
icon: matched.icon || fixedMenu.icon,
id: matched.id || fixedMenu.id
};
})
.filter(Boolean);
},
getMenuKey(menu) {
return String(
menu?.component ||
menu?.path ||
menu?.id ||
menu?.menuId ||
menu?.name ||
''
);
},
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() {
const user = this.$store.getters.user;
if (!user) return [];
// 从用户信息中获取菜单
return user.menus || user.permissions || [];
},
// 处理菜单数据
processMenus(menus, limit = 24) {
// 1. 展平所有子菜单(使用 flatMap 替代 map + filter)
const allChildren = menus.flatMap(item =>
item.children || [] // 确保 children 存在,否则返回空数组
);
// 2. 过滤有效菜单并限制数量
let validMenus = allChildren.filter(menu =>
menu &&
menu.component &&
menu.component !== 'Layout' &&
menu.meta &&
menu.meta.title
);
if (typeof limit === 'number' && limit > 0) {
validMenus = validMenus.slice(0, limit); // 默认最多显示24个
}
// 3. 生成随机颜色的函数
const generateRandomColor = () => {
const colorPool = [
'#FF6B6B', '#4ECDC4', '#FFD166', '#06D6A0', '#118AB2',
'#EF476F', '#26547C', '#FFD166', '#06D6A0', '#118AB2',
'#7209B7', '#3A86FF', '#FB5607', '#8338EC', '#3A86FF'
];
return colorPool[Math.floor(Math.random() * colorPool.length)];
};
// 4. 转换为目标格式
return validMenus.map(menu => {
return {
id: menu.id || menu.menuId || menu.component || menu.path || menu.meta?.title || '',
name: menu.meta?.title || '未命名菜单',
path: menu.path || '',
component: menu.component || '',
permissions: menu.permissions || [],
icon: menu.meta?.icon || '',
color: generateRandomColor(), // 随机颜色
isFixed: true // 标记为已固定
};
});
},
showStoreSelector() {
if (!this.userInfo.employees || this.userInfo.employees.length <= 1) {
return;
}
// 提取店铺名称列表
const storeList = this.userInfo.employees.map(emp => emp.store_name);
uni.showActionSheet({
itemList: storeList,
success: (res) => {
const selectedIndex = res.tapIndex;
if (selectedIndex >= 0 && selectedIndex < this.userInfo.employees.length) {
const selectedEmployee = this.userInfo.employees[selectedIndex];
this.handleStoreSwitch(selectedEmployee);
}
},
fail: (err) => {
console.log('用户取消选择', err);
}
});
},
async handleStoreSwitch(selectedEmployee) {
try {
// 显示加载中
uni.showLoading({
title: '切换中...',
mask: true
});
const result = await this.$store.dispatch('changeEmployee', selectedEmployee.id);
this.loadUserInfo();
} catch (error) {
uni.hideLoading();
}
},
toPath(item) {
const menuName = String(item?.name || '').trim();
const isGoodsQuery = menuName === '商品查询' || (menuName.includes('商品') && menuName.includes('查询'));
const rawPath = item?.component || item?.path || '';
const targetPath = isGoodsQuery
? '/pages/work/product/query'
: (rawPath ? (rawPath.startsWith('/') ? rawPath : `/${rawPath}`) : '');
if (!targetPath) {
uni.showToast({
title: '功能暂未开放',
icon: 'none',
duration: 1500
});
return;
}
uni.navigateTo({
url: targetPath,
animationType: 'slide-in-right'
});
},
toMessagePage() {
uni.navigateTo({
url: '/pages/message/index'
});
},
toNewsPage() {
uni.switchTab({
url: '/pages/oa/index'
});
},
toEditPage() {
uni.navigateTo({
url: '/pages/home/edit-quick-actions'
});
},
prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage++;
}
},
onSwiperChange(e) {
this.currentPage = e.detail.current + 1;
}
}
}
</script>
<style lang="scss" scoped>
.pda-home {
display: flex;
flex-direction: column;
background: #f8f9fa;
}
// 紧凑型头部
.compact-header {
padding:0 20rpx;
background: white;
border-bottom: 1rpx solid #f0f0f0;
.org-switcher {
display: flex;
align-items: center;
margin-bottom: 6px;
.org-icon {
margin-right: 15rpx;
}
.org-info {
flex: 1;
.org-name {
font-size: 28rpx;
font-weight: 600;
color: #333;
display: block;
line-height: 1.4;
}
.org-id {
font-size: 22rpx;
color: #909399;
display: block;
}
}
}
.header-actions {
display: flex;
align-items: center;
justify-content: space-between;
.news-wrapper {
flex: 1;
margin-right: 20rpx;
overflow: hidden;
::v-deep .u-notice-bar {
height: 30rpx !important;
line-height: 30rpx !important;
}
}
.message-bell {
position: relative;
width: 60rpx;
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
&:active {
background: #f0f0f0;
}
}
}
}
// 主滚动区域
.main-scroll {
flex: 1;
overflow: hidden;
}
// 快捷入口区域
.quick-section {
padding: 20rpx 20rpx 30rpx;
background: white;
margin: 20rpx;
border-radius: 16rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
.section-title-bar {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 25rpx;
padding-bottom: 15rpx;
border-bottom: 1rpx solid #f0f0f0;
.title-left {
display: flex;
align-items: center;
.section-title {
font-size: 32rpx;
font-weight: 600;
color: #333;
margin-left: 10rpx;
}
.drag-tip {
margin-left: 16rpx;
font-size: 22rpx;
color: #909399;
}
}
.title-right {
display: flex;
align-items: center;
.edit-btn {
font-size: 24rpx;
color: #2979ff;
padding: 6rpx 12rpx;
border-radius: 6rpx;
&:active {
background: #f0f7ff;
}
}
.divider {
color: #e0e0e0;
margin: 0 15rpx;
}
.page-indicator {
font-size: 24rpx;
color: #909399;
}
}
}
}
// 网格容器
.quick-grid-container {
display: flex;
align-items: center;
margin-bottom: 20rpx;
.nav-arrow {
width: 20rpx;
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: #f8f9fa;
&:active:not(.disabled) {
background: #e9ecef;
}
&.disabled {
opacity: 0.3;
}
}
.quick-grid-wrapper {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
position: relative;
.quick-swiper {
width: 100%;
height: 540rpx; // 3行 × 180rpx
}
.grid-page {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 10rpx;
padding: 0 10rpx;
.grid-item {
height: 170rpx;
display: flex;
flex-direction: column;
align-items: center;
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;
height: 100rpx;
border-radius: 20rpx;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 12rpx;
}
.item-name {
font-size: 30rpx;
font-weight: bold;
color: #333;
text-align: center;
line-height: 1.3;
padding: 0 5rpx;
}
.item-badge {
position: absolute;
top: 10rpx;
right: 10rpx;
}
&:active {
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;
}
}
}
}
// 分页指示器
.pagination-dots {
display: flex;
justify-content: center;
margin-top: 20rpx;
.pagination-dot {
width: 10rpx;
height: 10rpx;
border-radius: 50%;
background: #e0e0e0;
margin: 0 6rpx;
&.active {
background: #2979ff;
width: 30rpx;
border-radius: 5rpx;
}
}
}
// 加载状态
.loading-state {
padding: 100rpx 0;
display: flex;
flex-direction: column;
align-items: center;
.loading-text {
margin-top: 20rpx;
color: #909399;
font-size: 24rpx;
}
}
// 空状态提示
.empty-state {
padding: 80rpx 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
.empty-text {
display: block;
font-size: 32rpx;
color: #999;
margin-top: 30rpx;
margin-bottom: 15rpx;
}
.empty-hint {
display: block;
font-size: 26rpx;
color: #ccc;
}
}
</style>