• wangxl's avatar
    11 · 4b9f69fd
    wangxl authored
    4b9f69fd
app.js 7.64 KB
// @ts-nocheck
import router from '@/router'

const title = process.env.VUE_APP_SYSTEM_TITLE

const state = {
    openKeys: [],
    activeKeys: [],
    tabsArray: [],
    tabsActive: '',
    cachedViews: [],
    projType: null,
}
const mutations = {
    setTabsActive: (state, payload) => {
        state.tabsActive = payload
    },
    setTabsArray: (state, payload) => {
        state.tabsArray = payload
    },
    setProjType: (state, payload) => {
        state.projType = payload
    },
    addTabs: (state, payload) => {
        if (state.tabsArray.some(v => v.key === payload.key)) {
            return
        }
        state.tabsArray.push(
            Object.assign({}, payload, { title: payload.title || 'no-name' })
        )
    },
    closeTabs: (state, payload) => {
        state.tabsArray.splice(payload, 1)
    },
    setOpenKeys (state, payload) {
        if (state.openKeys.some(v => v === payload)) {
            return
        }
        state.openKeys = payload
    },
    setActiveKeys (state, payload) {
        state.activeKeys = payload
    },
    setCachedViews: (state, payload) => {
        state.cachedViews = payload
    },
    addCachedViews: (state, payload) => {
        if (payload.keepAlive && payload.keepAlive == 1) {
            if (state.cachedViews.some(v => v === payload.code)) {
                return
            }
            state.cachedViews.push(payload.code)
        }
    },
    delCachedViews: (state, payload) => {
        for (let i = 0; i < state.cachedViews.length; i++) {
            if (state.cachedViews[i] == payload.code) {
                state.cachedViews.splice(i, 1)
            }
        }
    },
    //点击页签
    clickCard (state, payload) {
        this.commit('app/setTabsActive', payload)
        if (payload.length > 2) {
            this.commit('app/setActiveKeys', [parseInt(payload)])
            // this.commit('app/setOpenKeys', [parseInt(payload.substring(0, 2))])
        }
        let { obj, index } = filterTabsBykey(state.tabsArray, payload)
        router.push(obj.router)
    },
    // 打开一个新页签
    addCard (state, payload) {
        this.commit('app/setTabsActive', payload.key)
        this.commit('app/addTabs', payload)
        this.commit('app/addCachedViews', payload)
        router.push(payload.router)
    },
    // 关闭某一个打开页签
    closeCard (state, payload) {
        let list = state.tabsArray
        let { obj, index } = filterTabsBykey(list, payload)
        if (payload == state.tabsActive) {
            this.commit('app/setTabsActive', list[index - 1].key)
            if (list[index - 1].key.length > 2) {
                this.commit('app/setActiveKeys', [parseInt(list[index - 1].key)])
                // this.commit('app/setOpenKeys', [parseInt((list[index - 1].key).substring(0, 2))])
            }
            else
                this.commit('app/setActiveKeys', [0])
            router.push(list[index - 1].router)
        }
        this.commit('app/closeTabs', index)
        this.commit('app/delCachedViews', obj)
    },
    /**
     * 页签卡片操作
     * @param type 0 点击切换  1 关闭当前页签  2关闭其他标签 3关闭所有标签
     * @param key
     */
    optCard (state, payload) {
        let list = state.tabsArray
        let { obj, index } = filterTabsBykey(list, payload.key)
        if (payload.key != '0') {
            if (payload.type == 1) {
                this.commit('app/setTabsActive', list[index - 1].key)
                if (list[index - 1].key.length > 2) {
                    this.commit('app/setActiveKeys', [parseInt(list[index - 1].key)])
                    // this.commit('app/setOpenKeys', [parseInt((list[index - 1].key).substring(0, 2))])
                }
                this.commit('app/closeTabs', index)
                this.commit('app/delCachedViews', obj)
                router.push(list[index - 1].router)
            }
            else if (payload.type == 2) {
                this.commit('app/setTabsArray', [list[0]])
                this.commit('app/setCachedViews', ['home', obj.code])
            }
            else {
                this.commit('app/setTabsArray', [list[0]])
                this.commit('app/setTabsActive', '0')
                this.commit('app/setActiveKeys', [])
                this.commit('app/setCachedViews', ['home'])
                router.push('/')
            }
        } else {
            this.commit('app/setTabsArray', [list[0]])
            this.commit('app/setTabsActive', '0')
            this.commit('app/setActiveKeys', [])
            this.commit('app/setCachedViews', ['home'])
        }
    },
    initStore (state, payload) {
        this.commit('app/setOpenKeys', payload)
        this.commit('app/setActiveKeys', [])
        this.commit('app/setTabsArray', [{ title: '首页', key: '0', code: 'home', keepAlive: 1, router: '/home', closable: false }])
        this.commit('app/setTabsActive', '0')
    },
    clearStore (state) {
        this.commit('app/setOpenKeys', [])
        this.commit('app/setActiveKeys', [])
        this.commit('app/setTabsArray', [])
        this.commit('app/setTabsActive', '')
        this.commit('app/setCachedViews', [])
        this.commit('app/setProjType', null)
    },
}

const actions = {
    auth ({ commit, state }, path,) {
        return new Promise((resolve, reject) => {
            try {
                let tabs = null
                tabs = filterTabsByPath(state.tabsArray, path)
                if (!tabs) {
                    tabs = filterTabsByPath(defaultTabsList, path)
                    if (!tabs) {
                        tabs = filterAsyncMenuList(JSON.parse(window.sessionStorage.getItem('menuList')), path)
                    }
                    if (tabs) {
                        commit('addTabs', tabs)
                    }
                }
                if (tabs) {
                    resolve(true)
                    commit('setTabsActive', tabs.key)
                } else {
                    resolve(false)
                }
            } catch (error) {
                reject(error)
            }
        })
    },
}

const defaultTabsList = [
    { title: '个人设置', key: '1005', code: 'personInfo', keepAlive: 1, router: '/person/info', closable: true },
    { title: '项目创建', key: '1299', code: 'projectCreate', keepAlive: 1, router: '/project/create', closable: true },
    { title: '首页', key: '0', code: 'home', keepAlive: 1, router: '/home', closable: false },
    { title: '404', key: '404', code: '404', keepAlive: 1, router: '/404', closable: true },
    { title: '403', key: '403', code: '403', keepAlive: 1, router: '/403', closable: true }
]

const filterTabsBykey = (list, key) => {
    let res = { obj: null, index: 0 }
    for (var i = 0; i < list.length; i++) {
        if (list[i].key == key) {
            res = { obj: list[i], index: i }
            break
        }
    }
    return res
}

const filterTabsByPath = (list, path) => {
    let res = null
    for (var i = 0; i < list.length; i++) {
        if (list[i].router == path) {
            res = list[i]
            break
        }
    }
    return res
}

const filterAsyncMenuList = (list, path) => {
    let res = null
    for (var i = 0; i < list.length; i++) {
        if (list[i].frontActionUrl == path) {
            res = { title: list[i].name, key: list[i].id + '', code: list[i].code, keepAlive: list[i].keepAlive, router: list[i].frontActionUrl, closable: true }
            break
        }
        if (list[i].children && list[i].children.length > 0) {
            res = filterAsyncMenuList(list[i].children, path)
            if (res) break
        }
        if (res) break
    }
    return res
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}