feat: 新增环境变量配置|状态管理
This commit is contained in:
@@ -1,77 +1,77 @@
|
||||
import { BASE_URL } from "../../constant/base";
|
||||
// import { BASE_URL } from "../../constant/base";
|
||||
import { goLogin } from "@/hooks/useGoLogin";
|
||||
|
||||
const defaultConfig = {
|
||||
header: {
|
||||
Authorization: '', // 可在此动态设置 token
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
header: {
|
||||
Authorization: "", // 可在此动态设置 token
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
const BASE_URL = import.meta.env.VITE_BASE_URL;
|
||||
|
||||
function request(url, args = {}, method = 'POST', customConfig = {}) {
|
||||
// 判断 url 是否以 http 开头
|
||||
if (!/^http/.test(url)) {
|
||||
url = BASE_URL + url;
|
||||
function request(url, args = {}, method = "POST", customConfig = {}) {
|
||||
// 判断 url 是否以 http 开头
|
||||
if (!/^http/.test(url)) {
|
||||
url = BASE_URL + url;
|
||||
}
|
||||
// 动态获取 token
|
||||
const token = uni.getStorageSync("token");
|
||||
|
||||
let header = {
|
||||
...defaultConfig.header,
|
||||
...customConfig.header,
|
||||
};
|
||||
// 判断是否需要 token
|
||||
if (customConfig.noToken) {
|
||||
delete header.Authorization;
|
||||
} else {
|
||||
if (token) {
|
||||
header.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
// 动态获取 token
|
||||
const token = uni.getStorageSync('token');
|
||||
}
|
||||
|
||||
let header = {
|
||||
...defaultConfig.header,
|
||||
...customConfig.header
|
||||
};
|
||||
// 判断是否需要 token
|
||||
if (customConfig.noToken) {
|
||||
delete header.Authorization;
|
||||
} else {
|
||||
if (token) {
|
||||
header.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
}
|
||||
console.log("请求头customConfig:" + JSON.stringify(customConfig));
|
||||
|
||||
console.log("请求头customConfig:" + JSON.stringify(customConfig))
|
||||
const config = {
|
||||
...defaultConfig,
|
||||
...customConfig,
|
||||
header,
|
||||
};
|
||||
|
||||
const config = {
|
||||
...defaultConfig,
|
||||
...customConfig,
|
||||
header
|
||||
};
|
||||
console.log("请求接口:" + url);
|
||||
console.log("请求头:" + JSON.stringify(config));
|
||||
console.log("请求参数:" + JSON.stringify(args));
|
||||
|
||||
console.log("请求接口:" + url)
|
||||
console.log("请求头:" + JSON.stringify(config))
|
||||
console.log("请求参数:" + JSON.stringify(args))
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url,
|
||||
data: args,
|
||||
method,
|
||||
...config,
|
||||
success: (res) => {
|
||||
console.log("请求响应:" + JSON.stringify(res))
|
||||
resolve(res.data)
|
||||
if(res.statusCode && res.statusCode === 424) {
|
||||
uni.setStorageSync('token', '')
|
||||
goLogin();
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error("请求失败:", err);
|
||||
reject(err)
|
||||
}
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url,
|
||||
data: args,
|
||||
method,
|
||||
...config,
|
||||
success: (res) => {
|
||||
console.log("请求响应:" + JSON.stringify(res));
|
||||
resolve(res.data);
|
||||
if (res.statusCode && res.statusCode === 424) {
|
||||
uni.setStorageSync("token", "");
|
||||
goLogin();
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error("请求失败:", err);
|
||||
reject(err);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 默认 POST
|
||||
request.post = function(url, args = {}, config = {}) {
|
||||
return request(url, args, 'POST', config);
|
||||
request.post = function (url, args = {}, config = {}) {
|
||||
return request(url, args, "POST", config);
|
||||
};
|
||||
|
||||
// 支持 GET
|
||||
request.get = function(url, args = {}, config = {}) {
|
||||
return request(url, args, 'GET', config);
|
||||
request.get = function (url, args = {}, config = {}) {
|
||||
return request(url, args, "GET", config);
|
||||
};
|
||||
|
||||
|
||||
export default request;
|
||||
|
||||
Reference in New Issue
Block a user