friendStore.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { defineStore } from 'pinia';
  2. import http from '../common/request'
  3. import { TERMINAL_TYPE } from '../common/enums.js'
  4. export default defineStore('friendStore', {
  5. state: () => {
  6. return {
  7. friends: [],
  8. timer: null
  9. }
  10. },
  11. actions: {
  12. setFriends(friends) {
  13. friends.forEach((f) => {
  14. f.online = false;
  15. f.onlineWeb = false;
  16. f.onlineApp = false;
  17. })
  18. this.friends = friends;
  19. },
  20. updateFriend(friend) {
  21. let f = this.findFriend(friend.id);
  22. let copy = JSON.parse(JSON.stringify(f));
  23. Object.assign(f, friend);
  24. f.online = copy.online;
  25. f.onlineWeb = copy.onlineWeb;
  26. f.onlineApp = copy.onlineApp;
  27. },
  28. removeFriend(id) {
  29. this.friends.filter(f => f.id == id).forEach(f => f.deleted = true);
  30. },
  31. addFriend(friend) {
  32. if (this.friends.some((f) => f.id == friend.id)) {
  33. this.updateFriend(friend)
  34. } else {
  35. this.friends.unshift(friend);
  36. }
  37. },
  38. setOnlineStatus(onlineTerminals) {
  39. this.friends.forEach((f) => {
  40. let userTerminal = onlineTerminals.find((o) => f.id == o.userId);
  41. if (userTerminal) {
  42. f.online = true;
  43. f.onlineWeb = userTerminal.terminals.indexOf(TERMINAL_TYPE.WEB) >= 0
  44. f.onlineApp = userTerminal.terminals.indexOf(TERMINAL_TYPE.APP) >= 0
  45. } else {
  46. f.online = false;
  47. f.onlineWeb = false;
  48. f.onlineApp = false;
  49. }
  50. });
  51. },
  52. refreshOnlineStatus() {
  53. let userIds = this.friends.filter((f) => !f.deleted).map((f) => f.id);
  54. if (userIds.length == 0) {
  55. return;
  56. }
  57. http({
  58. url: '/user/terminal/online?userIds=' + userIds.join(','),
  59. method: 'GET'
  60. }).then((onlineTerminals) => {
  61. this.setOnlineStatus(onlineTerminals);
  62. })
  63. // 30s后重新拉取
  64. clearTimeout(this.timer);
  65. this.timer = setTimeout(() => {
  66. this.refreshOnlineStatus();
  67. }, 30000)
  68. },
  69. setDnd(id, isDnd) {
  70. let friend = this.findFriend(id);
  71. friend.isDnd = isDnd;
  72. },
  73. clear() {
  74. clearTimeout(this.timer);
  75. this.friends = [];
  76. this.timer = null;
  77. },
  78. loadFriend() {
  79. return new Promise((resolve, reject) => {
  80. http({
  81. url: '/friend/list',
  82. method: 'GET'
  83. }).then((friends) => {
  84. this.setFriends(friends);
  85. this.refreshOnlineStatus();
  86. resolve()
  87. }).catch((res) => {
  88. reject();
  89. })
  90. });
  91. }
  92. },
  93. getters: {
  94. isFriend: (state) => (userId) => {
  95. return state.friends.filter((f) => !f.deleted).some((f) => f.id == userId);
  96. },
  97. findFriend: (state) => (id) => {
  98. return state.friends.find((f) => f.id == id);
  99. }
  100. }
  101. })