auth.js 5.22 KB
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
  }
  const { accountName, merchantName } = merchant || {}
  // 检查是否满足特殊账号条件
  const isSpecialAccount = accountName === config.accountName && merchantName === config.merchantName && location.pathname === config.pathname
  if (isSpecialAccount && Array.isArray(menus)) {
    // 1. 过滤逻辑:只留下 label == "客户" 的菜单
    const filteredMenus = menus.filter(item => item.label === config.keepLabel)
    // 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 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