login.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3. import _ from 'lodash';
  4. import { Notification } from 'element-ui';
  5. const jwt = require('jsonwebtoken');
  6. Vue.use(Vuex);
  7. const api = {
  8. interface: `/api/train/login`,
  9. user: id => `/api/train/user/update/${id}`,
  10. };
  11. const state = () => ({});
  12. const mutations = {};
  13. const actions = {
  14. async login({ commit }, { user, router }) {
  15. const res = await this.$axios.$post(`${api.interface}`, user);
  16. if (res.errcode === 0) {
  17. let user = jwt.decode(res.data);
  18. if (user.type == process.env.VUE_APP_USER_TYPE) {
  19. localStorage.setItem('user', JSON.stringify(user));
  20. commit('setUser', user, { root: true });
  21. Notification({
  22. title: '登录成功',
  23. message: `欢迎,${user.name}`,
  24. type: 'success',
  25. duration: 2000,
  26. });
  27. router.push('/');
  28. } else {
  29. Notification({
  30. title: '请重新登陆',
  31. message: `原因:非当前端用户,需要重新登陆`,
  32. type: 'warning',
  33. });
  34. console.warn('非当前端用户,需要重新登陆');
  35. }
  36. } else {
  37. Notification({
  38. title: '登录失败',
  39. message: `失败原因:${res.errmsg}`,
  40. type: 'error',
  41. });
  42. }
  43. },
  44. async update({ commit }, { id, ...info }) {
  45. let res = await this.$axios.$post(`${api.user(id)}`, info);
  46. return res;
  47. },
  48. };
  49. export default {
  50. namespaced: true,
  51. state,
  52. mutations,
  53. actions,
  54. };