user.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. //司机ID
  41. userId: storage.get(constant.userId),
  42. //任务ID
  43. driverId: storage.get(constant.driverId),
  44. //登录账号
  45. loginName: storage.get(constant.loginName),
  46. //登录密码
  47. loginPassWord: storage.get(constant.loginPassWord),
  48. loginArr: storage.get(constant.loginArr) ? storage.get(constant.loginArr) : []
  49. },
  50. mutations: {
  51. SET_LOGINARR: (state, item) => {
  52. let index = state.loginArr.findIndex(cur => cur.username == item.username)
  53. index < 0 ? state.loginArr.push(item) : state.loginArr.splice(index, 1, item)
  54. if (state.loginArr.length > 3) {
  55. state.loginArr.splice(0, 1)
  56. }
  57. storage.set(constant.loginArr, state.loginArr)
  58. },
  59. SET_LOGINNAME: (state, loginName) => {
  60. state.loginName = loginName
  61. storage.set(constant.loginName, loginName)
  62. },
  63. SET_LOGINPASSWORD: (state, loginPassWord) => {
  64. state.loginPassWord = loginPassWord
  65. storage.set(constant.loginPassWord, loginPassWord)
  66. },
  67. SET_DRIVERID: (state, driverId) => {
  68. state.driverId = driverId
  69. storage.set(constant.driverId, driverId)
  70. },
  71. SET_USERID: (state, userId) => {
  72. state.userId = userId
  73. storage.set(constant.userId, userId)
  74. },
  75. SET_NICKNAME: (state, nickName) => {
  76. state.nickName = nickName
  77. storage.set(constant.nickName, nickName)
  78. },
  79. SET_ROLESNAME: (state, rolesName) => {
  80. state.rolesName = rolesName
  81. storage.set(constant.rolesName, rolesName)
  82. },
  83. SET_USERNAME: (state, userName) => {
  84. state.userName = userName
  85. storage.set(constant.userName, userName)
  86. },
  87. SET_COMPANYABBREVIATION: (state, companyAbbreviation) => {
  88. state.companyAbbreviation = companyAbbreviation
  89. storage.set(constant.companyAbbreviation, companyAbbreviation)
  90. },
  91. SET_COMPANYID: (state, companyId) => {
  92. state.companyId = companyId
  93. storage.set(constant.companyId, companyId)
  94. },
  95. SET_COMPANYNAME: (state, companyName) => {
  96. state.companyName = companyName
  97. storage.set(constant.companyName, companyName)
  98. },
  99. SET_TOKEN: (state, token) => {
  100. state.token = token
  101. },
  102. SET_NAME: (state, name) => {
  103. state.name = name
  104. storage.set(constant.name, name)
  105. },
  106. SET_AVATAR: (state, avatar) => {
  107. state.avatar = avatar
  108. storage.set(constant.avatar, avatar)
  109. },
  110. SET_ROLES: (state, roles) => {
  111. state.roles = roles
  112. storage.set(constant.roles, roles)
  113. },
  114. SET_PERMISSIONS: (state, permissions) => {
  115. state.permissions = permissions
  116. storage.set(constant.permissions, permissions)
  117. }
  118. },
  119. actions: {
  120. // 登录
  121. Login({
  122. commit
  123. }, userInfo) {
  124. const username = userInfo.username.trim()
  125. const password = userInfo.password
  126. const code = userInfo.code
  127. const uuid = userInfo.uuid
  128. return new Promise((resolve, reject) => {
  129. login(username, password, code, uuid).then(res => {
  130. console.log('登录', res);
  131. if (res.data) {
  132. setToken(res.data.access_token)
  133. commit('SET_TOKEN', res.data.access_token)
  134. }
  135. commit('SET_LOGINPASSWORD', userInfo.password)
  136. commit('SET_LOGINNAME', userInfo.username.trim())
  137. resolve(res)
  138. }).catch(error => {
  139. reject(error)
  140. })
  141. })
  142. },
  143. // 获取用户信息
  144. GetInfo({
  145. commit,
  146. state
  147. }) {
  148. return new Promise((resolve, reject) => {
  149. getInfo().then(res => {
  150. console.log(res, 'res')
  151. // const user = res.user
  152. // const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
  153. // const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  154. // if (res.roles && res.roles.length > 0) {
  155. // commit('SET_ROLES', res.roles)
  156. // commit('SET_PERMISSIONS', res.permissions)
  157. // } else {
  158. // commit('SET_ROLES', ['ROLE_DEFAULT'])
  159. // }
  160. // commit('SET_NAME', username)
  161. // commit('SET_AVATAR', avatar)
  162. let name = '城管';
  163. // 角色标识(企业账号:mt_company;驾驶员账号:mt_driver)
  164. if (res.data.roles == 'mt_company') {
  165. name = '企业'
  166. } else if (res.data.roles == 'mt_driver') {
  167. name = '司机'
  168. }
  169. commit('SET_ROLESNAME', name)
  170. commit('SET_USERID', res.data.userId)
  171. commit('SET_COMPANYID', res.data.companyId)
  172. commit('SET_COMPANYNAME', res.data.companyName)
  173. commit('SET_COMPANYABBREVIATION', res.data.companyAbbreviation)
  174. commit('SET_USERNAME', res.data.userName)
  175. commit('SET_NICKNAME', res.data.nickName)
  176. resolve(res)
  177. }).catch(error => {
  178. reject(error)
  179. })
  180. })
  181. },
  182. // 退出系统
  183. LogOut({
  184. commit,
  185. state
  186. }) {
  187. return new Promise((resolve, reject) => {
  188. logout(state.token).then(() => {
  189. commit('SET_TOKEN', '')
  190. commit('SET_ROLES', [])
  191. commit('SET_PERMISSIONS', [])
  192. commit('SET_ROLESNAME', '')
  193. commit('SET_USERID', '')
  194. commit('SET_COMPANYID', '')
  195. commit('SET_COMPANYNAME', '')
  196. commit('SET_COMPANYABBREVIATION', '')
  197. commit('SET_USERNAME', '')
  198. commit('SET_NICKNAME', '')
  199. commit('SET_DRIVERID', '')
  200. removeToken()
  201. storage.clean()
  202. resolve()
  203. }).catch(error => {
  204. reject(error)
  205. })
  206. })
  207. }
  208. }
  209. }
  210. export default user