Files
zkxw/mobile-h5/utils/menu.js
T
2026-10-15 14:27:42 +08:00

75 lines
2.4 KiB
JavaScript

import { get } from './request.js'
import { getToken } from './auth-session.js'
import { resolveMenuIconColors } from './menu-icon-colors.js'
function authHeader() {
const token = getToken()
return token ? { Authorization: 'Bearer ' + token } : {}
}
/**
* 市场业务叶子 path → van-icon 名(与 static/menu-icons/market-*.png 对齐)。
* 后端未下发 / 回落 apps-o 时由前端兜底,避免空置或灰格宫格。
*/
export function resolveMarketMenuIcon(path) {
const p = String(path || '').toLowerCase()
if (p.indexOf('market/webview') < 0 || p.indexOf('entry=') < 0) {
return ''
}
if (p.indexOf('payment-chase') >= 0) return 'market-payment-chase'
if (p.indexOf('presign') >= 0) return 'market-presign'
if (p.indexOf('protocol') >= 0) return 'market-protocol'
if (p.indexOf('payment') >= 0) return 'market-payment'
if (p.indexOf('freight') >= 0) return 'market-freight'
return ''
}
/**
* 分区标题:优先用接口 groupTitle;缺失时按 path 推断(兼容测试库未跑目录重组 SQL)。
*/
export function resolveMenuGroupTitle(path, groupTitle) {
const existing = String(groupTitle || '').trim()
if (existing) return existing
const p = String(path || '').toLowerCase()
if (p.indexOf('market/webview') >= 0 || p.indexOf('/pages/market/') >= 0) {
return '市场部'
}
if (p.indexOf('/pages/oa/') >= 0 || p.indexOf('pages/oa/') >= 0) {
return 'OA工作流'
}
return ''
}
/** 当前用户移动端菜单(扁平列表) */
export async function fetchMyMobileMenus() {
const data = await get('/mini/menus', authHeader())
const list = data?.list || data || []
return list.map(normalizeMenu).filter((m) => m.path)
}
export function normalizeMenu(m = {}) {
const hasExplicitIcon = Object.prototype.hasOwnProperty.call(m, 'icon')
let icon = hasExplicitIcon ? String(m.icon ?? '') : 'approve'
const path = m.path || ''
const marketIcon = resolveMarketMenuIcon(path)
// 市场入口始终用专用 PNG(覆盖空串 / apps-o 等后端回落值)
if (marketIcon) {
icon = marketIcon
}
const colors = resolveMenuIconColors({
icon,
iconBg: m.iconBg || m.icon_bg,
iconFg: m.iconFg || m.icon_fg
})
return {
id: m.id,
title: m.title || m.menuName || m.menu_name,
groupTitle: resolveMenuGroupTitle(path, m.groupTitle || m.group_title),
icon,
path,
iconBg: colors.iconBg,
iconFg: colors.iconFg,
tone: m.tone || ''
}
}