No Description

WsApi.ts 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, delete_message: boolean = false) {
  64. return this.request('/v1/delete/conversation', { contact: contactId, delete_message });
  65. }
  66. /** @end 会话管理 */
  67. /** @start 群组管理 */
  68. // 列出所有的群组
  69. listGroups() {
  70. return this.sendGetCmdMsg('listGroups');
  71. }
  72. // 获取群组信息
  73. queryGroupInfo(roomId: string) {
  74. return this.sendGetCmdMsg('queryGroupInfo', { to: roomId });
  75. }
  76. // 查询群组的成员
  77. queryRoomMember(roomId: string) {
  78. return this.sendGetCmdMsg('queryRoomMember', { to: roomId });
  79. }
  80. // 获取群组黑名单
  81. getGroupBlackList(roomId: string) {
  82. return this.sendGetCmdMsg('getGroupBlackList', { to: roomId });
  83. }
  84. // 建立群组
  85. createGroup(groupOption: GroupOption) {
  86. return this.sendPostCmdMsg('createGroup', null, { ext: groupOption });
  87. }
  88. // 更新群组
  89. changeGroupInfo(groupOption: GroupOption) {
  90. return this.sendPostCmdMsg('changeGroupInfo', null, { ext: groupOption });
  91. }
  92. // 将好友加入群组
  93. addGroupMembers(roomId: string, userIds: string[]) {
  94. return this.sendPostCmdMsg('addGroupMembers', [roomId], {
  95. ext: {
  96. members: userIds
  97. }
  98. });
  99. }
  100. // 将成员踢出群组
  101. addToGroupBlackList(roomId: string, userIds: string[]) {
  102. return this.sendPostCmdMsg('addToGroupBlackList', [roomId], {
  103. ext: {
  104. members: userIds
  105. }
  106. });
  107. }
  108. // 解散群组
  109. destroyGroup(roomId: string) {
  110. return this.sendPostCmdMsg('destroyGroup', [roomId]);
  111. }
  112. // 退出群组
  113. leaveGroup(roomId: string) {
  114. return this.sendPostCmdMsg('leaveGroup', [roomId]);
  115. }
  116. /** @end 群组管理 */
  117. // 创建聊天室群组
  118. createRoom(groupOption: GroupOption) {
  119. return this.sendPostCmdMsg('createGroup', null, { ext: groupOption });
  120. }
  121. // 列出所有的聊天室
  122. listRooms() {
  123. return this.sendGetCmdMsg('listRooms');
  124. }
  125. // 退出聊天室
  126. joinRoom(roomId: string) {
  127. return this.sendPostCmdMsg('joinRoom', [roomId]);
  128. }
  129. // 退出聊天室
  130. quitRoom(roomId: string) {
  131. return this.sendPostCmdMsg('quitRoom', [roomId]);
  132. }
  133. /** @start 聊天室管理 */
  134. /** @end 聊天室管理 */
  135. }