user.ts 4.6 KB

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