user.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. console.log('getUserId()', getUserId());
  88. getInfo({
  89. id: getUserId()
  90. }).then(res => {
  91. console.log(res, 'res的数据-----');
  92. const user = res.data.user
  93. console.log(user, 'user的数据--------');
  94. const avatar = (user == null || user.avatar === "" || user.avatar == null) ?
  95. `${baseUrlImg}/bianjiziliao.png`: user.avatar
  96. const nickname = (user == null || user.nickname === "" || user.nickname ==
  97. null) ? "" : user.nickname
  98. const id = (user == null || user.id === "" || user.id == null) ? "" : user.id
  99. console.log('id', id);
  100. if (res.roles && res.roles.length > 0) {
  101. commit('SET_ROLES', res.roles)
  102. commit('SET_PERMISSIONS', res.permissions)
  103. } else {
  104. commit('SET_ROLES', ['ROLE_DEFAULT'])
  105. }
  106. commit('SET_NAME', nickname)
  107. commit('SET_AVATAR', avatar)
  108. commit('SET_ID', id)
  109. // 本地存储getUserInfo 用户信息
  110. getUserInfo({
  111. id: getUserId()
  112. }).then(res => {
  113. try {
  114. uni.setStorageSync('getUserInfo_key', res.data);
  115. } catch (e) {
  116. // error
  117. console.log('本地存储失败!' + e);
  118. }
  119. // 本地存储department 部门信息
  120. streetPage({pageNo:2,pageSize:99}).then(res => {
  121. try {
  122. console.log(res,'街道信息');
  123. uni.setStorageSync('getDepartment_key', res.data.list);
  124. } catch (e) {
  125. // error
  126. console.log('本地存储失败!' + e);
  127. }
  128. })
  129. // 本地存储listSimpleDictDatas 数据字典信息
  130. listSimpleDictDatas({}).then(res => {
  131. try {
  132. uni.setStorageSync('getlistSimpleDict_key', res.data);
  133. } catch (e) {
  134. // error
  135. console.log('本地存储失败!' + e);
  136. }
  137. })
  138. // Orgid的值
  139. console.log('getOrgInfo===res========', res.data[0].orgId);
  140. const orgId = (res.data == null || res.data === "" || res.data ==
  141. undefined) ? [] : res.data.userInfo.orgId
  142. commit('SET_ORGID', orgId)
  143. resolve(res)
  144. }).catch(error => {
  145. reject(error)
  146. })
  147. resolve(res)
  148. }).catch(error => {
  149. reject(error)
  150. })
  151. })
  152. },
  153. // 退出系统
  154. LogOut({
  155. commit,
  156. state
  157. }) {
  158. return new Promise((resolve, reject) => {
  159. logout(state.token).then(() => {
  160. commit('SET_ROLES', [])
  161. commit('SET_PERMISSIONS', [])
  162. removeToken()
  163. removeUserId()
  164. storage.clean()
  165. resolve()
  166. }).catch(error => {
  167. reject(error)
  168. })
  169. })
  170. }
  171. }
  172. }
  173. export default user