user.js 5.4 KB

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