message.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { defineStore } from "pinia";
  2. import { Toast, format, setStorage, getStorage } from '@/utils';
  3. import { RONGIMCALL_OPEN, RONGIMCALL_CLOSE, RONGIMCALL_HISTORY, RONGIMCALL_SEND } from '@/api';
  4. import { useSocketStore } from "@/store";
  5. export const useMessageStore = defineStore("chat_message", {
  6. state: () => ({
  7. globalMap: {
  8. serviceChannel: {
  9. type: 'service',
  10. unreadCount: 0,
  11. lastTime: getStorage('serviceChannel') ? getStorage('serviceChannel') : ''
  12. },
  13. noticeChannel: {
  14. type: 'notice',
  15. unreadCount: 0,
  16. lastTime: getStorage('noticeChannel') ? getStorage('noticeChannel') : ''
  17. }
  18. },
  19. messageMap: {},
  20. userSessionChannel: ''
  21. }),
  22. getters: {
  23. userSessionChannelInfo() {
  24. if (!this.userSessionChannel) return null;
  25. return this.messageMap[this.userSessionChannel];
  26. },
  27. },
  28. actions: {
  29. initMessageMap(key, data = {}) {
  30. if (!key) return;
  31. this.messageMap[key] = {
  32. messageList: [],
  33. unreadCount: 0,
  34. openInfo: {
  35. ...data
  36. }
  37. }
  38. },
  39. updateStatus(key, state) {
  40. if (!key || !(state >= 0)) return;
  41. if (!this.messageMap[key]) return;
  42. this.messageMap[key].openInfo.status = state;
  43. },
  44. /**
  45. * @description 更新未读数量
  46. * @param {Number} count 为 -1 时重置未读数量
  47. */
  48. updateUnreadCount(key, count = 1) {
  49. if (!key) return;
  50. if (!this.messageMap[key]) return;
  51. if (count > 0) {
  52. this.messageMap[key].unreadCount += count
  53. }
  54. else if (count == -1) {
  55. this.messageMap[key].unreadCount = 0
  56. }
  57. },
  58. updateGlobalMapUnreadCount(key, count = 1) {
  59. if (!key) return;
  60. if (!this.globalMap[key]) return;
  61. if (count > 0) {
  62. this.globalMap[key].unreadCount += count
  63. }
  64. else if (count == -1) {
  65. this.globalMap[key].unreadCount = 0
  66. }
  67. this.getTotalUnreadCount()
  68. },
  69. updateGlobalMapLasttime(key, timestamp) {
  70. if (!key) return;
  71. if (!this.globalMap[key]) return;
  72. if (timestamp >= 0) {
  73. this.globalMap[key].lastTime = timestamp
  74. setStorage(key, timestamp)
  75. }
  76. else if (count == -1) {
  77. this.globalMap[key].lastTime = 0
  78. }
  79. },
  80. /**
  81. * @description 将 globalMap 中所有渠道的未读数量重置为 0
  82. */
  83. resetAllGlobalUnreadCount() {
  84. Object.keys(this.globalMap).forEach(key => {
  85. if (this.globalMap[key] && typeof this.globalMap[key].unreadCount === 'number') {
  86. this.globalMap[key].unreadCount = 0; // 重置为 0
  87. }
  88. });
  89. this.getTotalUnreadCount();
  90. },
  91. getTotalUnreadCount() {
  92. let total = 0;
  93. for (const key in this.globalMap) {
  94. if (this.globalMap[key].unreadCount) {
  95. total += this.globalMap[key].unreadCount;
  96. }
  97. }
  98. // #ifdef APP-PLUS
  99. plus.runtime.setBadgeNumber(total);
  100. // #endif
  101. return total;
  102. },
  103. /**
  104. * @description 更新消息列表
  105. * @param {string} key
  106. * @param {string} type 'push' | 'unshift'
  107. */
  108. updateMessageList(key, data, type = 'unshift') {
  109. if (!key || !data) return;
  110. if (!this.messageMap[key]) return;
  111. if (type == 'push') {
  112. this.messageMap[key].messageList.push(data)
  113. }
  114. else {
  115. this.messageMap[key].messageList.unshift(data)
  116. }
  117. },
  118. openChannel(query = {}) {
  119. return new Promise((resolve, reject) => {
  120. RONGIMCALL_OPEN(query).then(result => {
  121. const { data } = result;
  122. this.userSessionChannel = data.auth.channel;
  123. this.initMessageMap(data.auth.channel, data);
  124. const useScoket = useSocketStore();
  125. useScoket.send({
  126. event: "pusher:subscribe",
  127. data: data.auth
  128. })
  129. resolve(result);
  130. })
  131. .catch(err => {
  132. Toast(err.msg);
  133. reject(err);
  134. })
  135. })
  136. },
  137. closeChannel(query = {}) {
  138. return new Promise((resolve, reject) => {
  139. RONGIMCALL_CLOSE(query).then(result => {
  140. const { data } = result;
  141. resolve(result);
  142. })
  143. .catch(err => {
  144. Toast(err.msg);
  145. reject(err);
  146. })
  147. })
  148. },
  149. sendMessage(data, type = 'service', other = {}) {
  150. return new Promise(async (resolve, reject) => {
  151. try {
  152. const result = await RONGIMCALL_SEND({
  153. type: type,
  154. msg: JSON.stringify(data),
  155. ...other
  156. });
  157. resolve(result.data);
  158. }
  159. catch (err) {
  160. Toast(err.msg);
  161. reject(err);
  162. }
  163. })
  164. }
  165. }
  166. })