jiangping
2023-10-26 ea87c908fb6cdfc3e227a584a53e6730efb8262a
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
import Vue from 'vue'
import Vuex from 'vuex'
import {
    pageCount,
    getTreeList,
    getUserInfo
} from '@/util/api/index.js'
 
Vue.use(Vuex) // vue的插件机制
 
const navHeight = uni.getStorageSync('navHeight');
const statusbarHeight = uni.getStorageSync('statusbarHeight');
const menuButtonWidth = uni.getStorageSync('menuButtonWidth');
const token = uni.getStorageSync('token');
const userInfo = uni.getStorageSync('userInfo');
const Menu = uni.getStorageSync('MenuList');
const session = uni.getStorageSync('session');
 
 
// Vuex.Store 构造器选项
const store = new Vuex.Store({
    // 为了不和页面或组件的data中的造成混淆,state中的变量前面建议加上$符号
    state: {
        // 用户信息
        userInfo: userInfo || null,
        token: token || null,
        menuButtonWidth: menuButtonWidth || '0',
        statusbarHeight: statusbarHeight || '0',
        navHeight: navHeight || '0',
        session: session ? session : '',
        // 菜单权限
        Menu: Menu ? JSON.parse(Menu) : [],
        // 待办数量
        upcomingNum: {
            d: 0,
            y: 0
        },
    },
    mutations: {
        setHeight(state, val) {
            state.navHeight = val.navHeight
            state.statusbarHeight = val.statusbarHeight
            state.menuButtonWidth = val.menuButtonWidth
            uni.setStorageSync('navHeight', val.navHeight);
            uni.setStorageSync('statusbarHeight', val.statusbarHeight);
            uni.setStorageSync('menuButtonWidth', val.menuButtonWidth);
        },
        SETTOKEN(state, val) {
            state.token = val
            uni.setStorageSync('token', val);
        },
        SETUSERINFO(state, val) {
            state.userInfo = val
            uni.setStorageSync('userInfo', val);
        },
        SETNUM(state, val) {
            state.upcomingNum.d = val.d
            state.upcomingNum.y = val.y
        },
        SETMENU(state, data) {
            state.Menu = data
            uni.setStorageSync('MenuList', JSON.stringify(data));
        },
        SETSESSION(state, data) {
            state.session = data;
            uni.setStorageSync('session', data);
        },
        clearCache(state) {
            state.userInfo = ''
            state.token = ''
            state.session = ''
            state.Menu = []
            state.upcomingNum.d = 0
            state.upcomingNum.y = 0
            uni.clearStorageSync()
        }
    },
    actions: {
        // 向后端获取菜单栏权限
        async getMenuList(content, type) {
            let res = await getTreeList({
                type: type
            })
            if (res.code === 200) {
                content.commit('SETMENU', res.data)
            }
        },
        // 获取状态高度
        getHeight(context) {
            let res = uni.getMenuButtonBoundingClientRect()
            let status = uni.getSystemInfoSync()
            let menuButtonWidth = res.width
            let height = res.height
            let statusbarHeight = status.statusBarHeight
            let navHeight = res.height + (res.top - statusbarHeight) * 2;
            context.commit('setHeight', {
                statusbarHeight,
                navHeight,
                height,
                menuButtonWidth
            })
        },
        async getUpcomingNum(context) {
            let res = await pageCount({})
            if (res.code === 200) {
                context.commit('SETNUM', {
                    d: res.data.startNum,
                    y: res.data.endNum
                })
            }
        },
        // 向后端获取个人信息
        async getUserInfos(content) {
            let res = await getUserInfo()
            if (res.code === 200) {
                content.commit('SETUSERINFO', res.data)
                return true;
            }
        }
    }
})
 
export default store