user.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. initFace:storage.get(constant.initFace),
  26. },
  27. mutations: {
  28. SET_TOKEN: (state, token) => {
  29. state.token = token
  30. },
  31. SET_NAME: (state, name) => {
  32. state.name = name
  33. storage.set(constant.name, name)
  34. },
  35. SET_AVATAR: (state, avatar) => {
  36. state.avatar = avatar
  37. storage.set(constant.avatar, avatar)
  38. },
  39. SET_ROLES: (state, roles) => {
  40. state.roles = roles
  41. storage.set(constant.roles, roles)
  42. },
  43. SET_PERMISSIONS: (state, permissions) => {
  44. state.permissions = permissions
  45. storage.set(constant.permissions, permissions)
  46. },
  47. SET_USERID: (state, userId) => {
  48. state.userId = userId
  49. storage.set(constant.userId, userId)
  50. },
  51. SET_NICKNAME: (state, nickName) => {
  52. state.nickName = nickName
  53. storage.set(constant.nickName, nickName)
  54. },
  55. SET_DEPTID: (state, deptId) => {
  56. state.deptId = deptId
  57. storage.set(constant.deptId, deptId)
  58. },
  59. SET_DEPNAME: (state, deptName) => {
  60. state.deptName = deptName
  61. storage.set(constant.deptName, deptName)
  62. },
  63. SET_WGTCODE: (state, wgtcode) => {
  64. state.wgtcode = wgtcode
  65. storage.set(constant.wgtcode, wgtcode)
  66. },
  67. SET_AUTOLOGIN: (state, autologin) => {
  68. state.autologin = autologin
  69. storage.set(constant.autologin, autologin)
  70. },
  71. SET_PHONENUMBER: (state, phonenumber) => {
  72. state.phonenumber = phonenumber
  73. storage.set(constant.phonenumber, phonenumber)
  74. },
  75. switch_loading(state,tf){
  76. /* if(tf){
  77. state.loading = tf;
  78. }else{
  79. state.loading = !state.loading
  80. } */
  81. state.loading = tf;
  82. },
  83. SET_INITFACE: (state, initFace) => {
  84. state.initFace = initFace
  85. storage.set(constant.initFace, initFace)
  86. },
  87. },
  88. actions: {
  89. // 版本号
  90. SetwgtFn({ commit}, code ){
  91. commit('SET_WGTCODE',code)
  92. },
  93. //修改认证状态
  94. checkInitFace({commit},data){
  95. commit('SET_INITFACE', data)
  96. },
  97. // 登录
  98. Login({ commit }, userInfo) {
  99. const username = userInfo.username.trim()
  100. const password = userInfo.password
  101. const code = userInfo.code
  102. const uuid = userInfo.uuid
  103. const type = userInfo.type
  104. return new Promise((resolve, reject) => {
  105. login(username, password, code, uuid,type).then(res => {
  106. setToken(res.token)
  107. commit('SET_TOKEN', res.token)
  108. resolve()
  109. }).catch(error => {
  110. reject(error)
  111. })
  112. })
  113. },
  114. // 获取用户信息
  115. GetInfo({ commit, state }) {
  116. return new Promise((resolve, reject) => {
  117. getInfo().then(res => {
  118. const user = res.user
  119. let avatar = user.avatar || ""
  120. if (!isHttp(avatar)) {
  121. avatar = (isEmpty(avatar)) ? defAva : baseUrl + avatar
  122. }
  123. const userid = (isEmpty(user) || isEmpty(user.userId)) ? "" : user.userId
  124. const username = (isEmpty(user) || isEmpty(user.userName)) ? "" : user.userName
  125. const nickName = (user == null || user.nickName == "" || user.nickName == null) ? "" : user.nickName
  126. const phonenumber = (user == null || user.phonenumber == "" || user.phonenumber == null) ? "" : user.phonenumber
  127. const deptId = (user == null || user.deptId == "" || user.deptId == null) ? "" : user.deptId
  128. const deptName = (user == null || user.dept == "" || user.dept == null||user.dept.deptName==''||user.dept.deptName==null) ? "" : user.dept.deptName
  129. if (res.roles && res.roles.length > 0) {
  130. commit('SET_ROLES', res.roles)
  131. commit('SET_PERMISSIONS', res.permissions)
  132. } else {
  133. commit('SET_ROLES', ['ROLE_DEFAULT'])
  134. }
  135. commit('SET_USERID', userid)
  136. commit('SET_NAME', username)
  137. commit('SET_AVATAR', avatar)
  138. commit('SET_NICKNAME', nickName)
  139. commit('SET_PHONENUMBER', phonenumber)
  140. commit('SET_DEPTID', deptId)
  141. commit('SET_DEPNAME', deptName)
  142. commit('SET_AUTOLOGIN',true)
  143. resolve(res)
  144. }).catch(error => {
  145. reject(error)
  146. })
  147. })
  148. },
  149. // 退出系统
  150. LogOut({ commit, state }) {
  151. return new Promise((resolve, reject) => {
  152. logout(state.token).then(() => {
  153. commit('SET_TOKEN', '')
  154. commit('SET_ROLES', [])
  155. commit('SET_PERMISSIONS', [])
  156. removeToken()
  157. storage.clean()
  158. resolve()
  159. }).catch(error => {
  160. reject(error)
  161. })
  162. })
  163. }
  164. }
  165. }
  166. export default user