lrf402788946 5 年之前
父节点
当前提交
d8a0a4a3a9
共有 4 个文件被更改,包括 104 次插入5 次删除
  1. 1 1
      router/before.js
  2. 19 4
      store/login.js
  3. 44 0
      store/user/auth-user.js
  4. 40 0
      store/user/role.js

+ 1 - 1
router/before.js

@@ -2,7 +2,7 @@ import store from '@/store/index';
 
 const checkLogin = router => {
   router.beforeEach((to, form, next) => {
-    store.commit('setUser');
+    store.dispatch('login/toGetUser');
     // if (to.name === 'login') {
     //   next();
     //   return;

+ 19 - 4
store/login.js

@@ -1,5 +1,6 @@
 import Vue from 'vue';
 import Vuex from 'vuex';
+import axios from 'axios';
 import _ from 'lodash';
 import { Notification } from 'element-ui';
 const jwt = require('jsonwebtoken');
@@ -7,6 +8,8 @@ Vue.use(Vuex);
 const api = {
   interface: `/api/auth/login`,
   getUser: `/api/auth/token`,
+  logout: '/api/auth/logout',
+  getMenu: `/api/auth/user/menus`,
   updatePassword: '/api/user/pwd_edit',
 };
 const state = () => ({});
@@ -56,12 +59,24 @@ const actions = {
     if (!key) {
       console.log('游客身份');
     }
-    let res = await this.$axios.$post(api.getUser, { key: key });
-    if (res.errcode == '0') {
-      let user = jwt.decode(res.data.token);
-      commit('setUser', user, { root: true });
+    let res = await axios.post(api.getUser, { key: key });
+    if (res.data.errcode == '0') {
+      let token = _.get(res, `data.data.token`);
+      if (token) {
+        let user = jwt.decode(token);
+        commit('setUser', user, { root: true });
+      }
     }
   },
+  async toGetMenu({ commit }, payload) {
+    const res = await this.$axios.$get(api.getMenu, payload);
+    return res;
+  },
+  async logout({ commit }, payload) {
+    let key = localStorage.getItem('token');
+    const res = await this.$axios.$post(api.logout, { key: key });
+    commit('deleteUser');
+  },
   async update({ commit }, payload) {
     let res = await this.$axios.$post(`${api.updatePassword}`, {
       data: payload,

+ 44 - 0
store/user/auth-user.js

@@ -0,0 +1,44 @@
+import Vue from 'vue';
+import Vuex from 'vuex';
+import _ from 'lodash';
+//用户的菜单选项增删改查
+Vue.use(Vuex);
+const api = {
+  interface: `/api/auth/user`,
+  getMenu: `/api/auth/user/menus`,
+};
+const state = () => ({});
+const mutations = {};
+
+const actions = {
+  async query({ commit }, { skip = 0, limit = undefined, ...info } = {}) {
+    const res = await this.$axios.$get(api.interface, { skip, limit, ...info });
+    return res;
+  },
+  async create({ commit }, payload) {
+    const res = await this.$axios.$post(`${api.interface}`, payload);
+    return res;
+  },
+  async fetch({ commit }, payload) {
+    //特殊变化,查用户菜单
+    const res = await this.$axios.$get(`${api.getMenu}`, payload);
+    return res;
+  },
+  async update({ commit }, { id, ...info } = {}) {
+    const res = await this.$axios.$post(`${api.interface}/update/${id}`, {
+      ...info,
+    });
+    return res;
+  },
+  async delete({ commit }, payload) {
+    const res = await this.$axios.$delete(`${api.interface}/${payload}`);
+    return res;
+  },
+};
+
+export default {
+  namespaced: true,
+  state,
+  mutations,
+  actions,
+};

+ 40 - 0
store/user/role.js

@@ -0,0 +1,40 @@
+import Vue from 'vue';
+import Vuex from 'vuex';
+import _ from 'lodash';
+//用户的菜单选项增删改查
+Vue.use(Vuex);
+const api = {
+  interface: `/api/auth/role`,
+};
+const state = () => ({});
+const mutations = {};
+
+const actions = {
+  async query({ commit }, { skip = 0, limit = undefined, ...info } = {}) {
+    const res = await this.$axios.$get(api.interface, { skip, limit, ...info });
+    return res;
+  },
+  async create({ commit }, payload) {
+    const res = await this.$axios.$post(`${api.interface}`, payload);
+    return res;
+  },
+  async fetch({ commit }, payload) {
+    const res = await this.$axios.$get(`${api.interface}/${payload}`);
+    return res;
+  },
+  async update({ commit }, { id, ...info } = {}) {
+    const res = await this.$axios.$post(`${api.interface}/update/${id}`, { ...info });
+    return res;
+  },
+  async delete({ commit }, payload) {
+    const res = await this.$axios.$delete(`${api.interface}/${payload}`);
+    return res;
+  },
+};
+
+export default {
+  namespaced: true,
+  state,
+  mutations,
+  actions,
+};