user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 acctID = userInfo.acctID
  62. const lcid = '2052'
  63. return new Promise((resolve, reject) => {
  64. login(username, password, acctID,lcid).then(res => {
  65. console.log(res, 'asfasfasf')
  66. console.log(res.IsSuccessByAPI, 'asfasfasf')
  67. // if (res.Code == "0000") {
  68. // setToken(res.Result.UserKey)
  69. // setServerUrl(FacadeName)
  70. // setUserGuid(res.Result.UserGuid)
  71. // uni.setStorageSync('Username', res.Result[0].Username)
  72. // commit('SET_TOKEN', res.Result.UserKey)
  73. // resolve()
  74. // } else {
  75. // reject(res.message)
  76. // return
  77. // }
  78. if (res.IsSuccessByAPI) {
  79. // setToken(res.Result.UserKey)
  80. // setServerUrl(FacadeName)
  81. // setUserGuid(res.Result.UserGuid)
  82. // uni.setStorageSync('Username', res.Result[0].Username)
  83. // commit('SET_TOKEN', res.Result.UserKey)
  84. resolve()
  85. } else {
  86. reject(res.Message)
  87. return
  88. }
  89. }).catch(error => {
  90. console.log(error)
  91. reject(error)
  92. })
  93. })
  94. },
  95. // 获取用户信息
  96. GetInfo({
  97. commit,
  98. state
  99. }) {
  100. return new Promise((resolve, reject) => {
  101. getInfo().then(res => {
  102. const user = res.data
  103. // const avatar = ""//(user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
  104. // const UserName = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  105. // if (res.roles && res.roles.length > 0) {
  106. // commit('SET_ROLES', res.roles)
  107. // commit('SET_PERMISSIONS', res.permissions)
  108. // } else {
  109. // commit('SET_ROLES', ['ROLE_DEFAULT'])
  110. // }
  111. // commit('SET_NAME', UserName)
  112. // commit('SET_AVATAR', avatar)
  113. setAdminType(user.adminType)
  114. commit('SET_ADMINTYPE', user.adminType)
  115. resolve(res)
  116. }).catch(error => {
  117. reject(error)
  118. })
  119. })
  120. },
  121. // 退出系统
  122. LogOut({
  123. commit,
  124. state
  125. }) {
  126. return new Promise((resolve, reject) => {
  127. logout(state.token).then(() => {
  128. commit('SET_TOKEN', '')
  129. commit('SET_ROLES', [])
  130. commit('SET_PERMISSIONS', [])
  131. removeToken()
  132. removeAdminType()
  133. storage.clean()
  134. resolve()
  135. }).catch(error => {
  136. reject(error)
  137. })
  138. })
  139. }
  140. }
  141. }
  142. export default user