login.js 848 B

12345678910111213141516171819202122232425262728293031323334
  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3. import _ from 'lodash';
  4. const jwt = require('jsonwebtoken');
  5. Vue.use(Vuex);
  6. const api = {
  7. interface: `/api/train/login`,
  8. user: id => `/api/train/user/update/${id}`,
  9. };
  10. const state = () => ({});
  11. const mutations = {};
  12. const actions = {
  13. async login({ commit }, payload) {
  14. const res = await this.$axios.$post(`${api.interface}`, payload);
  15. let result = true;
  16. if (res.errcode === 0) {
  17. let info = jwt.decode(res.data);
  18. sessionStorage.setItem('user', JSON.stringify(info));
  19. commit('setUser', info, { root: true });
  20. } else result = res;
  21. return result;
  22. },
  23. async update({ commit }, { id, ...info }) {
  24. let res = await this.$axios.$post(`${api.user(id)}`, info);
  25. return res;
  26. },
  27. };
  28. export default {
  29. namespaced: true,
  30. state,
  31. mutations,
  32. actions,
  33. };