工作端小程序
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.
 
 
 
 
 

40 lines
1.3 KiB

// utils/request.js
const BASE_URL = 'https://your-api-url.com'; // 替换为你的后端地址
export default function request(options = {}) {
// 获取token(假设存储在本地storage)
const token = uni.getStorageSync('token') || '';
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + options.url,
method: options.method || "GET",
data:
options.method === "GET" ? options.data : JSON.stringify(options.data),
header: {
"Content-Type": options.contentType || "application/json",
Authorization: token ? `Bearer ${token}` : "",
...options.header,
},
success: (res) => {
// 这里可根据后端返回结构统一处理
if (res.statusCode === 200) {
resolve(res.data);
} else if (res.statusCode === 401) {
uni.showToast({ title: "登录失效,请重新登录", icon: "none" });
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);
},
});
});
}