user.js 3.8 KB

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