user.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import config from '@/config'
  2. import storage from '@/utils/storage'
  3. import constant from '@/utils/constant'
  4. import {
  5. login,
  6. logout,
  7. getInfo,
  8. getUserInfo,
  9. getDepartment,
  10. streetPage,//街道信息
  11. listSimpleDictDatas
  12. } from '@/api/login'
  13. import {
  14. setToken,
  15. removeToken,
  16. setUserId,
  17. removeUserId,
  18. getUserId
  19. } from '@/utils/auth'
  20. const baseUrl = config.baseUrl
  21. const baseUrlImg=config.baseUrlImg
  22. const user = {
  23. state: {
  24. id: storage.get(constant.id), // 用户编号
  25. orgId: storage.get(constant.orgId), // 单位id
  26. name: storage.get(constant.name),
  27. avatar: storage.get(constant.avatar),
  28. roles: storage.get(constant.roles),
  29. permissions: storage.get(constant.permissions)
  30. },
  31. mutations: {
  32. SET_ID: (state, id) => {
  33. state.id = id
  34. storage.set(constant.id, id)
  35. },
  36. SET_ORGID: (state, orgId) => {
  37. state.orgId = orgId
  38. storage.set(constant.orgId, orgId)
  39. },
  40. SET_NAME: (state, name) => {
  41. state.name = name
  42. storage.set(constant.name, name)
  43. },
  44. SET_AVATAR: (state, avatar) => {
  45. state.avatar = avatar
  46. storage.set(constant.avatar, avatar)
  47. },
  48. SET_ROLES: (state, roles) => {
  49. state.roles = roles
  50. storage.set(constant.roles, roles)
  51. },
  52. SET_PERMISSIONS: (state, permissions) => {
  53. state.permissions = permissions
  54. storage.set(constant.permissions, permissions)
  55. },
  56. },
  57. actions: {
  58. // 登录
  59. Login({
  60. commit
  61. }, userInfo) {
  62. const username = userInfo.username.trim()
  63. const password = userInfo.password
  64. const socialType = userInfo.socialType
  65. const socialCode = userInfo.socialCode
  66. const socialState=userInfo.socialState
  67. const captchaVerification = userInfo.captchaVerification
  68. return new Promise((resolve, reject) => {
  69. login(username, password,socialCode,socialType,socialState,captchaVerification).then(res => {
  70. res = res.data;
  71. // 设置 token
  72. setToken(res)
  73. // 设置UserId
  74. setUserId(res)
  75. resolve()
  76. }).catch(error => {
  77. reject(error)
  78. })
  79. })
  80. },
  81. // 获取用户信息
  82. GetInfo({
  83. commit,
  84. state
  85. }) {
  86. return new Promise((resolve, reject) => {
  87. getInfo({
  88. id: getUserId()
  89. }).then(res => {
  90. const user = res.data.user
  91. const avatar = (user == null || user.avatar === "" || user.avatar == null) ?
  92. `${baseUrlImg}/bianjiziliao.png`: user.avatar
  93. const nickname = (user == null || user.nickname === "" || user.nickname ==
  94. null) ? "" : user.nickname
  95. const id = (user == null || user.id === "" || user.id == null) ? "" : user.id
  96. if (res.roles && res.roles.length > 0) {
  97. commit('SET_ROLES', res.roles)
  98. commit('SET_PERMISSIONS', res.permissions)
  99. } else {
  100. commit('SET_ROLES', ['ROLE_DEFAULT'])
  101. }
  102. commit('SET_NAME', nickname)
  103. commit('SET_AVATAR', avatar)
  104. commit('SET_ID', id)
  105. // 本地存储getUserInfo 用户信息
  106. getUserInfo({
  107. id: getUserId()
  108. }).then(res => {
  109. try {
  110. uni.setStorageSync('getUserInfo_key', res.data);
  111. } catch (e) {
  112. // error
  113. }
  114. // 本地存储department 部门信息
  115. streetPage({pageNo:2,pageSize:99}).then(res => {
  116. try {
  117. uni.setStorageSync('getDepartment_key', res.data.list);
  118. } catch (e) {
  119. // error
  120. }
  121. })
  122. // 本地存储listSimpleDictDatas 数据字典信息
  123. listSimpleDictDatas({}).then(res => {
  124. try {
  125. uni.setStorageSync('getlistSimpleDict_key', res.data);
  126. } catch (e) {
  127. // error
  128. }
  129. })
  130. // Orgid的值
  131. const orgId = (res.data == null || res.data === "" || res.data ==
  132. undefined) ? [] : res.data.userInfo.orgId
  133. commit('SET_ORGID', orgId)
  134. resolve(res)
  135. }).catch(error => {
  136. reject(error)
  137. })
  138. resolve(res)
  139. }).catch(error => {
  140. reject(error)
  141. })
  142. })
  143. },
  144. // 退出系统
  145. LogOut({
  146. commit,
  147. state
  148. }) {
  149. return new Promise((resolve, reject) => {
  150. logout(state.token).then(() => {
  151. commit('SET_ROLES', [])
  152. commit('SET_PERMISSIONS', [])
  153. removeToken()
  154. removeUserId()
  155. storage.clean()
  156. resolve()
  157. }).catch(error => {
  158. reject(error)
  159. })
  160. })
  161. }
  162. }
  163. }
  164. export default user