zsl-im-sdk 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/api/common/common.api.d.ts +1 -0
  2. package/dist/api/common/common.api.js +13 -0
  3. package/dist/api/conversation/conversation.api.d.ts +54 -0
  4. package/dist/api/conversation/conversation.api.js +88 -0
  5. package/dist/api/im-login.d.ts +11 -0
  6. package/dist/api/im-login.js +16 -0
  7. package/dist/api/message/message.api.d.ts +23 -0
  8. package/dist/api/message/message.api.js +36 -0
  9. package/dist/index.d.ts +26 -0
  10. package/dist/index.js +76 -0
  11. package/dist/store.d.ts +61 -0
  12. package/dist/store.js +493 -0
  13. package/dist/types/conversation.type.d.ts +42 -0
  14. package/dist/types/conversation.type.js +8 -0
  15. package/dist/types/im-user.type.d.ts +22 -0
  16. package/dist/types/im-user.type.js +2 -0
  17. package/{types/index.ts → dist/types/index.d.ts} +4 -5
  18. package/dist/types/index.js +21 -0
  19. package/dist/types/message-type.type.d.ts +76 -0
  20. package/dist/types/message-type.type.js +48 -0
  21. package/dist/types/socket-data.type.d.ts +38 -0
  22. package/dist/types/socket-data.type.js +19 -0
  23. package/dist/types.d.ts +62 -0
  24. package/dist/types.js +5 -0
  25. package/dist/utils/http.d.ts +41 -0
  26. package/dist/utils/http.js +159 -0
  27. package/dist/websocket.d.ts +46 -0
  28. package/dist/websocket.js +302 -0
  29. package/package.json +34 -5
  30. package/api/common/common.api.ts +0 -7
  31. package/api/conversation/conversation.api.ts +0 -93
  32. package/api/im-login.ts +0 -18
  33. package/api/message/message.api.ts +0 -51
  34. package/index.ts +0 -36
  35. package/jsconfig.json +0 -14
  36. package/store.ts +0 -692
  37. package/types/conversation.type.ts +0 -60
  38. package/types/im-user.type.ts +0 -31
  39. package/types/message-type.type.ts +0 -94
  40. package/types/socket-data.type.ts +0 -55
  41. package/types.ts +0 -66
  42. package/utils/http.ts +0 -195
  43. package/websocket.ts +0 -367
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MessageStatus = exports.MessageTypeData = exports.MessageType = void 0;
4
+ // 消息类型枚举
5
+ var MessageType;
6
+ (function (MessageType) {
7
+ MessageType["TEXT"] = "TEXT";
8
+ MessageType["IMAGE"] = "IMAGE";
9
+ MessageType["VIDEO"] = "VIDEO";
10
+ MessageType["CUSTOM"] = "CUSTOM";
11
+ MessageType["TEXT_QUOTE"] = "TEXT_QUOTE";
12
+ MessageType["AUDIO"] = "AUDIO";
13
+ MessageType["IMAGE_TRANSMIT"] = "IMAGE_TRANSMIT";
14
+ MessageType["EMOJI_PACK"] = "EMOJI_PACK";
15
+ MessageType["RED_ENVELOPE"] = "RED_ENVELOPE";
16
+ MessageType["MAP"] = "MAP";
17
+ MessageType["GROUP_NOTICE"] = "GROUP_NOTICE";
18
+ MessageType["UPDATE_GROUP_NAME"] = "UPDATE_GROUP_NAME";
19
+ MessageType["ARTICLE"] = "ARTICLE";
20
+ MessageType["SHARE_SBCF"] = "SHARE_SBCF";
21
+ MessageType["SHARE_MALL"] = "SHARE_MALL";
22
+ })(MessageType || (exports.MessageType = MessageType = {}));
23
+ // 消息类型枚举
24
+ exports.MessageTypeData = {
25
+ TEXT: "TEXT",
26
+ IMAGE: "IMAGE",
27
+ VIDEO: "VIDEO",
28
+ CUSTOM: "CUSTOM",
29
+ TEXT_QUOTE: "TEXT_QUOTE",
30
+ AUDIO: "AUDIO",
31
+ IMAGE_TRANSMIT: "IMAGE_TRANSMIT",
32
+ EMOJI_PACK: "EMOJI_PACK",
33
+ RED_ENVELOPE: "RED_ENVELOPE",
34
+ MAP: "MAP",
35
+ GROUP_NOTICE: "GROUP_NOTICE",
36
+ UPDATE_GROUP_NAME: "UPDATE_GROUP_NAME",
37
+ ARTICLE: "ARTICLE",
38
+ SHARE_SBCF: "SHARE_SBCF",
39
+ SHARE_MALL: "SHARE_MALL",
40
+ };
41
+ // 消息状态枚举
42
+ var MessageStatus;
43
+ (function (MessageStatus) {
44
+ MessageStatus["SENT"] = "SENT";
45
+ MessageStatus["DELIVERED"] = "DELIVERED";
46
+ MessageStatus["READ"] = "READ";
47
+ MessageStatus["RECALLED"] = "RECALLED";
48
+ })(MessageStatus || (exports.MessageStatus = MessageStatus = {}));
@@ -0,0 +1,38 @@
1
+ import { MessageType } from './message-type.type';
2
+ export declare enum SocketAction {
3
+ SEND_MESSAGE = "SEND_MESSAGE",
4
+ READ_RECEIPT = "READ_RECEIPT",
5
+ RECALL_MESSAGE = "RECALL_MESSAGE",
6
+ PING = "PING",
7
+ PONG = "PONG",
8
+ ACK = "ACK",
9
+ MESSAGE = "MESSAGE",
10
+ DELIVERED = "DELIVERED",
11
+ RECALLED = "RECALLED",
12
+ ERROR = "ERROR"
13
+ }
14
+ export interface ISendMessage {
15
+ id?: string | number;
16
+ conversationId?: string;
17
+ senderId?: string | number;
18
+ recipientId?: string | number;
19
+ messageType?: MessageType;
20
+ content?: string;
21
+ mediaUrl?: string;
22
+ metadata?: string;
23
+ time?: string;
24
+ }
25
+ export interface IReadReceipt {
26
+ conversationId?: string;
27
+ lastReadMessageId?: number;
28
+ }
29
+ export interface IRecalledMessage {
30
+ messageId?: number;
31
+ }
32
+ export interface IPing {
33
+ }
34
+ export type Payload = ISendMessage | IReadReceipt | IRecalledMessage | IPing;
35
+ export interface SocketData<T = Payload> {
36
+ action: SocketAction;
37
+ payload: T;
38
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SocketAction = void 0;
4
+ // SocketAction枚举
5
+ var SocketAction;
6
+ (function (SocketAction) {
7
+ // client -> server
8
+ SocketAction["SEND_MESSAGE"] = "SEND_MESSAGE";
9
+ SocketAction["READ_RECEIPT"] = "READ_RECEIPT";
10
+ SocketAction["RECALL_MESSAGE"] = "RECALL_MESSAGE";
11
+ SocketAction["PING"] = "PING";
12
+ SocketAction["PONG"] = "PONG";
13
+ // server -> client
14
+ SocketAction["ACK"] = "ACK";
15
+ SocketAction["MESSAGE"] = "MESSAGE";
16
+ SocketAction["DELIVERED"] = "DELIVERED";
17
+ SocketAction["RECALLED"] = "RECALLED";
18
+ SocketAction["ERROR"] = "ERROR";
19
+ })(SocketAction || (exports.SocketAction = SocketAction = {}));
@@ -0,0 +1,62 @@
1
+ /**
2
+ * SDK 核心类型定义
3
+ */
4
+ /** 消息类型 */
5
+ export interface IMessage {
6
+ id: string;
7
+ conversationId: string;
8
+ content: string;
9
+ messageType: string;
10
+ senderId: string | number;
11
+ createdAt: string;
12
+ status: 'SENDING' | 'SENT' | 'FAILED';
13
+ icon?: string;
14
+ recipientId?: string;
15
+ mediaUrl?: string;
16
+ }
17
+ /** 用户类型 */
18
+ export interface IUser {
19
+ id: string | number;
20
+ name: string;
21
+ avatar?: string;
22
+ }
23
+ /** 会话类型 */
24
+ export interface IConversation {
25
+ conversationId: string;
26
+ conversationType: 'P2P' | 'GROUP';
27
+ lastMessageAt: string;
28
+ lastMessageContent: string;
29
+ lastMessageType: string;
30
+ unreadCount: number;
31
+ user: {
32
+ userId: string | number;
33
+ displayName: string;
34
+ account: string;
35
+ avatarUrl: string;
36
+ };
37
+ [key: string]: any;
38
+ }
39
+ /** Store 状态类型 */
40
+ export interface IState {
41
+ conversation: {
42
+ conversations: IConversation[];
43
+ selectedConversationId: string;
44
+ };
45
+ message: {
46
+ messages: Record<string, IMessage[]>;
47
+ messageQueue: IMessage[];
48
+ };
49
+ user: {
50
+ currentUser: IUser | null;
51
+ users: Record<string | number, IUser>;
52
+ };
53
+ websocket: {
54
+ isConnected: boolean;
55
+ reconnectAttempts: number;
56
+ imInstance: any;
57
+ };
58
+ global: {
59
+ loading: boolean;
60
+ error: string | null;
61
+ };
62
+ }
package/dist/types.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ /**
3
+ * SDK 核心类型定义
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,41 @@
1
+ type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS';
2
+ export interface IResponse<T = any> {
3
+ data: T;
4
+ message?: string;
5
+ msg?: string;
6
+ success: boolean;
7
+ code?: number;
8
+ }
9
+ export interface HttpConfig {
10
+ baseURL?: string;
11
+ timeout?: number;
12
+ headers?: Record<string, string>;
13
+ beforeRequest?: (options: any) => void;
14
+ afterResponse?: (response: any, options: any) => any;
15
+ }
16
+ export declare class HttpError extends Error {
17
+ code: number | string;
18
+ constructor(code: number | string, message: string);
19
+ }
20
+ export declare class HttpClient {
21
+ private baseURL;
22
+ private timeout;
23
+ private headers;
24
+ private beforeRequest?;
25
+ private afterResponse?;
26
+ constructor(config?: HttpConfig);
27
+ setBaseURL(url: string): void;
28
+ setHeaders(headers: Record<string, string>): void;
29
+ setBeforeRequest(callback: (options: any) => void): void;
30
+ setAfterResponse(callback: (response: any, options: any) => any): void;
31
+ request<T = any>(method: Method, url: string, data?: any, options?: any): Promise<T>;
32
+ get<T = any>(url: string, params?: any, options?: any): Promise<T>;
33
+ post<T = any>(url: string, data?: any, options?: any): Promise<T>;
34
+ put<T = any>(url: string, data?: any, options?: any): Promise<T>;
35
+ delete<T = any>(url: string, data?: any, options?: any): Promise<T>;
36
+ upload<T = any>(url: string, filePath: string, name: string, formData?: Record<string, any>, options?: any): Promise<T>;
37
+ download(url: string, options?: any): Promise<string>;
38
+ }
39
+ declare const http: HttpClient;
40
+ export declare const setBaseURL: (url: string) => void;
41
+ export default http;
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setBaseURL = exports.HttpClient = exports.HttpError = void 0;
4
+ // HTTP错误类
5
+ class HttpError extends Error {
6
+ constructor(code, message) {
7
+ super(message);
8
+ this.code = code;
9
+ this.name = 'HttpError';
10
+ }
11
+ }
12
+ exports.HttpError = HttpError;
13
+ // HTTP客户端类
14
+ class HttpClient {
15
+ constructor(config = {}) {
16
+ this.baseURL = config.baseURL || '';
17
+ this.timeout = config.timeout || 60000;
18
+ this.headers = {
19
+ 'Content-Type': 'application/json',
20
+ ...config.headers
21
+ };
22
+ this.beforeRequest = config.beforeRequest;
23
+ this.afterResponse = config.afterResponse;
24
+ }
25
+ // 设置基础URL
26
+ setBaseURL(url) {
27
+ this.baseURL = url;
28
+ }
29
+ // 设置默认headers
30
+ setHeaders(headers) {
31
+ this.headers = { ...this.headers, ...headers };
32
+ }
33
+ // 添加请求拦截器
34
+ setBeforeRequest(callback) {
35
+ this.beforeRequest = callback;
36
+ }
37
+ // 添加响应拦截器
38
+ setAfterResponse(callback) {
39
+ this.afterResponse = callback;
40
+ }
41
+ // 发送请求
42
+ request(method, url, data, options = {}) {
43
+ return new Promise((resolve, reject) => {
44
+ // 构建请求选项
45
+ const requestOptions = {
46
+ url: this.baseURL + url,
47
+ method,
48
+ data,
49
+ header: { ...this.headers, ...options.header },
50
+ timeout: options.timeout || this.timeout,
51
+ success: (response) => {
52
+ try {
53
+ // 处理响应拦截器
54
+ const processedResponse = this.afterResponse ? this.afterResponse(response, requestOptions) : response.data;
55
+ resolve(processedResponse);
56
+ }
57
+ catch (error) {
58
+ reject(error);
59
+ }
60
+ },
61
+ fail: (error) => {
62
+ reject(new HttpError(error.errMsg, '网络请求失败'));
63
+ }
64
+ };
65
+ // 处理请求拦截器
66
+ if (this.beforeRequest) {
67
+ this.beforeRequest(requestOptions);
68
+ }
69
+ // 发送请求
70
+ uni.request(requestOptions);
71
+ });
72
+ }
73
+ // GET请求
74
+ get(url, params, options) {
75
+ return this.request('GET', url, params, options);
76
+ }
77
+ // POST请求
78
+ post(url, data, options) {
79
+ return this.request('POST', url, data, options);
80
+ }
81
+ // PUT请求
82
+ put(url, data, options) {
83
+ return this.request('PUT', url, data, options);
84
+ }
85
+ // DELETE请求
86
+ delete(url, data, options) {
87
+ return this.request('DELETE', url, data, options);
88
+ }
89
+ // 文件上传
90
+ upload(url, filePath, name, formData, options = {}) {
91
+ return new Promise((resolve, reject) => {
92
+ const uploadOptions = {
93
+ url: this.baseURL + url,
94
+ filePath,
95
+ name,
96
+ formData,
97
+ header: { ...this.headers, ...options.header },
98
+ success: (response) => {
99
+ try {
100
+ const data = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
101
+ resolve(data);
102
+ }
103
+ catch (error) {
104
+ reject(new HttpError('PARSE_ERROR', '响应数据解析失败'));
105
+ }
106
+ },
107
+ fail: (error) => {
108
+ reject(new HttpError(error.errMsg, '文件上传失败'));
109
+ }
110
+ };
111
+ uni.uploadFile(uploadOptions);
112
+ });
113
+ }
114
+ // 文件下载
115
+ download(url, options = {}) {
116
+ return new Promise((resolve, reject) => {
117
+ const downloadOptions = {
118
+ url: this.baseURL + url,
119
+ header: { ...this.headers, ...options.header },
120
+ success: (response) => {
121
+ resolve(response.tempFilePath);
122
+ },
123
+ fail: (error) => {
124
+ reject(new HttpError(error.errMsg, '文件下载失败'));
125
+ }
126
+ };
127
+ uni.downloadFile(downloadOptions);
128
+ });
129
+ }
130
+ }
131
+ exports.HttpClient = HttpClient;
132
+ // 创建默认HTTP客户端
133
+ // 移除默认配置的baseURL,改为支持动态设置
134
+ const http = new HttpClient({
135
+ headers: {
136
+ 'Content-Type': 'application/json'
137
+ },
138
+ afterResponse: (response) => {
139
+ try {
140
+ // debugger;
141
+ const data = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
142
+ if (data.success === false) {
143
+ throw new HttpError(data.code || 'ERROR', data.message || data.msg || '请求失败');
144
+ }
145
+ return data.data || data;
146
+ }
147
+ catch (error) {
148
+ // 如果响应不是JSON格式,返回原始数据
149
+ console.error('HTTP响应解析失败:', error);
150
+ throw new HttpError('PARSE_ERROR', '响应数据格式不正确');
151
+ }
152
+ }
153
+ });
154
+ // 导出设置BaseURL的方法
155
+ const setBaseURL = (url) => {
156
+ http.setBaseURL(url);
157
+ };
158
+ exports.setBaseURL = setBaseURL;
159
+ exports.default = http;
@@ -0,0 +1,46 @@
1
+ export * from "./types";
2
+ import { MessageType, SocketData } from "./types/index";
3
+ export type EventType = "message" | "connect" | "disconnect" | "error" | "reconnect" | "recalled";
4
+ export type EventCallback<T = any> = (data: T) => void;
5
+ export interface IMOptions {
6
+ url: string;
7
+ token: string;
8
+ userId: string | number;
9
+ maxReconnectCount?: number;
10
+ reconnectInterval?: number;
11
+ heartbeatInterval?: number;
12
+ }
13
+ export default class IM {
14
+ private ws;
15
+ private url;
16
+ private token;
17
+ private userId;
18
+ private isConnected;
19
+ private reconnectTimer;
20
+ private reconnectCount;
21
+ private maxReconnectCount;
22
+ private reconnectInterval;
23
+ private messageQueue;
24
+ private listeners;
25
+ private heartbeatTimer;
26
+ private heartbeatInterval;
27
+ constructor(options: IMOptions);
28
+ private getWsUrl;
29
+ connect(): Promise<void>;
30
+ disconnect(): void;
31
+ private handleReconnect;
32
+ private handleMessage;
33
+ send(data: SocketData): Promise<void>;
34
+ sendText(conversationId: string, recipientId: string | number, content: string, messageType: MessageType, mediaUrl: string | undefined): Promise<void>;
35
+ sendReadReceipt(conversationId: string, lastReadMessageId?: number): Promise<void>;
36
+ sendRecallMessage(messageId: number): Promise<void>;
37
+ private sendPingPong;
38
+ private startHeartbeat;
39
+ private stopHeartbeat;
40
+ private flushMessageQueue;
41
+ private getCurrentTime;
42
+ on<T = any>(event: EventType, callback: EventCallback<T>): void;
43
+ off<T = any>(event: EventType, callback: EventCallback<T>): void;
44
+ private emit;
45
+ getConnectionStatus(): boolean;
46
+ }