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.

145 lines
4.2 KiB

1 year ago
import qs from "../lib/qs/shim.js";
import { isLogin } from "../api/user.js";
import { useAppStore } from "../store/index.js";
import { getFileName } from "../utils/index.js";
1 year ago
import settings from "../config/settings.js";
// const requestSettings = {
// baseURL: "/api",
// };
1 year ago
const addToken = async (options) => {
if (await isLogin()) {
const appStore = useAppStore();
options.headers ??= {};
options.headers.Authorization = `Bearer ${appStore.token}`;
}
};
const getUrl = (url) => {
if (url.startsWith("http")) {
1 year ago
return url;
}
let result = settings.baseURL;
return (result += `/${url}`);
// let result = requestSettings.baseURL;
// if (withoutCulture) {
// result += `/${url}`;
// } else {
// const appStore = useAppStore();
// result += withoutCulture ? "/" : `/${appStore.localization.locale}/`;
// result += url;
// }
// return result;
1 year ago
};
const getResult = async (response) => {
const messages = new Map([
[200, "操作成功"],
[201, "已创建"],
[204, "无返回值"],
[400, "请求参数错误"],
[401, "未登录"],
[403, "权限不足"],
[500, "服务器异常"],
]);
const result = {
status: response.status,
message: messages.get(response.status),
};
if (response.status == 200) {
const contentType = response.headers.get("Content-Type");
if (contentType.indexOf("application/json") > -1) {
result.data = await response.json();
} else if (contentType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
result.data = await response.blob();
result.filename = getFileName(response.headers.get("Content-Disposition"));
}
1 year ago
} else if (response.status === 400 || response.status === 500) {
result.errors = await response.json();
}
return result;
};
const get = async (url, data, options, withoutToken = false) => {
url = getUrl(url);
1 year ago
if (data) {
url = `${url}?${qs.stringify(data)}`;
}
const defaultOptions = {
headers: { "Accept-Language": "zh-Hans" },
1 year ago
};
if (options) {
Object.assign(defaultOptions, options);
}
if (!withoutToken) {
await addToken(defaultOptions);
}
const response = await fetch(url, defaultOptions);
return getResult(response);
};
const post = async (url, data, options, withoutToken = false) => {
url = getUrl(url);
let defaultOptions = {
1 year ago
method: "POST",
headers: { "Accept-Language": "zh-Hans" },
1 year ago
};
if (!(data instanceof FormData)) {
defaultOptions.headers["Content-Type"] = "application/json";
}
1 year ago
if (options) {
Object.assign(defaultOptions, options);
}
if (data && !defaultOptions.body) {
if (defaultOptions.headers["Content-Type"]?.startsWith("application/x-www-form-urlencoded")) {
1 year ago
defaultOptions.body = qs.stringify(data);
} else if (defaultOptions.headers["Content-Type"]?.startsWith("application/json")) {
1 year ago
defaultOptions.body = JSON.stringify(data);
} else {
defaultOptions.body = data;
1 year ago
}
}
if (!withoutToken) {
await addToken(defaultOptions);
}
const response = await fetch(url, defaultOptions);
return getResult(response);
};
async function request(url, data, options, withoutToken = false) {
url = getUrl(url);
let defaultOptions = {
method: "POST",
headers: {
"Accept-Language": "zh-Hans",
},
};
if (options) {
Object.assign(defaultOptions, options);
}
if (defaultOptions.method === "GET" && data) {
url = `${url}?${qs.stringify(data)}`;
} else {
if (defaultOptions.headers["Content-Type"]?.startsWith("application/x-www-form-urlencoded")) {
defaultOptions.body = qs.stringify(data);
} else if (defaultOptions.headers["Content-Type"]?.startsWith("application/json")) {
defaultOptions.body = JSON.stringify(data);
} else if (data instanceof FormData) {
delete defaultOptions.headers["Content-Type"];
defaultOptions.body = data;
} else if (data) {
defaultOptions.headers["Content-Type"] = "application/json";
defaultOptions.body = JSON.stringify(data);
}
}
if (!withoutToken) {
await addToken(defaultOptions);
}
const response = await fetch(url, defaultOptions);
return getResult(response);
}
export default request;
export { get, post };