No Description

WsApi.ts 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { WebSocketClient } from './../WebSocketClient';
  2. import { MsgBase, GroupOption } from '../protocol/MsgBase';
  3. import { CmdMsg } from '../protocol/CmdMsg';
  4. /** WebSocket Api */
  5. export class WsApi extends WebSocketClient {
  6. // 保存公共的状态信息
  7. token: string;
  8. from: string;
  9. async sendMsg(msg: Partial<MsgBase>) {
  10. return this.sendMessage(msg);
  11. }
  12. /** @start 好友管理 */
  13. // 通用查询
  14. async sendGetCmdMsg(action: string, extra: {} = {}) {
  15. const msg = CmdMsg.createMsg({ from: this.from, action, ...extra });
  16. return this.sendMsg(msg);
  17. }
  18. // 通用操作
  19. async sendPostCmdMsg(action: string, userIds: string[] | null, extra: {} = {}) {
  20. const msg = CmdMsg.createMsg({
  21. from: this.from,
  22. to: (userIds || []).join(','),
  23. action,
  24. ...extra
  25. });
  26. return this.sendMsg(msg);
  27. }
  28. // 查询好友列表
  29. getRoster() {
  30. return this.sendGetCmdMsg('getRoster');
  31. }
  32. // 添加好友
  33. addFriends(userIds: string[]) {
  34. return this.sendPostCmdMsg('addFriends', userIds);
  35. }
  36. // 同意好友请求
  37. agreeFriends(userIds: string[]) {
  38. return this.sendPostCmdMsg('agreeFriends', userIds);
  39. }
  40. // 拒绝好友请求
  41. rejectFriends(userIds: string[]) {
  42. return this.sendPostCmdMsg('rejectFriends', userIds);
  43. }
  44. // 移除
  45. removeFriends(userIds: string[]) {
  46. return this.sendPostCmdMsg('removeFriends', userIds);
  47. }
  48. // 获取黑名单列表
  49. getBlacklist() {
  50. return this.sendGetCmdMsg('getBlacklist');
  51. }
  52. // 加入黑名单
  53. addToBlackList(userIds: string[]) {
  54. return this.sendPostCmdMsg('addToBlackList', userIds);
  55. }
  56. // 移出黑名单
  57. removeBlackList(userIds: string[]) {
  58. return this.sendPostCmdMsg('removeBlackList', userIds);
  59. }
  60. /** @end 好友管理 */
  61. /** @start 会话管理 */
  62. // 删除会话
  63. deleteConversation(contactId: string, deleteMessage: boolean = false) {
  64. return this.request('/v1/delete/conversation', {
  65. contact: contactId,
  66. delete_message: deleteMessage
  67. });
  68. }
  69. /** @end 会话管理 */
  70. /** @start 群组管理 */
  71. // 列出所有的群组
  72. listGroups() {
  73. return this.sendGetCmdMsg('listGroups');
  74. }
  75. // 获取群组信息
  76. queryGroupInfo(roomId: string) {
  77. return this.sendGetCmdMsg('queryGroupInfo', { to: roomId });
  78. }
  79. // 查询群组的成员
  80. queryRoomMember(roomId: string) {
  81. return this.sendGetCmdMsg('queryRoomMember', { to: roomId });
  82. }
  83. // 获取群组黑名单
  84. getGroupBlackList(roomId: string) {
  85. return this.sendGetCmdMsg('getGroupBlackList', { to: roomId });
  86. }
  87. // 建立群组
  88. createGroup(groupOption: GroupOption) {
  89. return this.sendPostCmdMsg('createGroup', null, { ext: groupOption });
  90. }
  91. // 更新群组
  92. changeGroupInfo(groupOption: GroupOption) {
  93. return this.sendPostCmdMsg('changeGroupInfo', null, { ext: groupOption });
  94. }
  95. // 将好友加入群组
  96. addGroupMembers(roomId: string, userIds: string[]) {
  97. return this.sendPostCmdMsg('addGroupMembers', [roomId], {
  98. ext: {
  99. members: userIds
  100. }
  101. });
  102. }
  103. // 将成员踢出群组
  104. addToGroupBlackList(roomId: string, userIds: string[]) {
  105. return this.sendPostCmdMsg('addToGroupBlackList', [roomId], {
  106. ext: {
  107. members: userIds
  108. }
  109. });
  110. }
  111. // 解散群组
  112. destroyGroup(roomId: string) {
  113. return this.sendPostCmdMsg('destroyGroup', [roomId]);
  114. }
  115. // 退出群组
  116. leaveGroup(roomId: string) {
  117. return this.sendPostCmdMsg('leaveGroup', [roomId]);
  118. }
  119. /** @end 群组管理 */
  120. // 创建聊天室群组
  121. createRoom(groupOption: GroupOption) {
  122. return this.sendPostCmdMsg('createGroup', null, { ext: groupOption });
  123. }
  124. // 列出所有的聊天室
  125. listRooms() {
  126. return this.sendGetCmdMsg('listRooms');
  127. }
  128. // 退出聊天室
  129. joinRoom(roomId: string) {
  130. return this.sendPostCmdMsg('joinRoom', [roomId]);
  131. }
  132. // 退出聊天室
  133. quitRoom(roomId: string) {
  134. return this.sendPostCmdMsg('quitRoom', [roomId]);
  135. }
  136. /** @start 聊天室管理 */
  137. /** @end 聊天室管理 */
  138. }