user.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import { login, logout, getInfo } from '@/api/login'
  5. import { getToken, setToken, removeToken,getAdminType,setAdminType,removeAdminType } from '@/utils/auth'
  6. const baseUrl = config.baseUrl
  7. const user = {
  8. state: {
  9. token: getToken(),
  10. name: storage.get(constant.name),
  11. avatar: storage.get(constant.avatar),
  12. roles: storage.get(constant.roles),
  13. permissions: storage.get(constant.permissions),
  14. adminType: getAdminType(),
  15. },
  16. mutations: {
  17. SET_TOKEN: (state, token) => {
  18. state.token = token
  19. },
  20. SET_NAME: (state, name) => {
  21. state.name = name
  22. storage.set(constant.name, name)
  23. },
  24. SET_AVATAR: (state, avatar) => {
  25. state.avatar = avatar
  26. storage.set(constant.avatar, avatar)
  27. },
  28. SET_ROLES: (state, roles) => {
  29. state.roles = roles
  30. storage.set(constant.roles, roles)
  31. },
  32. SET_PERMISSIONS: (state, permissions) => {
  33. state.permissions = permissions
  34. storage.set(constant.permissions, permissions)
  35. },
  36. SET_ADMINTYPE:(state, adminType) =>{
  37. state.adminType = adminType
  38. }
  39. },
  40. actions: {
  41. // 登录
  42. Login({ commit }, userInfo) {
  43. const username = userInfo.username.trim()
  44. const password = userInfo.password
  45. return new Promise((resolve, reject) => {
  46. login(username, password).then(res => {
  47. if(res.success){
  48. setToken(res.data)
  49. commit('SET_TOKEN', res.data)
  50. resolve()
  51. }else{
  52. reject(res.message)
  53. return
  54. }
  55. }).catch(error => {
  56. reject('用户名或密码错误!')
  57. })
  58. })
  59. },
  60. // 获取用户信息
  61. GetInfo({ commit, state }) {
  62. return new Promise((resolve, reject) => {
  63. getInfo().then(res => {
  64. const user = res.data
  65. // const avatar = ""//(user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
  66. // const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  67. // if (res.roles && res.roles.length > 0) {
  68. // commit('SET_ROLES', res.roles)
  69. // commit('SET_PERMISSIONS', res.permissions)
  70. // } else {
  71. // commit('SET_ROLES', ['ROLE_DEFAULT'])
  72. // }
  73. // commit('SET_NAME', username)
  74. // commit('SET_AVATAR', avatar)
  75. setAdminType(user.adminType)
  76. commit('SET_ADMINTYPE',user.adminType)
  77. resolve(res)
  78. }).catch(error => {
  79. reject(error)
  80. })
  81. })
  82. },
  83. // 退出系统
  84. LogOut({ commit, state }) {
  85. return new Promise((resolve, reject) => {
  86. logout(state.token).then(() => {
  87. commit('SET_TOKEN', '')
  88. commit('SET_ROLES', [])
  89. commit('SET_PERMISSIONS', [])
  90. removeToken()
  91. removeAdminType()
  92. storage.clean()
  93. resolve()
  94. }).catch(error => {
  95. reject(error)
  96. })
  97. })
  98. }
  99. }
  100. }
  101. export default user