user.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. import { UserState, UserForm } from '@/types/store'
  7. import { Module } from 'vuex'
  8. import WebSocketManager from '@/utils/WebSocketManager.js';
  9. const baseUrl = config.baseUrl
  10. const user: Module<UserState, UserState> = {
  11. state: {
  12. token: getToken(),
  13. name: storage.get(constant.name),
  14. avatar: storage.get(constant.avatar),
  15. roles: storage.get(constant.roles),
  16. permissions: storage.get(constant.permissions),
  17. userOrWorker: storage.get(constant.userOrWorker),
  18. nickName: storage.get(constant.nickName),
  19. userId: storage.get(constant.userId),
  20. messageCount: 0,
  21. wsManager: null,
  22. addressInfo: {
  23. name: '重庆市永川区',
  24. latitude: 29.333000000000002,
  25. longitude: 105.94909000000001,
  26. cityCode: {
  27. code: [500000, 500100, 500118],
  28. data: ['重庆市', '重庆市', '永川区'],
  29. index: [21, 0, 17],
  30. },
  31. }
  32. },
  33. mutations: {
  34. SET_TOKEN: (state, token: string) => {
  35. state.token = token
  36. },
  37. SET_NAME: (state, name: string) => {
  38. state.name = name
  39. storage.set(constant.name, name)
  40. },
  41. SET_AVATAR: (state, avatar: string) => {
  42. state.avatar = avatar
  43. storage.set(constant.avatar, avatar)
  44. },
  45. SET_ROLES: (state, roles: Array<string>) => {
  46. state.roles = roles
  47. storage.set(constant.roles, roles)
  48. },
  49. SET_PERMISSIONS: (state, permissions: Array<string>) => {
  50. state.permissions = permissions
  51. storage.set(constant.permissions, permissions)
  52. },
  53. SET_USERORWORKER: (state, userOrWorker: Array<string>) => {
  54. state.userOrWorker = userOrWorker
  55. storage.set(constant.userOrWorker, userOrWorker)
  56. },
  57. SET_NICKNAME: (state, nickName: String) => {
  58. state.nickName = nickName
  59. storage.set(constant.nickName, nickName)
  60. },
  61. SET_USERID: (state, id: String) => {
  62. state.userId = id
  63. },
  64. SET_MESSAGECOUNT: (state, num: Number) => {
  65. console.log("TCL: num", num)
  66. state.messageCount = num
  67. },
  68. SET_MESSAGE: (state, ms:any) => {
  69. state.wsManager = ms
  70. },
  71. SET_ADDRESSINFO: (state, data:any) => {
  72. state.addressInfo = data
  73. },
  74. },
  75. actions: {
  76. // 登录
  77. Login({ commit }, userInfo: UserForm) {
  78. const username = userInfo.username
  79. const password = userInfo.password
  80. const code = userInfo.code
  81. const uuid = userInfo.uuid
  82. const referrerType = userInfo.referrerType
  83. const referrerId = userInfo.referrerId
  84. return new Promise((resolve, reject) => {
  85. login(username, password, code, uuid, referrerType, referrerId).then((res: any) => {
  86. setToken(res.token)
  87. commit('SET_TOKEN', res.token)
  88. resolve(res)
  89. }).catch(error => {
  90. reject(error)
  91. })
  92. })
  93. },
  94. // 获取用户信息
  95. GetInfo({ commit, state }) {
  96. return new Promise((resolve, reject) => {
  97. getInfo().then((res: any) => {
  98. const user = res.user
  99. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? "/static/serverImg/mine/user.png" : user.avatar
  100. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  101. if (res.roles && res.roles.length > 0) {
  102. commit('SET_ROLES', res.roles)
  103. commit('SET_PERMISSIONS', res.permissions)
  104. } else {
  105. commit('SET_ROLES', ['ROLE_DEFAULT'])
  106. }
  107. console.log(res.user.userOrWorker, '>>>>>>res.user.userOrWorker');
  108. if (res.user) commit('SET_USERORWORKER', res.user.userOrWorker)
  109. console.log(state.userOrWorker, '>>>>>>');
  110. commit('SET_NAME', username)
  111. commit('SET_AVATAR', avatar)
  112. commit('SET_NICKNAME', res.user.nickName)
  113. commit('SET_USERID', res.user.userId)
  114. if (this.wsManager) {
  115. this.wsManager.closeConnection();
  116. }else{
  117. //获取账户时,连接soket
  118. const ms = new WebSocketManager(res.user.userId);
  119. // 设置消息回调
  120. ms.onMessage(data => {
  121. console.log("处理消息逻辑", data.data)
  122. // 处理消息逻辑
  123. if (data.type === 'msgUnreadCount') {
  124. commit('SET_MESSAGECOUNT', data.data);
  125. }
  126. });
  127. // 建立连接
  128. ms.connect();
  129. commit('SET_MESSAGE', ms)
  130. }
  131. resolve(res)
  132. }).catch(error => {
  133. reject(error)
  134. })
  135. })
  136. },
  137. // 退出系统
  138. LogOut({ commit, state }) {
  139. return new Promise((resolve, reject) => {
  140. logout().then((res) => {
  141. commit('SET_TOKEN', '')
  142. commit('SET_ROLES', [])
  143. commit('SET_PERMISSIONS', [])
  144. removeToken()
  145. uni.setStorageSync('userType', 1)
  146. // uni.setStorageSync('userId', null)
  147. uni.removeStorageSync('userId')
  148. storage.clean()
  149. resolve(res)
  150. }).catch(error => {
  151. reject(error)
  152. })
  153. })
  154. },
  155. handleMessageCount({ commit, state }, count) {
  156. commit('SET_MESSAGECOUNT', count);
  157. },
  158. handlerAddress({ commit, state }, address) {
  159. commit('SET_ADDRESSINFO', address);
  160. },
  161. }
  162. }
  163. export default user