user.js 4.4 KB

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