user.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import {
  5. login,
  6. logout,
  7. getInfo
  8. } from '@/api/login'
  9. import {
  10. getToken,
  11. setToken,
  12. removeToken,
  13. setServerUrl,
  14. setUserGuid,
  15. getAdminType,
  16. setAdminType,
  17. removeAdminType
  18. } from '@/utils/auth'
  19. const baseUrl = config.baseUrl
  20. const user = {
  21. state: {
  22. token: getToken(),
  23. name: storage.get(constant.name),
  24. avatar: storage.get(constant.avatar),
  25. roles: storage.get(constant.roles),
  26. permissions: storage.get(constant.permissions),
  27. adminType: getAdminType(),
  28. },
  29. mutations: {
  30. SET_TOKEN: (state, token) => {
  31. state.token = token
  32. },
  33. SET_NAME: (state, name) => {
  34. state.name = name
  35. storage.set(constant.name, name)
  36. },
  37. SET_AVATAR: (state, avatar) => {
  38. state.avatar = avatar
  39. storage.set(constant.avatar, avatar)
  40. },
  41. SET_ROLES: (state, roles) => {
  42. state.roles = roles
  43. storage.set(constant.roles, roles)
  44. },
  45. SET_PERMISSIONS: (state, permissions) => {
  46. state.permissions = permissions
  47. storage.set(constant.permissions, permissions)
  48. },
  49. SET_ADMINTYPE: (state, adminType) => {
  50. state.adminType = adminType
  51. }
  52. },
  53. actions: {
  54. // 登录
  55. Login({
  56. commit
  57. }, userInfo) {
  58. console.log(userInfo)
  59. const UserName = userInfo.UserName.trim()
  60. const Password = userInfo.Password
  61. const FacadeName = userInfo.FacadeName
  62. return new Promise((resolve, reject) => {
  63. login(UserName, Password, FacadeName).then(res => {
  64. console.log(res, 'asfasfasf')
  65. if (res.Code == "0000") {
  66. setToken(res.Result.UserKey)
  67. setServerUrl(FacadeName)
  68. setUserGuid(res.Result.UserGuid)
  69. uni.setStorageSync('UserName', res.Result.UserName)
  70. commit('SET_TOKEN', res.Result.UserKey)
  71. resolve()
  72. } else {
  73. reject(res.message)
  74. return
  75. }
  76. }).catch(error => {
  77. console.log(error)
  78. reject(error)
  79. })
  80. })
  81. },
  82. // 获取用户信息
  83. GetInfo({
  84. commit,
  85. state
  86. }) {
  87. return new Promise((resolve, reject) => {
  88. getInfo().then(res => {
  89. const user = res.data
  90. // const avatar = ""//(user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
  91. // const UserName = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  92. // if (res.roles && res.roles.length > 0) {
  93. // commit('SET_ROLES', res.roles)
  94. // commit('SET_PERMISSIONS', res.permissions)
  95. // } else {
  96. // commit('SET_ROLES', ['ROLE_DEFAULT'])
  97. // }
  98. // commit('SET_NAME', UserName)
  99. // commit('SET_AVATAR', avatar)
  100. setAdminType(user.adminType)
  101. commit('SET_ADMINTYPE', user.adminType)
  102. resolve(res)
  103. }).catch(error => {
  104. reject(error)
  105. })
  106. })
  107. },
  108. // 退出系统
  109. LogOut({
  110. commit,
  111. state
  112. }) {
  113. return new Promise((resolve, reject) => {
  114. logout(state.token).then(() => {
  115. commit('SET_TOKEN', '')
  116. commit('SET_ROLES', [])
  117. commit('SET_PERMISSIONS', [])
  118. removeToken()
  119. removeAdminType()
  120. storage.clean()
  121. resolve()
  122. }).catch(error => {
  123. reject(error)
  124. })
  125. })
  126. }
  127. }
  128. }
  129. export default user