diff --git a/src/apis/personal.js b/src/apis/personal.js new file mode 100644 index 0000000..d2f170e --- /dev/null +++ b/src/apis/personal.js @@ -0,0 +1,9 @@ +import request from "@/request/index.js" + +export const userInfo = (data) => { + return request({ + url: '/personal/userinfo', + method: 'get', + data, + }) +} diff --git a/src/store/index.js b/src/store/index.js index c47cada..36a7afd 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,12 +1,27 @@ import {createStore} from "vuex"; +import user from "./modules/user.js" + export default createStore({ + modules: { + user + }, state: { - count: 0, + routeLoaded: false, // 菜单和路由是否已经加载 + firstRoute: null, // 第一个路由,用于设置进入主页 的redirect + menuTree: null, // 菜单树 }, mutations: { - increase (state, count) { - state.count += count; - } - } -}) \ No newline at end of file + // 改变菜单和路由的加载状态 + setRouteLoaded(state, loaded) { + state.routeLoaded = loaded; + }, + setFirstRoute(state, route) { + state.firstRoute = route; + }, + setMenuTree(state, data) { + state.menuTree = data + }, + }, + actions: {}, +}) diff --git a/src/store/modules/user.js b/src/store/modules/user.js new file mode 100644 index 0000000..05ecaa9 --- /dev/null +++ b/src/store/modules/user.js @@ -0,0 +1,37 @@ +import { userInfo } from "@/apis/personal.js" +import {info} from "sass"; +export default { + namespaced: true, + state: { + token: '', + userInfo: {} // 用户基本信息 + }, + getters: { + isLogin(state) { + return !!state.token || !!localStorage.getItem("pm_token"); + } + }, + mutations: { + setToken(state, token) { + localStorage.setItem('pm_token', token); + state.token = token; + }, + clearToken(state) { + state.token = ''; + localStorage.removeItem('pm_token'); + }, + setUserInfo(state, info) { + state.userInfo = info || {}; + }, + clearUserInfo(state) { + state.userInfo = {} + } + }, + actions: { + refreshInfo({commit}) { + userInfo().then((res) => { + commit('setUserInfo', res.data) + }) + } + } +}