当前位置: 首页 > news >正文

前端--导出

这边记录我们公司后端做的导出接口和前端是如何对接的
    这边的技术栈是:
               1: react 

               2: fetch

第一步:简单封装--导出界面

import { DrawerForm } from '@ant-design/pro-components';
import { CloseOutlined } from '@ant-design/icons';
import { Col, Input, Row, Select, DatePicker, message, InputNumber, Card, Button } from 'antd'
import React, { useEffect, useState } from 'react';
import { connect } from 'react-redux';
import { DEPT_NAMESPACE } from "@/actions/dept";
import { getCurrentUser } from "@/utils/authority";
import { getDictBiz } from "@/utils/utils";
import { DICT_BIZ_NAMESPACE } from "@/actions/dictbiz";
import { clubSetMealSave, clubSetMealUpdate } from "@/services/club";
import { list } from '@/services/menu';import style from './common.less';const { TextArea } = Input;function NewExp(props) {//show  是根据需要是否展示下方的弹窗//hang 是根据返回结果关闭弹窗//changeHange 是根据需要把参数返回出去//title  是弹窗的标题//Component  是触发弹窗的关键const {  title, Component, changeHange, hang, show } = props//  时间const [startDate, setStartDate] = useState(null)const [startDate1, setStartDate1] = useState(null)const [flag, setflag] = useState(false)const submit = async () => {// 判断时间if (!show) {if (startDate != null || startDate1 != null) {// 时间比较大小if (startDate1 < startDate) {return message.error('结束时间不能小于开始时间')}let data = {d1: startDate,d2: startDate1,}changeHange(data)if (hang() == 1) {setflag(false)setStartDate(null)setStartDate1(null)}}} else {changeHange()if (hang() == 1) {setflag(false)setStartDate(null)setStartDate1(null)}}// if (res?.success) {//     message.success('保存成功')//     setflag(false)//     changeupadte()// }}const onChange = (date, dateString) => {setStartDate(dateString)};const onChange1 = (date, dateString) => {setStartDate1(dateString)};return (<>{!show ?<><DrawerFormclassName={style.common}submitter={{resetButtonProps: { type: 'dashed' },submitButtonProps: { style: { display: 'none' } },resetButtonRender: (_, dom) => null,//   <Button key="submit" type="primary" onClick={() => { submit() }}>保存</Button>render: (props, defaultDoms) => {return [<Button key="clean" onClick={() => { setflag(false) }}>取消</Button>,<Button key="close" type="primary" onClick={() => { submit('pass') }}>下载</Button>,]}}} drawerProps={{closeIcon: null,destroyOnClose: true,extra: <CloseOutlined onClick={() => setflag(false)} />}}visible={flag}onVisibleChange={(e) => {setflag(e)}}width={440}onFinish={() => submit()}title={<span style={{color: '#1F1F1F',fontSize: '18px',fontWeight: 600,height: '22px',lineHeight: '22px'}}>{title}</span>}trigger={Component}><div style={{ ...style0 }}><div style={{ ...style1 }}>导出开始时间</div><DatePicker placeholder='请选择开始时间' style={{ ...style2 }} onChange={onChange} /></div><div style={{ ...style0, margin: '20px 0' }}><div style={{ ...style1, }}>导出结束结束</div><DatePicker placeholder='请选择结束时间' style={{ ...style2 }} onChange={onChange1} /></div></DrawerForm></>:<><ButtononClick={() => { submit('pass') }}type="primary">导出</Button></>}</>)
}
const style0 = {display: 'flex',justifyContent: 'space-between',alignItems: 'center',
}
const style1 = {width: '22%'
}
const style2 = {width: '76%'
}
const mapStateToProps = (state) => {return {};
};export default connect(mapStateToProps)(NewExp)

 

第二步:封装请求导出js-----utils
     

import {  stringify } from 'qs';//引入qs库export async function download(url, params) {// /api/blade-cust/custhubAppointment/export-customerconst urls = `/api/${url}?${stringify(params)}`;const response = await fetch(urls, {method: 'GET',headers: {'Authorization': ``,'Blade-Auth': `Bearer ${getAccessToken()}` //请求token,'Content-Type': 'application/json' //JSON形式----有时候也是具体什么时候用看需求                  //responseType:"blob",,}});if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}const blob = await response.blob();const contentDisposition = response.headers.get('content-disposition');const filename = contentDisposition ? contentDisposition.split('filename=')[1].replace(/"/g, '') : 'exported_data.csv';const urlBlob = window.URL.createObjectURL(blob);const a = document.createElement('a');if (a) {
//创建了一个a  直接下载  a.href = urlBlob;a.download = filename;document.body.appendChild(a);a.click();a.remove();window.URL.revokeObjectURL(urlBlob);return true //这边是给我返回成功的状态}}

 第三步:导入封好的js---使用

import { download } from '@/utils/utils'const hang = () => {return 1}const changeHange = async (e) => {try {
//这个都是 参数---接口需要const startTime = e.d1;const endTime = e.d2;const type = userRole;const bigType = defaultvalue;const params1 = {startTime,endTime,type,bigType};let d = await download('blade-cust/custhubCustomer/export-customer', params1)if (d) {message.success('导出成功,请保存')hang()}} catch (error) {console.error('Export failed:', error);}}//导出显示组件<NewExps title={'导出'}hang={hang}Component={<Button type="primary">导出</Button>}changeHange={changeHange}></NewExps>

 上方是不用打开新的页面---直接可以点击下载的
 这个是打开新界面---代码奉献上---弹窗一个 直接会弹出对应的下载框

import { Upload, Switch, Button, Card, Col, Input, message, Modal, Row, Tree } from 'antd';Modal.confirm({title: '用户导出确认',content: '是否导出用户数据?',okText: '确定',okType: 'danger',cancelText: '取消',onOk() {const account = params.account || '';const realName = params.realName || '';window.open(`/api/blade-system/user/export-user?Blade-Auth=bearer ${getAccessToken()}&account=${account}&realName=${realName}`);},onCancel() {},});

http://www.lryc.cn/news/365264.html

相关文章:

  • 【数据库系统概论】触发器
  • 小白跟做江科大32单片机之按键控制LED
  • 每天写java到期末考试(6.6)-java文件输入输出流实验
  • Word2021中的The Mathtype DLL cannot be found问题解决(office 16+mathtype7+非初次安装)
  • 【Android面试八股文】在Java中传参数时是将值进行传递,还是传递引用?
  • 神经网络 torch.nn---Linear Layers(nn.Linear)
  • PPT视频如何16倍速或者加速播放
  • 【ai】DeepStream 简介
  • 如何学习使用淘宝API?淘宝API运营场景
  • Java 面试题:Java 的动态代理是基于什么原理?
  • Python logging 模块详解
  • http://account.battlenet.com.cn
  • java第二十课 —— 面向对象习题
  • Flask的模块化实践
  • 锁存器(Latch)的产生与特点
  • 搜维尔科技:「案例」Faceware电影中面部动画的演变历程
  • 特征工程技巧—Bert
  • 更改 Docker 的默认存储位置
  • 搜索与图论:图中点的层次
  • NLP入门——数据预处理:编码规范化
  • 代码随想录算法训练营第四十八天| 70. 爬楼梯 (进阶)、322. 零钱兑换、279.完全平方数
  • c++11 constexpr关键字
  • ios 获取图片的一部分区域
  • 数据结构(3)栈、队列、数组
  • 数据库(入门)
  • ChatTTS+Python编程搞定语音报时小程序
  • 【Mac】Alfred 5 for Mac(苹果效率提升工具)v5.5软件介绍及安装教程
  • PDF文件处理不再复杂:9个Python库让一切变得简单
  • 安防视频融合汇聚平台EasyCVR如何实现视频画面自定义标签?
  • Liunx音频