user.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. },
  23. mutations: {
  24. SET_TOKEN: (state, token: string) => {
  25. state.token = token
  26. },
  27. SET_NAME: (state, name: string) => {
  28. state.name = name
  29. storage.set(constant.name, name)
  30. },
  31. SET_AVATAR: (state, avatar: string) => {
  32. state.avatar = avatar
  33. storage.set(constant.avatar, avatar)
  34. },
  35. SET_ROLES: (state, roles: Array<string>) => {
  36. state.roles = roles
  37. storage.set(constant.roles, roles)
  38. },
  39. SET_PERMISSIONS: (state, permissions: Array<string>) => {
  40. state.permissions = permissions
  41. storage.set(constant.permissions, permissions)
  42. },
  43. SET_USERORWORKER: (state, userOrWorker: Array<string>) => {
  44. state.userOrWorker = userOrWorker
  45. storage.set(constant.userOrWorker, userOrWorker)
  46. },
  47. SET_NICKNAME: (state, nickName: String) => {
  48. state.nickName = nickName
  49. storage.set(constant.nickName, nickName)
  50. },
  51. SET_USERID: (state, id: String) => {
  52. state.userId = id
  53. },
  54. SET_MESSAGECOUNT: (state, num: Number) => {
  55. state.messageCount = num
  56. },
  57. SET_MESSAGE: (state, ms:any) => {
  58. state.wsManager = ms
  59. },
  60. },
  61. actions: {
  62. // 登录
  63. Login({ commit }, userInfo: UserForm) {
  64. const username = userInfo.username
  65. const password = userInfo.password
  66. const code = userInfo.code
  67. const uuid = userInfo.uuid
  68. const referrerType = userInfo.referrerType
  69. const referrerId = userInfo.referrerId
  70. return new Promise((resolve, reject) => {
  71. login(username, password, code, uuid, referrerType, referrerId).then((res: any) => {
  72. setToken(res.token)
  73. commit('SET_TOKEN', res.token)
  74. resolve(res)
  75. }).catch(error => {
  76. reject(error)
  77. })
  78. })
  79. },
  80. // 获取用户信息
  81. GetInfo({ commit, state }) {
  82. return new Promise((resolve, reject) => {
  83. getInfo().then((res: any) => {
  84. const user = res.user
  85. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? "/static/serverImg/mine/user.png" : user.avatar
  86. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  87. if (res.roles && res.roles.length > 0) {
  88. commit('SET_ROLES', res.roles)
  89. commit('SET_PERMISSIONS', res.permissions)
  90. } else {
  91. commit('SET_ROLES', ['ROLE_DEFAULT'])
  92. }
  93. console.log(res.user.userOrWorker, '>>>>>>res.user.userOrWorker');
  94. if (res.user) commit('SET_USERORWORKER', res.user.userOrWorker)
  95. console.log(state.userOrWorker, '>>>>>>');
  96. commit('SET_NAME', username)
  97. commit('SET_AVATAR', avatar)
  98. commit('SET_NICKNAME', res.user.nickName)
  99. commit('SET_USERID', res.user.userId)
  100. console.log("TCL: GetInfo -> wsManager1", this.wsManager)
  101. if (this.wsManager) {
  102. this.wsManager.closeConnection();
  103. }else{
  104. //获取账户时,连接soket
  105. const ms = new WebSocketManager(res.user.userId);
  106. // 设置消息回调
  107. ms.onMessage(data => {
  108. // 处理消息逻辑
  109. if (data.type === 'msgUnreadCount') {
  110. commit('SET_MESSAGECOUNT', data.data);
  111. }
  112. });
  113. // 建立连接
  114. ms.connect();
  115. commit('SET_MESSAGE', ms)
  116. }
  117. resolve(res)
  118. }).catch(error => {
  119. reject(error)
  120. })
  121. })
  122. },
  123. // 退出系统
  124. LogOut({ commit, state }) {
  125. return new Promise((resolve, reject) => {
  126. logout().then((res) => {
  127. commit('SET_TOKEN', '')
  128. commit('SET_ROLES', [])
  129. commit('SET_PERMISSIONS', [])
  130. removeToken()
  131. uni.setStorageSync('userType', 1)
  132. // uni.setStorageSync('userId', null)
  133. uni.removeStorageSync('userId')
  134. storage.clean()
  135. resolve(res)
  136. }).catch(error => {
  137. reject(error)
  138. })
  139. })
  140. },
  141. handleMessageCount({ commit, state }, count) {
  142. commit('SET_MESSAGECOUNT', count);
  143. },
  144. }
  145. }
  146. export default user