-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
唐道勇
authored and
唐道勇
committed
Apr 12, 2022
1 parent
6a28872
commit ad8556c
Showing
10 changed files
with
216 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# 格式 等号前后没有空格 字符串无需加引号 | ||
VITE_APP_BASE_API=/dev-api | ||
VITE_APP_BASE_HOST=https://test.openalpha.cn/open | ||
VITE_APP_BASE_HOST=https://test.openalpha.cn/support-api/bigV | ||
# build time | ||
VITE_APP_BUILD_TIME=build time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,10 @@ | ||
/* | ||
* @Author: matiastang | ||
* @Date: 2021-11-11 18:55:21 | ||
* @Date: 2022-04-12 18:16:59 | ||
* @LastEditors: matiastang | ||
* @LastEditTime: 2021-12-08 19:04:50 | ||
* @FilePath: /datumwealth-openalpha-front/src/common/request/requestThrottle.ts | ||
* @Description: | ||
* @LastEditTime: 2022-04-12 18:58:48 | ||
* @FilePath: /matias-axios-throttle/src/throttle/index.ts | ||
* @Description: 导出 | ||
*/ | ||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' | ||
import { getObjectHash } from './util' | ||
import { addTask, sameTask, removeTask } from './task' | ||
|
||
/** | ||
* 发送请求 | ||
* @param {Object} options 可以包含uni.request中的所有非Funciton的参数 | ||
*/ | ||
function requestThrottle(httpAxios: AxiosInstance, options: AxiosRequestConfig) { | ||
const requestHash = getObjectHash({ | ||
data: options.data, | ||
params: options.params, | ||
baseURL: options.baseURL, | ||
url: options.url, | ||
method: options.method, | ||
}) | ||
return new Promise( | ||
(resolve: (value: AxiosResponse<any, any>) => void, reject: (reason?: any) => void) => { | ||
const task = sameTask(requestHash) | ||
if (task) { | ||
// 重复请求 | ||
// 添加回调 | ||
task.relevance.push({ | ||
resolve, | ||
reject, | ||
}) | ||
return | ||
} | ||
const CancelToken = axios.CancelToken | ||
const source = CancelToken.source() | ||
addTask(requestHash, options, source) | ||
httpAxios | ||
.request({ | ||
...options, | ||
cancelToken: source.token, | ||
}) | ||
.then((res) => { | ||
_sendSuccess(requestHash, res) | ||
resolve(res) | ||
}) | ||
.catch((err) => { | ||
_sendFail(requestHash, err) | ||
reject(err) | ||
}) | ||
} | ||
) | ||
} | ||
|
||
/** | ||
* 请求成功处理 | ||
* @param {Object} hash | ||
* @param {Object} res | ||
*/ | ||
function _sendSuccess(hash: string, res: AxiosResponse<any, any>) { | ||
const task = sameTask(hash, true) | ||
if (task === null) { | ||
console.warn(`success未找到${hash}`) | ||
return | ||
} | ||
for (const relevance of task.relevance) { | ||
// 触发成功回调 | ||
relevance.resolve(res) | ||
} | ||
removeTask(hash) | ||
} | ||
|
||
/** | ||
* 请求失败处理 | ||
* @param {Object} hash | ||
* @param {Object} err | ||
*/ | ||
function _sendFail(hash: string, err: any) { | ||
const task = sameTask(hash, true) | ||
if (task === null) { | ||
console.warn(`error未找到${hash}`) | ||
return | ||
} | ||
for (const relevance of task.relevance) { | ||
// 触发失败回调 | ||
relevance.reject(err) | ||
} | ||
removeTask(hash) | ||
} | ||
|
||
export default requestThrottle | ||
export * from './throttle' | ||
export { abortAll, abortRequestTasks, keepRequestTasks, getRequestUrls } from './task' |
Oops, something went wrong.