เพิ่ม function fetchStructureTree
This commit is contained in:
parent
6ee3227f16
commit
3666cd61a9
2 changed files with 65 additions and 39 deletions
55
src/stores/structureTree.ts
Normal file
55
src/stores/structureTree.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import { useQuasar } from "quasar";
|
||||
|
||||
import http from "@/plugins/http";
|
||||
import config from "@/app.config";
|
||||
import { useCounterMixin } from "@/stores/mixin";
|
||||
|
||||
const $q = useQuasar();
|
||||
const { showLoader, hideLoader, messageError } = useCounterMixin();
|
||||
|
||||
export const useStructureTree = defineStore("structureTree", () => {
|
||||
const activeId = ref<string>("");
|
||||
const dataStore = ref<{ [key: string]: any[] }>({});
|
||||
|
||||
async function fetchStructureTree(sysKey: string) {
|
||||
if (dataStore.value[sysKey]) {
|
||||
return dataStore.value[sysKey] || [];
|
||||
} else {
|
||||
activeId.value === "" && (await fetchActive());
|
||||
const data = await fetchData(sysKey);
|
||||
return data || [];
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchActive() {
|
||||
try {
|
||||
const res = await http.get(config.API.activeOrganization);
|
||||
const data = res.data.result;
|
||||
activeId.value = data.activeId;
|
||||
} catch (err) {
|
||||
messageError($q, err);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchData(sysKey: string) {
|
||||
// showLoader();
|
||||
try {
|
||||
const res = await http.get(
|
||||
config.API.orgByIdSystem(activeId.value, sysKey)
|
||||
);
|
||||
const data = res.data.result;
|
||||
dataStore.value[sysKey] = data;
|
||||
return data;
|
||||
} catch (err) {
|
||||
messageError($q, err);
|
||||
} finally {
|
||||
hideLoader();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
fetchStructureTree,
|
||||
};
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue