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. wgtcode:storage.get(constant.wgtcode),
  15. autologin:storage.get(constant.autologin),
  16. deptId: storage.get(constant.deptId),
  17. deptName: storage.get(constant.deptName),
  18. userId: storage.get(constant.userId),
  19. phonenumber: storage.get(constant.phonenumber),
  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_WGTCODE: (state, wgtcode) => {
  42. state.wgtcode = wgtcode
  43. storage.set(constant.wgtcode, wgtcode)
  44. },
  45. SET_AUTOLOGIN: (state, autologin) => {
  46. state.autologin = autologin
  47. storage.set(constant.autologin, autologin)
  48. },
  49. SET_DEPID: (state, deptId) => {
  50. state.deptId = deptId
  51. storage.set(constant.deptId, deptId)
  52. },
  53. SET_DEPTNAME: (state, deptName) => {
  54. state.deptName = deptName
  55. storage.set(constant.deptName, deptName)
  56. },
  57. SET_USERID: (state, userId) => {
  58. state.userId = userId
  59. storage.set(constant.userId, userId)
  60. },
  61. SET_PHONENUMBER: (state, phonenumber) => {
  62. state.phonenumber = phonenumber
  63. storage.set(constant.phonenumber, phonenumber)
  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 uuid = userInfo.uuid
  77. const strfrom = userInfo.strfrom||""
  78. return new Promise((resolve, reject) => {
  79. login(username, password, code, uuid).then(res => {
  80. setToken(res.token)
  81. commit('SET_TOKEN', res.token)
  82. commit('SET_AUTOLOGIN',true)
  83. resolve()
  84. }).catch(error => {
  85. if(error==500&&strfrom=='request'){
  86. uni.hideLoading()
  87. // 清空数据
  88. uni.showModal({
  89. title: '提示',
  90. content: '登录状态已过期,您可以继续留在该页面,或者重新登录?',
  91. cancelText: '取消',
  92. confirmText: '确定',
  93. success: function(res) {
  94. if (res.confirm) {
  95. commit('SET_TOKEN', '')
  96. commit('SET_ROLES', [])
  97. commit('SET_PERMISSIONS', [])
  98. removeToken()
  99. storage.clean()
  100. uni.reLaunch({ url: '/pages/login' })
  101. }else{
  102. commit('SET_AUTOLOGIN',false)
  103. }
  104. }
  105. })
  106. }
  107. reject(error)
  108. })
  109. })
  110. },
  111. // 获取用户信息
  112. GetInfo({ commit, state }) {
  113. return new Promise((resolve, reject) => {
  114. getInfo().then(res => {
  115. const user = res.user
  116. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
  117. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  118. const deptId = (user == null || user.deptId == "" || user.deptId == null) ? "" : user.deptId
  119. const deptName = (user == null ||user.dept==null|| user.dept == ""||user.dept.deptName == "" || user.dept.deptName == null) ? "" : user.dept.deptName
  120. const userId = (user == null || user.userId == "" || user.userId == null) ? "" : user.userId
  121. const phonenumber = (user == null || user.phonenumber == "" || user.phonenumber == null) ? "" : user.phonenumber
  122. if (res.roles && res.roles.length > 0) {
  123. commit('SET_ROLES', res.roles)
  124. commit('SET_PERMISSIONS', res.permissions)
  125. } else {
  126. commit('SET_ROLES', ['ROLE_DEFAULT'])
  127. }
  128. commit('SET_NAME', username)
  129. commit('SET_AVATAR', avatar)
  130. commit('SET_DEPID', deptId)
  131. commit('SET_DEPTNAME', deptName)
  132. commit('SET_USERID', userId)
  133. commit('SET_PHONENUMBER', phonenumber)
  134. resolve(res)
  135. }).catch(error => {
  136. reject(error)
  137. })
  138. })
  139. },
  140. // 退出系统
  141. LogOut({ commit, state }) {
  142. return new Promise((resolve, reject) => {
  143. logout(state.token).then(() => {
  144. commit('SET_TOKEN', '')
  145. commit('SET_ROLES', [])
  146. commit('SET_PERMISSIONS', [])
  147. removeToken()
  148. storage.clean()
  149. resolve()
  150. }).catch(error => {
  151. reject(error)
  152. })
  153. })
  154. }
  155. }
  156. }
  157. export default user