|
@@ -13,8 +13,8 @@ const instance = axios.create({
|
|
|
instance.interceptors.request.use(
|
|
|
(config: AxiosRequestConfig): any => {
|
|
|
// 可在请求发送前对config进行修改,如添加请求头等
|
|
|
- const { getToken } = useStore()
|
|
|
- const token = getToken
|
|
|
+ const { getToken } = useStore();
|
|
|
+ const token = getToken;
|
|
|
const headers = config.headers || {};
|
|
|
headers["Token"] = token;
|
|
|
config.headers = headers;
|
|
@@ -46,38 +46,65 @@ instance.interceptors.response.use(
|
|
|
|
|
|
// 封装GET请求
|
|
|
export async function get(url: string, params?: any): Promise<any> {
|
|
|
- return instance
|
|
|
- .get(url, { params })
|
|
|
- .then((response) => response.data)
|
|
|
- .catch((error) => {
|
|
|
- throw error;
|
|
|
- });
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ instance
|
|
|
+ .get(url, { params })
|
|
|
+ .then(({ data }: any) => {
|
|
|
+ const { code } = data;
|
|
|
+ if (code === "ok") {
|
|
|
+ resolve(data);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ reject(data);
|
|
|
+ })
|
|
|
+ .catch((error) => {
|
|
|
+ reject(error);
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
// 封装POST请求
|
|
|
export async function post(url: string, data?: any): Promise<any> {
|
|
|
- return instance
|
|
|
- .post(url, data, {
|
|
|
- headers: {
|
|
|
- "Content-type": " application/x-www-form-urlencoded; charset=UTF-8",
|
|
|
- },
|
|
|
- })
|
|
|
- .then((response) => response.data)
|
|
|
- .catch((error) => {
|
|
|
- throw error;
|
|
|
- });
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ instance
|
|
|
+ .post(url, data, {
|
|
|
+ headers: {
|
|
|
+ "Content-type": " application/x-www-form-urlencoded; charset=UTF-8",
|
|
|
+ },
|
|
|
+ })
|
|
|
+ .then(({ data }: any) => {
|
|
|
+ const { code } = data;
|
|
|
+ if (code === "ok") {
|
|
|
+ resolve(data);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ reject(data);
|
|
|
+ })
|
|
|
+ .catch((error) => {
|
|
|
+ reject(error);
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
// 封装文件上传请求
|
|
|
export async function uploadFile(url: string, data?: any): Promise<any> {
|
|
|
- return instance
|
|
|
- .post(url, data, {
|
|
|
- headers: {
|
|
|
- "Content-type": "multipart/form-data",
|
|
|
- },
|
|
|
- })
|
|
|
- .then((response) => response.data)
|
|
|
- .catch((error) => {
|
|
|
- throw error;
|
|
|
- });
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ instance
|
|
|
+ .post(url, data, {
|
|
|
+ headers: {
|
|
|
+ "Content-type": "multipart/form-data",
|
|
|
+ },
|
|
|
+ })
|
|
|
+ .then(({ data }: any) => {
|
|
|
+ const { code } = data;
|
|
|
+ if (code === "ok") {
|
|
|
+ resolve(data);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ reject(data);
|
|
|
+ })
|
|
|
+ .catch((error) => {
|
|
|
+ reject(error);
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|