magic-online.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div class="magic-online">
  3. <magic-avatar-group :users="users" :max="9"/>
  4. <span>{{ $i('online.onlines', users.length) }}</span>
  5. </div>
  6. </template>
  7. <script setup>
  8. import { inject, reactive } from 'vue'
  9. import bus from '../../../scripts/bus'
  10. import constants from '../../../scripts/constants.js'
  11. import $i from '../../../scripts/i18n.js'
  12. import Socket from '../../../scripts/constants/socket.js'
  13. import Message from '../../../scripts/constants/message.js'
  14. const users = reactive([])
  15. const activateUserFiles = inject('activateUserFiles')
  16. const processFiles = (cid, fileId) => {
  17. const user = users.find(it => it.cid === cid)
  18. if(user){
  19. Object.values(activateUserFiles.value).forEach(values => {
  20. const index = values.findIndex(it => it.cid === cid)
  21. if(index > -1){
  22. values.splice(index, 1)
  23. }
  24. })
  25. activateUserFiles.value[fileId] = activateUserFiles.value[fileId] || []
  26. activateUserFiles.value[fileId].push(user)
  27. }
  28. }
  29. const addUser = user => {
  30. if(!users.some(it => it.cid === user.cid)){
  31. users.push(user)
  32. }
  33. }
  34. let timer = null
  35. bus.$event(Socket.LOGIN_RESPONSE, ([ret, user]) => {
  36. activateUserFiles.value = {}
  37. users.splice(0, users.length)
  38. if(timer !== null){
  39. clearInterval(timer)
  40. }
  41. if(ret === '1') {
  42. timer = setInterval(() => bus.send(Socket.PING, new Date().getTime()), 10000)
  43. addUser(user)
  44. } else if (ret === '-1'){
  45. }
  46. })
  47. bus.$event(Socket.USER_LOGIN, ([user]) => {
  48. if(constants.CLIENT_ID !== user.cid ){
  49. bus.$emit(Message.NOTIFY, {
  50. title: $i('online.login'),
  51. content: $i('online.loginTips', user.username, user.ip),
  52. duration: 3000
  53. })
  54. bus.status('online.loginTips', true, user.username, user.ip)
  55. }
  56. addUser(user)
  57. })
  58. bus.$event(Socket.USER_LOGOUT, ([user]) => {
  59. if(constants.CLIENT_ID !== user.cid ){
  60. bus.$emit(Message.NOTIFY, {
  61. title: $i('online.logout'),
  62. content: $i('online.logoutTips', user.username, user.ip),
  63. duration: 3000
  64. })
  65. bus.status('online.logoutTips', true, user.username, user.ip)
  66. }
  67. const index = users.findIndex(it => it.cid === user.cid)
  68. processFiles(user.cid, '0')
  69. if(index > -1){
  70. users.splice(index, 1)
  71. }
  72. })
  73. bus.$event(Socket.ONLINE_USERS, ([ rows ]) => {
  74. rows.forEach(user => {
  75. if(constants.CLIENT_ID !== user.cid){
  76. addUser(user)
  77. user.fileId && processFiles(user.cid, user.fileId)
  78. }
  79. })
  80. })
  81. bus.$event(Socket.INTO_FILE_ID, ([cid, fileId]) => processFiles(cid, fileId))
  82. </script>
  83. <style scoped>
  84. .magic-online{
  85. height: 30px;
  86. line-height: 30px;
  87. float: right;
  88. padding-left: 10px;
  89. max-width: 500px;
  90. }
  91. .magic-online > span{
  92. padding-left: 5px;
  93. }
  94. </style>