common.js 1.52 KB
import { NativeApi } from '@/api/NativeApis'

const AppInfo = {
    versionCode: 100,
    versionName: '2.1',
    statusBarHeight: 0,
    sourceFlag: false,
}

export default {
    namespaced: true,
    state: {
        AppInfo: { ...AppInfo },
        keepAlives: ['Home']
    },
    mutations: {
        MSetAppInfo(state, data) {
            Object.assign(state.AppInfo, data)
        },
        MSaveKeepAlive(state, data) {
            if (data) {
                const index = state.keepAlives.indexOf(data)
                if (index <= -1) {
                    state.keepAlives.push(data)
                }
            }
        },
        MRemoveKeepAlive(state, data) {
            if (data) {
                const index = state.keepAlives.indexOf(data)
                if (index > -1) {
                    state.keepAlives.splice(index, 1)
                }
            }
        },
    },
    actions: {
        AGetAppInfo({ state, commit }) {
            if (state.AppInfo.sourceFlag) {
                return
            }
            NativeApi.nativeAppInfo({}).then(res => {
                if (res.data) {
                    Object.assign(res.data, { sourceFlag: true })
                }
                commit('MSetAppInfo', res.data)
            }).catch(e => {
                commit('MSetAppInfo', AppInfo)
            })
        }
    },
    getters: {
        GStatusBarHeight(state) {
            return state.AppInfo.statusBarHeight
        },
        GKeepAlives(state) {
            return state.keepAlives
        }
    }
}