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.

91 lines
2.3 KiB

1 year ago
import qs from "../lib/qs/shim.js";
import { isLogin } from "../api/user.js";
import { useAppStore } from "../store/index.js";
import settings from "../config/settings.js";
const addToken = async (options) => {
if (await isLogin()) {
const appStore = useAppStore();
options.headers ??= {};
options.headers.Authorization = `Bearer ${appStore.token}`;
}
};
const getUrl = (url) => {
if (url.indexOf("/") === 0) {
return url;
}
let result = settings.baseURL;
return (result += `/${url}`);
};
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) {
result.data = await response.json();
} else if (response.status === 400 || response.status === 500) {
result.errors = await response.json();
}
return result;
};
const get = async (url, data, options, withoutToken = false, withoutCulture = false) => {
url = getUrl(url, withoutCulture);
if (data) {
url = `${url}?${qs.stringify(data)}`;
}
const defaultOptions = {
headers: {
"Accept-Language": "zh-Hans",
},
};
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, withoutCulture = false) => {
url = getUrl(url, withoutCulture);
const defaultOptions = {
method: "POST",
headers: {
"Accept-Language": "zh-Hans",
"Content-Type": "application/json",
},
};
if (options) {
Object.assign(defaultOptions, options);
}
if (data && !defaultOptions.body) {
if (defaultOptions.headers["Content-Type"] === "application/x-www-form-urlencoded") {
defaultOptions.body = qs.stringify(data);
} else {
defaultOptions.body = JSON.stringify(data);
}
}
if (!withoutToken) {
await addToken(defaultOptions);
}
const response = await fetch(url, defaultOptions);
return getResult(response);
};
export { getUrl, get, post };