user.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import { isHttp, isEmpty } from "@/utils/validate"
  5. import { login, logout, getInfo } from '@/api/login'
  6. import { getToken, setToken, removeToken } from '@/utils/auth'
  7. import defAva from '@/static/images/profile.jpg'
  8. const baseUrl = config.baseUrl
  9. const user = {
  10. state: {
  11. token: getToken(),
  12. id: storage.get(constant.id),
  13. name: storage.get(constant.name),
  14. avatar: storage.get(constant.avatar),
  15. roles: storage.get(constant.roles),
  16. permissions: storage.get(constant.permissions),
  17. userId: storage.get(constant.userId),
  18. nickName: storage.get(constant.nickName),
  19. wgtcode:storage.get(constant.wgtcode),
  20. autologin:storage.get(constant.autologin),
  21. phonenumber: storage.get(constant.phonenumber),
  22. deptId: storage.get(constant.deptId),
  23. deptName: storage.get(constant.deptName),
  24. loading: false,
  25. },
  26. mutations: {
  27. SET_TOKEN: (state, token) => {
  28. state.token = token
  29. },
  30. SET_NAME: (state, name) => {
  31. state.name = name
  32. storage.set(constant.name, name)
  33. },
  34. SET_AVATAR: (state, avatar) => {
  35. state.avatar = avatar
  36. storage.set(constant.avatar, avatar)
  37. },
  38. SET_ROLES: (state, roles) => {
  39. state.roles = roles
  40. storage.set(constant.roles, roles)
  41. },
  42. SET_PERMISSIONS: (state, permissions) => {
  43. state.permissions = permissions
  44. storage.set(constant.permissions, permissions)
  45. },
  46. SET_USERID: (state, userId) => {
  47. state.userId = userId
  48. storage.set(constant.userId, userId)
  49. },
  50. SET_NICKNAME: (state, nickName) => {
  51. state.nickName = nickName
  52. storage.set(constant.nickName, nickName)
  53. },
  54. SET_DEPTID: (state, deptId) => {
  55. state.deptId = deptId
  56. storage.set(constant.deptId, deptId)
  57. },
  58. SET_DEPNAME: (state, deptName) => {
  59. state.deptName = deptName
  60. storage.set(constant.deptName, deptName)
  61. },
  62. SET_WGTCODE: (state, wgtcode) => {
  63. state.wgtcode = wgtcode
  64. storage.set(constant.wgtcode, wgtcode)
  65. },
  66. SET_AUTOLOGIN: (state, autologin) => {
  67. state.autologin = autologin
  68. storage.set(constant.autologin, autologin)
  69. },
  70. SET_PHONENUMBER: (state, phonenumber) => {
  71. state.phonenumber = phonenumber
  72. storage.set(constant.phonenumber, phonenumber)
  73. },
  74. switch_loading(state,tf){
  75. /* if(tf){
  76. state.loading = tf;
  77. }else{
  78. state.loading = !state.loading
  79. } */
  80. state.loading = tf;
  81. },
  82. },
  83. actions: {
  84. // 版本号
  85. SetwgtFn({ commit}, code ){
  86. commit('SET_WGTCODE',code)
  87. },
  88. // 登录
  89. Login({ commit }, userInfo) {
  90. const username = userInfo.username.trim()
  91. const password = userInfo.password
  92. const code = userInfo.code
  93. const uuid = userInfo.uuid
  94. return new Promise((resolve, reject) => {
  95. login(username, password, code, uuid).then(res => {
  96. setToken(res.token)
  97. commit('SET_TOKEN', res.token)
  98. resolve()
  99. }).catch(error => {
  100. reject(error)
  101. })
  102. })
  103. },
  104. // 获取用户信息
  105. GetInfo({ commit, state }) {
  106. return new Promise((resolve, reject) => {
  107. getInfo().then(res => {
  108. const user = res.user
  109. let avatar = user.avatar || ""
  110. if (!isHttp(avatar)) {
  111. avatar = (isEmpty(avatar)) ? defAva : baseUrl + avatar
  112. }
  113. const userid = (isEmpty(user) || isEmpty(user.userId)) ? "" : user.userId
  114. const username = (isEmpty(user) || isEmpty(user.userName)) ? "" : user.userName
  115. const nickName = (user == null || user.nickName == "" || user.nickName == null) ? "" : user.nickName
  116. const phonenumber = (user == null || user.phonenumber == "" || user.phonenumber == null) ? "" : user.phonenumber
  117. const deptId = (user == null || user.deptId == "" || user.deptId == null) ? "" : user.deptId
  118. const deptName = (user == null || user.dept == "" || user.dept == null||user.dept.deptName==''||user.dept.deptName==null) ? "" : user.dept.deptName
  119. if (res.roles && res.roles.length > 0) {
  120. commit('SET_ROLES', res.roles)
  121. commit('SET_PERMISSIONS', res.permissions)
  122. } else {
  123. commit('SET_ROLES', ['ROLE_DEFAULT'])
  124. }
  125. commit('SET_USERID', userid)
  126. commit('SET_NAME', username)
  127. commit('SET_AVATAR', avatar)
  128. commit('SET_NICKNAME', nickName)
  129. commit('SET_PHONENUMBER', phonenumber)
  130. commit('SET_DEPTID', deptId)
  131. commit('SET_DEPNAME', deptName)
  132. commit('SET_AUTOLOGIN',true)
  133. resolve(res)
  134. }).catch(error => {
  135. reject(error)
  136. })
  137. })
  138. },
  139. // 退出系统
  140. LogOut({ commit, state }) {
  141. return new Promise((resolve, reject) => {
  142. logout(state.token).then(() => {
  143. commit('SET_TOKEN', '')
  144. commit('SET_ROLES', [])
  145. commit('SET_PERMISSIONS', [])
  146. removeToken()
  147. storage.clean()
  148. resolve()
  149. }).catch(error => {
  150. reject(error)
  151. })
  152. })
  153. }
  154. }
  155. }
  156. export default user