Files
fe-manage/src/hooks/useRequest.js
2023-10-10 20:45:54 +08:00

46 lines
1.0 KiB
JavaScript

import { isRef, reactive, ref, toRefs, unref, watch, watchEffect } from "vue";
import http from '@/api/configPublic'
const useTotalPage = (_url, params, config = {}) => {
const s = _url.split(" ");
const url = s[0];
let methods = 'post'
if (s[1]) methods = 'get'
const state = reactive({
data: [],
total: 1,
current: 1,
pages: 1,
pageNo: 1,
pageSize: 10,
loading: false
});
if (isRef(_url)) {
watchEffect(fetch);
}
function reset() {
state.data = [];
state.loading = false;
}
function fetch() {
state.loading = true;
return http[methods](unref(url), !s[1] ? unref(params) : { params: unref(params) }, { ...config }).then(r => {
state.data = r.data?.records || [];
state.total = r.data?.total || 0;
state.loading = false;
})
}
return {
...toRefs(state),
fetch,
reset,
};
}
export {
useTotalPage
}