user.js 5.5 KB

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