import { inject, provide, ref, type Ref } from "vue"; import { showError } from "@/common/composeabe/ErrorDisplay"; import DefaultCofig from "./DefaultCategory.json"; import type { IHomeCommonCategoryDefine } from "./CommonCategoryDefine"; import CommonCategoryApi from "./api/CommonCategoryApi"; // 全局加载默认分类 export const COMMON_CATEGORY_KEY = 'DefaultCategory'; const defaultCommonCategory = ref({ page: [], }); export function injectCommonCategory() { return inject(COMMON_CATEGORY_KEY, defaultCommonCategory) as Ref; } export function useCommonCategoryGlobalLoader() { const commonCategoryData = ref(defaultCommonCategory.value); provide(COMMON_CATEGORY_KEY, commonCategoryData); async function loadCommonCategory() { uni.showLoading({ title: '加载中' }); try { /* if (uni.getSystemInfoSync().platform === 'devtools') { commonCategoryData.value = DefaultCofig as IHomeCommonCategoryDefine; return; } */ const category = (await CommonCategoryApi.getConfig()) as any as IHomeCommonCategoryDefine; if (category) commonCategoryData.value = category; else showError(undefined, '默认分类未配置'); } catch (error) { showError(error, '加载默认分类失败'); } finally { uni.hideLoading(); } } return { loadCommonCategory, } }