user.js 4.9 KB

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