liufumingren 20f0454c79 refactor(ui): 首页/退货/称重/顶部导航统一到全局排版规范
- 字号、行高、圆角、间距改用 :root token 与 5px 网格,删除页面内重复的通用样式
- 顶部导航高度统一 --wms-nav-height;页面 padding/背景统一 --wms-page-pad / --wms-page-bg
- 4 个页面 renderjs 统一为 #ifndef APP-PLUS 包裹 rem 初始化(H5 专用),消除 semicolon-after-return 编译告警
2026-09-27 03:15:12 +08:00

301 lines
8.7 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="layout-page">
<top-nav
:menus="topMenus"
:active-path="activeMenuPath"
:use-redirect="false"
:user-info="currentUser"
:fetch-user="false"
@menu-click="handleMenuClick"
@user-changed="handleUserChanged"
/>
<view class="layout-main">
<keep-alive v-if="activeViewComponent">
<component :is="activeViewComponent" :embedded="true" compact-top />
</keep-alive>
<view v-else class="module-placeholder">
<text class="module-placeholder-title">{{ getActiveMenuTitle() || '请选择功能菜单' }}</text>
<text v-if="activeMenuPath" class="module-placeholder-desc">该菜单页面尚未配置({{ activeMenuPath }})</text>
</view>
</view>
</view>
</template>
<script>
import { fetchUserInfo } from '@/api/index'
import TopNav from '@/components/top-nav.vue'
import { hasMenuView, normalizeMenuPath, resolveMenuView } from '@/router/menuRoutes'
export default {
components: {
TopNav,
},
onReady() {
uni.hideKeyboard?.()
;[80, 180, 320, 520].forEach((delay) => {
setTimeout(() => {
uni.hideKeyboard?.()
}, delay)
})
},
onShow() {
this.lockLandscape()
setTimeout(() => {
this.lockLandscape()
}, 300)
uni.hideKeyboard?.()
;[80, 180, 320, 520].forEach((delay) => {
setTimeout(() => {
uni.hideKeyboard?.()
}, delay)
})
},
onLoad(options) {
const token = uni.getStorageSync('token')
if (!token) {
uni.reLaunch({
url: '/pages/login/login',
})
return
}
// 刷新恢复时优先取 hash 路由参数,其次取 URL 外层 query(见 syncMenuToUrl)
let menu = options?.menu
if (!menu && typeof window !== 'undefined') {
const fromQuery = new URLSearchParams(window.location.search).get('menu')
if (fromQuery) {
menu = fromQuery
}
}
this.activeMenuPath = normalizeMenuPath(menu)
this.loadCurrentUserInfo()
},
data() {
return {
currentUser: null,
activeMenuPath: '',
}
},
computed: {
topMenus() {
const menus = this.currentUser?.menus
if (!Array.isArray(menus)) {
return []
}
return menus.filter((menu) => !menu?.hidden)
},
activeViewComponent() {
return resolveMenuView(this.activeMenuPath)
},
},
methods: {
// 强制锁定横屏 + 全屏重排:真机 reLaunch 后新 WebView 可能以竖屏尺寸创建,需 setStyle 强制全屏
lockLandscape() {
// #ifdef APP-PLUS
try {
const screen = plus && plus.screen
if (screen && typeof screen.lockOrientation === 'function') {
screen.lockOrientation('landscape')
console.log('[index] lockOrientation=landscape done')
}
if (typeof plus !== 'undefined' && plus.webview) {
const wv = plus.webview.currentWebview()
if (wv && typeof wv.setStyle === 'function') {
wv.setStyle({ top: '0px', bottom: '0px', left: '0px', right: '0px' })
console.log('[index] webview setStyle fullscreen done')
}
}
return true
} catch (e) {
console.error('[index] lockOrientation 异常:', e)
}
// #endif
return false
},
getMenuTitle(menu) {
return menu?.meta?.title || menu?.name || '未命名菜单'
},
isMenuActive(menu) {
const path = normalizeMenuPath(menu?.path)
return !!path && this.activeMenuPath === path
},
getActiveMenu() {
return this.topMenus.find((menu) => normalizeMenuPath(menu?.path) === this.activeMenuPath)
},
getActiveMenuTitle() {
return this.getMenuTitle(this.getActiveMenu())
},
syncMenuToUrl(menuPath) {
const normalizedPath = normalizeMenuPath(menuPath)
// #ifdef H5
if (typeof window !== 'undefined' && window.history?.replaceState) {
const query = normalizedPath ? `?menu=${encodeURIComponent(normalizedPath)}` : ''
// history 模式:pathname 即当前路由,仅替换 query,不再拼接 hash
const base = window.location.pathname || '/'
window.history.replaceState(null, '', `${base}${query}`)
}
// #endif
},
setActiveMenu(menuPath, options = {}) {
const { syncUrl = true } = options
const normalizedPath = normalizeMenuPath(menuPath)
if (!normalizedPath) {
return
}
this.activeMenuPath = normalizedPath
if (syncUrl) {
this.syncMenuToUrl(normalizedPath)
}
},
ensureActiveMenuValid() {
const menus = this.topMenus
if (!menus.length) {
this.activeMenuPath = ''
return
}
const matched = menus.find((menu) => normalizeMenuPath(menu?.path) === this.activeMenuPath)
if (matched) {
return
}
const firstPath = normalizeMenuPath(menus[0]?.path)
if (firstPath) {
this.setActiveMenu(firstPath)
}
},
handleUserChanged() {
// 角色切换后重新拉取用户信息,再经 user-info 传给 top-nav
this.loadCurrentUserInfo()
},
handleMenuClick(menu) {
const rawPath = String(menu?.path || '').trim()
if (!rawPath) {
uni.showToast({
title: '菜单路径为空',
icon: 'none',
})
return
}
const normalizedPath = normalizeMenuPath(rawPath)
this.setActiveMenu(normalizedPath)
if (!hasMenuView(normalizedPath)) {
uni.showToast({
title: '该功能页面暂未开放',
icon: 'none',
})
}
},
persistUserContext(user) {
if (!user || typeof user !== 'object') {
return
}
const storeId =
user?.store_id ?? user?.storeId ?? user?.store?.id ?? user?.store?.store_id
const warehouseId =
user?.warehouse_id ?? user?.warehouseId ?? user?.warehouse?.id ?? user?.warehouse?.warehouse_id
if (storeId !== undefined && storeId !== null && storeId !== '') {
uni.setStorageSync('store_id', storeId)
}
if (warehouseId !== undefined && warehouseId !== null && warehouseId !== '') {
uni.setStorageSync('warehouse_id', warehouseId)
}
},
loadCurrentUserInfo() {
fetchUserInfo()
.then((body) => {
const user = body?.data || body?.result || body || null
this.currentUser = user
// 持久化用户上下文 store_id / warehouse_id(面单补打团单模式按接口约定 warehouse_id 须与 store_id 一致)
this.persistUserContext(user)
this.ensureActiveMenuValid()
if (!this.activeMenuPath && this.topMenus.length) {
const firstPath = normalizeMenuPath(this.topMenus[0]?.path)
if (firstPath) {
this.setActiveMenu(firstPath)
}
}
})
.catch(() => {
/* 静默失败,避免打断主导航 */
})
},
},
}
</script>
<script module="remInject" lang="renderjs">
import { applyRemBase } from '../../render-common/rem-base.js'
export default {
mounted() {
// #ifndef APP-PLUS
// rem 基准统一走 render-common/rem-base.js(满格 1rem = 100px,>1280 宽不再放大字体),
// 字号偏大靠改 CSS 的 font-size 值(已全局 ×0.85),不动基准
// 各页面不再各算一份,避免系数漂移(历史上 returns 页曾写成 0.20,与其他页相差 2.1 倍)
// 真机(APP-PLUS)rem 由 App.vue 全局纯 CSS(html { font-size: 7.8125vw !important }=100/1280)承担。
// ⚠️ 这里用 #ifndef,不要写成 `#ifdef APP-PLUS + return`:后者会让 esbuild 报
// “semicolon-after-return”,且以后往 mounted 里补的视图层代码会变成真机永不执行。
this._remResizeHandler = applyRemBase
applyRemBase()
window.addEventListener('resize', this._remResizeHandler)
// #endif
},
beforeDestroy() {
if (this._remResizeHandler) {
window.removeEventListener('resize', this._remResizeHandler)
this._remResizeHandler = null
}
}
}
</script>
<style>
.layout-page {
height: 100vh;
min-height: 100vh;
overflow: hidden;
min-width: 6.14rem;
box-sizing: border-box;
padding: 0;
display: flex;
flex-direction: column;
align-items: stretch;
justify-content: flex-start;
background-color: var(--wms-page-bg);
}
.layout-main {
flex: 1;
min-height: 0;
padding: 0;
box-sizing: border-box;
display: flex;
flex-direction: column;
}
.module-placeholder {
background-color: var(--wms-card-bg);
border-radius: 0.13rem;
padding: 0.25rem;
}
.module-placeholder-title {
display: block;
font-size: 0.21rem;
color: var(--wms-text);
}
.module-placeholder-desc {
display: block;
margin-top: 0.1rem;
font-size: 0.17rem;
color: var(--wms-text-placeholder);
}
</style>