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
|
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex)
import { login, logout } from '../apis/user.js';
const store = new Vuex.Store({ state: { token: '', name:'', avatar:'', roles:'' }, mutations: { SET_TOKEN: (state, token) => { state.token = token; }, SET_NAME: (state, name) => { state.name = name; }, SET_AVATAR: (state, avatar) => { state.avatar = avatar; }, SET_ROLES: (state, roles) => { state.roles = roles; } }, actions: { login({ commit }, userInfo) { return new Promise((resolve, reject) => { login(userInfo).then(data => { console.log('login', data) const obj = data; commit('SET_TOKEN', obj.token); commit('SET_NAME', obj.name); commit('SET_AVATAR', obj.userId); commit('SET_ROLES', 'admin'); uni.setStorageSync('token', obj.token); uni.setStorageSync('userName', obj.name); uni.setStorageSync('userSn', obj.userId); uni.setStorageSync('userGroupId', obj.userGroupId); uni.setStorageSync('unitGroupId', obj.unitGroupId); uni.setStorageSync('sysAdmin', obj.sysAdmin); resolve(data); }).catch(error => { reject(error.msg); }); }); },
logout({ commit, state }) { return new Promise((resolve, reject) => { logout(state.token).then(() => { commit('SET_TOKEN', ''); commit('SET_NAME', ''); commit('SET_AVATAR', ''); commit('SET_ROLES', []); uni.clearStorage(); resolve(); }).catch(error => { reject(error); }); }); } } }) export default store
|