user.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import Vue from 'vue'
  2. import { login, getLoginUser, logout } from '@/api/modular/system/loginManage'
  3. import { sysDictTypeTree } from '@/api/modular/system/dictManage'
  4. import { sysMenuChange } from '@/api/modular/system/menuManage'
  5. import { sysUserUpdatePwd } from '@/api/modular/system/userManage'
  6. import { ACCESS_TOKEN, ALL_APPS_MENU, DICT_TYPE_TREE_DATA } from '@/store/mutation-types'
  7. import { welcome } from '@/utils/util'
  8. import store from '../index'
  9. import router from '../../router'
  10. const user = {
  11. state: {
  12. token: '',
  13. name: '',
  14. welcome: '',
  15. avatar: '',
  16. buttons: [], // 按钮权限
  17. admintype: '', // 是否是超管
  18. roles: [],
  19. info: {}
  20. },
  21. mutations: {
  22. SET_TOKEN: (state, token) => {
  23. state.token = token
  24. },
  25. SET_NAME: (state, { name, welcome }) => {
  26. state.name = name
  27. state.welcome = welcome
  28. },
  29. SET_AVATAR: (state, avatar) => {
  30. state.avatar = avatar
  31. },
  32. SET_ROLES: (state, roles) => {
  33. state.roles = roles
  34. },
  35. SET_INFO: (state, info) => {
  36. state.info = info
  37. },
  38. SET_BUTTONS: (state, buttons) => {
  39. state.buttons = buttons
  40. },
  41. SET_ADMINTYPE: (state, admintype) => {
  42. state.admintype = admintype
  43. }
  44. },
  45. actions: {
  46. // 登录
  47. Login ({ commit }, userInfo) {
  48. return new Promise((resolve, reject) => {
  49. login(userInfo).then(response => {
  50. if (!response.success) {
  51. reject(response.message)
  52. return
  53. }
  54. const result = response.data
  55. Vue.ls.set(ACCESS_TOKEN, result, 7 * 24 * 60 * 60 * 1000)
  56. commit('SET_TOKEN', result)
  57. resolve()
  58. }).catch(error => {
  59. reject(error)
  60. })
  61. })
  62. },
  63. // 获取用户信息
  64. GetInfo ({ commit }) {
  65. return new Promise((resolve, reject) => {
  66. getLoginUser().then(response => {
  67. if (response.success) {
  68. const data = response.data
  69. commit('SET_ADMINTYPE', data.adminType)
  70. commit('SET_ROLES', 1)
  71. commit('SET_BUTTONS', data.permissions)
  72. commit('SET_INFO', data)
  73. commit('SET_NAME', { name: data.name, welcome: welcome() })
  74. commit('SET_AVATAR', data.avatar)
  75. resolve(data)
  76. } else {
  77. // eslint-disable-next-line no-undef
  78. reject(new Error(data.message))
  79. }
  80. }).catch(error => {
  81. reject(error)
  82. })
  83. })
  84. },
  85. // 登出
  86. Logout ({ commit, state }) {
  87. return new Promise((resolve) => {
  88. logout(state.token).then(() => {
  89. resolve()
  90. }).catch(() => {
  91. resolve()
  92. }).finally(() => {
  93. commit('SET_TOKEN', '')
  94. commit('SET_ROLES', [])
  95. commit('SET_BUTTONS', [])
  96. commit('SET_ADMINTYPE', '')
  97. Vue.ls.remove(ACCESS_TOKEN)
  98. Vue.ls.remove(ALL_APPS_MENU)
  99. Vue.ls.remove(DICT_TYPE_TREE_DATA)
  100. })
  101. })
  102. },
  103. // 加载所有字典数据
  104. dictTypeData () {
  105. return new Promise((resolve, reject) => {
  106. sysDictTypeTree().then((data) => {
  107. if (data.success) {
  108. const result = data.data
  109. Vue.ls.set(DICT_TYPE_TREE_DATA, result)
  110. resolve()
  111. } else {
  112. // eslint-disable-next-line no-undef
  113. reject(new Error(data.message))
  114. }
  115. }).catch(error => {
  116. reject(error)
  117. })
  118. })
  119. },
  120. // 切换应用菜单
  121. MenuChange ({ commit }, application) {
  122. return new Promise((resolve) => {
  123. sysMenuChange({ application: application.code }).then((res) => {
  124. const apps = { 'code': '', 'name': '', 'active': '', 'menu': '' }
  125. apps.active = true
  126. apps.menu = res.data
  127. // eslint-disable-next-line camelcase
  128. const all_app_menu = Vue.ls.get(ALL_APPS_MENU)
  129. // eslint-disable-next-line camelcase
  130. const new_false_all_app_menu = []
  131. // 先去除所有默认的,以为此时切换的即将成为前端缓存默认的应用
  132. all_app_menu.forEach(item => {
  133. if (item.active) {
  134. item.active = false
  135. }
  136. new_false_all_app_menu.push(item)
  137. })
  138. // 此时缓存中全部都是不默认的应用
  139. Vue.ls.set(ALL_APPS_MENU, new_false_all_app_menu)
  140. apps.name = application.name
  141. apps.code = application.code
  142. const applocationR = []
  143. applocationR.push(apps)
  144. Vue.ls.set(ALL_APPS_MENU, applocationR)
  145. resolve(res)
  146. const antDesignmenus = res.data
  147. store.dispatch('GenerateRoutes', { antDesignmenus }).then(() => {
  148. router.addRoutes(store.getters.addRouters)
  149. })
  150. // 切换应用刷新整体界面,暂且取消
  151. // window.location.reload()
  152. }).catch(() => {
  153. resolve()
  154. })
  155. })
  156. },
  157. // 修改密码
  158. UpdatePwd ({ commit }, passwords) {
  159. return new Promise((resolve, reject) => {
  160. sysUserUpdatePwd(passwords).then((res) => {
  161. resolve(res)
  162. }).catch(() => {
  163. resolve()
  164. })
  165. })
  166. }
  167. }
  168. }
  169. export default user