import { $t } from '@/locales'
|
|
const ROLE_STATUS_META = {
|
1: { type: 'success', key: 'common.status.normal', bool: true },
|
0: { type: 'danger', key: 'common.status.disabled', bool: false }
|
}
|
|
export function createRoleSearchState() {
|
return {
|
name: '',
|
code: '',
|
memo: '',
|
status: void 0,
|
condition: ''
|
}
|
}
|
|
function hasValue(value) {
|
return value !== '' && value !== void 0 && value !== null
|
}
|
|
function normalizeText(value) {
|
return String(value ?? '').trim()
|
}
|
|
export function createRoleFormState() {
|
return {
|
id: void 0,
|
name: '',
|
code: '',
|
memo: '',
|
status: 1
|
}
|
}
|
|
export function getRoleStatusOptions() {
|
return [
|
{ label: $t('common.status.normal'), value: 1 },
|
{ label: $t('common.status.disabled'), value: 0 }
|
]
|
}
|
|
export function buildRoleSearchParams(params = {}) {
|
const searchParams = {
|
name: normalizeText(params.name),
|
code: normalizeText(params.code),
|
memo: normalizeText(params.memo),
|
status: params.status,
|
condition: normalizeText(params.condition)
|
}
|
|
return Object.fromEntries(Object.entries(searchParams).filter(([, value]) => hasValue(value)))
|
}
|
|
export function buildRolePageQueryParams(params = {}) {
|
const { current, size, pageSize, ...filters } = params
|
return {
|
current: current || 1,
|
pageSize: pageSize || size || 20,
|
...buildRoleSearchParams(filters)
|
}
|
}
|
|
export function getRolePaginationKey() {
|
return {
|
current: 'current',
|
size: 'pageSize'
|
}
|
}
|
|
const ROLE_REPORT_COLUMNS = [
|
{ source: 'name', labelKey: 'pages.system.role.table.name' },
|
{ source: 'code', labelKey: 'pages.system.role.table.code' },
|
{ source: 'statusText', labelKey: 'pages.system.role.table.status' },
|
{ source: 'memo', labelKey: 'pages.system.role.table.memo' },
|
{ source: 'createTimeText', labelKey: 'pages.system.role.table.createTime' },
|
{ source: 'updateTimeText', labelKey: 'pages.system.role.table.updateTime' }
|
]
|
|
const ROLE_REPORT_SOURCE_ALIAS = {
|
status: 'statusText'
|
}
|
|
export function getRoleReportTitle() {
|
return $t('pages.system.role.reportTitle')
|
}
|
|
export const ROLE_REPORT_STYLE = {
|
titleAlign: 'center',
|
titleLevel: 'strong'
|
}
|
|
export function getRoleReportColumns() {
|
return ROLE_REPORT_COLUMNS.map((column) => ({
|
source: column.source,
|
label: $t(column.labelKey)
|
}))
|
}
|
|
export function resolveRoleReportColumns(columns = []) {
|
if (!Array.isArray(columns)) {
|
return []
|
}
|
|
const allowedColumns = new Map(ROLE_REPORT_COLUMNS.map((column) => [column.source, column]))
|
const seenSources = new Set()
|
|
return columns
|
.map((column) => {
|
if (!column || typeof column !== 'object') {
|
return null
|
}
|
|
const source =
|
ROLE_REPORT_SOURCE_ALIAS[column.source ?? column.prop] ?? column.source ?? column.prop
|
if (!source || !allowedColumns.has(source) || seenSources.has(source)) {
|
return null
|
}
|
|
seenSources.add(source)
|
const allowedColumn = allowedColumns.get(source)
|
return {
|
source,
|
label: column.label || $t(allowedColumn.labelKey)
|
}
|
})
|
.filter(Boolean)
|
}
|
|
export function buildRolePrintRows(records = []) {
|
if (!Array.isArray(records)) {
|
return []
|
}
|
|
return records.map((record) => normalizeRoleListRow(record))
|
}
|
|
export function buildRoleReportMeta({
|
previewMeta = {},
|
count = 0,
|
titleAlign = ROLE_REPORT_STYLE.titleAlign,
|
titleLevel = ROLE_REPORT_STYLE.titleLevel
|
} = {}) {
|
return {
|
reportTitle: getRoleReportTitle(),
|
reportDate: previewMeta.reportDate,
|
printedAt: previewMeta.printedAt,
|
operator: previewMeta.operator,
|
count,
|
reportStyle: {
|
titleAlign,
|
titleLevel,
|
orientation: 'portrait',
|
density: 'compact',
|
showSequence: true
|
}
|
}
|
}
|
|
export function buildRoleDialogModel(record = {}) {
|
return {
|
...createRoleFormState(),
|
...buildRoleFormData(record)
|
}
|
}
|
|
export function buildRoleSavePayload(form = {}) {
|
return buildRoleFormData(form)
|
}
|
|
export function normalizeRoleListRow(record = {}) {
|
const statusMeta = getRoleStatusMeta(record.statusBool ?? record.status)
|
return {
|
...record,
|
statusBool: record.statusBool !== void 0 ? Boolean(record.statusBool) : statusMeta.bool,
|
statusText: $t(statusMeta.key),
|
statusType: statusMeta.type,
|
createTimeText: normalizeText(record.createTime$ || record.createTime),
|
updateTimeText: normalizeText(record.updateTime$ || record.updateTime)
|
}
|
}
|
|
export function getRoleStatusMeta(status) {
|
if (status === true || status === 1 || status === '1') {
|
return ROLE_STATUS_META[1]
|
}
|
if (status === false || status === 0 || status === '0') {
|
return ROLE_STATUS_META[0]
|
}
|
return { type: 'info', key: 'common.status.unknown', bool: false }
|
}
|
|
export function getRoleScopeConfig(scopeType) {
|
const configMap = {
|
menu: {
|
scopeType: 'menu',
|
title: $t('pages.system.role.scopes.menu'),
|
listUrl: '/role/scope/list',
|
treeUrl: '/menu/tree'
|
},
|
pda: {
|
scopeType: 'pda',
|
title: $t('pages.system.role.scopes.pda'),
|
listUrl: '/rolePda/scope/list',
|
treeUrl: '/menuPda/tree'
|
},
|
matnr: {
|
scopeType: 'matnr',
|
title: $t('pages.system.role.scopes.matnr'),
|
listUrl: '/roleMatnr/scope/list',
|
treeUrl: '/menuMatnrGroup/tree'
|
},
|
warehouse: {
|
scopeType: 'warehouse',
|
title: $t('pages.system.role.scopes.warehouse'),
|
listUrl: '/roleWarehouse/scope/list',
|
treeUrl: '/menuWarehouse/tree'
|
}
|
}
|
|
const config = configMap[scopeType]
|
if (!config) {
|
throw new Error(`Unsupported scope type: ${scopeType}`)
|
}
|
return config
|
}
|
|
export function buildRoleScopeSubmitPayload(roleId, checkedKeys = [], halfCheckedKeys = []) {
|
return {
|
id: normalizeRoleId(roleId),
|
menuIds: {
|
checked: normalizeScopeKeys(checkedKeys),
|
halfChecked: normalizeScopeKeys(halfCheckedKeys)
|
}
|
}
|
}
|
|
export function normalizeRoleScopeTreeData(scopeType, treeData = []) {
|
if (!Array.isArray(treeData)) {
|
return []
|
}
|
|
return treeData.map((node) => normalizeRoleScopeNode(scopeType, node)).filter(Boolean)
|
}
|
|
function normalizeRoleScopeNode(scopeType, node) {
|
if (!node || typeof node !== 'object') {
|
return null
|
}
|
|
if (scopeType === 'menu' && node.type === 1) {
|
return buildScopeAuthNode(node)
|
}
|
|
const children = Array.isArray(node.children)
|
? normalizeRoleScopeTreeData(scopeType, node.children)
|
: []
|
const metaSource = node.meta && typeof node.meta === 'object' ? node.meta : node
|
const authNodes =
|
scopeType === 'menu' && Array.isArray(metaSource.authList) && metaSource.authList.length
|
? metaSource.authList.map((auth, index) => ({
|
id: normalizeScopeKey(auth.id ?? auth.authMark ?? `${node.id || 'auth'}-${index}`),
|
label: normalizeScopeTitle(auth.title || auth.name || auth.authMark || ''),
|
type: 1,
|
isAuthButton: true,
|
authMark: auth.authMark || auth.authority || auth.code || '',
|
children: []
|
}))
|
: []
|
|
const mergedChildren =
|
authNodes.length > 0 && !children.some((child) => child.isAuthButton)
|
? [...children, ...authNodes]
|
: children
|
|
return {
|
id: normalizeScopeKey(node.id ?? node.value),
|
label: normalizeScopeTitle(
|
node.label || node.title || node.name || metaSource.title || node.code || ''
|
),
|
type: node.type,
|
path: node.path || '',
|
component: node.component || '',
|
isAuthButton: Boolean(node.isAuthButton),
|
authMark: node.authMark || metaSource.authMark || metaSource.authority || metaSource.code || '',
|
meta: metaSource,
|
children: mergedChildren
|
}
|
}
|
|
function buildScopeAuthNode(node) {
|
const metaSource = node.meta && typeof node.meta === 'object' ? node.meta : node
|
return {
|
id: normalizeScopeKey(node.id ?? node.value),
|
label: normalizeScopeTitle(node.label || node.title || node.name || metaSource.title || ''),
|
type: 1,
|
isAuthButton: true,
|
authMark: node.authMark || metaSource.authMark || metaSource.authority || metaSource.code || '',
|
children: []
|
}
|
}
|
|
export function normalizeScopeKeys(keys = []) {
|
if (!Array.isArray(keys)) {
|
return []
|
}
|
|
return Array.from(
|
new Set(keys.map((key) => normalizeRoleId(key)).filter((key) => key !== void 0))
|
)
|
}
|
|
export function normalizeScopeKey(value) {
|
const normalized = normalizeRoleId(value)
|
return normalized === void 0 ? '' : String(normalized)
|
}
|
|
function normalizeScopeTitle(title) {
|
if (typeof title !== 'string') {
|
return ''
|
}
|
const trimmedTitle = title.trim()
|
if (trimmedTitle.startsWith('menu.')) {
|
return `menus.${trimmedTitle.slice('menu.'.length)}`
|
}
|
return trimmedTitle
|
}
|
|
function normalizeRoleId(value) {
|
if (!hasValue(value)) {
|
return void 0
|
}
|
const numeric = Number(value)
|
if (Number.isNaN(numeric)) {
|
return value
|
}
|
return numeric
|
}
|
|
function buildRoleFormData(source = {}) {
|
return {
|
id: normalizeRoleId(source.id),
|
name: normalizeText(source.name),
|
code: normalizeText(source.code),
|
memo: normalizeText(source.memo),
|
status: hasValue(source.status) ? source.status : 1
|
}
|
}
|