user.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { getToken, setToken, removeToken, setUserId, TokenKey } from '@/utils/auth'
  2. import { resetRouter } from '@/router'
  3. import { Query } from "@/api/webApi"
  4. import { isValue } from "@/utils/validate"
  5. import { getAuth, signIn, loginOut } from '@/api/login'
  6. //import '@/config/staticConfig' //按钮权限 全局自定义指令v-is
  7. const getDefaultState = () => {
  8. return {
  9. token: getToken(),
  10. name: '',
  11. avatar: '',
  12. isLogin: false,
  13. roles: [],
  14. UserType: sessionStorage.getItem('UserType') ? sessionStorage.getItem('UserType') : '',
  15. UserId: sessionStorage.getItem('User_Id') ? sessionStorage.getItem('User_Id') : '',
  16. userPoewrList: sessionStorage.getItem('userAuthList') ? JSON.parse(sessionStorage.getItem('userAuthList')) : [],
  17. // userPoewrList: sessionStorage.setItem('userAuthList') ? JSON.parse(sessionStorage.getItem('userAuthList')) : []
  18. userChecked: !!sessionStorage.getItem('userChecked'),
  19. userLoginList: localStorage.getItem('userLoginList') ? JSON.parse(localStorage.getItem('userLoginList')) : [],
  20. userLoginState: localStorage.getItem('userLoginState') ? JSON.parse(localStorage.getItem('userLoginState')) : { state: false, time: 60 * PLATFROM_CONFIG.maxTime },
  21. }
  22. }
  23. const state = getDefaultState()
  24. const mutations = {
  25. RESET_STATE: state => {
  26. Object.assign(state, getDefaultState())
  27. },
  28. SET_TOKEN: (state, token) => {
  29. state.token = token
  30. },
  31. SET_LOGIN: (state, login) => {
  32. state.isLogin = login
  33. },
  34. SET_NAME: (state, name) => {
  35. state.name = name
  36. },
  37. SET_UserId: (state, UserId) => {
  38. state.UserId = UserId
  39. },
  40. SET_UserType: (state, UserType) => {
  41. state.UserType = UserType
  42. sessionStorage.setItem('UserType', UserType)
  43. },
  44. SET_ROLES: (state, roles) => {
  45. state.roles = roles
  46. },
  47. SET_AVATAR: (state, avatar) => {
  48. state.avatar = avatar
  49. },
  50. SET_UserPoewrList: (state, arr) => {
  51. state.userPoewrList = arr
  52. },
  53. SET_UserLoginList: (state, arr) => {
  54. state.userLoginList = arr
  55. localStorage.setItem('userLoginList', JSON.stringify(arr))
  56. },
  57. SET_UserLoginState: (state, arr) => {
  58. state.userLoginState = arr
  59. localStorage.setItem('userLoginState', JSON.stringify(arr))
  60. },
  61. SET_USER_CHECKED (state, flag) {
  62. state.userChecked = flag
  63. if (flag) {
  64. sessionStorage.setItem('userChecked', 1)
  65. } else {
  66. sessionStorage.clearItem('userChecked')
  67. }
  68. }
  69. }
  70. const actions = {
  71. // user login
  72. login ({ commit }, userInfo) {
  73. return new Promise((resolve, reject) => {
  74. signIn(userInfo)
  75. .then(response => {
  76. if (response.code == 0 && isValue(response.returnData)) {
  77. const { user_token, user_id } = response.returnData
  78. commit('SET_TOKEN', user_token)
  79. commit('SET_UserType', 1)
  80. commit('SET_UserId', user_id)
  81. setToken('userName', userInfo.username)
  82. setToken(TokenKey, user_token)
  83. setUserId(user_id)
  84. setToken('userid', user_id)
  85. resolve(true)
  86. } else {
  87. reject(false)
  88. }
  89. })
  90. .catch(error => {
  91. reject(error)
  92. })
  93. })
  94. },
  95. getMenuInfo ({ commit }) {
  96. return new Promise((resolve, reject) => {
  97. Query({
  98. serviceId: SERVICE_ID.sysAuthId,
  99. dataContent: [{ user_id: getToken('userid') }],
  100. page: 1,
  101. pageSize: 999,
  102. event: '0'
  103. })
  104. .then((res) => {
  105. if (res.code == 0 && isValue(res.returnData)) {
  106. commit('SET_ROLES', ['admin'])
  107. resolve(res.returnData)
  108. } else {
  109. reject(res.returnData)
  110. }
  111. })
  112. .catch((error) => {
  113. reject(error)
  114. })
  115. })
  116. },
  117. // user logout
  118. logout ({ commit }) {
  119. return new Promise((resolve, reject) => {
  120. loginOut({
  121. user_id: getToken('userid'),
  122. user_token: getToken(TokenKey)
  123. }).then(result => {
  124. if (result) {
  125. sessionStorage.clear()
  126. removeToken() // must remove token first
  127. removeToken('codeToken')
  128. removeToken('systemSet')
  129. removeToken('userid')
  130. resetRouter()
  131. commit('RESET_STATE')
  132. commit('SET_ROLES', [])
  133. resolve(true)
  134. } else {
  135. reject(false)
  136. }
  137. })
  138. })
  139. },
  140. // remove token
  141. resetToken ({ commit }) {
  142. return new Promise(resolve => {
  143. removeToken() // must remove token first
  144. commit('RESET_STATE')
  145. commit('SET_ROLES', [])
  146. resolve()
  147. })
  148. },
  149. setPowerList ({ commit }, arr) {
  150. commit('SET_UserPoewrList', arr)
  151. },
  152. setLoginList ({ commit }, arr) {
  153. commit('SET_UserLoginList', arr)
  154. },
  155. setLoginState ({ commit }, arr) {
  156. commit('SET_UserLoginState', arr)
  157. },
  158. setPowerToken ({ commit }, token) {
  159. commit('SET_TOKEN', token)
  160. },
  161. setPowerName ({ commit }, name) {
  162. commit('SET_NAME', name)
  163. },
  164. setIsLogin ({ commit }, login) {
  165. commit('SET_LOGIN', login)
  166. },
  167. setUserChecked ({ commit }, flag) {
  168. commit('SET_USER_CHECKED', flag)
  169. }
  170. }
  171. export default {
  172. namespaced: true,
  173. state,
  174. mutations,
  175. actions
  176. }