Bläddra i källkod

chore: update

wxyyxc1992 5 år sedan
förälder
incheckning
fe9d75b845

+ 157
- 0
packages/cts-api/dist/cjs/api/WsApi.js Visa fil

@@ -0,0 +1,157 @@
1
+"use strict";
2
+var __assign = (this && this.__assign) || function () {
3
+    __assign = Object.assign || function(t) {
4
+        for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+            s = arguments[i];
6
+            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+                t[p] = s[p];
8
+        }
9
+        return t;
10
+    };
11
+    return __assign.apply(this, arguments);
12
+};
13
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+    return new (P || (P = Promise))(function (resolve, reject) {
15
+        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
18
+        step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+    });
20
+};
21
+var __generator = (this && this.__generator) || function (thisArg, body) {
22
+    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+    function verb(n) { return function (v) { return step([n, v]); }; }
25
+    function step(op) {
26
+        if (f) throw new TypeError("Generator is already executing.");
27
+        while (_) try {
28
+            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+            if (y = 0, t) op = [op[0] & 2, t.value];
30
+            switch (op[0]) {
31
+                case 0: case 1: t = op; break;
32
+                case 4: _.label++; return { value: op[1], done: false };
33
+                case 5: _.label++; y = op[1]; op = [0]; continue;
34
+                case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+                default:
36
+                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+                    if (t[2]) _.ops.pop();
41
+                    _.trys.pop(); continue;
42
+            }
43
+            op = body.call(thisArg, _);
44
+        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+    }
47
+};
48
+Object.defineProperty(exports, "__esModule", { value: true });
49
+var CmdMsg_1 = require("../protocol/CmdMsg");
50
+var WsApi = (function () {
51
+    function WsApi() {
52
+    }
53
+    WsApi.prototype.sendMsg = function (msg) {
54
+        return __awaiter(this, void 0, void 0, function () {
55
+            return __generator(this, function (_a) {
56
+                console.log(msg);
57
+                return [2];
58
+            });
59
+        });
60
+    };
61
+    WsApi.prototype.sendGetCmdMsg = function (action, extra) {
62
+        if (extra === void 0) { extra = {}; }
63
+        return __awaiter(this, void 0, void 0, function () {
64
+            var msg;
65
+            return __generator(this, function (_a) {
66
+                msg = CmdMsg_1.CmdMsg.createMsg(__assign({ from: this.from, action: action }, extra));
67
+                return [2, this.sendMsg(msg)];
68
+            });
69
+        });
70
+    };
71
+    WsApi.prototype.sendPostCmdMsg = function (action, userIds, extra) {
72
+        if (extra === void 0) { extra = {}; }
73
+        return __awaiter(this, void 0, void 0, function () {
74
+            var msg;
75
+            return __generator(this, function (_a) {
76
+                msg = CmdMsg_1.CmdMsg.createMsg(__assign({ from: this.from, to: (userIds || []).join(','), action: action }, extra));
77
+                return [2, this.sendMsg(msg)];
78
+            });
79
+        });
80
+    };
81
+    WsApi.prototype.getRoster = function () {
82
+        return this.sendGetCmdMsg('getRoster');
83
+    };
84
+    WsApi.prototype.addFriends = function (userIds) {
85
+        return this.sendPostCmdMsg('addFriends', userIds);
86
+    };
87
+    WsApi.prototype.agreeFriends = function (userIds) {
88
+        return this.sendPostCmdMsg('agreeFriends', userIds);
89
+    };
90
+    WsApi.prototype.rejectFriends = function (userIds) {
91
+        return this.sendPostCmdMsg('rejectFriends', userIds);
92
+    };
93
+    WsApi.prototype.removeFriends = function (userIds) {
94
+        return this.sendPostCmdMsg('removeFriends', userIds);
95
+    };
96
+    WsApi.prototype.getBlacklist = function () {
97
+        return this.sendGetCmdMsg('getBlacklist');
98
+    };
99
+    WsApi.prototype.addToBlackList = function (userIds) {
100
+        return this.sendPostCmdMsg('addToBlackList', userIds);
101
+    };
102
+    WsApi.prototype.removeBlackList = function (userIds) {
103
+        return this.sendPostCmdMsg('removeBlackList', userIds);
104
+    };
105
+    WsApi.prototype.listGroups = function () {
106
+        return this.sendGetCmdMsg('listGroups');
107
+    };
108
+    WsApi.prototype.queryGroupInfo = function (roomId) {
109
+        return this.sendGetCmdMsg('queryGroupInfo', { to: roomId });
110
+    };
111
+    WsApi.prototype.queryRoomMember = function (roomId) {
112
+        return this.sendGetCmdMsg('queryRoomMember', { to: roomId });
113
+    };
114
+    WsApi.prototype.getGroupBlackList = function (roomId) {
115
+        return this.sendGetCmdMsg('getGroupBlackList', { to: roomId });
116
+    };
117
+    WsApi.prototype.createGroup = function (groupOption) {
118
+        return this.sendPostCmdMsg('createGroup', null, { ext: groupOption });
119
+    };
120
+    WsApi.prototype.changeGroupInfo = function (groupOption) {
121
+        return this.sendPostCmdMsg('changeGroupInfo', null, { ext: groupOption });
122
+    };
123
+    WsApi.prototype.addGroupMembers = function (roomId, userIds) {
124
+        return this.sendPostCmdMsg('addGroupMembers', [roomId], {
125
+            ext: {
126
+                members: userIds
127
+            }
128
+        });
129
+    };
130
+    WsApi.prototype.addToGroupBlackList = function (roomId, userIds) {
131
+        return this.sendPostCmdMsg('addToGroupBlackList', [roomId], {
132
+            ext: {
133
+                members: userIds
134
+            }
135
+        });
136
+    };
137
+    WsApi.prototype.destroyGroup = function (roomId) {
138
+        return this.sendPostCmdMsg('destroyGroup', [roomId]);
139
+    };
140
+    WsApi.prototype.leaveGroup = function (roomId) {
141
+        return this.sendPostCmdMsg('leaveGroup', [roomId]);
142
+    };
143
+    WsApi.prototype.createRoom = function (groupOption) {
144
+        return this.sendPostCmdMsg('createGroup', null, { ext: groupOption });
145
+    };
146
+    WsApi.prototype.listRooms = function () {
147
+        return this.sendGetCmdMsg('listRooms');
148
+    };
149
+    WsApi.prototype.joinRoom = function (roomId) {
150
+        return this.sendPostCmdMsg('joinRoom', [roomId]);
151
+    };
152
+    WsApi.prototype.quitRoom = function (roomId) {
153
+        return this.sendPostCmdMsg('quitRoom', [roomId]);
154
+    };
155
+    return WsApi;
156
+}());
157
+exports.WsApi = WsApi;

+ 0
- 0
packages/cts-api/dist/cjs/api/index.js Visa fil


+ 0
- 0
packages/cts-api/dist/cjs/protocol/AttachmentMsg.js Visa fil


+ 31
- 0
packages/cts-api/dist/cjs/protocol/CmdMsg.js Visa fil

@@ -0,0 +1,31 @@
1
+"use strict";
2
+var __extends = (this && this.__extends) || (function () {
3
+    var extendStatics = function (d, b) {
4
+        extendStatics = Object.setPrototypeOf ||
5
+            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+        return extendStatics(d, b);
8
+    };
9
+    return function (d, b) {
10
+        extendStatics(d, b);
11
+        function __() { this.constructor = d; }
12
+        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+    };
14
+})();
15
+Object.defineProperty(exports, "__esModule", { value: true });
16
+var MsgBase_1 = require("./MsgBase");
17
+var CmdMsg = (function (_super) {
18
+    __extends(CmdMsg, _super);
19
+    function CmdMsg() {
20
+        var _this = _super !== null && _super.apply(this, arguments) || this;
21
+        _this.type = 'cmd';
22
+        return _this;
23
+    }
24
+    CmdMsg.createMsg = function (baseMsg) {
25
+        var msg = new CmdMsg();
26
+        Object.assign(msg, baseMsg);
27
+        return msg;
28
+    };
29
+    return CmdMsg;
30
+}(MsgBase_1.MsgBase));
31
+exports.CmdMsg = CmdMsg;

+ 55
- 0
packages/cts-api/dist/cjs/protocol/MediaMsg.js Visa fil

@@ -0,0 +1,55 @@
1
+"use strict";
2
+var __extends = (this && this.__extends) || (function () {
3
+    var extendStatics = function (d, b) {
4
+        extendStatics = Object.setPrototypeOf ||
5
+            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+        return extendStatics(d, b);
8
+    };
9
+    return function (d, b) {
10
+        extendStatics(d, b);
11
+        function __() { this.constructor = d; }
12
+        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+    };
14
+})();
15
+var __assign = (this && this.__assign) || function () {
16
+    __assign = Object.assign || function(t) {
17
+        for (var s, i = 1, n = arguments.length; i < n; i++) {
18
+            s = arguments[i];
19
+            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
+                t[p] = s[p];
21
+        }
22
+        return t;
23
+    };
24
+    return __assign.apply(this, arguments);
25
+};
26
+Object.defineProperty(exports, "__esModule", { value: true });
27
+var MsgBase_1 = require("./MsgBase");
28
+var MediaMsg = (function (_super) {
29
+    __extends(MediaMsg, _super);
30
+    function MediaMsg() {
31
+        var _this = _super !== null && _super.apply(this, arguments) || this;
32
+        _this.type = 'media';
33
+        return _this;
34
+    }
35
+    MediaMsg.createMsg = function (baseMsg) {
36
+        var msg = new MediaMsg();
37
+        Object.assign(msg, baseMsg);
38
+        return msg;
39
+    };
40
+    MediaMsg.createFromClipboard = function (baseMsg, clipboardData) {
41
+        if (clipboardData && clipboardData.types) {
42
+            if (clipboardData.items.length > 0) {
43
+                var blob = clipboardData.items[0].getAsFile();
44
+                var url = window.URL.createObjectURL(blob);
45
+                return MediaMsg.createMsg(__assign({}, baseMsg, { file: {
46
+                        data: blob,
47
+                        url: url
48
+                    } }));
49
+            }
50
+        }
51
+        return null;
52
+    };
53
+    return MediaMsg;
54
+}(MsgBase_1.MsgBase));
55
+exports.MediaMsg = MediaMsg;

+ 21
- 0
packages/cts-api/dist/cjs/protocol/MsgBase.js Visa fil

@@ -0,0 +1,21 @@
1
+"use strict";
2
+Object.defineProperty(exports, "__esModule", { value: true });
3
+var uuid_1 = require("../utils/uuid");
4
+var MsgBase = (function () {
5
+    function MsgBase() {
6
+        this.id = uuid_1.uuid();
7
+    }
8
+    MsgBase.prototype.isValid = function () {
9
+        if (!this.from || !this.to) {
10
+            return false;
11
+        }
12
+        return true;
13
+    };
14
+    MsgBase.createMsg = function (baseMsg) {
15
+        var msg = new MsgBase();
16
+        Object.assign(msg, baseMsg);
17
+        return msg;
18
+    };
19
+    return MsgBase;
20
+}());
21
+exports.MsgBase = MsgBase;

+ 31
- 0
packages/cts-api/dist/cjs/protocol/TextMsg.js Visa fil

@@ -0,0 +1,31 @@
1
+"use strict";
2
+var __extends = (this && this.__extends) || (function () {
3
+    var extendStatics = function (d, b) {
4
+        extendStatics = Object.setPrototypeOf ||
5
+            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+        return extendStatics(d, b);
8
+    };
9
+    return function (d, b) {
10
+        extendStatics(d, b);
11
+        function __() { this.constructor = d; }
12
+        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+    };
14
+})();
15
+Object.defineProperty(exports, "__esModule", { value: true });
16
+var MsgBase_1 = require("./MsgBase");
17
+var TextMsg = (function (_super) {
18
+    __extends(TextMsg, _super);
19
+    function TextMsg() {
20
+        var _this = _super !== null && _super.apply(this, arguments) || this;
21
+        _this.type = 'text';
22
+        return _this;
23
+    }
24
+    TextMsg.createMsg = function (baseMsg) {
25
+        var msg = new TextMsg();
26
+        Object.assign(msg, baseMsg);
27
+        return msg;
28
+    };
29
+    return TextMsg;
30
+}(MsgBase_1.MsgBase));
31
+exports.TextMsg = TextMsg;

+ 0
- 0
packages/cts-api/dist/cjs/protocol/index.js Visa fil


+ 4
- 0
packages/cts-api/dist/cjs/utils/uuid.js Visa fil

@@ -0,0 +1,4 @@
1
+"use strict";
2
+Object.defineProperty(exports, "__esModule", { value: true });
3
+var uuidV1 = require("uuid/v1");
4
+exports.uuid = uuidV1;

+ 30
- 0
packages/cts-api/dist/types/api/WsApi.d.ts Visa fil

@@ -0,0 +1,30 @@
1
+import { MsgBase, GroupOption } from '../protocol/MsgBase';
2
+export declare class WsApi {
3
+    token: string;
4
+    from: string;
5
+    sendMsg(msg: Partial<MsgBase>): Promise<void>;
6
+    sendGetCmdMsg(action: string, extra?: {}): Promise<void>;
7
+    sendPostCmdMsg(action: string, userIds: string[] | null, extra?: {}): Promise<void>;
8
+    getRoster(): Promise<void>;
9
+    addFriends(userIds: string[]): Promise<void>;
10
+    agreeFriends(userIds: string[]): Promise<void>;
11
+    rejectFriends(userIds: string[]): Promise<void>;
12
+    removeFriends(userIds: string[]): Promise<void>;
13
+    getBlacklist(): Promise<void>;
14
+    addToBlackList(userIds: string[]): Promise<void>;
15
+    removeBlackList(userIds: string[]): Promise<void>;
16
+    listGroups(): Promise<void>;
17
+    queryGroupInfo(roomId: string): Promise<void>;
18
+    queryRoomMember(roomId: string): Promise<void>;
19
+    getGroupBlackList(roomId: string): Promise<void>;
20
+    createGroup(groupOption: GroupOption): Promise<void>;
21
+    changeGroupInfo(groupOption: GroupOption): Promise<void>;
22
+    addGroupMembers(roomId: string, userIds: string[]): Promise<void>;
23
+    addToGroupBlackList(roomId: string, userIds: string[]): Promise<void>;
24
+    destroyGroup(roomId: string): Promise<void>;
25
+    leaveGroup(roomId: string): Promise<void>;
26
+    createRoom(groupOption: GroupOption): Promise<void>;
27
+    listRooms(): Promise<void>;
28
+    joinRoom(roomId: string): Promise<void>;
29
+    quitRoom(roomId: string): Promise<void>;
30
+}

+ 0
- 0
packages/cts-api/dist/types/api/index.d.ts Visa fil


+ 0
- 0
packages/cts-api/dist/types/protocol/AttachmentMsg.d.ts Visa fil


+ 7
- 0
packages/cts-api/dist/types/protocol/CmdMsg.d.ts Visa fil

@@ -0,0 +1,7 @@
1
+import { MsgBase, MsgType } from './MsgBase';
2
+export declare class CmdMsg extends MsgBase {
3
+    type: MsgType;
4
+    msg: string;
5
+    action: string;
6
+    static createMsg(baseMsg: Partial<CmdMsg>): CmdMsg;
7
+}

+ 10
- 0
packages/cts-api/dist/types/protocol/MediaMsg.d.ts Visa fil

@@ -0,0 +1,10 @@
1
+import { MsgBase, MsgType } from './MsgBase';
2
+export declare class MediaMsg extends MsgBase {
3
+    type: MsgType;
4
+    file: {
5
+        data: Blob | null;
6
+        url: string;
7
+    };
8
+    static createMsg(baseMsg: Partial<MediaMsg>): MediaMsg;
9
+    static createFromClipboard(baseMsg: Partial<MediaMsg>, clipboardData: DataTransfer): MediaMsg | null;
10
+}

+ 21
- 0
packages/cts-api/dist/types/protocol/MsgBase.d.ts Visa fil

@@ -0,0 +1,21 @@
1
+export declare type MsgType = 'text' | 'media' | 'cmd' | 'attachment';
2
+export declare type RoomType = 'single' | 'room';
3
+export declare type GroupOption = {
4
+    subject: string;
5
+    description: string;
6
+    members: string[];
7
+    optionsPublic: boolean;
8
+    optionsModerate: boolean;
9
+    optionsMembersOnly: boolean;
10
+    optionsAllowInvites: boolean;
11
+};
12
+export declare class MsgBase {
13
+    id: string;
14
+    from: string;
15
+    to: string;
16
+    type: MsgType;
17
+    roomType: RoomType;
18
+    ext: Record<string, string>;
19
+    isValid(): boolean;
20
+    static createMsg(baseMsg: Partial<MsgBase>): MsgBase;
21
+}

+ 6
- 0
packages/cts-api/dist/types/protocol/TextMsg.d.ts Visa fil

@@ -0,0 +1,6 @@
1
+import { MsgBase, MsgType } from './MsgBase';
2
+export declare class TextMsg extends MsgBase {
3
+    type: MsgType;
4
+    msg: string;
5
+    static createMsg(baseMsg: Partial<TextMsg>): TextMsg;
6
+}

+ 0
- 0
packages/cts-api/dist/types/protocol/index.d.ts Visa fil


+ 1
- 0
packages/cts-api/dist/types/utils/uuid.d.ts Visa fil

@@ -0,0 +1 @@
1
+export declare const uuid: import("uuid/interfaces").v1;

+ 158
- 0
packages/cts-api/src/api/WsApi.ts Visa fil

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

+ 0
- 0
packages/cts-api/src/api/index.ts Visa fil


+ 0
- 0
packages/cts-api/src/protocol/AttachmentMsg.ts Visa fil


+ 16
- 0
packages/cts-api/src/protocol/CmdMsg.ts Visa fil

@@ -0,0 +1,16 @@
1
+import { MsgBase, MsgType } from './MsgBase';
2
+
3
+export class CmdMsg extends MsgBase {
4
+  type: MsgType = 'cmd';
5
+
6
+  // 消息内容
7
+  msg: string;
8
+  // 命令
9
+  action: string;
10
+
11
+  static createMsg(baseMsg: Partial<CmdMsg>): CmdMsg {
12
+    const msg = new CmdMsg();
13
+    Object.assign(msg, baseMsg);
14
+    return msg;
15
+  }
16
+}

+ 35
- 0
packages/cts-api/src/protocol/MediaMsg.ts Visa fil

@@ -0,0 +1,35 @@
1
+import { MsgBase, MsgType } from './MsgBase';
2
+
3
+export class MediaMsg extends MsgBase {
4
+  type: MsgType = 'media';
5
+
6
+  // 消息内容
7
+  file: { data: Blob | null; url: string };
8
+
9
+  // 创建消息
10
+  static createMsg(baseMsg: Partial<MediaMsg>): MediaMsg {
11
+    const msg = new MediaMsg();
12
+    Object.assign(msg, baseMsg);
13
+    return msg;
14
+  }
15
+
16
+  // 从剪贴板中创建
17
+  static createFromClipboard(baseMsg: Partial<MediaMsg>, clipboardData: DataTransfer) {
18
+    if (clipboardData && clipboardData.types) {
19
+      if (clipboardData.items.length > 0) {
20
+        const blob = clipboardData.items[0].getAsFile();
21
+        const url = window.URL.createObjectURL(blob);
22
+
23
+        return MediaMsg.createMsg({
24
+          ...baseMsg,
25
+          file: {
26
+            data: blob,
27
+            url
28
+          }
29
+        });
30
+      }
31
+    }
32
+
33
+    return null;
34
+  }
35
+}

+ 50
- 0
packages/cts-api/src/protocol/MsgBase.ts Visa fil

@@ -0,0 +1,50 @@
1
+import { uuid } from '../utils/uuid';
2
+
3
+export type MsgType = 'text' | 'media' | 'cmd' | 'attachment';
4
+
5
+export type RoomType = 'single' | 'room';
6
+
7
+export type GroupOption = {
8
+  subject: string; // 群名称
9
+  description: string; // 群简介
10
+  members: string[]; // 成员列表
11
+  optionsPublic: boolean; // 允许任何人加入
12
+  optionsModerate: boolean; // 加入需审批
13
+  optionsMembersOnly: boolean; // 不允许任何人主动加入
14
+  optionsAllowInvites: boolean; // 允许群人员邀请
15
+};
16
+
17
+export class MsgBase {
18
+  // 消息编号
19
+  id: string = uuid();
20
+
21
+  // 消息发送方,如果是从多个人发出,则用 , 逗号分隔
22
+  from: string;
23
+
24
+  // 消息接收方/目标方,如果是发送给多个人,则用 , 逗号分隔
25
+  to: string;
26
+
27
+  // 消息类型
28
+  type: MsgType;
29
+
30
+  // 聊天室类型
31
+  roomType: RoomType;
32
+
33
+  // 扩展消息
34
+  ext: Record<string, string>;
35
+
36
+  /** 校验当前消息是否有效 */
37
+  isValid() {
38
+    if (!this.from || !this.to) {
39
+      return false;
40
+    }
41
+
42
+    return true;
43
+  }
44
+
45
+  static createMsg(baseMsg: Partial<MsgBase>): MsgBase {
46
+    const msg = new MsgBase();
47
+    Object.assign(msg, baseMsg);
48
+    return msg;
49
+  }
50
+}

+ 14
- 0
packages/cts-api/src/protocol/TextMsg.ts Visa fil

@@ -0,0 +1,14 @@
1
+import { MsgBase, MsgType } from './MsgBase';
2
+
3
+export class TextMsg extends MsgBase {
4
+  type: MsgType = 'text';
5
+
6
+  // 消息内容
7
+  msg: string;
8
+
9
+  static createMsg(baseMsg: Partial<TextMsg>): TextMsg {
10
+    const msg = new TextMsg();
11
+    Object.assign(msg, baseMsg);
12
+    return msg;
13
+  }
14
+}

+ 0
- 0
packages/cts-api/src/protocol/index.ts Visa fil


+ 3
- 0
packages/cts-api/src/utils/uuid.ts Visa fil

@@ -0,0 +1,3 @@
1
+import * as uuidV1 from 'uuid/v1';
2
+
3
+export const uuid = uuidV1;