import Vue from 'vue' import Vuex from 'vuex' import api from '@/common/api.js' Vue.use(Vuex); const store = new Vuex.Store({ state: { login: true, token: '', avatarUrl: '', userName: '', userInfo: null, dictObj: null, allowLoading: true, goodsAuditionConfigIdList: [], //当前访问页面的试听节ID shoppingCartList: [], //购物车支付商品 applyData: {}, //预约考试数据存放 backPageApplyData: {}, //预约考试返回页面的数据存放 playSectionId: 0, copyData: null, //存放审核资料数据 }, getters: { userInfo: state => { if (state.userInfo == null) { if (uni.getStorageSync('user_account')) { getUserInfo(state) } } return state.userInfo }, dictObj: state => { if (state.dictObj == null) { api.dictList().then(res => { if (res.data.code === 200) { let newList = {} let list = res.data.data for (let i = 0; i < list.length; i++) { let item = list[i] if (newList.hasOwnProperty(item.dictType)) { newList[item.dictType].push(item.dictLabel) } else { newList[item.dictType] = [item.dictLabel] } } state.dictObj = newList; } }); } return state.dictObj }, allowLoading: state => { return state.allowLoading }, goodsAuditionConfigIdList: state => { return state.goodsAuditionConfigIdList }, shoppingCartList: state => { return state.shoppingCartList }, getApplyData: state => { return state.applyData }, getBackPageApplyData: state => { return state.backPageApplyData }, playSectionId: state => { return state.playSectionId }, getCopyData: state => state.copyData, }, mutations: { tabNum(state, nums) { if (nums) { uni.setTabBarBadge({ index: 3, text: nums + '' }) } else { uni.removeTabBarBadge({ index: 3 }) } }, updataCopyData(state, objs) { state.copyData = objs }, updateApplyData(state, arrays) { state.applyData = arrays; }, updateBackApplyData(state, arrays) { state.backPageApplyData = arrays; }, updateUserInfo(state, provider) { state.userInfo = provider.userInfo; }, setGoodsAuditionConfigIdList(state, provider) { state.goodsAuditionConfigIdList = provider.goodsAuditionConfigIdList; }, setShoppingCartList(state, provider) { state.shoppingCartList = provider.shoppingCartList; }, setPlaySectionId(state, provider) { state.playSectionId = provider.playSectionId; }, }, actions: { changeTabsNum({ commit }) { api.getinfoAttached().then(res => { if (res.data.code === 200) { const nums = res.data.data.informSum + res.data.data.orderSum + res.data.data .periodSum + res.data.data.planSum + res.data.data.subscribeSum commit('tabNum', nums) } }) } } }) async function getUserInfo(state) { const resdata = await api.getInfo() if (resdata.data.code == 200) { state.userInfo = resdata.data.data; } } export default store