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

// utils/request.js
1 month ago
import { baseUrl } from "./config";
export default function request(options = {}) {
// 获取token(假设存储在本地storage)
const token = uni.getStorageSync('token') || '';
return new Promise((resolve, reject) => {
uni.request({
1 month ago
url: baseUrl + options.url,
method: options.method || "GET",
data:
1 month ago
options.method === "GET" ? options.data : options.data || {},
header: {
1 month ago
"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);
},
});
});
}