import request from '@/utils/http' export function buildUserListParams(params = {}) { return { current: params.current || 1, pageSize: params.pageSize || params.size || 20, ...(params.username !== undefined ? { username: params.username } : {}), ...(params.nickname !== undefined ? { nickname: params.nickname } : {}), ...(params.phone !== undefined ? { phone: params.phone } : {}), ...(params.email !== undefined ? { email: params.email } : {}), ...(params.status !== undefined ? { status: params.status } : {}), ...(params.deptId !== undefined ? { deptId: params.deptId } : {}) } } export function buildRoleListParams(params = {}) { return { current: params.current || 1, pageSize: params.pageSize || params.size || 20, ...(params.name !== undefined ? { name: params.name } : {}), ...(params.code !== undefined ? { code: params.code } : {}), ...(params.memo !== undefined ? { memo: params.memo } : {}), ...(params.status !== undefined ? { status: params.status } : {}) } } function fetchGetUserList(params) { return request.post({ url: '/user/page', params: buildUserListParams(params) }) } function fetchSaveUser(params) { return request.post({ url: '/user/save', params }) } function fetchUpdateUser(params) { return request.post({ url: '/user/update', params }) } function fetchDeleteUser(id) { return request.post({ url: `/user/remove/${id}` }) } function fetchResetUserPassword(params) { const normalizedParams = normalizeAdminPasswordUpdateParams(params) return request.post({ url: '/user/update', params: normalizedParams }) } function fetchUpdateUserStatus(params) { return request.post({ url: '/user/update', params }) } function fetchGetUserDetail(id) { return request.get({ url: `/user/${id}` }) } function fetchGetRoleList(params) { return request.post({ url: '/role/page', params: buildRoleListParams(params) }) } function fetchSaveRole(params) { return request.post({ url: '/role/save', params }) } function fetchUpdateRole(params) { return request.post({ url: '/role/update', params }) } function fetchDeleteRole(id) { return request.post({ url: `/role/remove/${id}` }) } function fetchGetRoleOptions(params) { return request.post({ url: '/role/list', params }) } function buildRolePageParams(params = {}) { return { current: params.current || 1, pageSize: params.pageSize || params.size || 20, ...(params.name !== undefined ? { name: params.name } : {}), ...(params.code !== undefined ? { code: params.code } : {}), ...(params.memo !== undefined ? { memo: params.memo } : {}), ...(params.status !== undefined ? { status: params.status } : {}), ...(params.condition !== undefined ? { condition: params.condition } : {}) } } function fetchRolePage(params = {}) { return request.post({ url: '/role/page', params: buildRolePageParams(params) }) } const fetchRolePrintPage = fetchRolePage function normalizeRoleManyIds(ids) { if (Array.isArray(ids)) { return ids .map((id) => normalizeLegacyId(id)) .filter((id) => id !== '') .join(',') } return normalizeLegacyId(ids) } function fetchGetRoleMany(ids) { const normalizedIds = normalizeRoleManyIds(ids) return request.post({ url: `/role/many/${normalizedIds}` }) } async function fetchExportRoleReport(payload = {}, options = {}) { return fetch(`${import.meta.env.VITE_API_URL}/role/export`, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(options.headers || {}) }, body: JSON.stringify(payload) }) } function fetchGetDeptTree(params) { return request.post({ url: '/dept/tree', params }) } function fetchGetMenuTree(params = {}) { return request.post({ url: '/menu/tree', params }) } function fetchSaveMenu(params) { return request.post({ url: '/menu/save', params }) } function fetchUpdateMenu(params) { return request.post({ url: '/menu/update', params }) } function fetchDeleteMenu(id) { return request.post({ url: `/menu/remove/${id}` }) } function assertAdminPasswordUpdatePayload(params) { if (!isPlainObject(params)) { throw new Error('fetchResetUserPassword requires an object payload') } if (params.id === undefined || params.id === null || params.id === '') { throw new Error('fetchResetUserPassword requires a user id') } } function normalizeAdminPasswordUpdateParams(params) { assertAdminPasswordUpdatePayload(params) if (params.password !== undefined) { return params } if (params.newPassword !== undefined) { return { ...params, password: params.newPassword } } throw new Error('fetchResetUserPassword requires a password payload') } function getScopeListUrl(scopeType) { const urlMap = { menu: '/role/scope/list', pda: '/rolePda/scope/list', matnr: '/roleMatnr/scope/list', warehouse: '/roleWarehouse/scope/list' } const url = urlMap[scopeType] if (!url) { throw new Error(`Unsupported scope type: ${scopeType}`) } return url } function getScopeUpdateUrl(scopeType) { const urlMap = { menu: '/role/scope/update', pda: '/rolePda/scope/update', matnr: '/roleMatnr/scope/update', warehouse: '/roleWarehouse/scope/update' } const url = urlMap[scopeType] if (!url) { throw new Error(`Unsupported scope type: ${scopeType}`) } return url } function getScopeTreeUrl(scopeType) { const urlMap = { menu: '/menu/tree', pda: '/menuPda/tree', matnr: '/menuMatnrGroup/tree', warehouse: '/menuWarehouse/tree' } const url = urlMap[scopeType] if (!url) { throw new Error(`Unsupported scope type: ${scopeType}`) } return url } function fetchGetRoleScopeList(scopeType, roleId) { return request.get({ url: getScopeListUrl(scopeType), params: { roleId } }) } function fetchUpdateRoleScope(scopeType, params) { return request.post({ url: getScopeUpdateUrl(scopeType), params }) } function fetchGetRoleScopeTree(scopeType, params = {}) { return request.post({ url: getScopeTreeUrl(scopeType), params }) } function fetchGetUserLoginList(params) { return request.post({ url: '/userLogin/page', params }) } function fetchGetMenuList(params = {}) { return request.post({ url: '/menu/list', params }).then((menuList) => adaptLegacyMenuTree(buildLegacyMenuTree(menuList))) } function adaptLegacyMenuTree(menuTree) { if (!Array.isArray(menuTree)) { return [] } return menuTree .map((node) => adaptLegacyMenuNode(node)) .filter(Boolean) } function buildLegacyMenuTree(menuList) { if (!Array.isArray(menuList)) { return [] } const nodeMap = new Map() const roots = [] menuList.forEach((node) => { if (!node || typeof node !== 'object') { return } nodeMap.set(normalizeLegacyId(node.id), { ...node, children: [] }) }) nodeMap.forEach((node) => { const parentId = normalizeLegacyId(node.parentId ?? 0) if (parentId && parentId !== '0' && nodeMap.has(parentId)) { nodeMap.get(parentId).children.push(node) return } roots.push(node) }) return sortLegacyMenuTree(roots) } function sortLegacyMenuTree(nodes) { return nodes .sort((left, right) => { const leftSort = Number(left?.sort ?? 0) const rightSort = Number(right?.sort ?? 0) if (leftSort !== rightSort) { return leftSort - rightSort } return normalizeLegacyId(left?.id).localeCompare(normalizeLegacyId(right?.id)) }) .map((node) => ({ ...node, children: Array.isArray(node.children) ? sortLegacyMenuTree(node.children) : [] })) } function adaptLegacyMenuNode(node) { if (!node || typeof node !== 'object' || node.type === 1) { return null } const menuChildren = [] const authList = [] const rawChildren = Array.isArray(node.children) ? node.children : [] rawChildren.forEach((child) => { if (!child || typeof child !== 'object') { return } if (child.type === 1) { authList.push(buildLegacyAuthItem(child)) return } const adaptedChild = adaptLegacyMenuNode(child) if (adaptedChild) { menuChildren.push(adaptedChild) } }) const adapted = { id: normalizeLegacyId(node.id), parentId: normalizeLegacyId(node.parentId ?? 0), parentName: node.parentName || '', name: buildLegacyRouteName(node), route: typeof node.route === 'string' ? node.route : '', path: buildLegacyMenuPath(node), component: buildLegacyMenuComponent(node), authority: node.authority || '', icon: node.icon || '', sort: node.sort ?? 0, status: node.status ?? 1, memo: node.memo || '', type: node.type ?? 0, meta: buildLegacyMenuMeta(node), children: menuChildren } if (authList.length > 0) { adapted.meta.authList = authList } return adapted } function buildLegacyMenuMeta(node) { const metaSource = node.meta && typeof node.meta === 'object' ? node.meta : node const meta = { title: normalizeLegacyTitle(node.name || metaSource.title || '') } const supportedKeys = [ 'icon', 'sort', 'keepAlive', 'isHide', 'isHideTab', 'fixedTab', 'link', 'isIframe', 'roles', 'showBadge', 'showTextBadge', 'activePath', 'isFullPage' ] supportedKeys.forEach((key) => { if (metaSource[key] !== undefined) { meta[key] = metaSource[key] } }) meta.isEnable = normalizeLegacyEnableState(metaSource) return meta } function buildLegacyAuthItem(node) { const metaSource = node.meta && typeof node.meta === 'object' ? node.meta : node return { id: normalizeLegacyId(node.id), parentId: normalizeLegacyId(node.parentId ?? 0), parentName: node.parentName || '', name: node.name || metaSource.title || '', title: normalizeLegacyTitle(node.name || metaSource.title || ''), route: typeof node.route === 'string' ? node.route : '', component: buildLegacyMenuComponent(node), authMark: metaSource.authMark || metaSource.authority || metaSource.code || '', authority: metaSource.authority || metaSource.authMark || metaSource.code || '', icon: metaSource.icon || '', sort: metaSource.sort ?? 0, status: metaSource.status ?? 1, memo: metaSource.memo || '', type: node.type ?? 1 } } function buildLegacyMenuPath(node) { const rawPath = typeof node.route === 'string' && node.route.trim() ? node.route : typeof node.path === 'string' ? node.path : '' return normalizeLegacyPath(rawPath) } function buildLegacyMenuComponent(node) { if (typeof node.component === 'string') { return node.component } return '' } function buildLegacyRouteName(node) { if (typeof node.name === 'string' && node.name.trim()) { return node.name.trim() } if (typeof node.component === 'string' && node.component.trim()) { return node.component.trim() } const path = buildLegacyMenuPath(node) if (path) { return path .split('/') .filter(Boolean) .map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1)) .join('') } return `Menu${normalizeLegacyId(node.id)}` } function normalizeLegacyTitle(title) { if (typeof title !== 'string') { return '' } const trimmedTitle = title.trim() if (trimmedTitle.startsWith('menu.')) { return `menus.${trimmedTitle.slice('menu.'.length)}` } return trimmedTitle } function normalizeLegacyPath(path) { if (typeof path !== 'string') { return '' } return path.replace(/^\/+/, '').replace(/\/+$/, '') } function normalizeLegacyId(id) { if (id === null || id === undefined || id === '') { return '' } return String(id) } function normalizeLegacyEnableState(metaSource) { if (metaSource.isEnable !== undefined) { return Boolean(metaSource.isEnable) } if (metaSource.enabled !== undefined) { return Boolean(metaSource.enabled) } if (metaSource.statusBool !== undefined) { return Boolean(metaSource.statusBool) } if (metaSource.status !== undefined) { return Number(metaSource.status) === 1 } return true } function isPlainObject(value) { return Boolean(value) && typeof value === 'object' && !Array.isArray(value) } export { fetchGetUserList, fetchSaveUser, fetchUpdateUser, fetchDeleteUser, fetchResetUserPassword, fetchUpdateUserStatus, fetchGetUserDetail, fetchGetRoleList, fetchSaveRole, fetchUpdateRole, fetchDeleteRole, fetchGetRoleOptions, fetchRolePrintPage, fetchRolePage, fetchExportRoleReport, fetchGetRoleMany, fetchGetDeptTree, fetchGetMenuTree, fetchSaveMenu, fetchUpdateMenu, fetchDeleteMenu, fetchGetRoleScopeList, fetchGetRoleScopeTree, fetchUpdateRoleScope, fetchGetUserLoginList, fetchGetMenuList }