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}`, bind: `/api/train/user/bind`, userBind: `/api/train/user/userbind`, }; const state = () => ({}); const mutations = {}; const actions = { async login({ commit }, { user, router, path = '/', needReturn = false, typeCheck = true }) { const res = await this.$axios.$post(`${api.interface}`, user); const setUser = (user, commit) => { localStorage.setItem('user', JSON.stringify(user)); commit('setUser', user, { root: true }); }; if (res.errcode === 0) { let user = jwt.decode(res.data); if (!typeCheck) { setUser(user, commit); if (needReturn) return res; else router.push(path); } if (user.type == process.env.VUE_APP_USER_TYPE) { setUser(user, commit); Notification({ title: '登录成功', message: `欢迎,${user.name}`, type: 'success', duration: 2000, }); if (needReturn) return res; else router.push(path); } else { Notification({ title: '请重新登陆', message: `原因:非当前端用户,需要重新登陆`, type: 'warning', }); console.warn('非当前端用户,需要重新登陆'); } } else { if (needReturn) return res; 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; }, async bind({ commit }, payload) { let res = await this.$axios.$post(`${api.bind}`, payload); return res; }, async userBind({ commit }, payload) { let res = await this.$axios.$post(`${api.userBind}`, payload); return res; }, }; export default { namespaced: true, state, mutations, actions, };