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,
    };
 }}

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

相关推荐
逝缘~1 小时前
css百分比布局中height:100%不起作用
前端·css·小程序·html·js
Passion不晚1 小时前
打造古风炫酷个人网页:用HTML和CSS3传递笔墨韵味
前端·html·css3
浏览器爱好者2 小时前
如何在Chrome中使用HTML构建交互式网页
前端·chrome·html
gs801402 小时前
安装node 报错需要:glibc >= 2.28
linux·服务器·前端·node.js
Random_index5 小时前
#名词区别篇:npx pnpm npm yarn区别
前端·npm
B.-5 小时前
Remix 学习 - 路由模块(Route Module)
前端·javascript·学习·react·web
陈大爷(有低保)5 小时前
JS基础语法
开发语言·javascript·ecmascript
不修×蝙蝠6 小时前
Javascript应用(TodoList表格)
前端·javascript·css·html
加勒比海涛6 小时前
ElementUI 布局——行与列的灵活运用
前端·javascript·elementui
你不讲 wood6 小时前
postcss 插件实现移动端适配
开发语言·前端·javascript·css·vue.js·ui·postcss