user.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { login, logout } from '@/api/user'
  2. import { getToken, setToken, removeToken, setSessionStorage, removeSessionStorage } from '@/utils/auth'
  3. import { getRemoteImg } from '@/api/ftp'
  4. import { resetRouter } from '@/router'
  5. const sha1Hex = require('sha1-hex')
  6. const getDefaultState = () => {
  7. return {
  8. token: sessionStorage.getItem('token') || '',
  9. name: '',
  10. avatar: '',
  11. username: '',
  12. realName: '',
  13. userId: '',
  14. departmentId: '',
  15. applied: false
  16. }
  17. }
  18. const state = getDefaultState()
  19. const mutations = {
  20. RESET_STATE: (state) => {
  21. Object.assign(state, getDefaultState())
  22. },
  23. SET_TOKEN: (state, token) => {
  24. state.token = token
  25. },
  26. // SET_NAME: (state, name) => {
  27. // state.name = name
  28. // },
  29. SET_USERNAME: (state, username) => {
  30. state.username = username
  31. },
  32. SET_AVATAR: (state, avatar) => {
  33. state.avatar = avatar
  34. },
  35. SET_REALNAME: (state, realName) => {
  36. state.realName = realName
  37. },
  38. SET_USERID: (state, userId) => {
  39. state.userId = userId
  40. },
  41. SET_DEPTS: (state, departmentId) => {
  42. state.departmentId = departmentId
  43. },
  44. COMPLETE_APPLY: (state, payload) => {
  45. state.applied = payload
  46. }
  47. }
  48. const actions = {
  49. // user login
  50. login({ commit, dispatch }, userInfo) {
  51. const { username, password } = userInfo
  52. // setSessionStorage('username', username)
  53. return new Promise((resolve, reject) => {
  54. // login({ username: username, password: sha1Hex(password) })
  55. login({ loginUser: 'zhgdlogin' })
  56. .then((response) => {
  57. // console.log('2222', response)
  58. // const data = {
  59. // accessToken: 1111111,
  60. // username: 'admin',
  61. // realName: '超级管理员'
  62. // }
  63. const { accessToken, username, avatar, realName, id, departmentId } = response.result.result.result
  64. // const { accessToken, username, avatar, realName } = data
  65. // setToken(accessToken)
  66. commit('SET_TOKEN', accessToken)
  67. commit('SET_USERNAME', username)
  68. commit('SET_REALNAME', realName)
  69. commit('SET_USERID', id)
  70. commit('SET_DEPTS', departmentId)
  71. setSessionStorage('token', accessToken)
  72. setSessionStorage('username', username)
  73. setSessionStorage('realName', realName)
  74. setSessionStorage('userId', id)
  75. setSessionStorage('departmentId', departmentId)
  76. if (avatar) {
  77. // getRemoteImg(avatar).then(res => {
  78. // if (res.status === 200) {
  79. // commit('SET_AVATAR', res.config.url)
  80. // setSessionStorage('avatar', res.config.url)
  81. // }
  82. // })
  83. commit('SET_AVATAR', getRemoteImg(avatar))
  84. setSessionStorage('avatar', getRemoteImg(avatar))
  85. }
  86. resolve(response)
  87. })
  88. .catch((error) => {
  89. reject(error)
  90. })
  91. })
  92. },
  93. // set name
  94. // setName({ commit }, userInfo) {
  95. // commit('SET_NAME', userInfo)
  96. // },
  97. // user logout
  98. logout({ commit, state, dispatch }) {
  99. return new Promise((resolve, reject) => {
  100. const data = {
  101. token: state.token
  102. }
  103. logout(data)
  104. .then(() => {
  105. // removeToken() // must remove token first
  106. removeSessionStorage('token')
  107. removeSessionStorage('username')
  108. removeSessionStorage('avatar')
  109. removeSessionStorage('realName')
  110. removeSessionStorage('userId')
  111. removeSessionStorage('departmentId')
  112. removeSessionStorage('last-route')
  113. resetRouter()
  114. commit('RESET_STATE')
  115. dispatch('tagsView/loginOutdel', {}, { root: true }) // 调用tagsView中的清空
  116. dispatch('routeSetting/loginOutdel', {}, { root: true }) // 调用tagsView中的清空
  117. resolve({})
  118. })
  119. .catch((error) => {
  120. reject(error)
  121. })
  122. })
  123. },
  124. // remove token
  125. resetToken({ commit }) {
  126. return new Promise((resolve) => {
  127. // removeToken() // must remove token first
  128. removeSessionStorage('token')
  129. removeSessionStorage('avatar')
  130. removeSessionStorage('username')
  131. removeSessionStorage('realName')
  132. removeSessionStorage('userId')
  133. removeSessionStorage('departmentId')
  134. removeSessionStorage('last-route')
  135. commit('RESET_STATE')
  136. resolve({})
  137. })
  138. },
  139. // 账号申请
  140. finnishApply({ commit }, status) {
  141. commit('COMPLETE_APPLY', status)
  142. }
  143. }
  144. export default {
  145. namespaced: true,
  146. state,
  147. mutations,
  148. actions
  149. }