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.
41 lines
1.1 KiB
41 lines
1.1 KiB
import request from '@/services/request';
|
|
import useSWR, { useSWRInfinite } from 'swr';
|
|
export const PAGE_SIZE = 10;
|
|
|
|
//swr 返回获取数据函数
|
|
export const fetcher = ({ url, data }, cb) => {
|
|
return async function () {
|
|
const res = await request.get(url, data);
|
|
const payload = cb ? cb(res.data) : res.data;
|
|
if (res.statusCode === 200 || res.statusCode === 204) {
|
|
return payload;
|
|
} else {
|
|
//向外抛出错误
|
|
throw new Error(res.data.message || '请求出错');
|
|
}
|
|
}
|
|
}
|
|
//分页获取数据
|
|
export const pageFetcher = async (url) => {
|
|
const res = await request.get(url);
|
|
const payload = res.data;
|
|
if (res.statusCode === 200 || res.statusCode === 204) {
|
|
if(payload && payload.rows){
|
|
return payload.rows
|
|
}
|
|
return payload;
|
|
} else {
|
|
//向外抛出错误
|
|
throw new Error(res.data.message || '请求出错');
|
|
}
|
|
}
|
|
|
|
export const useCommonSwr = (url) => {
|
|
const { data, error } = useSWR(url, fetcher({ url }));
|
|
return {
|
|
data: data,
|
|
isLoading: !error && !data,
|
|
error: error
|
|
}
|
|
|
|
}
|