uniapp封装网络请求
第一步先创建utils文件夹
第二步新建request.js
import env from "@/utils/env";
function service(options = {}) {
options.url = `${env.baseUrl}${options.url}`;
let token = uni.getStorageSync("token");
options.header = {
"content-type": "application/json",
Authorization: `Bearer ${token || false}`,
};
return new Promise((resolved, rejected) => {
options.success = (res) => {
if (res.data.code !== 200) {
uni.showToast({
icon: "none",
duration: 3000,
title: `${res.data.msg}`,
});
rejected(res);
} else {
resolved(res.data.data);
}
};
options.fail = (err) => {
uni.showToast({
icon: "none",
duration: 3000,
title: "服务器错误,请稍后再试",
});
rejected(err);
};
uni.request(options);
});
}
export default service;
第三步 新建env.js
export default {
appid: "",
baseUrl: "https://localhost:8080/",
};
如何使用:
import service from "@/utils/request.js";
export const getBanner = () => {
return service({
url: "/home/swiperdata",
method: "get",
});
};