response-handler.ts
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { RequestConfig, ResponseData } from "../../types";
import { createEventBus } from "../pubsub";
// 创建全局事件总线
const eventBus = createEventBus();
/**
* 事件类型定义
*/
export type EventType = "logout" | "error" | "success" | "request" | "response";
/**
* 事件处理器类型定义
*/
export type EventHandler = (data: any, config: RequestConfig) => void;
/**
* 注册事件处理器
* @param {EventType} eventType 事件类型
* @param {EventHandler} handler 处理函数
* @returns {Function} 取消订阅的函数
*/
export function addEventListener(
eventType: EventType,
handler: EventHandler
): Function {
const unsubscribe = eventBus.subscribe(eventType, handler);
return unsubscribe;
}
/**
* 移除事件处理器
* @param {EventType} eventType 事件类型
* @param {EventHandler} handler 处理函数
*/
export function removeEventListener(
eventType: EventType,
handler?: EventHandler
): void {
eventBus.unsubscribe(eventType, handler);
}
/**
* 触发事件
* @param {EventType} eventType 事件类型
* @param {any} data 事件数据
* @param {RequestConfig} config 请求配置
*/
export function dispatchEvent(
eventType: EventType,
data: any,
config: RequestConfig
): void {
eventBus.publish(eventType, data, config);
}
/**
* 处理响应数据,集成用户提供的响应拦截器逻辑
* @param {ResponseData} responseData 响应数据
* @param {RequestConfig} config 请求配置
* @returns {ResponseData} 处理后的响应数据
*/
export function processResponse(
responseData: ResponseData<any>,
config: RequestConfig
): ResponseData<any> {
// 触发响应事件,无论状态如何
dispatchEvent("response", responseData, config);
const { data: res } = responseData;
const { returnAll, httpOptions } = config;
// 如果没有提供httpOptions或不需要特殊处理,直接返回原始响应
if (!httpOptions || !httpOptions.customer) {
return responseData;
}
const {
response: {
code_key,
errId = "errId",
code_success_value = "0",
data_key = "data",
errId_logout_value,
} = {},
} = httpOptions.customer;
// 如果没有提供code_key,直接返回原始响应
if (!code_key) {
return responseData;
}
// 查找适用的code键
let codekey = Array.isArray(code_key)
? code_key.find((key) => res.hasOwnProperty(key))
: code_key;
let code = res[codekey];
let errValue = res[errId];
// 如果需要返回完整响应或不需要弹窗提示
if (returnAll) {
// 检查是否需要登出
if (
(code && code == errId_logout_value) ||
(errValue && errValue == errId_logout_value)
) {
// 触发登出事件
dispatchEvent("logout", res, config);
}
return { ...responseData, data: res };
}
// 特殊的code处理 - 使用发布订阅模式
const codeString = code + "";
// 对于成功状态码,使用成功处理器
if (codeString === code_success_value + "") {
// 触发成功事件
dispatchEvent("success", res, config);
}
// 对于登出状态码,使用登出处理器
else if (
codeString === errId_logout_value + "" ||
(errValue && errValue == errId_logout_value)
) {
// 触发登出事件
dispatchEvent("logout", res, config);
}
// 其他情况使用默认处理器
else {
// 触发错误事件
dispatchEvent("error", res, config);
}
// 如果没有处理器或处理器没有返回结果,则返回默认数据
return { ...responseData, data: res[data_key] };
}