user.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { login, loginUp, getInfo } from '@/api/login'
  2. import { getToken, setToken, removeToken, setUserId, TokenKey, setLogId, removeLogId, setUserName } from '@/utils/auth'
  3. import { resetRouter } from '@/router'
  4. import { wsSocketClose } from '@/utils/socket'
  5. const getDefaultState = () => {
  6. return {
  7. token: getToken(),
  8. name: '',
  9. avatar: '',
  10. roles: [],
  11. firstLogin: getToken('FirstLogin') ? JSON.parse(getToken('FirstLogin')) : false,
  12. UserType: sessionStorage.getItem('UserType') ? sessionStorage.getItem('UserType') : '',
  13. UserId: sessionStorage.getItem('User_Id') ? sessionStorage.getItem('User_Id') : '',
  14. userPoewrList: sessionStorage.getItem('userAuthList') ? JSON.parse(sessionStorage.getItem('userAuthList')) : []
  15. }
  16. }
  17. const state = getDefaultState()
  18. const mutations = {
  19. RESET_STATE: (state) => {
  20. Object.assign(state, getDefaultState())
  21. },
  22. SET_TOKEN: (state, token) => {
  23. state.token = token
  24. },
  25. SET_NAME: (state, name) => {
  26. state.name = name
  27. },
  28. SET_UserId: (state, UserId) => {
  29. state.UserId = UserId
  30. },
  31. SET_UserType: (state, UserType) => {
  32. state.UserType = UserType
  33. sessionStorage.setItem('UserType', UserType)
  34. },
  35. SET_ROLES: (state, roles) => {
  36. state.roles = roles
  37. },
  38. SET_AVATAR: (state, avatar) => {
  39. state.avatar = avatar
  40. },
  41. SET_UserPoewrList: (state, arr) => {
  42. state.userPoewrList = arr
  43. },
  44. SET_LOGIN: (state, login) => {
  45. state.firstLogin = login
  46. },
  47. }
  48. const actions = {
  49. // user login
  50. login ({ commit }, userInfo) {
  51. return new Promise((resolve, reject) => {
  52. login(userInfo).then(res => {
  53. if (res.code == 0) {
  54. const { returnData } = res
  55. commit('SET_TOKEN', returnData.Token)
  56. commit('SET_UserType', returnData.UserType)
  57. commit('SET_UserId', returnData.UserId)
  58. commit('SET_LOGIN', returnData.FirstLogin)
  59. sessionStorage.setItem("userName", userInfo.LoginName);
  60. sessionStorage.setItem("token", returnData.Token);
  61. setToken(TokenKey, returnData.Token);
  62. setToken('FirstLogin', returnData.FirstLogin);
  63. setLogId(returnData.LogId)
  64. setUserId(returnData.UserId)
  65. setUserName(returnData.UserName)
  66. let nowDate = new Date();
  67. let oldDate = new Date(returnData.ValidTime);
  68. let diffTime = oldDate - nowDate;
  69. setTimeout(function () {
  70. resetToken()
  71. }, diffTime)
  72. resolve()
  73. } else {
  74. reject(res)
  75. }
  76. }).catch(error => {
  77. reject(error)
  78. })
  79. })
  80. },
  81. // get user info
  82. getInfo ({ commit, state }) {
  83. return new Promise((resolve, reject) => {
  84. let data = {
  85. roles: state.userPoewrList,
  86. introduction: '',
  87. avatar: '',
  88. firstLogin: state.firstLogin,
  89. name: sessionStorage.getItem("userName"),
  90. token: state.token
  91. }
  92. const { name, avatar, roles, firstLogin } = data
  93. commit('SET_LOGIN', firstLogin)
  94. commit('SET_NAME', name)
  95. commit('SET_AVATAR', avatar)
  96. commit('SET_ROLES', roles ? roles : ['admin'])
  97. resolve(data)
  98. })
  99. },
  100. // user logout
  101. logout ({ commit, dispatch, state }) {
  102. // console.log(state)
  103. return new Promise((resolve, reject) => {
  104. loginUp({ "UserId": state.UserId, "UserType": state.UserType }).then(result => {
  105. dispatch('tagsView/delAllViews', {}, { root: true });
  106. sessionStorage.removeItem("userName");
  107. sessionStorage.removeItem("UserType");
  108. sessionStorage.removeItem("User_Id");
  109. sessionStorage.removeItem("token");
  110. sessionStorage.removeItem("userAuthList");
  111. removeToken(); // must remove token first
  112. removeToken('codeToken');
  113. removeToken('systemSet');
  114. removeToken('FirstLogin');
  115. removeToken('userAuthList');
  116. removeLogId()
  117. resetRouter();
  118. commit('RESET_STATE');
  119. commit('SET_ROLES', []);
  120. wsSocketClose();
  121. resolve();
  122. })
  123. // logout(state.token).then(() => {
  124. // removeToken() // must remove token first
  125. // resetRouter()
  126. // commit('RESET_STATE')
  127. // resolve()
  128. // }).catch(error => {
  129. // reject(error)
  130. // })
  131. })
  132. },
  133. // remove token
  134. resetToken ({ commit }) {
  135. return new Promise(resolve => {
  136. removeToken() // must remove token first
  137. commit('RESET_STATE')
  138. commit('SET_ROLES', [])
  139. resolve()
  140. })
  141. },
  142. setPowerList ({ commit }, arr) {
  143. commit('SET_UserPoewrList', arr);
  144. }
  145. }
  146. export default {
  147. namespaced: true,
  148. state,
  149. mutations,
  150. actions
  151. }