MrShi
3 天以前 4fabfe4dbd2eb28d07a4350597d314958cc1c281
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
import Vue from 'vue'
import Vuex from 'vuex'
 
Vue.use(Vuex)
 
const navHeight = uni.getStorageSync('navHeight');
const statusbarHeight = uni.getStorageSync('statusbarHeight');
const userInfo = uni.getStorageSync('userInfo');
const token = uni.getStorageSync('token');
const openid = uni.getStorageSync('openid');
 
const store = new Vuex.Store({
    
    state: {
        navHeight: navHeight || '',
        statusbarHeight: statusbarHeight || '',
        userInfo: userInfo || {},
        token: token || '',
        openid:token || ''
    },
    
    mutations: {
        // 设置导航栏高度
        setHeight(state, val) {
            state.navHeight = val.navHeight
            state.statusbarHeight = val.statusbarHeight
            uni.setStorageSync('navHeight', val.navHeight);
            uni.setStorageSync('statusbarHeight', val.statusbarHeight);
        },
        // 设置token
        setToken(state, val) {
            state.token = val
            uni.setStorageSync('token', val);
        },
        // 设置token
        setOpenid(state, val) {
            state.openid = val
            uni.setStorageSync('openid', val);
        },
        // 设置用户信息
        setUserInfo(state, val) {
            state.userInfo = val
            uni.setStorageSync('userInfo', val);
        },
        // 清除缓存数据
        empty(state) {
            state.userInfo = ''
            state.token = ''
            uni.removeStorageSync('token');
            uni.removeStorageSync('userInfo');
        }
    },
    
    actions: {
        // 获取状态高度
        getHeight(context) {
            let res = uni.getMenuButtonBoundingClientRect()
            let status = uni.getSystemInfoSync()
            var height = res.height
            let statusbarHeight = status.statusBarHeight
            let navHeight = res.height + (res.top - statusbarHeight) * 2;
            context.commit('setHeight', { statusbarHeight, navHeight, height })
        }
    }
    
})
 
export default store;