user.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. } from '@/utils/auth'
  14. import {
  15. getUserCompany
  16. } from '../../api/company/company'
  17. const baseUrl = config.baseUrl
  18. const user = {
  19. state: {
  20. token: getToken(),
  21. name: storage.get(constant.name),
  22. avatar: storage.get(constant.avatar),
  23. // roles: storage.get(constant.roles),
  24. permissions: storage.get(constant.permissions),
  25. // 企业id
  26. companyId: storage.get(constant.companyId),
  27. // 企业名
  28. companyName: storage.get(constant.companyName),
  29. // 企业简称
  30. companyAbbreviation:storage.get(constant.companyAbbreviation),
  31. // 账号
  32. userName: storage.get(constant.userName),
  33. // mt_company
  34. //权限 角色标识(企业账号:mt_company;驾驶员账号:mt_driver)
  35. roles: storage.get(constant.roles),
  36. //权限名
  37. rolesName:storage.get(constant.rolesName),
  38. //司机姓名
  39. nickName:storage.get(constant.nickName),
  40. },
  41. mutations: {
  42. SET_NICKNAME: (state, nickName) => {
  43. state.nickName = nickName
  44. storage.set(constant.nickName, nickName)
  45. },
  46. SET_ROLESNAME: (state, rolesName) => {
  47. state.rolesName = rolesName
  48. storage.set(constant.rolesName, rolesName)
  49. },
  50. SET_USERNAME: (state, userName) => {
  51. state.userName = userName
  52. storage.set(constant.userName, userName)
  53. },
  54. SET_COMPANYABBREVIATION: (state, companyAbbreviation) => {
  55. state.companyAbbreviation = companyAbbreviation
  56. storage.set(constant.companyAbbreviation, companyAbbreviation)
  57. },
  58. SET_COMPANYID: (state, companyId) => {
  59. state.companyId = companyId
  60. storage.set(constant.companyId, companyId)
  61. },
  62. SET_COMPANYNAME: (state, companyName) => {
  63. state.companyName = companyName
  64. storage.set(constant.companyName, companyName)
  65. },
  66. SET_TOKEN: (state, token) => {
  67. state.token = token
  68. },
  69. SET_NAME: (state, name) => {
  70. state.name = name
  71. storage.set(constant.name, name)
  72. },
  73. SET_AVATAR: (state, avatar) => {
  74. state.avatar = avatar
  75. storage.set(constant.avatar, avatar)
  76. },
  77. SET_ROLES: (state, roles) => {
  78. state.roles = roles
  79. storage.set(constant.roles, roles)
  80. },
  81. SET_PERMISSIONS: (state, permissions) => {
  82. state.permissions = permissions
  83. storage.set(constant.permissions, permissions)
  84. }
  85. },
  86. actions: {
  87. // 登录
  88. Login({
  89. commit
  90. }, userInfo) {
  91. const username = userInfo.username.trim()
  92. const password = userInfo.password
  93. const code = userInfo.code
  94. const uuid = userInfo.uuid
  95. return new Promise((resolve, reject) => {
  96. login(username, password, code, uuid).then(res => {
  97. setToken(res.data.access_token)
  98. commit('SET_TOKEN', res.data.access_token)
  99. resolve()
  100. }).catch(error => {
  101. reject(error)
  102. })
  103. })
  104. },
  105. // 获取用户信息
  106. GetInfo({
  107. commit,
  108. state
  109. }) {
  110. return new Promise((resolve, reject) => {
  111. getInfo().then(res => {
  112. console.log(res, 'res')
  113. // const user = res.user
  114. // const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
  115. // const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  116. // if (res.roles && res.roles.length > 0) {
  117. // commit('SET_ROLES', res.roles)
  118. // commit('SET_PERMISSIONS', res.permissions)
  119. // } else {
  120. // commit('SET_ROLES', ['ROLE_DEFAULT'])
  121. // }
  122. // commit('SET_NAME', username)
  123. // commit('SET_AVATAR', avatar)
  124. let name='城管';
  125. // 角色标识(企业账号:mt_company;驾驶员账号:mt_driver)
  126. if(res.data.user=='mt_company'){
  127. name='企业'
  128. }else if(res.data.user=='mt_driver'){
  129. name='司机'
  130. }
  131. commit('SET_ROLESNAME', name)
  132. commit('SET_COMPANYID', res.data.companyId)
  133. commit('SET_COMPANYNAME', res.data.companyName)
  134. commit('SET_COMPANYABBREVIATION', res.data.companyAbbreviation)
  135. commit('SET_USERNAME', res.data.userName)
  136. commit('SET_NICKNAME', res.data.nickName)
  137. resolve(res)
  138. }).catch(error => {
  139. reject(error)
  140. })
  141. })
  142. },
  143. // 退出系统
  144. LogOut({
  145. commit,
  146. state
  147. }) {
  148. return new Promise((resolve, reject) => {
  149. logout(state.token).then(() => {
  150. commit('SET_TOKEN', '')
  151. commit('SET_ROLES', [])
  152. commit('SET_PERMISSIONS', [])
  153. removeToken()
  154. storage.clean()
  155. resolve()
  156. }).catch(error => {
  157. reject(error)
  158. })
  159. })
  160. }
  161. }
  162. }
  163. export default user