i
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import Taro from '@tarojs/taro'
|
||||
import Taro from "@tarojs/taro";
|
||||
|
||||
const BASE_URL = process.env.NODE_ENV === 'development' ? 'http://192.168.2.3:9000' : 'https://www.wanzhuanyongcheng.cn'
|
||||
export const BASE_URL =
|
||||
process.env.NODE_ENV === "development"
|
||||
? "http://192.168.2.3:9000/app"
|
||||
: "https://www.wanzhuanyongcheng.cn/app";
|
||||
|
||||
interface Res<T> {
|
||||
code: number;
|
||||
@@ -8,41 +11,47 @@ interface Res<T> {
|
||||
msg: string;
|
||||
}
|
||||
|
||||
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
type Method = "GET" | "POST" | "PUT" | "DELETE";
|
||||
|
||||
const request = (
|
||||
url: string,
|
||||
data: object = {},
|
||||
method: Method = 'GET'
|
||||
method: Method = "GET"
|
||||
): Promise<Res<any>> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
Taro.showLoading({
|
||||
title: '加载中...',
|
||||
mask: true
|
||||
})
|
||||
title: "加载中...",
|
||||
mask: true,
|
||||
});
|
||||
Taro.request({
|
||||
url: BASE_URL + '/app' + url,
|
||||
url: BASE_URL + url,
|
||||
data: data,
|
||||
method: method,
|
||||
header: {
|
||||
'content-type': 'application/json',
|
||||
token: Taro.getStorageSync('token')
|
||||
"content-type": "application/json",
|
||||
token: Taro.getStorageSync("token"),
|
||||
},
|
||||
success: ({data}) => {
|
||||
Taro.hideLoading()
|
||||
if (data.code !== 200)
|
||||
return reject({code: 1, msg: data.msg})
|
||||
resolve(data)
|
||||
success: ({ data }) => {
|
||||
Taro.hideLoading();
|
||||
if (data.code !== 200) {
|
||||
Taro.showToast({
|
||||
title: data.msg,
|
||||
icon: "none",
|
||||
});
|
||||
reject(data);
|
||||
return;
|
||||
}
|
||||
resolve(data);
|
||||
},
|
||||
fail: () => {
|
||||
Taro.showToast({
|
||||
title: '服务器异常',
|
||||
icon: 'none'
|
||||
})
|
||||
reject({code: 1, msg: '服务器异常'})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
title: "服务器异常",
|
||||
icon: "none",
|
||||
});
|
||||
reject({ code: 1, msg: "服务器异常" });
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default request
|
||||
export default request;
|
||||
|
||||
Reference in New Issue
Block a user