9 changed files with 226 additions and 86 deletions
@ -0,0 +1,3 @@ |
|||
{ |
|||
"aa": "1231" |
|||
} |
@ -0,0 +1,97 @@ |
|||
import { AxiosRequestConfig, AxiosResponse, AxiosError } from "axios" |
|||
import { IPlugin } from "../base" |
|||
import { Id, toast } from "react-toastify" |
|||
|
|||
declare module "axios" { |
|||
interface AxiosRequestConfig { |
|||
loadingAttrs?: Partial<IConfig> |
|||
} |
|||
} |
|||
|
|||
type AxiosRequestConfigPlus = AxiosRequestConfig & { |
|||
$loading_ToastID?: Id |
|||
$loading_CreateTimeStamp?: number |
|||
$loading_ToastShowFn?: () => void |
|||
$loading_ToastHideFn?: () => void |
|||
} |
|||
|
|||
interface IConfig { |
|||
text: string |
|||
min: number |
|||
minShow: boolean |
|||
} |
|||
|
|||
// 取消重复请求
|
|||
export class LoadingPlugin implements IPlugin { |
|||
static name: string = "loading" |
|||
name: string = "loading" |
|||
|
|||
defaultConfig: IConfig = { |
|||
text: "加载中", |
|||
min: 1000, |
|||
minShow: false, |
|||
} |
|||
config: IConfig = JSON.parse(JSON.stringify(this.defaultConfig)) |
|||
|
|||
constructor(config: Partial<IConfig> = {}) { |
|||
; (Object.keys(config) as (keyof IConfig)[]).forEach(v => { |
|||
if (config[v]) { |
|||
(this.config as any)[v] = config[v] |
|||
} |
|||
}) |
|||
} |
|||
|
|||
getConfig<T extends keyof IConfig>(key: T, config: Partial<IConfig> = {}) { |
|||
const keys = Object.keys(config) as T[] |
|||
for (let i = 0; i < keys.length; i++) { |
|||
const v = keys[i]; |
|||
if (v === key) { |
|||
return (config as IConfig)[key] |
|||
} |
|||
} |
|||
return this.config[key] |
|||
} |
|||
|
|||
beforeRequestConfig({ config }: { config: AxiosRequestConfigPlus }) { |
|||
const text = this.getConfig("text", config.loadingAttrs) |
|||
config.$loading_ToastShowFn = () => { |
|||
const id = toast.loading(text) |
|||
config.$loading_CreateTimeStamp = new Date().getTime() |
|||
config.$loading_ToastID = id |
|||
} |
|||
if (!this.getConfig("minShow", config.loadingAttrs)) { |
|||
config.$loading_ToastShowFn() |
|||
delete config.$loading_ToastShowFn |
|||
} |
|||
} |
|||
beforeResponse({ response }: { response: AxiosResponse }) { |
|||
const { config } = response as { config: AxiosRequestConfigPlus } |
|||
if (!this.getConfig("minShow", config.loadingAttrs)) { |
|||
const min = this.getConfig("min", config.loadingAttrs) |
|||
if (config.$loading_ToastID) { |
|||
const [ToastID, showTimeStamp] = [config.$loading_ToastID, config.$loading_CreateTimeStamp] |
|||
const oft = new Date().getTime() - showTimeStamp! |
|||
if (oft < min) { |
|||
setTimeout(() => { |
|||
toast.dismiss(ToastID) |
|||
}, min - oft); |
|||
} else { |
|||
toast.dismiss(ToastID) |
|||
} |
|||
delete config.$loading_ToastID |
|||
delete config.$loading_CreateTimeStamp |
|||
} |
|||
} else { |
|||
|
|||
} |
|||
|
|||
} |
|||
beforeResponseError(argu: { error: AxiosError }) { |
|||
const requestConfig = argu.error.config! as AxiosRequestConfigPlus |
|||
if (requestConfig.$loading_ToastID) { |
|||
const [ToastID] = requestConfig.$loading_ToastID.split("$") |
|||
toast.dismiss(ToastID) |
|||
delete requestConfig.$loading_ToastID |
|||
} |
|||
} |
|||
} |
@ -1,14 +1,39 @@ |
|||
import { Fly } from "./http" |
|||
import { RepeatPlugin } from "./http/Repeat" |
|||
import { RepeatPlugin } from "./http/plugins/Repeat" |
|||
import { LoadingPlugin } from "./http/plugins/Loading" |
|||
|
|||
const allPlugin = { |
|||
loading: new LoadingPlugin(), |
|||
repeat: new RepeatPlugin(), |
|||
} |
|||
|
|||
Fly.use([allPlugin.loading]) |
|||
|
|||
Fly.init("$defalut", { |
|||
timeout: 10000, |
|||
}, [new RepeatPlugin()]) |
|||
}, [allPlugin.repeat]) |
|||
|
|||
Fly.init("empty", { |
|||
timeout: 10000, |
|||
}) |
|||
// Fly.disposeAll()
|
|||
// https://api.52vmy.cn/
|
|||
// Fly.invoke().request({ method: "get", url: "http://localhost:5173/asd", global: false }).then((res) => console.log(res.data)).catch(console.log)
|
|||
// Fly.invoke().request({ method: "get", url: "http://localhost:5173/", global: true }).then((res) => console.log(res.data)).catch(console.log)
|
|||
|
|||
// Fly.invoke().request({ method: "get", url: "https://api.52vmy.cn/api/wl/word/bing/tu", global: true }).then((res) => console.log(res.data)).catch(console.log)
|
|||
|
|||
// Fly.invoke().request({ method: "get", url: "https://api.52vmy.cn/api/wl/word/bing/tu", global: true }).then((res) => console.log(res.data)).catch(console.log)
|
|||
|
|||
// Fly.invoke("empty").request({ method: "get", url: "https://api.52vmy.cn/api/wl/yan/bay" }).then((res) => console.log(res.data)).catch(console.log)
|
|||
|
|||
// Fly.invoke("empty").request({ method: "get", url: "https://api.52vmy.cn/api/img/tu/girl" }).then((res) => console.log(res.data)).catch(console.log)
|
|||
|
|||
// setTimeout(()=>{
|
|||
// Fly.invoke("empty").request({method: "get", url: "https://api.52vmy.cn/api/img/tu/man"}).then((res)=>console.log(res.data)).catch(console.log)
|
|||
|
|||
// Fly.invoke().callPluginByName<RepeatPlugin>(RepeatPlugin.name, "clearPendingPool")
|
|||
// }, 0)
|
|||
|
|||
export { |
|||
Fly |
|||
|
Loading…
Reference in new issue