(
event: string,
data = {},
{ toast = true } = {},
)
| 7 | let isSeal = false; |
| 8 | |
| 9 | export default function fetch<T = any>( |
| 10 | event: string, |
| 11 | data = {}, |
| 12 | { toast = true } = {}, |
| 13 | ): Promise<[string | null, T | null]> { |
| 14 | if (isSeal) { |
| 15 | Message.error(SEAL_TEXT); |
| 16 | return Promise.resolve([SEAL_TEXT, null]); |
| 17 | } |
| 18 | return new Promise((resolve) => { |
| 19 | socket.emit(event, data, (res: any) => { |
| 20 | if (typeof res === 'string') { |
| 21 | if (toast) { |
| 22 | Message.error(res); |
| 23 | } |
| 24 | /** |
| 25 | * 服务端返回封禁状态后, 本地存储该状态 |
| 26 | * 用户再触发接口请求时, 直接拒绝 |
| 27 | */ |
| 28 | if (res === SEAL_TEXT) { |
| 29 | isSeal = true; |
| 30 | // 用户封禁和ip封禁时效不同, 这里用的短时间 |
| 31 | setTimeout(() => { |
| 32 | isSeal = false; |
| 33 | }, SEAL_USER_TIMEOUT); |
| 34 | } |
| 35 | resolve([res, null]); |
| 36 | } else { |
| 37 | resolve([null, res]); |
| 38 | } |
| 39 | }); |
| 40 | }); |
| 41 | } |
no outgoing calls
no test coverage detected