i
This commit is contained in:
46
src/utils/request.ts
Normal file
46
src/utils/request.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import Taro from "@tarojs/taro";
|
||||
|
||||
const BASE_URL = () => {
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
return "http://192.168.2.3:9000";
|
||||
} else {
|
||||
return "https://api.imooc.hybrid.lgdsunday.club";
|
||||
}
|
||||
};
|
||||
|
||||
// interface Res {
|
||||
//
|
||||
// }
|
||||
|
||||
type Method = "GET" | "POST" | "PUT" | "DELETE";
|
||||
|
||||
export const request = (
|
||||
url: string,
|
||||
data: object = {},
|
||||
method: Method = "GET"
|
||||
): Promise<any> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
Taro.request({
|
||||
url: BASE_URL() + "/app" + url,
|
||||
data: data,
|
||||
method: method,
|
||||
header: {
|
||||
"content-type": "application/json",
|
||||
token: Taro.getStorageSync("token"),
|
||||
},
|
||||
success: ({data}) => {
|
||||
console.log(data);
|
||||
if (data.code !== 200)
|
||||
return reject({code: 1, msg: data.msg});
|
||||
resolve(data);
|
||||
},
|
||||
fail: () => {
|
||||
Taro.showToast({
|
||||
title: "服务器异常",
|
||||
icon: "none",
|
||||
});
|
||||
reject({code: 1, msg: "服务器异常"});
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user