user.js 3.2 KB

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