551 lines
14 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>
<view class="top-nav" @click="closeMinePopup">
<image class="top-nav-logo" src="/static/images/1.png" mode="heightFix"></image>
<view class="top-nav-menu-wrap">
<view
v-for="(item, index) in displayMenus"
:key="item.key"
:class="['top-nav-menu-item', activeIndex === index ? 'top-nav-menu-item--active' : '']"
@click="handleMenuClick(item, index)"
>
<text class="top-nav-menu-text">{{ item.title }}</text>
</view>
</view>
<view class="top-nav-right" @click.stop>
<view class="top-nav-user" @click="openMinePopup">
<view class="top-nav-mine-icon" aria-hidden="true">
<view class="top-nav-mine-icon-head"></view>
<view class="top-nav-mine-icon-body"></view>
</view>
<text class="top-nav-user-name">{{ getTopNavRealName() }}</text>
</view>
<view v-if="showMinePopup" class="popup-card" @click.stop>
<view class="popup-user-info" v-if="currentUser && currentUser.employees && currentUser.employees.length">
<view
v-for="employee in currentUser.employees"
:key="employee.id"
:class="['popup-user-item', isCurrentRole(employee) ? 'popup-user-item--current' : '']"
@click="handleChangeRole(employee)"
>
<text class="popup-user-text">{{ employee.store_name }} | {{ employee.real_name }}</text>
</view>
</view>
<button class="logout-btn" type="warn" @click="handleLogout">退出登录</button>
<button class="close-btn" @click="closeMinePopup">关闭</button>
</view>
</view>
</view>
<view v-show="switchMaskVisible" :class="['switch-mask', switchMaskActive ? 'switch-mask--active' : '']"></view>
</view>
</template>
<script>
import { changeUserRole, fetchUserInfo, logout } from '@/api/index'
export default {
name: 'TopNav',
props: {
menus: {
type: Array,
default: () => [],
},
activePath: {
type: String,
default: '',
},
useRedirect: {
type: Boolean,
default: true,
},
userName: {
type: String,
default: '未登录用户',
},
},
data() {
return {
activeIndex: 0,
showMinePopup: false,
isNavigating: false,
lastMenuClickAt: 0,
routeSyncTimer: null,
lastRoutePath: '',
switchMaskVisible: false,
switchMaskActive: false,
switchMaskTimer: null,
switchMaskFadeTimer: null,
switchMaskStartedAt: 0,
currentUser: null,
defaultMenus: [
{
title: '订单复核1',
path: '/pages/order/index',
},
{
title: '称重',
path: '/pages/weighing/index',
},
{
title: '退件',
path: '/pages/returns/index',
},
],
}
},
computed: {
displayMenus() {
const sourceMenus = Array.isArray(this.menus) && this.menus.length ? this.menus : this.defaultMenus
return sourceMenus
.map((menu, index) => {
const path = this.normalizePath(menu?.path)
return {
raw: menu,
key: menu?.path || menu?.name || String(index),
path,
title: this.getMenuTitle(menu),
}
})
.filter((menu) => !!menu.path)
},
},
watch: {
activePath() {
this.syncActiveIndex()
},
menus: {
deep: true,
handler() {
this.syncActiveIndex()
},
},
},
created() {
this.syncActiveIndex()
this.loadCurrentUserInfo()
},
mounted() {
this.lastRoutePath = this.getCurrentRoutePath()
if (this.useRedirect && !this.activePath) {
this.routeSyncTimer = setInterval(() => {
const currentPath = this.getCurrentRoutePath()
if (!currentPath || currentPath === this.lastRoutePath) {
return
}
this.lastRoutePath = currentPath
this.syncActiveByRoute()
}, 120)
}
},
beforeDestroy() {
if (this.routeSyncTimer) {
clearInterval(this.routeSyncTimer)
this.routeSyncTimer = null
}
if (this.switchMaskTimer) {
clearTimeout(this.switchMaskTimer)
this.switchMaskTimer = null
}
if (this.switchMaskFadeTimer) {
clearTimeout(this.switchMaskFadeTimer)
this.switchMaskFadeTimer = null
}
},
methods: {
normalizePath(path) {
const raw = String(path || '').trim()
if (!raw) {
return ''
}
return raw.startsWith('/') ? raw : `/${raw}`
},
getMenuTitle(menu) {
return menu?.meta?.title || menu?.title || menu?.name || '未命名菜单'
},
syncActiveIndex() {
const normalizedActivePath = this.normalizePath(this.activePath)
if (normalizedActivePath) {
const targetIndex = this.displayMenus.findIndex((item) => item.path === normalizedActivePath)
this.activeIndex = targetIndex >= 0 ? targetIndex : 0
return
}
this.syncActiveByRoute()
},
syncActiveByRoute() {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
const route = currentPage?.route ? `/${currentPage.route}` : ''
const targetIndex = this.displayMenus.findIndex((item) => item.path === route)
this.activeIndex = targetIndex >= 0 ? targetIndex : 0
},
getCurrentRoutePath() {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
return currentPage?.route ? `/${currentPage.route}` : ''
},
showNavigationMask() {
this.switchMaskStartedAt = Date.now()
if (this.switchMaskTimer) {
clearTimeout(this.switchMaskTimer)
this.switchMaskTimer = null
}
if (this.switchMaskFadeTimer) {
clearTimeout(this.switchMaskFadeTimer)
this.switchMaskFadeTimer = null
}
// 先把遮罩放回 DOM(透明状态),下一帧再激活透明度触发淡入
this.switchMaskActive = false
this.switchMaskVisible = true
this.$nextTick(() => {
this.switchMaskActive = true
})
},
hideNavigationMask() {
const elapsed = Date.now() - this.switchMaskStartedAt
// 至少保持可见一段时间,给新页面留出首帧渲染时间,避免刚出现就消失造成闪烁
const minVisible = 180
const delay = elapsed >= minVisible ? 0 : minVisible - elapsed
if (this.switchMaskTimer) {
clearTimeout(this.switchMaskTimer)
}
this.switchMaskTimer = setTimeout(() => {
this.switchMaskTimer = null
// 触发淡出过渡
this.switchMaskActive = false
if (this.switchMaskFadeTimer) {
clearTimeout(this.switchMaskFadeTimer)
}
// 淡出动画结束后再移除 DOM,保证过渡完整执行
this.switchMaskFadeTimer = setTimeout(() => {
this.switchMaskFadeTimer = null
this.switchMaskVisible = false
}, 200)
}, delay)
},
navigateToMenu(targetPath) {
const pages = getCurrentPages()
const currentIndex = pages.length - 1
const targetRoute = targetPath.replace(/^\//, '')
const targetIndex = pages.map((page) => page?.route).lastIndexOf(targetRoute)
const finishNavigate = () => {
this.hideNavigationMask()
setTimeout(() => {
this.isNavigating = false
}, 60)
}
if (targetIndex >= 0 && currentIndex > targetIndex) {
uni.navigateBack({
delta: currentIndex - targetIndex,
animationType: 'none',
animationDuration: 0,
complete: finishNavigate,
})
return
}
uni.navigateTo({
url: targetPath,
animationType: 'none',
animationDuration: 0,
success: finishNavigate,
fail: () => {
uni.redirectTo({
url: targetPath,
animationType: 'none',
animationDuration: 0,
complete: finishNavigate,
})
},
})
},
handleMenuClick(item, index) {
const now = Date.now()
if (now - this.lastMenuClickAt < 150) {
return
}
this.lastMenuClickAt = now
if (this.isNavigating) {
return
}
const targetPath = this.normalizePath(item?.path)
if (!targetPath) {
return
}
const currentPath = this.getCurrentRoutePath()
if (this.useRedirect && currentPath === targetPath) {
this.syncActiveByRoute()
this.closeMinePopup()
return
}
if (!this.useRedirect) {
this.activeIndex = index
}
this.$emit('menu-click', item.raw)
if (this.useRedirect) {
this.isNavigating = true
this.showNavigationMask()
this.navigateToMenu(targetPath)
}
this.closeMinePopup()
},
getTopNavRealName() {
const employees = this.currentUser?.employees || []
if (employees.length && employees[0]?.real_name) {
return employees[0].real_name
}
if (this.userName && this.userName !== '未登录用户') {
return this.userName
}
return this.currentUser?.real_name || '未命名用户'
},
isCurrentRole(employee) {
return Number(employee?.is_primary) === 1
},
openMinePopup() {
this.showMinePopup = true
},
closeMinePopup() {
this.showMinePopup = false
},
loadCurrentUserInfo() {
fetchUserInfo()
.then((body) => {
this.currentUser = body?.data || body?.result || body || null
})
.catch(() => {
this.currentUser = null
})
},
handleChangeRole(employee) {
const employeeId = employee?.id
const token = uni.getStorageSync('token')
if (!employeeId || !token) {
return
}
changeUserRole(employeeId)
.then((body) => {
const isSuccess = body?.code === 0 || body?.success === true || body?.status === 1
if (!isSuccess && (body?.msg || body?.message)) {
uni.showToast({
title: body?.msg || body?.message,
icon: 'none',
})
return
}
this.loadCurrentUserInfo()
this.closeMinePopup()
uni.showToast({
title: '角色切换成功',
icon: 'success',
})
})
.catch(() => {
uni.showToast({
title: '角色切换失败',
icon: 'none',
})
})
},
finishLogout() {
uni.removeStorageSync('token')
uni.showToast({
title: '已退出登录',
icon: 'none',
})
setTimeout(() => {
uni.reLaunch({
url: '/pages/login/login',
})
}, 200)
},
handleLogout() {
const token = uni.getStorageSync('token')
if (!token) {
this.finishLogout()
return
}
logout().finally(() => {
this.finishLogout()
})
},
},
}
</script>
<style scoped>
.top-nav {
width: 100%;
margin-bottom: 0.614rem;
padding: 0.461rem 0.717rem;
box-sizing: border-box;
background-color: #2f7dff;
display: flex;
align-items: center;
justify-content: space-between;
}
.top-nav-logo {
width: 6.144rem;
height: 2.304rem;
flex-shrink: 0;
}
.top-nav-right {
position: relative;
margin-left: auto;
}
.top-nav-menu-wrap {
display: flex;
align-items: center;
margin-left: 0.922rem;
gap: 0.358rem;
min-width: 0;
}
.top-nav-menu-item {
padding: 0.256rem 0.461rem;
border-radius: 0.205rem;
}
.top-nav-menu-item--active {
background-color: rgba(255, 255, 255, 0.24);
}
.top-nav-menu-text {
font-size: 0.819rem;
color: #fff;
font-weight: 500;
}
.top-nav-user {
display: flex;
align-items: center;
gap: 0.256rem;
cursor: pointer;
}
.top-nav-user-name {
color: #fff;
font-size: 0.819rem;
line-height: 1;
}
.top-nav-mine-icon {
width: 1.126rem;
height: 1.126rem;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-radius: 25.574rem;
background-color: rgba(255, 255, 255, 0.2);
box-sizing: border-box;
}
.top-nav-mine-icon-head {
width: 0.307rem;
height: 0.307rem;
border: 0.051rem solid #fff;
border-radius: 50%;
box-sizing: border-box;
}
.top-nav-mine-icon-body {
width: 0.512rem;
height: 0.256rem;
margin-top: 0.102rem;
border: 0.051rem solid #fff;
border-top: none;
border-radius: 0 0 0.307rem 0.307rem;
box-sizing: border-box;
}
.popup-card {
position: absolute;
right: 0;
top: 1.792rem;
min-width: 8.448rem;
background: #fff;
border-radius: 0.307rem;
box-shadow: 0 0.307rem 0.819rem rgba(0, 0, 0, 0.16);
padding: 0.41rem;
box-sizing: border-box;
z-index: 30;
}
.popup-user-info {
max-height: 8.192rem;
overflow-y: auto;
}
.popup-user-item {
padding: 0.307rem;
border-radius: 0.205rem;
}
.popup-user-item--current {
background-color: #edf4ff;
}
.popup-user-text {
font-size: 0.717rem;
color: #333;
}
.popup-user-item--current .popup-user-text {
color: #2f7dff;
}
.logout-btn {
margin-top: 0.41rem;
height: 1.536rem;
line-height: 1.536rem;
font-size: 0.717rem;
}
.close-btn {
margin-top: 0.256rem;
height: 1.536rem;
line-height: 1.536rem;
font-size: 0.717rem;
background-color: #f5f7fb;
color: #333;
}
.switch-mask {
position: fixed;
left: 0;
top: 0;
right: 0;
bottom: 0;
background-color: #f8f8f8;
z-index: 9999;
pointer-events: none;
opacity: 0;
visibility: hidden;
transition: opacity 0.2s ease, visibility 0.2s ease;
}
.switch-mask--active {
opacity: 1;
visibility: visible;
}
</style>