auth.js
5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { utils } from 'glsk-common'
import { constants } from '@/common'
import { This } from '@/main'
import api from '@/api'
const auth = {
namespaced: true,
state: () => ({
session: null,
tracker: null
}),
mutations: {
SET_SESSION(state, session) {
state.session = session
},
async LOGOUT(state) {
await api.app.logout({ sessionId: state.session.token })
},
CLEAR(state) {
state.session = null
}
},
actions: {
setSession({ commit, dispatch }, session) {
commit('SET_SESSION', session)
dispatch('saveSession', session)
},
/* eslint-disable */
saveSession({ commit }, session) {
utils.storage.setSession(constants.SCRM_BASE_SESSION, session)
},
async logout({ commit }) {
await commit('LOGOUT')
},
async permission({}, merchant) {
const { response, productCode } = This('$config')
/* eslint-disable */
const { sessionId, permissionMerchantIds, uuid, loginList, ...merchantItems } = merchant
let { code, message, data, menus, sessionData } = await api.app.permission({ productCode, sessionId })
if (code == response.code_success_value) {
menus = data[0]?.products[0]?.merchantProTreeResponses
// 特殊账号菜单过滤
menus = handleSpecialMenuFilter(menus, merchant)
if (menus?.length) {
data[0].active = true
sessionData = { ...merchantItems, loginList: data, token: sessionId }
// dispatch('setSession', { ...merchantItems, loginList: data, token: sessionId })
} else {
;({ CODE: code, MESSAGE: message } = constants.SCRM_BASE_ERRORS.NO_PERMISSION_MENUS)
}
}
return { code, message, menus, sessionData }
},
clear({ commit }) {
commit('CLEAR')
}
}
}
/**
* 处理特殊账号的菜单过滤
* Handle menu filtering for special accounts
* @param {Array} menus Original menu list
* @param {Object} merchant Merchant/User info
* @returns {Array} Filtered menu list
*/
function handleSpecialMenuFilter(menus, merchant) {
// 从全局配置中获取特殊账号过滤规则
// Get special account filtering rules from global configuration
const globalConfig = This('$config')
/**
* scMenuFilter: {
accountName: 'zhkchy',
merchantName: '中海康城花园',
pathname: '/',
keepLabel: '客户'
}
}
*/
const config = globalConfig?.scMenuFilter
// 如果没有配置或配置为空,则不进行过滤
if (!config) {
return menus
}
console.log('config', config, merchant, location)
const { accountName, merchantName } = merchant || {}
// 检查是否满足特殊账号条件
const isSpecialAccount = accountName === config.accountName && merchantName === config.merchantName && location.pathname === config.pathname
console.log(isSpecialAccount, Array.isArray(menus))
// console.log((isSpecialAccount = accountName === config.accountName && merchantName === config.merchantName && location.pathname === config.pathname))
if (isSpecialAccount && Array.isArray(menus)) {
// 1. 过滤逻辑:只留下 label == "客户" 的菜单(兼容 null/undefined)
const normalizedMenus = menus.filter(Boolean)
const filteredMenus = normalizedMenus.filter(item => item?.label === config.keepLabel)
// 如果过滤后为空,回退为原菜单,避免后续流程异常
if (!filteredMenus.length) return menus
// 2. 新增菜单 (TODO: 请根据实际菜单数据结构配置以下内容)
const findByLabel = (list, label) => {
for (let i = 0; i < list.length; i++) {
const item = list[i]
if (item?.label === label) return item
if (Array.isArray(item?.children) && item.children.length) {
const found = findByLabel(item.children, label)
if (found) return found
}
}
return null
}
const target = findByLabel(filteredMenus, '客户')
if (target) {
target.children = Array.isArray(target.children) ? target.children : []
// 过滤掉 '标签管理' 和 '客户群管理'
const hiddenLabels = ['标签管理', '客户群管理']
target.children = target.children.filter(child => !hiddenLabels.includes(child?.label))
// const exists = target.children.some(child => child?.label === '行为分析')
// if (!exists) {
const scMenu = {
id: 2981,
label: '行为分析',
value: null,
logo: null,
disabled: null,
parentId: 275,
superParentId: null,
menuUrl: '^',
functionUrl: null,
rootIds: '275-298',
children: [
{
id: 298101,
label: '用户行为路径',
value: null,
logo: null,
disabled: null,
parentId: 2735,
superParentId: null,
menuUrl: '/behaviorManage/pathList',
functionUrl: null,
rootIds: '2735-2938',
children: []
},
{
id: 298102,
label: 'LTV分析',
value: null,
logo: null,
disabled: null,
parentId: 2735,
superParentId: null,
menuUrl: '/behaviorManage/LTVList',
functionUrl: null,
rootIds: '2735-2938',
children: []
},
{
id: 298103,
label: '属性分析',
value: null,
logo: null,
disabled: null,
parentId: 2735,
superParentId: null,
menuUrl: '/behaviorManage/attributeList',
functionUrl: null,
rootIds: '2735-2938',
children: []
}
]
}
target.children.push(scMenu)
// }
}
return filteredMenus
}
return menus
}
export default auth