guoshilong
2023-10-27 31e6b27fc0b7fb2ae715efa4287d8f5539f33d78
角色切换,保存菜单至store
3 files modified
93 ■■■■■ changed files
store/index.js 48 ●●●●● patch | view | raw | blame | history
subPackage/user/role/index.vue 18 ●●●●● patch | view | raw | blame | history
utils/func.js 27 ●●●●● patch | view | raw | blame | history
store/index.js
@@ -48,10 +48,12 @@
        // 如果version无需保存到本地永久存储,无需lifeData.version方式
        version: '1.0.0',
        activeRoleId: "",
        tabBarList: [],
        // tabBarList: [],
        adCodeBase: {},
        tabbarIndex: uni.getStorageSync("tabbarIndex") || 0,
        // tabbarIndex: uni.getStorageSync("tabbarIndex") || 0,
        hasUpdate: false, //true为不可登录,需要更新,false则是最新版
        menu: [],
    },
    mutations: {
@@ -75,31 +77,37 @@
            // 保存变量到本地,见顶部函数定义
            saveLifeData(saveKey, state[saveKey])
        },
        // 设置菜单
        setTabBarList(state, data) {
            let i = 0
            if (state.tabBarList.length > 0) {
                i = state.tabBarList.length
            }
            data.forEach(e => {
                e.i = i
                i++
            })
        // // 设置菜单
        // setTabBarList(state, data) {
        //     let i = 0
        //     if (state.tabBarList.length > 0) {
        //         i = state.tabBarList.length
        //     }
        //     data.forEach(e => {
        //         e.i = i
        //         i++
        //     })
            state.tabBarList = data;
            uni.setStorageSync('tabBarList', data)
            // console.log(uni.getStorageSync('tabBarList'))
        },
        //     state.tabBarList = data;
        //     uni.setStorageSync('tabBarList', data)
        //     // console.log(uni.getStorageSync('tabBarList'))
        // },
        //设置当前激活的角色
        setActiveRoleId(state, roleId) {
            state.activeRoleId = roleId
            uni.setStorageSync('activeRoleId', roleId)
        },
        //设置底部导航栏索引
        SET_TABBAT_INDEX(state, index) {
            state.tabbarIndex = index
            uni.setStorageSync('tabbarIndex', index)
        SET_MENU(state, menuList) {
            state.menu = menuList
            uni.setStorageSync('menu', menuList)
        },
        //设置底部导航栏索引
        // SET_TABBAT_INDEX(state, index) {
        //     state.tabbarIndex = index
        //     uni.setStorageSync('tabbarIndex', index)
        // },
        SET_HAS_UPDATE(state, val) {
            state.hasUpdate = val
            uni.setStorageSync('hasUpdate', val)
subPackage/user/role/index.vue
@@ -66,6 +66,8 @@
            },
            async submit() {
                const that = this
                //保存当前激活的角色
                store.commit("setActiveRoleId", this.selectRole.id)
@@ -76,9 +78,23 @@
                let data = res.data
                const appMenu = data.filter(e => e.name == 'app')[0]
                console.log("菜单:", appMenu)
                store.commit("SET_MENU", appMenu)
                this.$u.func.showToast("切换角色成功")
                uni.showToast({
                    title: "切换身份成功",
                    icon: "success",
                    duration: data.duration || 1500,
                    success: () => {
                        setTimeout(() => {
                            this.$u.func.globalNavigator("/pages/home/index", "switchTab")
                        }, data.duration || 1500)
                    }
                })
            }
        }
    }
utils/func.js
@@ -5,12 +5,6 @@
import {
    logAdd
} from "@/api/system/logManage"
import {
    messageCount
} from "@/api/system/message"
import {
    saveCId
} from "@/api/system/user.js"
// 全局公共方法
const install = (Vue, vm) => {
@@ -22,11 +16,24 @@
        vm.$u.vuex('accessToken', userInfo.access_token)
        vm.$u.vuex('isLogin', true)
        //添加当前激活的roleId(默认取第一个)
        vm.$u.vuex('activeRoleId', userInfo.role_id.split(",")[0])
        let activeRoleId = userInfo.role_id.split(",")[0]
        console.log("当前激活角色的id:", activeRoleId)
        uni.switchTab({
            url: '/pages/home/index'
        //添加当前激活的roleId(默认取第一个)
        vm.$u.vuex('activeRoleId', activeRoleId)
        //获取角色菜单
        dynamicMenu(activeRoleId).then(res => {
            let data = res.data
            let appMenu = data.filter(e => e.name == 'app')[0]
            console.log("当前角色菜单:", appMenu)
            vm.$u.vuex('menu', appMenu)
            //跳转到首页
            uni.switchTab({
                url: '/pages/home/index'
            })
        })
    }