React 实现请求接口封装

接口封装

一、正常接口请求

java 复制代码
useEffect(()=>{
	//获取下拉数据
	async function fetchDateWrapper() {
	try{
		const res = await 接口();
		console.log("res",res.data);
		const value = res.data.map(item ={
			return { label: item.valueName,value:valueCode }
		});
		setData(value);
		console.log("aaa",value);
	} catch (error) {
	
	} finally {
	
	}
	fetchDateWrapper();
},[])

二、antd组件请求封装

java 复制代码
 request={async (params) => {
 	const { current: pageNum, pageSize, ...otherParams } = params;
    if(otherParams?.yearMonth){
    	otherParams.beginDate=dayjs(otherParams?.yearMonth[0]).format('YYYY-MM')
        otherParams.endDate=dayjs(otherParams?.yearMonth[1]).format('YYYY-MM')
     }
    console.log('查询机构提交参数', {
        pageCurrent: pageNum,
        pageSize,
        ...otherParams,
    });
    //return []
    let {
        data,
        success,
    } = await pageQry({
        pageCurrent: pageNum,
        pageSize,
        ...otherParams,
    });
    console.log("data", data)
    console.log("data.pageInfo.records", data.pageInfo.records)
    let aaa = data.pageInfo.records
    const bbb = aaa.map((item: any, index: number) => {
        // console.log("itemitemitemitem", item)
        item.no = Number(pageSize) * (Number(pageNum) - 1) + index + 1;
        return { ...item };
    });
    setDataSourceS(bbb)
    return {
        // data: dataSource,
        success,
        total: data.pageInfo.total,
    };
 }}

出自 申创衡金科技网络有限公司

相关推荐
qq_2461000519 分钟前
CSDN risk probe 1773588273
开发语言·javascript·ecmascript
ByteCraze27 分钟前
Vue 递归组件实战:手写一个文件/文件夹树形组件
javascript·vue.js·ecmascript
前端Hardy37 分钟前
前端如何防止用户重复提交表单?4 种可靠方案(附防坑指南)
前端·javascript·面试
前端Hardy37 分钟前
用户真的关掉页面了吗?前端精准检测页面卸载的 4 种方法(附避坑指南)
前端·javascript·面试
yangyanping201081 小时前
Vue入门到精通七之关键字const
前端·javascript·vue.js
姝然_95271 小时前
Jetpack Compose 绘制流程与自定义布局
前端
lxh01131 小时前
重复的DNA序列
开发语言·javascript·ecmascript
姝然_95271 小时前
Jetpack Compose Brush 渐变
前端
阿鑫_9961 小时前
通用-ESLint+Prettier基础知识
前端·后端
ai超级个体1 小时前
金三银四,一个面试官连连夸赞的个人网页技术分享
前端·面试·three.js·threejs·网页设计·网页灵感·网页分享