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

3 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /**
  2. * 统一格式化站点编码,避免大小写、空白和通配符差异导致匹配失败
  3. * @param {*} siteCode
  4. * @returns {string}
  5. */
  6. function normalizeSiteCode (siteCode) {
  7. return String(siteCode || '')
  8. .replace(/%/g, '')
  9. .trim()
  10. .toUpperCase()
  11. }
  12. /**
  13. * 站点编码去重并清理空值
  14. * @param {Array} siteCodeList
  15. * @returns {Array<string>}
  16. */
  17. function normalizeSiteCodeList (siteCodeList) {
  18. if (!Array.isArray(siteCodeList)) {
  19. return []
  20. }
  21. return Array.from(
  22. new Set(
  23. siteCodeList
  24. .map(code => normalizeSiteCode(code))
  25. .filter(code => !!code)
  26. )
  27. )
  28. }
  29. /**
  30. * 获取当前用户授权子site编码列表
  31. * 优先读取Vuex,若为空再回退localStorage,避免刷新后丢失授权缓存
  32. * @param {Array} storeSubSiteCodeList
  33. * @returns {Array<string>}
  34. */
  35. export function getAuthorizedSubSiteCodeList (storeSubSiteCodeList) {
  36. const subSiteCodeListFromStore = normalizeSiteCodeList(storeSubSiteCodeList)
  37. if (subSiteCodeListFromStore.length > 0) {
  38. return subSiteCodeListFromStore
  39. }
  40. try {
  41. const localSubSiteCodeList = JSON.parse(localStorage.getItem('userSubSiteCodeList') || '[]')
  42. return normalizeSiteCodeList(localSubSiteCodeList)
  43. } catch (error) {
  44. console.error('解析userSubSiteCodeList失败:', error)
  45. return []
  46. }
  47. }
  48. /**
  49. * 按授权子site过滤列表数据,避免site前缀查询返回未授权子site数据
  50. * @param {Array} rows 列表原始数据
  51. * @param {Array} authorizedSubSiteCodeList 用户授权子site编码列表
  52. * @param {Array} siteFieldList 数据中可能存放站点编码的字段
  53. * @returns {Array}
  54. */
  55. export function filterRowsByAuthorizedSubSite (rows, authorizedSubSiteCodeList, siteFieldList = ['contract']) {
  56. const sourceRows = Array.isArray(rows) ? rows : []
  57. const normalizedAuthorizedList = normalizeSiteCodeList(authorizedSubSiteCodeList)
  58. // 未拿到授权子site时,返回空列表,避免越权数据被前端展示
  59. if (normalizedAuthorizedList.length === 0) {
  60. return []
  61. }
  62. const authorizedSiteSet = new Set(normalizedAuthorizedList)
  63. const validSiteFieldList = Array.isArray(siteFieldList) && siteFieldList.length > 0 ? siteFieldList : ['contract']
  64. return sourceRows.filter(row => {
  65. if (!row || typeof row !== 'object') {
  66. return false
  67. }
  68. return validSiteFieldList.some(field => {
  69. const rowSiteCode = normalizeSiteCode(row[field])
  70. return rowSiteCode && authorizedSiteSet.has(rowSiteCode)
  71. })
  72. })
  73. }