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.
127 lines
3.7 KiB
127 lines
3.7 KiB
import { a as useAuthStore, u as useNuxtApp, b as useRouter } from './server.mjs';
|
|
import { computed, toRef, isRef } from 'vue';
|
|
|
|
const useStateKeyPrefix = "$s";
|
|
function useState(...args) {
|
|
const autoKey = typeof args[args.length - 1] === "string" ? args.pop() : void 0;
|
|
if (typeof args[0] !== "string") {
|
|
args.unshift(autoKey);
|
|
}
|
|
const [_key, init] = args;
|
|
if (!_key || typeof _key !== "string") {
|
|
throw new TypeError("[nuxt] [useState] key must be a string: " + _key);
|
|
}
|
|
if (init !== void 0 && typeof init !== "function") {
|
|
throw new Error("[nuxt] [useState] init must be a function: " + init);
|
|
}
|
|
const key = useStateKeyPrefix + _key;
|
|
const nuxtApp = useNuxtApp();
|
|
const state = toRef(nuxtApp.payload.state, key);
|
|
if (init) {
|
|
nuxtApp._state[key] ??= { _default: init };
|
|
}
|
|
if (state.value === void 0 && init) {
|
|
const initialValue = init();
|
|
if (isRef(initialValue)) {
|
|
nuxtApp.payload.state[key] = initialValue;
|
|
return initialValue;
|
|
}
|
|
state.value = initialValue;
|
|
}
|
|
return state;
|
|
}
|
|
function useLoading() {
|
|
const count = useState("app:loading", () => 0);
|
|
const loading = computed(() => count.value > 0);
|
|
function begin() {
|
|
count.value++;
|
|
}
|
|
function end() {
|
|
count.value = Math.max(0, count.value - 1);
|
|
}
|
|
return { count, loading, begin, end };
|
|
}
|
|
async function request(url, body, extraHeaders) {
|
|
const auth = useAuthStore();
|
|
const router = useRouter();
|
|
const headers = {
|
|
"Content-Type": "application/json"
|
|
};
|
|
if (auth.token) {
|
|
headers["Authorization"] = auth.token;
|
|
}
|
|
if (extraHeaders) {
|
|
Object.assign(headers, extraHeaders);
|
|
}
|
|
const response = await fetch(url, {
|
|
method: "POST",
|
|
headers,
|
|
body: JSON.stringify(body)
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error: ${response.status}`);
|
|
}
|
|
const result = await response.json();
|
|
if (result.code === 18) {
|
|
auth.logout();
|
|
router.push("/");
|
|
throw new Error(result.msg || "Unauthorized");
|
|
}
|
|
if (result.code !== 0) {
|
|
throw new Error(result.msg || "Unknown error");
|
|
}
|
|
return result.data;
|
|
}
|
|
const READONLY_RE = /^(get|list|load|query|fetch|search|count|stat|export|check)/i;
|
|
function isMutating(method) {
|
|
return !READONLY_RE.test(method);
|
|
}
|
|
function useApi() {
|
|
const auth = useAuthStore();
|
|
const { begin, end } = useLoading();
|
|
async function webApi(method, data) {
|
|
const extraHeaders = {};
|
|
if (auth.currentAppId) {
|
|
extraHeaders["X-App-Id"] = String(auth.currentAppId);
|
|
}
|
|
if (!isMutating(method)) {
|
|
return request(`/web/api/api_${method}`, data ?? {}, extraHeaders);
|
|
}
|
|
begin();
|
|
try {
|
|
return await request(`/web/api/api_${method}`, data ?? {}, extraHeaders);
|
|
} finally {
|
|
end();
|
|
}
|
|
}
|
|
async function consoleApi(path, data) {
|
|
return request(`/console/api/${path}`, data ?? {});
|
|
}
|
|
async function uploadFile(file, path) {
|
|
const contentType = file.type || "application/octet-stream";
|
|
const token = await webApi("getcostoken", { path, filename: file.name, content_type: contentType });
|
|
const uploadUrl = token.upload_url || token.url || "";
|
|
if (!uploadUrl) {
|
|
throw new Error("未获取到上传地址:请确认已在【第三方服务配置→存储】配置并启用「阿里云 OSS」");
|
|
}
|
|
const putResponse = await fetch(uploadUrl, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": contentType
|
|
},
|
|
body: file
|
|
});
|
|
if (!putResponse.ok) {
|
|
throw new Error(`Upload failed: ${putResponse.status}`);
|
|
}
|
|
return token.url || uploadUrl.split("?")[0];
|
|
}
|
|
return {
|
|
webApi,
|
|
consoleApi,
|
|
uploadFile
|
|
};
|
|
}
|
|
|
|
export { useState as a, useLoading as b, useApi as u };
|
|
//# sourceMappingURL=useApi-BNSkSrat.mjs.map
|
|
|