Compare commits
2 Commits
53f100cb4e
...
9e518a244a
| Author | SHA1 | Date | |
|---|---|---|---|
| 9e518a244a | |||
| a5b51b3492 |
@ -56,7 +56,9 @@ const install = function (app: App) {
|
|||||||
app.use(component as unknown as { install: () => any });
|
app.use(component as unknown as { install: () => any });
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const axios = Network.axios;
|
const axios = Network.axios;
|
||||||
|
const setGlobalErrorCallback = Network.setGlobalErrorCallback;
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
install
|
install
|
||||||
@ -65,6 +67,7 @@ export default {
|
|||||||
export {
|
export {
|
||||||
axios,
|
axios,
|
||||||
Network,
|
Network,
|
||||||
|
setGlobalErrorCallback,
|
||||||
|
|
||||||
UserAPI,
|
UserAPI,
|
||||||
CommonAPI,
|
CommonAPI,
|
||||||
|
|||||||
@ -2,6 +2,14 @@ import axios, { InternalAxiosRequestConfig } from "axios";
|
|||||||
import { Response } from "~/types/Model";
|
import { Response } from "~/types/Model";
|
||||||
import { Cooker, Time, userStore } from "~/index";
|
import { Cooker, Time, userStore } from "~/index";
|
||||||
|
|
||||||
|
type ErrorCallback = (response: Response) => void;
|
||||||
|
|
||||||
|
let globalErrorCallback: ErrorCallback | null = null;
|
||||||
|
|
||||||
|
export const setGlobalErrorCallback = (callback: ErrorCallback) => {
|
||||||
|
globalErrorCallback = callback;
|
||||||
|
};
|
||||||
|
|
||||||
const userTokenInterceptors = async (config: InternalAxiosRequestConfig<any>) => {
|
const userTokenInterceptors = async (config: InternalAxiosRequestConfig<any>) => {
|
||||||
let token = userStore.loginUser.token?.value;
|
let token = userStore.loginUser.token?.value;
|
||||||
const cookieToken = Cooker.get("Token");
|
const cookieToken = Cooker.get("Token");
|
||||||
@ -25,19 +33,22 @@ const userTokenInterceptors = async (config: InternalAxiosRequestConfig<any>) =>
|
|||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
axios.defaults.withCredentials = true;
|
axios.defaults.withCredentials = true;
|
||||||
axios.interceptors.response.use((response: any) => {
|
axios.interceptors.response.use((axiosResp: any) => {
|
||||||
if (!response.config.responseType) {
|
if (!axiosResp.config.responseType) {
|
||||||
// 服务端返回
|
// 服务端返回
|
||||||
const data = response.data as Response;
|
const serverResp = axiosResp.data as Response;
|
||||||
if (data.code < 40000) {
|
if (serverResp.code < 40000) {
|
||||||
// 200 或 300 HTTP 状态段视为成功
|
// 200 或 300 HTTP 状态段视为成功
|
||||||
return data.data;
|
return serverResp.data;
|
||||||
} else {
|
} else {
|
||||||
|
if (globalErrorCallback) {
|
||||||
|
globalErrorCallback(serverResp);
|
||||||
|
}
|
||||||
// 由调用方处理
|
// 由调用方处理
|
||||||
return Promise.reject(data.msg);
|
return Promise.reject(serverResp.msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return response.data;
|
return axiosResp.data;
|
||||||
}, (error: any) => {
|
}, (error: any) => {
|
||||||
// 请求错误
|
// 请求错误
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -57,5 +68,6 @@ axios.interceptors.response.use((response: any) => {
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
axios,
|
axios,
|
||||||
userTokenInterceptors
|
userTokenInterceptors,
|
||||||
|
setGlobalErrorCallback
|
||||||
};
|
};
|
||||||
|
|||||||
@ -144,7 +144,7 @@ export default class Toolkit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成随机数
|
* 生成随机数(整数)
|
||||||
*
|
*
|
||||||
* @param min 最小值
|
* @param min 最小值
|
||||||
* @param max 最大值
|
* @param max 最大值
|
||||||
@ -153,6 +153,16 @@ export default class Toolkit {
|
|||||||
return Math.floor(Math.random() * (max + 1 - min)) + min;
|
return Math.floor(Math.random() * (max + 1 - min)) + min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机数(浮点数)
|
||||||
|
*
|
||||||
|
* @param min 最小值
|
||||||
|
* @param max 最大值
|
||||||
|
*/
|
||||||
|
public static randomDouble(min = 0, max = 1): number {
|
||||||
|
return Math.random() * (max - min) + min;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base64 数据转文件
|
* Base64 数据转文件
|
||||||
*
|
*
|
||||||
@ -351,4 +361,66 @@ export default class Toolkit {
|
|||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static doNotNull(arg: any, func: (arg: any) => void): void {
|
||||||
|
if (arg) {
|
||||||
|
func(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static toCssSize(value: number | string): string {
|
||||||
|
if (typeof value === "number") {
|
||||||
|
return `${value}px`;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置随机间隔执行
|
||||||
|
*
|
||||||
|
* @param config 配置对象
|
||||||
|
* @param config.handler 处理函数,如果提供了 min 和 max,则会接收随机数作为参数
|
||||||
|
* @param config.handleRate 执行概率,0-1 之间,默认 1(总是执行)
|
||||||
|
* @param config.interval 间隔时间(毫秒)
|
||||||
|
* @param config.min 随机数最小值(可选),提供时会生成随机数传给 handler
|
||||||
|
* @param config.max 随机数最大值(可选),提供时会生成随机数传给 handler
|
||||||
|
* @returns 定时器 ID
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```js
|
||||||
|
* // 简单的随机执行
|
||||||
|
* setRandomInterval({
|
||||||
|
* handler: () => console.log('executed'),
|
||||||
|
* handleRate: 0.5,
|
||||||
|
* interval: 1000
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* // 带随机数参数的执行
|
||||||
|
* setRandomInterval({
|
||||||
|
* handler: (value) => console.log('random value:', value),
|
||||||
|
* handleRate: 1,
|
||||||
|
* min: 0,
|
||||||
|
* max: 100,
|
||||||
|
* interval: 1000
|
||||||
|
* })
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
public static setRandomInterval(config: {
|
||||||
|
handler: Function | ((value: number) => void);
|
||||||
|
handleRate?: number;
|
||||||
|
interval?: number;
|
||||||
|
min?: number;
|
||||||
|
max?: number;
|
||||||
|
}): NodeJS.Timeout {
|
||||||
|
const { handler, handleRate = 1, interval, min, max } = config;
|
||||||
|
return setInterval(() => {
|
||||||
|
if (Math.random() < handleRate) {
|
||||||
|
if (min !== undefined && max !== undefined) {
|
||||||
|
(handler as (value: number) => void)(this.randomDouble(min, max));
|
||||||
|
} else {
|
||||||
|
(handler as Function)();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, interval);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user