user.js 5.9 KB

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