123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import { login, loginUp, getInfo } from '@/api/login'
- import { getToken, setToken, removeToken, setUserId, TokenKey, setLogId, removeLogId, setUserName } from '@/utils/auth'
- import { resetRouter } from '@/router'
- import { wsSocketClose } from '@/utils/socket'
- const getDefaultState = () => {
- return {
- token: getToken(),
- name: '',
- avatar: '',
- roles: [],
- firstLogin: getToken('FirstLogin') ? JSON.parse(getToken('FirstLogin')) : false,
- UserType: sessionStorage.getItem('UserType') ? sessionStorage.getItem('UserType') : '',
- UserId: sessionStorage.getItem('User_Id') ? sessionStorage.getItem('User_Id') : '',
- userPoewrList: sessionStorage.getItem('userAuthList') ? JSON.parse(sessionStorage.getItem('userAuthList')) : []
- }
- }
- const state = getDefaultState()
- const mutations = {
- RESET_STATE: (state) => {
- Object.assign(state, getDefaultState())
- },
- SET_TOKEN: (state, token) => {
- state.token = token
- },
- SET_NAME: (state, name) => {
- state.name = name
- },
- SET_UserId: (state, UserId) => {
- state.UserId = UserId
- },
- SET_UserType: (state, UserType) => {
- state.UserType = UserType
- sessionStorage.setItem('UserType', UserType)
- },
- SET_ROLES: (state, roles) => {
- state.roles = roles
- },
- SET_AVATAR: (state, avatar) => {
- state.avatar = avatar
- },
- SET_UserPoewrList: (state, arr) => {
- state.userPoewrList = arr
- },
- SET_LOGIN: (state, login) => {
- state.firstLogin = login
- },
- }
- const actions = {
- // user login
- login ({ commit }, userInfo) {
- return new Promise((resolve, reject) => {
- login(userInfo).then(res => {
- if (res.code == 0) {
- const { returnData } = res
- commit('SET_TOKEN', returnData.Token)
- commit('SET_UserType', returnData.UserType)
- commit('SET_UserId', returnData.UserId)
- commit('SET_LOGIN', returnData.FirstLogin)
- sessionStorage.setItem("userName", userInfo.LoginName);
- sessionStorage.setItem("token", returnData.Token);
- setToken(TokenKey, returnData.Token);
- setToken('FirstLogin', returnData.FirstLogin);
- setLogId(returnData.LogId)
- setUserId(returnData.UserId)
- setUserName(returnData.UserName)
- let nowDate = new Date();
- let oldDate = new Date(returnData.ValidTime);
- let diffTime = oldDate - nowDate;
- setTimeout(function () {
- resetToken()
- }, diffTime)
- resolve()
- } else {
- reject(res)
- }
- }).catch(error => {
- reject(error)
- })
- })
- },
- // get user info
- getInfo ({ commit, state }) {
- return new Promise((resolve, reject) => {
- let data = {
- roles: state.userPoewrList,
- introduction: '',
- avatar: '',
- firstLogin: state.firstLogin,
- name: sessionStorage.getItem("userName"),
- token: state.token
- }
- const { name, avatar, roles, firstLogin } = data
- commit('SET_LOGIN', firstLogin)
- commit('SET_NAME', name)
- commit('SET_AVATAR', avatar)
- commit('SET_ROLES', roles ? roles : ['admin'])
- resolve(data)
- })
- },
- // user logout
- logout ({ commit, dispatch, state }) {
- // console.log(state)
- return new Promise((resolve, reject) => {
- loginUp({ "UserId": state.UserId, "UserType": state.UserType }).then(result => {
- dispatch('tagsView/delAllViews', {}, { root: true });
- sessionStorage.removeItem("userName");
- sessionStorage.removeItem("UserType");
- sessionStorage.removeItem("User_Id");
- sessionStorage.removeItem("token");
- sessionStorage.removeItem("userAuthList");
- removeToken(); // must remove token first
- removeToken('codeToken');
- removeToken('systemSet');
- removeToken('FirstLogin');
- removeToken('userAuthList');
- removeLogId()
- resetRouter();
- commit('RESET_STATE');
- commit('SET_ROLES', []);
- wsSocketClose();
- resolve();
- })
- // logout(state.token).then(() => {
- // removeToken() // must remove token first
- // resetRouter()
- // commit('RESET_STATE')
- // resolve()
- // }).catch(error => {
- // reject(error)
- // })
- })
- },
- // remove token
- resetToken ({ commit }) {
- return new Promise(resolve => {
- removeToken() // must remove token first
- commit('RESET_STATE')
- commit('SET_ROLES', [])
- resolve()
- })
- },
- setPowerList ({ commit }, arr) {
- commit('SET_UserPoewrList', arr);
- }
- }
- export default {
- namespaced: true,
- state,
- mutations,
- actions
- }
|