91 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-12-05 23:01:12 +08:00
import http from '/admin/support/http'
2022-12-06 19:27:38 +08:00
import {provide, ref, unref} from 'vue'
2022-12-05 23:01:12 +08:00
import { Code } from '/admin/enum/app'
import Message from '/admin/support/message'
const initLimit = 10
const initPage = 1;
2022-12-06 19:27:38 +08:00
const initTotal = 10;
2022-12-05 23:01:12 +08:00
// get table list
export function useGetList(path: string) {
const data = ref<object>()
2022-12-06 19:27:38 +08:00
const page = ref<number>(initPage)
const limit = ref<number>(initLimit)
const total = ref<number>(initTotal)
2022-12-05 23:01:12 +08:00
const query = ref<object>({
page: page.value,
limit: limit.value,
})
const loading = ref(true)
// fetch list
function getList() {
// when table's data page >= 100, it will loading
if (page.value >= 100) {
loading.value = true
}
http
.get(path, unref(query))
.then(r => {
closeLoading()
if (r.data.code === Code.SUCCESS) {
data.value = r.data
2022-12-06 19:27:38 +08:00
// @ts-ignore
total.value = data.value?.total
2022-12-05 23:01:12 +08:00
} else {
Message.error(r.data.message)
}
})
.finally(() => {
closeLoading()
})
}
// close loading
function closeLoading() {
loading.value = false
}
// search
function search() {
getList()
}
// reset
function reset() {
2022-12-06 19:27:38 +08:00
resetPage()
2022-12-05 23:01:12 +08:00
query.value = Object.assign({ page: page.value, limit: limit.value })
getList()
}
// change page
function changePage(p: number) {
page.value = p
// @ts-ignore
query.value.page = p
search()
}
2022-12-06 19:27:38 +08:00
function resetPage() {
page.value = 1
}
2022-12-05 23:01:12 +08:00
// change limit
function changeLimit(l: number) {
limit.value = l
2022-12-06 19:27:38 +08:00
resetPage()
2022-12-05 23:01:12 +08:00
// @ts-ignore
query.value.page = 1
// @ts-ignore
query.value.limit = l
2022-12-06 19:27:38 +08:00
2022-12-05 23:01:12 +08:00
search()
}
2022-12-06 19:27:38 +08:00
// provider for paginate component
provide('paginate', {page, limit, total, changePage, changeLimit})
return { data, query, search, reset, loading }
2022-12-05 23:01:12 +08:00
}