zsl-im-sdk 1.0.0
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.
- package/api/common/common.api.ts +7 -0
- package/api/conversation/conversation.api.ts +93 -0
- package/api/im-login.ts +18 -0
- package/api/message/message.api.ts +51 -0
- package/index.ts +36 -0
- package/jsconfig.json +14 -0
- package/package.json +21 -0
- package/store.ts +692 -0
- package/types/conversation.type.ts +60 -0
- package/types/im-user.type.ts +31 -0
- package/types/index.ts +5 -0
- package/types/message-type.type.ts +94 -0
- package/types/socket-data.type.ts +55 -0
- package/types.ts +66 -0
- package/utils/http.ts +195 -0
- package/websocket.ts +367 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { MessageType } from "./message-type.type";
|
|
2
|
+
|
|
3
|
+
export enum ConversationType {
|
|
4
|
+
P2P = 1, // 单聊
|
|
5
|
+
GROUP = 2, // 群聊
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
interface IConversationUserInfo {
|
|
10
|
+
/** 用户ID (长整型) */
|
|
11
|
+
id?: string; // 1999386093895651300
|
|
12
|
+
|
|
13
|
+
/** 业务系统标识 */
|
|
14
|
+
biz?: string; // "sys"
|
|
15
|
+
|
|
16
|
+
/** 用户账号/唯一标识 */
|
|
17
|
+
account?: string; // "20ad7f974d7f43abb6b8a4ac2443df01"
|
|
18
|
+
|
|
19
|
+
/** 认证令牌 */
|
|
20
|
+
token?: string; // "590f20c9b32b49bb94bda90b3472d7de"
|
|
21
|
+
|
|
22
|
+
/** 显示名称 */
|
|
23
|
+
displayName?: string; // "李四"
|
|
24
|
+
|
|
25
|
+
/** 头像URL */
|
|
26
|
+
avatarUrl?: string | null; // null
|
|
27
|
+
|
|
28
|
+
/** 用户状态 */
|
|
29
|
+
status?: string; // "ACTIVE"
|
|
30
|
+
|
|
31
|
+
/** 删除标记: 0=正常, 1=已删除 */
|
|
32
|
+
isDel?: 0 | 1; // 0
|
|
33
|
+
|
|
34
|
+
/** 创建时间 (ISO格式) */
|
|
35
|
+
createdAt?: string; // "2025-12-25T16:38:36"
|
|
36
|
+
|
|
37
|
+
/** 更新时间 (ISO格式) */
|
|
38
|
+
updatedAt?: string | null; // null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// 会话接口
|
|
44
|
+
export interface IConversation {
|
|
45
|
+
conversationId: string; // 会话id
|
|
46
|
+
conversationType: ConversationType; // 会话类型 1 P2P单聊, 2 Group群聊
|
|
47
|
+
isMuted: boolean; // 是否免打扰
|
|
48
|
+
isPinned: boolean; // 是否置顶
|
|
49
|
+
lastMessageAt: string; // 最后一条消息的发送时间
|
|
50
|
+
lastMessageContent: string; // 最后一条消息的摘要内容
|
|
51
|
+
unreadCount: number; // 未读消息数
|
|
52
|
+
user: IConversationUserInfo,
|
|
53
|
+
lastMessageType: MessageType; // 最后一条消息的类型
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
// ************** 新增 ****************
|
|
57
|
+
lastMessageSenderId: string; // 最后一条消息发言人ID
|
|
58
|
+
lastMessageSenderName: string; // 最后一条消息发言人姓名
|
|
59
|
+
lastMessageId: string; // 最后一条消息ID
|
|
60
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface IIMUserInfo {
|
|
2
|
+
/** 用户ID */
|
|
3
|
+
id?: string; // 1999386093895651300
|
|
4
|
+
|
|
5
|
+
/** 业务标识 */
|
|
6
|
+
biz?: string; // "sys"
|
|
7
|
+
|
|
8
|
+
/** 账号 */
|
|
9
|
+
account?: string; // "a050ad1f928a488f8ebc196e066bb687"
|
|
10
|
+
|
|
11
|
+
/** 认证令牌 */
|
|
12
|
+
token?: string; // "f0546f7489ef4d84a9b1bef4ab5065cb"
|
|
13
|
+
|
|
14
|
+
/** 显示名称 */
|
|
15
|
+
displayName?: string; // "张三"
|
|
16
|
+
|
|
17
|
+
/** 头像URL */
|
|
18
|
+
avatarUrl?: string | null; // null
|
|
19
|
+
|
|
20
|
+
/** 状态 */
|
|
21
|
+
status?: string; // "ACTIVE"
|
|
22
|
+
|
|
23
|
+
/** 是否删除 (0=未删除, 1=已删除) */
|
|
24
|
+
isDel?: number; // 0
|
|
25
|
+
|
|
26
|
+
/** 创建时间 */
|
|
27
|
+
createdAt?: string; // "2025-12-12T15:49:26"
|
|
28
|
+
|
|
29
|
+
/** 更新时间 */
|
|
30
|
+
updatedAt?: string | null; // null
|
|
31
|
+
}
|
package/types/index.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// 消息类型枚举
|
|
2
|
+
export enum MessageType {
|
|
3
|
+
TEXT = "TEXT",
|
|
4
|
+
IMAGE = "IMAGE",
|
|
5
|
+
VIDEO = "VIDEO",
|
|
6
|
+
CUSTOM = "CUSTOM",
|
|
7
|
+
TEXT_QUOTE = "TEXT_QUOTE",
|
|
8
|
+
AUDIO = "AUDIO",
|
|
9
|
+
IMAGE_TRANSMIT = "IMAGE_TRANSMIT",
|
|
10
|
+
EMOJI_PACK = "EMOJI_PACK",
|
|
11
|
+
RED_ENVELOPE = "RED_ENVELOPE",
|
|
12
|
+
MAP = "MAP",
|
|
13
|
+
GROUP_NOTICE = "GROUP_NOTICE",
|
|
14
|
+
UPDATE_GROUP_NAME = "UPDATE_GROUP_NAME",
|
|
15
|
+
ARTICLE = "ARTICLE",
|
|
16
|
+
SHARE_SBCF = "SHARE_SBCF",
|
|
17
|
+
SHARE_MALL = "SHARE_MALL",
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 消息类型枚举
|
|
21
|
+
export const MessageTypeData = {
|
|
22
|
+
TEXT: "TEXT",
|
|
23
|
+
IMAGE: "IMAGE",
|
|
24
|
+
VIDEO: "VIDEO",
|
|
25
|
+
CUSTOM: "CUSTOM",
|
|
26
|
+
TEXT_QUOTE: "TEXT_QUOTE",
|
|
27
|
+
AUDIO: "AUDIO",
|
|
28
|
+
IMAGE_TRANSMIT: "IMAGE_TRANSMIT",
|
|
29
|
+
EMOJI_PACK: "EMOJI_PACK",
|
|
30
|
+
RED_ENVELOPE: "RED_ENVELOPE",
|
|
31
|
+
MAP: "MAP",
|
|
32
|
+
GROUP_NOTICE: "GROUP_NOTICE",
|
|
33
|
+
UPDATE_GROUP_NAME: "UPDATE_GROUP_NAME",
|
|
34
|
+
ARTICLE: "ARTICLE",
|
|
35
|
+
SHARE_SBCF: "SHARE_SBCF",
|
|
36
|
+
SHARE_MALL: "SHARE_MALL",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// 消息状态枚举
|
|
40
|
+
export enum MessageStatus {
|
|
41
|
+
SENT = "SENT",
|
|
42
|
+
DELIVERED = "DELIVERED",
|
|
43
|
+
READ = "READ",
|
|
44
|
+
RECALLED = "RECALLED",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 消息接口
|
|
48
|
+
export interface IMessage {
|
|
49
|
+
id: string | number;
|
|
50
|
+
conversationId: string;
|
|
51
|
+
senderId: string | number;
|
|
52
|
+
recipientId?: string | number;
|
|
53
|
+
messageType?: MessageType;
|
|
54
|
+
content?: string;
|
|
55
|
+
mediaUrl?: string;
|
|
56
|
+
metadata?: string;
|
|
57
|
+
createdAt: string;
|
|
58
|
+
status: MessageStatus;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ISearchMessageRes {
|
|
62
|
+
/** 消息ID */
|
|
63
|
+
id?: string; // 0
|
|
64
|
+
|
|
65
|
+
/** 会话ID */
|
|
66
|
+
conversationId?: string; // ""
|
|
67
|
+
|
|
68
|
+
/** 发送者用户ID */
|
|
69
|
+
senderId?: string; // 0
|
|
70
|
+
|
|
71
|
+
/** 接收者用户ID (单聊) 或群组ID (群聊) */
|
|
72
|
+
recipientId?: string; // 0
|
|
73
|
+
|
|
74
|
+
/** 消息类型 */
|
|
75
|
+
messageType?: string; // ""
|
|
76
|
+
|
|
77
|
+
/** 消息内容 */
|
|
78
|
+
content?: string; // ""
|
|
79
|
+
|
|
80
|
+
/** 媒体文件URL */
|
|
81
|
+
mediaUrl?: string; // ""
|
|
82
|
+
|
|
83
|
+
/** 自定义消息元数据 (JSON格式) */
|
|
84
|
+
metadata?: string; // ""
|
|
85
|
+
|
|
86
|
+
/** 消息状态 */
|
|
87
|
+
status?: string; // ""
|
|
88
|
+
|
|
89
|
+
/** 消息创建时间 */
|
|
90
|
+
createdAt?: string; // ""
|
|
91
|
+
|
|
92
|
+
/** 消息更新时间 */
|
|
93
|
+
updatedAt?: string; // ""
|
|
94
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { MessageType } from './message-type.type';
|
|
2
|
+
|
|
3
|
+
// SocketAction枚举
|
|
4
|
+
export enum SocketAction {
|
|
5
|
+
// client -> server
|
|
6
|
+
SEND_MESSAGE = 'SEND_MESSAGE',
|
|
7
|
+
READ_RECEIPT = 'READ_RECEIPT',
|
|
8
|
+
RECALL_MESSAGE = 'RECALL_MESSAGE',
|
|
9
|
+
PING = 'PING',
|
|
10
|
+
PONG = 'PONG',
|
|
11
|
+
// server -> client
|
|
12
|
+
ACK = 'ACK',
|
|
13
|
+
MESSAGE = 'MESSAGE',
|
|
14
|
+
DELIVERED = 'DELIVERED',
|
|
15
|
+
RECALLED = 'RECALLED',
|
|
16
|
+
ERROR = 'ERROR'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 发送消息接口
|
|
20
|
+
export interface ISendMessage {
|
|
21
|
+
id?: string | number;
|
|
22
|
+
conversationId?: string;
|
|
23
|
+
senderId?: string | number;
|
|
24
|
+
recipientId?: string | number;
|
|
25
|
+
messageType?: MessageType;
|
|
26
|
+
content?: string;
|
|
27
|
+
mediaUrl?: string;
|
|
28
|
+
metadata?: string;
|
|
29
|
+
time?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// 已读回执接口
|
|
33
|
+
export interface IReadReceipt {
|
|
34
|
+
conversationId?: string;
|
|
35
|
+
lastReadMessageId?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 撤回消息接口
|
|
39
|
+
export interface IRecalledMessage {
|
|
40
|
+
messageId?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 心跳接口
|
|
44
|
+
export interface IPing {
|
|
45
|
+
// timestamp?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Socket负载类型
|
|
49
|
+
export type Payload = ISendMessage | IReadReceipt | IRecalledMessage | IPing;
|
|
50
|
+
|
|
51
|
+
// Socket数据接口
|
|
52
|
+
export interface SocketData<T = Payload> {
|
|
53
|
+
action: SocketAction;
|
|
54
|
+
payload: T;
|
|
55
|
+
}
|
package/types.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK 核心类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** 消息类型 */
|
|
6
|
+
export interface IMessage {
|
|
7
|
+
id: string;
|
|
8
|
+
conversationId: string;
|
|
9
|
+
content: string;
|
|
10
|
+
messageType: string;
|
|
11
|
+
senderId: string | number;
|
|
12
|
+
createdAt: string;
|
|
13
|
+
status: 'SENDING' | 'SENT' | 'FAILED';
|
|
14
|
+
icon?: string;
|
|
15
|
+
recipientId?: string;
|
|
16
|
+
mediaUrl?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 用户类型 */
|
|
20
|
+
export interface IUser {
|
|
21
|
+
id: string | number;
|
|
22
|
+
name: string;
|
|
23
|
+
avatar?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 会话类型 */
|
|
27
|
+
export interface IConversation {
|
|
28
|
+
conversationId: string;
|
|
29
|
+
conversationType: 'P2P' | 'GROUP';
|
|
30
|
+
lastMessageAt: string;
|
|
31
|
+
lastMessageContent: string;
|
|
32
|
+
lastMessageType: string;
|
|
33
|
+
unreadCount: number;
|
|
34
|
+
user: {
|
|
35
|
+
userId: string | number;
|
|
36
|
+
displayName: string;
|
|
37
|
+
account: string;
|
|
38
|
+
avatarUrl: string;
|
|
39
|
+
};
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Store 状态类型 */
|
|
44
|
+
export interface IState {
|
|
45
|
+
conversation: {
|
|
46
|
+
conversations: IConversation[];
|
|
47
|
+
selectedConversationId: string;
|
|
48
|
+
};
|
|
49
|
+
message: {
|
|
50
|
+
messages: Record<string, IMessage[]>;
|
|
51
|
+
messageQueue: IMessage[];
|
|
52
|
+
};
|
|
53
|
+
user: {
|
|
54
|
+
currentUser: IUser | null;
|
|
55
|
+
users: Record<string | number, IUser>;
|
|
56
|
+
};
|
|
57
|
+
websocket: {
|
|
58
|
+
isConnected: boolean;
|
|
59
|
+
reconnectAttempts: number;
|
|
60
|
+
imInstance: any;
|
|
61
|
+
};
|
|
62
|
+
global: {
|
|
63
|
+
loading: boolean;
|
|
64
|
+
error: string | null;
|
|
65
|
+
};
|
|
66
|
+
}
|
package/utils/http.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// 请求方法类型
|
|
2
|
+
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS';
|
|
3
|
+
|
|
4
|
+
// 响应数据接口
|
|
5
|
+
export interface IResponse<T = any> {
|
|
6
|
+
data: T;
|
|
7
|
+
message?: string;
|
|
8
|
+
msg?: string;
|
|
9
|
+
success: boolean;
|
|
10
|
+
code?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// 配置选项接口
|
|
14
|
+
export interface HttpConfig {
|
|
15
|
+
baseURL?: string;
|
|
16
|
+
timeout?: number;
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
beforeRequest?: (options: any) => void;
|
|
19
|
+
afterResponse?: (response: any, options: any) => any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// HTTP错误类
|
|
23
|
+
export class HttpError extends Error {
|
|
24
|
+
constructor(public code: number | string, message: string) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = 'HttpError';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// HTTP客户端类
|
|
31
|
+
export class HttpClient {
|
|
32
|
+
private baseURL: string;
|
|
33
|
+
private timeout: number;
|
|
34
|
+
private headers: Record<string, string>;
|
|
35
|
+
private beforeRequest?: (options: any) => void;
|
|
36
|
+
private afterResponse?: (response: any, options: any) => any;
|
|
37
|
+
|
|
38
|
+
constructor(config: HttpConfig = {}) {
|
|
39
|
+
this.baseURL = config.baseURL || '';
|
|
40
|
+
this.timeout = config.timeout || 60000;
|
|
41
|
+
this.headers = {
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
...config.headers
|
|
44
|
+
};
|
|
45
|
+
this.beforeRequest = config.beforeRequest;
|
|
46
|
+
this.afterResponse = config.afterResponse;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 设置基础URL
|
|
50
|
+
setBaseURL(url: string): void {
|
|
51
|
+
this.baseURL = url;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 设置默认headers
|
|
55
|
+
setHeaders(headers: Record<string, string>): void {
|
|
56
|
+
this.headers = { ...this.headers, ...headers };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 添加请求拦截器
|
|
60
|
+
setBeforeRequest(callback: (options: any) => void): void {
|
|
61
|
+
this.beforeRequest = callback;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 添加响应拦截器
|
|
65
|
+
setAfterResponse(callback: (response: any, options: any) => any): void {
|
|
66
|
+
this.afterResponse = callback;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 发送请求
|
|
70
|
+
request<T = any>(method: Method, url: string, data?: any, options: any = {}): Promise<T> {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
// 构建请求选项
|
|
73
|
+
const requestOptions: any = {
|
|
74
|
+
url: this.baseURL + url,
|
|
75
|
+
method,
|
|
76
|
+
data,
|
|
77
|
+
header: { ...this.headers, ...options.header },
|
|
78
|
+
timeout: options.timeout || this.timeout,
|
|
79
|
+
success: (response:any) => {
|
|
80
|
+
try {
|
|
81
|
+
// 处理响应拦截器
|
|
82
|
+
const processedResponse = this.afterResponse ? this.afterResponse(response, requestOptions) : response.data;
|
|
83
|
+
resolve(processedResponse as T);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
reject(error);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
fail: (error:any) => {
|
|
89
|
+
reject(new HttpError(error.errMsg, '网络请求失败'));
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// 处理请求拦截器
|
|
94
|
+
if (this.beforeRequest) {
|
|
95
|
+
this.beforeRequest(requestOptions);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 发送请求
|
|
99
|
+
uni.request(requestOptions);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// GET请求
|
|
104
|
+
get<T = any>(url: string, params?: any, options?: any): Promise<T> {
|
|
105
|
+
return this.request<T>('GET', url, params, options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// POST请求
|
|
109
|
+
post<T = any>(url: string, data?: any, options?: any): Promise<T> {
|
|
110
|
+
return this.request<T>('POST', url, data, options);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// PUT请求
|
|
114
|
+
put<T = any>(url: string, data?: any, options?: any): Promise<T> {
|
|
115
|
+
return this.request<T>('PUT', url, data, options);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// DELETE请求
|
|
119
|
+
delete<T = any>(url: string, data?: any, options?: any): Promise<T> {
|
|
120
|
+
return this.request<T>('DELETE', url, data, options);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 文件上传
|
|
124
|
+
upload<T = any>(url: string, filePath: string, name: string, formData?: Record<string, any>, options: any = {}): Promise<T> {
|
|
125
|
+
return new Promise((resolve, reject) => {
|
|
126
|
+
const uploadOptions = {
|
|
127
|
+
url: this.baseURL + url,
|
|
128
|
+
filePath,
|
|
129
|
+
name,
|
|
130
|
+
formData,
|
|
131
|
+
header: { ...this.headers, ...options.header },
|
|
132
|
+
success: (response:any) => {
|
|
133
|
+
try {
|
|
134
|
+
const data = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
|
|
135
|
+
resolve(data as T);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
reject(new HttpError('PARSE_ERROR', '响应数据解析失败'));
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
fail: (error:any) => {
|
|
141
|
+
reject(new HttpError(error.errMsg, '文件上传失败'));
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
uni.uploadFile(uploadOptions);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 文件下载
|
|
150
|
+
download(url: string, options: any = {}): Promise<string> {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const downloadOptions = {
|
|
153
|
+
url: this.baseURL + url,
|
|
154
|
+
header: { ...this.headers, ...options.header },
|
|
155
|
+
success: (response:any) => {
|
|
156
|
+
resolve(response.tempFilePath);
|
|
157
|
+
},
|
|
158
|
+
fail: (error :any) => {
|
|
159
|
+
reject(new HttpError(error.errMsg, '文件下载失败'));
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
uni.downloadFile(downloadOptions);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 创建默认HTTP客户端
|
|
169
|
+
// 移除默认配置的baseURL,改为支持动态设置
|
|
170
|
+
const http = new HttpClient({
|
|
171
|
+
headers: {
|
|
172
|
+
'Content-Type': 'application/json'
|
|
173
|
+
},
|
|
174
|
+
afterResponse: (response) => {
|
|
175
|
+
try {
|
|
176
|
+
// debugger;
|
|
177
|
+
const data = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
|
|
178
|
+
if (data.success === false) {
|
|
179
|
+
throw new HttpError(data.code || 'ERROR', data.message || data.msg || '请求失败');
|
|
180
|
+
}
|
|
181
|
+
return data.data || data;
|
|
182
|
+
} catch (error) {
|
|
183
|
+
// 如果响应不是JSON格式,返回原始数据
|
|
184
|
+
console.error('HTTP响应解析失败:', error);
|
|
185
|
+
throw new HttpError('PARSE_ERROR', '响应数据格式不正确');
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// 导出设置BaseURL的方法
|
|
191
|
+
export const setBaseURL = (url: string) => {
|
|
192
|
+
http.setBaseURL(url);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export default http;
|