import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); //vue的插件机制 import api from '@/api/api.js' //Vuex.Store 构造器选项 const store = new Vuex.Store({ state: { configList: [], //配置列表 shop : false, userInfo : {}, //用户信息 shopData : {}, buy : {}, }, getters: { // 角色 true 为供应商 false 为采购商 userShop(state){ return state.shop }, userInfo(state){ return state.userInfo }, }, mutations: { // 初始化配置 initConfig(state){ // api('getConfig', res => { // if(res.code == 200){ // state.configList = res.result // } // }) let config = ['getPrivacyPolicy', 'getUserAgreement'] config.forEach(k => { api(k, res => { if (res.code == 200) { state.configList[k] = res.result } }) }) }, login(state, form){ uni.showLoading({ title: '登录中...' }) api('loginUser', form, res => { uni.hideLoading() if(res.code != 200){ return } state.userInfo = res.result.userInfo state.buy = res.result.buy state.shop = res.result.shop uni.setStorageSync('token', res.result.token) if(!state.shop && !state.buy){ uni.reLaunch({ url: '/pages_order/auth/selectionIdentity?back=no&' }) }else{ uni.reLaunch({ url: '/pages/index/index' }) } }) }, getUserInfo(state){ api('infoGetInfo', res => { if(res.code == 200){ state.userInfo = res.result } }) }, logout(state){ uni.showModal({ title: '确认退出登录吗', success(r) { if(r.confirm){ state.userInfo = {} state.role = false uni.removeStorageSync('token') uni.redirectTo({ url: '/pages/index/index' }) } } }) }, }, actions: {}, }) export default store