user.js 4.4 KB

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