You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.5 KiB
80 lines
2.5 KiB
/**
|
|
* 统一格式化站点编码,避免大小写、空白和通配符差异导致匹配失败
|
|
* @param {*} siteCode
|
|
* @returns {string}
|
|
*/
|
|
function normalizeSiteCode (siteCode) {
|
|
return String(siteCode || '')
|
|
.replace(/%/g, '')
|
|
.trim()
|
|
.toUpperCase()
|
|
}
|
|
|
|
/**
|
|
* 站点编码去重并清理空值
|
|
* @param {Array} siteCodeList
|
|
* @returns {Array<string>}
|
|
*/
|
|
function normalizeSiteCodeList (siteCodeList) {
|
|
if (!Array.isArray(siteCodeList)) {
|
|
return []
|
|
}
|
|
return Array.from(
|
|
new Set(
|
|
siteCodeList
|
|
.map(code => normalizeSiteCode(code))
|
|
.filter(code => !!code)
|
|
)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 获取当前用户授权子site编码列表
|
|
* 优先读取Vuex,若为空再回退localStorage,避免刷新后丢失授权缓存
|
|
* @param {Array} storeSubSiteCodeList
|
|
* @returns {Array<string>}
|
|
*/
|
|
export function getAuthorizedSubSiteCodeList (storeSubSiteCodeList) {
|
|
const subSiteCodeListFromStore = normalizeSiteCodeList(storeSubSiteCodeList)
|
|
if (subSiteCodeListFromStore.length > 0) {
|
|
return subSiteCodeListFromStore
|
|
}
|
|
|
|
try {
|
|
const localSubSiteCodeList = JSON.parse(localStorage.getItem('userSubSiteCodeList') || '[]')
|
|
return normalizeSiteCodeList(localSubSiteCodeList)
|
|
} catch (error) {
|
|
console.error('解析userSubSiteCodeList失败:', error)
|
|
return []
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 按授权子site过滤列表数据,避免site前缀查询返回未授权子site数据
|
|
* @param {Array} rows 列表原始数据
|
|
* @param {Array} authorizedSubSiteCodeList 用户授权子site编码列表
|
|
* @param {Array} siteFieldList 数据中可能存放站点编码的字段
|
|
* @returns {Array}
|
|
*/
|
|
export function filterRowsByAuthorizedSubSite (rows, authorizedSubSiteCodeList, siteFieldList = ['contract']) {
|
|
const sourceRows = Array.isArray(rows) ? rows : []
|
|
const normalizedAuthorizedList = normalizeSiteCodeList(authorizedSubSiteCodeList)
|
|
|
|
// 未拿到授权子site时,返回空列表,避免越权数据被前端展示
|
|
if (normalizedAuthorizedList.length === 0) {
|
|
return []
|
|
}
|
|
|
|
const authorizedSiteSet = new Set(normalizedAuthorizedList)
|
|
const validSiteFieldList = Array.isArray(siteFieldList) && siteFieldList.length > 0 ? siteFieldList : ['contract']
|
|
|
|
return sourceRows.filter(row => {
|
|
if (!row || typeof row !== 'object') {
|
|
return false
|
|
}
|
|
return validSiteFieldList.some(field => {
|
|
const rowSiteCode = normalizeSiteCode(row[field])
|
|
return rowSiteCode && authorizedSiteSet.has(rowSiteCode)
|
|
})
|
|
})
|
|
}
|