user.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. return new Promise((resolve, reject) => {
  69. login(username, password, code, uuid).then((res: any) => {
  70. setToken(res.token)
  71. commit('SET_TOKEN', res.token)
  72. resolve(res)
  73. }).catch(error => {
  74. reject(error)
  75. })
  76. })
  77. },
  78. // 获取用户信息
  79. GetInfo({ commit, state }) {
  80. return new Promise((resolve, reject) => {
  81. getInfo().then((res: any) => {
  82. const user = res.user
  83. const avatar = (user == null || user.avatar == "" || user.avatar == null) ? "/static/serverImg/mine/user.png" : user.avatar
  84. const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
  85. if (res.roles && res.roles.length > 0) {
  86. commit('SET_ROLES', res.roles)
  87. commit('SET_PERMISSIONS', res.permissions)
  88. } else {
  89. commit('SET_ROLES', ['ROLE_DEFAULT'])
  90. }
  91. console.log(res.user.userOrWorker, '>>>>>>res.user.userOrWorker');
  92. if (res.user) commit('SET_USERORWORKER', res.user.userOrWorker)
  93. console.log(state.userOrWorker, '>>>>>>');
  94. commit('SET_NAME', username)
  95. commit('SET_AVATAR', avatar)
  96. commit('SET_NICKNAME', res.user.nickName)
  97. commit('SET_USERID', res.user.userId)
  98. console.log("TCL: GetInfo -> wsManager1", this.wsManager)
  99. if (this.wsManager) {
  100. this.wsManager.closeConnection();
  101. }else{
  102. //获取账户时,连接soket
  103. const ms = new WebSocketManager(res.user.userId);
  104. // 设置消息回调
  105. ms.onMessage(data => {
  106. // 处理消息逻辑
  107. if (data.type === 'msgUnreadCount') {
  108. commit('SET_MESSAGECOUNT', data.data);
  109. }
  110. });
  111. // 建立连接
  112. ms.connect();
  113. commit('SET_MESSAGE', ms)
  114. }
  115. resolve(res)
  116. }).catch(error => {
  117. reject(error)
  118. })
  119. })
  120. },
  121. // 退出系统
  122. LogOut({ commit, state }) {
  123. return new Promise((resolve, reject) => {
  124. logout().then((res) => {
  125. commit('SET_TOKEN', '')
  126. commit('SET_ROLES', [])
  127. commit('SET_PERMISSIONS', [])
  128. removeToken()
  129. uni.setStorageSync('userType', 1)
  130. // uni.setStorageSync('userId', null)
  131. uni.removeStorageSync('userId')
  132. storage.clean()
  133. resolve(res)
  134. }).catch(error => {
  135. reject(error)
  136. })
  137. })
  138. },
  139. handleMessageCount({ commit, state }, count) {
  140. commit('SET_MESSAGECOUNT', count);
  141. },
  142. }
  143. }
  144. export default user