user.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. loading: false,
  19. },
  20. mutations: {
  21. //tf作为主动控制的参数
  22. switch_loading(state,tf){
  23. /* if(tf){
  24. state.loading = tf;
  25. }else{
  26. state.loading = !state.loading
  27. } */
  28. state.loading = tf;
  29. },
  30. SET_TOKEN: (state, token) => {
  31. state.token = token
  32. },
  33. SET_NAME: (state, name) => {
  34. state.name = name
  35. storage.set(constant.name, name)
  36. },
  37. SET_AVATAR: (state, avatar) => {
  38. state.avatar = avatar
  39. storage.set(constant.avatar, avatar)
  40. },
  41. SET_ROLES: (state, roles) => {
  42. state.roles = roles
  43. storage.set(constant.roles, roles)
  44. },
  45. SET_PERMISSIONS: (state, permissions) => {
  46. state.permissions = permissions
  47. storage.set(constant.permissions, permissions)
  48. },
  49. SET_WGTCODE: (state, wgtcode) => {
  50. state.wgtcode = wgtcode
  51. storage.set(constant.wgtcode, wgtcode)
  52. },
  53. SET_AUTOLOGIN: (state, autologin) => {
  54. state.autologin = autologin
  55. storage.set(constant.autologin, autologin)
  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 userId = (user == null || user.userId == "" || user.userId == null) ? "" : user.userId
  119. const phonenumber = (user == null || user.phonenumber == "" || user.phonenumber == null) ? "" : user.phonenumber
  120. if (res.roles && res.roles.length > 0) {
  121. commit('SET_ROLES', res.roles)
  122. commit('SET_PERMISSIONS', res.permissions)
  123. } else {
  124. commit('SET_ROLES', ['ROLE_DEFAULT'])
  125. }
  126. commit('SET_NAME', username)
  127. commit('SET_AVATAR', avatar)
  128. commit('SET_USERID', userId)
  129. commit('SET_PHONENUMBER', phonenumber)
  130. resolve(res)
  131. }).catch(error => {
  132. reject(error)
  133. })
  134. })
  135. },
  136. // 退出系统
  137. LogOut({ commit, state }) {
  138. return new Promise((resolve, reject) => {
  139. logout(state.token).then(() => {
  140. commit('SET_TOKEN', '')
  141. commit('SET_ROLES', [])
  142. commit('SET_PERMISSIONS', [])
  143. removeToken()
  144. storage.clean()
  145. resolve()
  146. }).catch(error => {
  147. reject(error)
  148. })
  149. })
  150. }
  151. }
  152. }
  153. export default user