user.js 5.0 KB

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