import Vue from 'vue'; import Vuex from 'vuex'; import _ from 'lodash'; import { Notification } from 'element-ui'; const jwt = require('jsonwebtoken'); Vue.use(Vuex); const api = { interface: `/api/train/login`, user: id => `/api/train/user/update/${id}`, }; const state = () => ({}); const mutations = {}; const actions = { async login({ commit }, { user, router }) { const res = await this.$axios.$post(`${api.interface}`, user); if (res.errcode === 0) { let user = jwt.decode(res.data); if (user.type == process.env.VUE_APP_USER_TYPE) { localStorage.setItem('user', JSON.stringify(user)); commit('setUser', user, { root: true }); Notification({ title: '登录成功', message: `欢迎,${user.name}`, type: 'success', duration: 2000, }); router.push('/'); } else { Notification({ title: '请重新登陆', message: `原因:非当前端用户,需要重新登陆`, type: 'warning', }); console.warn('非当前端用户,需要重新登陆'); } } else { Notification({ title: '登录失败', message: `失败原因:${res.errmsg}`, type: 'error', }); } }, async update({ commit }, { id, ...info }) { let res = await this.$axios.$post(`${api.user(id)}`, info); return res; }, }; export default { namespaced: true, state, mutations, actions, };