ConversationUtil.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import StorageUtil from './StorageUtil';
  2. import TimeUtil from './TimeUtil';
  3. import Utils from './Utils';
  4. import UserInfoUtil from './UserInfoUtil';
  5. // 会话工具列
  6. export default class ConversationUtil {
  7. // 获取所有会话,username为null的话是获取所有的会话,username为当前登录用户,可以获取与该用户有关的所有会话
  8. static getConversations(username, callback) {
  9. StorageUtil.get('conversations', (error, object) => {
  10. if (!error && object && object.conversations) {
  11. let result = object.conversations;
  12. if (username != null) {
  13. result = object.conversations.filter(function (con) {
  14. return Utils.endWith(con.conversationId, username) || Utils.startWith(con.conversationId, username);
  15. });
  16. }
  17. callback && callback(result);
  18. } else {
  19. let conversations = [];
  20. StorageUtil.set('conversations', {'conversations': conversations});
  21. callback(conversations);
  22. }
  23. })
  24. }
  25. static getConversation(conversationId, callback) {
  26. this.getConversations(null, (result) => {
  27. let conversation = this.isConversationExists(result, conversationId);
  28. callback(conversation);
  29. });
  30. }
  31. // 根据用户名生成会话id,规则是将较小的用户名排在前面
  32. static generateConversationId(username1, username2) {
  33. if (username1 < username2) {
  34. return username1 + username2;
  35. }
  36. return username2 + username1;
  37. }
  38. // 判断某个会话是否存在
  39. static isConversationExists(conversations, conversationId) {
  40. for (let i = 0; i < conversations.length; i++) {
  41. if (conversations[i].conversationId == conversationId) {
  42. return conversations[i];
  43. }
  44. }
  45. return null;
  46. }
  47. static showConversations() {
  48. this.getConversations(null, (result) => {
  49. console.log('-----------show conversations-----------')
  50. console.log(result);
  51. console.log('-----------show conversations-----------')
  52. })
  53. }
  54. // 添加一条message
  55. static addMessage(message, callback) {
  56. let conversationId = message.conversationId;
  57. this.getConversations(null, (result) => {
  58. let conversation = this.isConversationExists(result, conversationId);
  59. if (conversation) {
  60. if (conversation.unreadCount) {
  61. conversation.unreadCount = conversation.unreadCount + 1;
  62. } else {
  63. conversation.unreadCount = 1;
  64. }
  65. conversation.messages.push(message);
  66. conversation.lastTime = TimeUtil.currentTime();
  67. } else {
  68. conversation = {
  69. 'conversationId': conversationId,
  70. 'messages': [message],
  71. 'lastTime': TimeUtil.currentTime(),
  72. 'avatar': null,
  73. 'unreadCount': 1
  74. };
  75. result.push(conversation);
  76. }
  77. result.sort(function (a, b) {
  78. if (a.lastTime > b.lastTime) {
  79. return -1;
  80. } else if (a.lastTime < b.lastTime) {
  81. return 1;
  82. }
  83. return 0;
  84. });
  85. StorageUtil.get('username', (error, object) => {
  86. if (!error && object) {
  87. let username = object.username;
  88. let chatWithUsername = message.from;
  89. if (chatWithUsername == username) {
  90. chatWithUsername = message.to;
  91. }
  92. UserInfoUtil.getUserInfo(chatWithUsername, (userInfo) => {
  93. if (userInfo != null) {
  94. conversation['avatar'] = userInfo.avatar;
  95. conversation['nick'] = userInfo.nick;
  96. }
  97. StorageUtil.set('conversations', {'conversations': result}, (error) => {
  98. callback && callback(error);
  99. });
  100. })
  101. }
  102. });
  103. })
  104. }
  105. // 将conversationId对应的会话未读书置为0
  106. static clearUnreadCount(conversationId, callback) {
  107. this.getConversations(null, (conversations)=>{
  108. if (conversations) {
  109. for (let i = 0; i < conversations.length; i++) {
  110. if (conversations[i].conversationId == conversationId) {
  111. conversations[i].unreadCount = 0;
  112. break;
  113. }
  114. }
  115. StorageUtil.set('conversations', {'conversations': conversations}, ()=>{
  116. callback && callback();
  117. });
  118. }
  119. })
  120. }
  121. }