You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.2 KiB
39 lines
1.2 KiB
// utils/request.js
|
|
import { baseUrl } from "./config";
|
|
|
|
export default function request(options = {}) {
|
|
// 获取token(假设存储在本地storage)
|
|
const token = uni.getStorageSync('token') || '';
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: baseUrl + options.url,
|
|
method: options.method || "GET",
|
|
data:
|
|
options.method === "GET" ? options.data : options.data || {},
|
|
header: {
|
|
"Content-Type": options.contentType || "application/json;charset=UTF-8",
|
|
Authorization: token ? `Bearer ${token}` : "",
|
|
...options.header,
|
|
},
|
|
success: (res) => {
|
|
// 这里可根据后端返回结构统一处理
|
|
if (res.statusCode === 200) {
|
|
resolve(res.data);
|
|
} else if (res.statusCode === 401) {
|
|
uni.redirectTo({
|
|
url: "/pages/login/login",
|
|
});
|
|
reject(res);
|
|
} else {
|
|
uni.showToast({ title: res.data.msg || "请求失败", icon: "none" });
|
|
reject(res);
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
uni.showToast({ title: "网络异常", icon: "none" });
|
|
reject(err);
|
|
},
|
|
});
|
|
});
|
|
}
|
|
|