const { ROLE_LABELS } = require('../../utils/constants'); const { getSession, setSession, clearSession } = require('../../utils/auth'); const { fetchMe } = require('../../utils/request'); const { buildUnionModules, resolveDisplayMobileRoles, normalizeRoleList } = require('../../modules/registry'); const { buildMergedFeaturePatch } = require('../../utils/roleFeatures'); const { goLogin } = require('../../utils/nav'); const { clearUnitDictionaryCache } = require('../../utils/units'); function mapRoleOptions(roles) { return (roles || []).map((item) => { if (typeof item === 'string') { return { value: item, label: ROLE_LABELS[item] || item }; } return { value: item.role_type, label: item.label || ROLE_LABELS[item.role_type] || item.role_type, }; }); } function buildCapabilityHint(roleOptions) { if (!roleOptions || roleOptions.length <= 1) return ''; const labels = roleOptions.map((item) => item.label || ROLE_LABELS[item.value] || item.value); return `可用功能覆盖:${labels.join('、')}`; } function avatarFromName(name) { const text = String(name || '用').trim(); return text ? text.slice(0, 1) : '用'; } function buildSupplierCategoryDisplay(profile, roleType) { if (roleType !== 'supplier') { return { supplierCategoryLabels: [] }; } const raw = profile?.supplier?.categories; const list = Array.isArray(raw) ? raw : []; const supplierCategoryLabels = list .map((row) => { const name = String(row?.category_name || row?.category_code || '').trim(); if (!name) return null; return { name, primary: row?.is_primary === true || row?.is_primary === 1 || row?.is_primary === 't', }; }) .filter(Boolean); return { supplierCategoryLabels }; } function resolveHeroBranchText(profile, roleType) { if (roleType === 'supplier') { const companyName = String(profile?.supplier?.company_name || '').trim(); if (companyName) return companyName; } if (profile?.branch_name) return profile.branch_name; if (profile?.branch_code) return '已绑定分公司'; return '已登录'; } Page({ data: { needLogin: true, roleType: '', roleLabel: '', displayName: '', avatarChar: '用', branchText: '', supplierCategoryLabels: [], availableRoles: [], availableRoleValues: [], canSwitchRole: false, roleSwitchHint: '', menuModules: [], supplierStatusEnabled: false, photoInspectionEnabled: false, pickingEnabled: false, deliveryEnabled: true, bidEnabled: false, }, onShow() { const session = getSession(); if (!session?.access_token) { this.setData({ needLogin: true }); return; } this.refreshFromSession(session); this.refreshSupplierProfileIfNeeded(session); }, refreshFromSession(session) { const profile = session.profile || {}; const displayName = profile.full_name || profile.username || '用户'; const roleType = session.role_type; const display = resolveDisplayMobileRoles(session.available_roles, roleType, session); const roleValues = display.roles; const roles = mapRoleOptions(roleValues); const patch = { needLogin: false, roleType: display.roleType, roleLabel: ROLE_LABELS[display.roleType] || roleType, displayName, avatarChar: avatarFromName(displayName), branchText: resolveHeroBranchText(profile, display.roleType), ...buildSupplierCategoryDisplay(profile, display.roleType), availableRoles: roles, availableRoleValues: roleValues, canSwitchRole: false, roleSwitchHint: buildCapabilityHint(roles), permissions: session.permissions || [], isSuperAdmin: session.is_super_admin === true || session.isSuperAdmin === true, ...buildMergedFeaturePatch(roleValues, session, display.roleType), }; this.setData(patch, () => { this.setData({ menuModules: buildUnionModules(this.data, roleValues), }); }); }, async refreshSupplierProfileIfNeeded(session) { const display = resolveDisplayMobileRoles(session.available_roles, session.role_type, session); if (display.roleType !== 'supplier') return; try { const me = await fetchMe(); if (!me?.profile) return; const nextSession = { ...(getSession() || session), profile: me.profile, role_type: me.role_type || session.role_type, }; if (Array.isArray(me.available_roles)) { nextSession.available_roles = normalizeRoleList( me.available_roles, nextSession.role_type ); } setSession(nextSession); this.refreshFromSession(nextSession); } catch { // 保留本地 session 档案 } }, onLogout() { clearUnitDictionaryCache(); clearSession(); wx.showToast({ title: '已退出', icon: 'success' }); setTimeout(() => goLogin(), 300); }, goLogin() { wx.navigateTo({ url: '/pages/login/login' }); }, });