user.js 6.0 KB

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