调用方法

1
2
3
4
5
6
7
8
import { get, post } from "../../request/request";

// GET请求
const _res = await get("https://api.vvhan.com/api/ian");
console.log(_res);
// POST请求
const _res = await post("https://api.vvhan.com/api/ian");
console.log(_res);

request.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const request = (url, options) => {
return new Promise((resolve) => {
options.isLoading &&
wx.showLoading({
title: "正在加载",
});
wx.request({
url,
method: options.method,
data: options.data,
header: {
"Content-Type": "application/x-www-form-urlencoded",
},
success(res) {
resolve(res.data);
options.isLoading && wx.hideLoading();
},
fail(error) {
options.isLoading && wx.hideLoading();
wx.showToast({
icon: "none",
title: "请求失败",
duration: 1400,
});
},
});
});
};

const get = (url, options = {}, isLoading = true) => {
return request(url, {
method: "GET",
data: options,
isLoading,
});
};

const post = (url, options = {}, isLoading = true) => {
return request(url, {
method: "POST",
data: options,
isLoading,
});
};
module.exports = {
get,
post,
};