index.jsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { ImportOutlined } from '@ant-design/icons';
  2. import React, { Component } from 'react';
  3. import PropTypes from 'prop-types';
  4. import { Button, Upload, message, Modal, Divider, Menu, Dropdown } from 'antd';
  5. // import Cookies from 'js-cookie';
  6. // 服务器地址,此处为了做演示,没有全局引入,直接写了
  7. const Host = 'http://112.124.59.133:8082';
  8. class CAUpload extends Component {
  9. constructor(props) {
  10. super(props);
  11. // 模板下载事件
  12. this.jumpTo = this.jumpTo.bind(this);
  13. // 表格导出事件
  14. this.exportExcel = this.exportExcel.bind(this);
  15. // 表格上传事件
  16. this.uploadProps.onChange = this.uploadProps.onChange.bind(this);
  17. this.customRequestCallback = props.customRequestCallback ? props.customRequestCallback.bind(this) : () => { };
  18. this.timer = null;
  19. this.file = null;
  20. this.templeteUrl='';
  21. this.label='';
  22. }
  23. // 模板下载
  24. jumpTo() {
  25. const userData = localStorage.getItem('userData');
  26. const { token } = JSON.parse(userData);
  27. window.open(`${Host}${this.templeteUrl}?token=${token}`);
  28. }
  29. // 上传参数
  30. uploadProps = {
  31. // // 发到后台的文件参数名
  32. // name: 'file',
  33. // // 接受的文件类型
  34. // accept: '.xls,.xlsx',
  35. // // 上传的地址
  36. // action: Host + this.props.url,
  37. // // 是否展示上传的文件
  38. // showUploadList:false,
  39. // // 上传的参数
  40. // data: {
  41. // // uid: Cookies.get('uid')
  42. // },
  43. // // 设置上传的请求头部,IE10 以上有效
  44. // headers: {
  45. // // authorization: 'authorization-text',
  46. // 'Content-Type':'multipart/form-data',
  47. // 'token':this.props.token,
  48. // },
  49. // 上传文件前的钩子函数
  50. showUploadList: false,
  51. beforeUpload() {
  52. message.loading('正在导入中...');
  53. return true;
  54. },
  55. customRequest: ({
  56. file, data, action, onSuccess
  57. }) => {
  58. console.log({ file, data, action, onSuccess });
  59. const formData = new FormData();
  60. formData.set('file', file);
  61. this.file = file;
  62. if (this.props.type != 'dialog') this.customRequestCallback(formData);
  63. },
  64. // 上传文件改变时的状态
  65. onChange(info) {
  66. // console.log({info});
  67. if (info.file.status !== 'uploading') {
  68. console.log(info.file, info.fileList);
  69. }
  70. if (info.file.status === 'done') {
  71. if (info.file.response.code !== 200) {
  72. setTimeout(() => {
  73. message.destroy();
  74. message.error(info.file.response.message);
  75. });
  76. } else {
  77. this.props.importSuccessCallback && this.props.importSuccessCallback();
  78. setTimeout(() => {
  79. message.destroy();
  80. message.success('导入成功');
  81. });
  82. }
  83. } else if (info.file.status === 'error') {
  84. setTimeout(() => {
  85. message.destroy();
  86. message.error('导入失败');
  87. });
  88. }
  89. },
  90. }
  91. // 导出Excel表格
  92. async exportExcel() {
  93. const userData = localStorage.getItem('userData');
  94. const { token } = JSON.parse(userData);
  95. const url = Host + this.props.url + `/export?token=${token}`;
  96. window.open(url);
  97. //获取 全院其他收支设置列表
  98. }
  99. renderMenu(){
  100. return (
  101. <Menu onClick={this.handleMenuClick.bind(this)}>
  102. {
  103. this.props.templateHrefs&&Array.isArray(this.props.templateHrefs)&&this.props.templateHrefs.map((item,index)=>{
  104. return (
  105. <Menu.Item key={item.value}>{item.label}</Menu.Item>
  106. )
  107. })
  108. }
  109. </Menu>
  110. )
  111. }
  112. handleMenuClick(e){
  113. console.log('菜单点击',e);
  114. const {key} = e;
  115. this.label = ''
  116. this.templeteUrl = key;
  117. this.jumpTo();
  118. }
  119. handleButtonClick(e){
  120. if(this.templeteUrl == ''){
  121. message.error('请选择下载模板!');
  122. }else{
  123. this.jumpTo();
  124. }
  125. }
  126. openModal() {
  127. const uploadProps = this.uploadProps;
  128. Modal.confirm({
  129. title: <React.Fragment>
  130. 导入数据
  131. <Divider plain></Divider>
  132. </React.Fragment>,
  133. icon: null,
  134. width: 600,
  135. okText: '确定',
  136. cancelText: '取消',
  137. onOk: () => {
  138. return new Promise((resolve, reject) => {
  139. this.timer = setTimeout(() => {
  140. resolve(true);
  141. this.customRequestCallback(this.file)
  142. }, 1500)
  143. }).then(res => {
  144. // console.log({res})
  145. })
  146. .catch(err => console.log({ err }))
  147. },
  148. content: <React.Fragment >
  149. {this.props.content}
  150. <Divider plain></Divider>
  151. <Upload style={{ marginLeft: 10 }} key='importExcel' {...uploadProps} >
  152. <Button icon={<ImportOutlined />} type="primary">选择上传文件</Button>
  153. </Upload>
  154. </React.Fragment>
  155. });
  156. }
  157. componentWillUnmount() {
  158. clearTimeout(this.timer);
  159. }
  160. render() {
  161. const uploadProps = this.uploadProps;
  162. const { type,templateHrefs } = this.props;
  163. // console.log({...this.props});
  164. if (type == 'normal') {
  165. return [
  166. // <Button style={{marginLeft: 10}} key='exportExcel' onClick={this.exportExcel}>导出</Button>,
  167. <Upload style={{ marginLeft: 10 }} key='importExcel' {...uploadProps} >
  168. <Button type="primary">导入</Button>
  169. </Upload>,
  170. <Button style={{ marginLeft: 10 }} key='templateDowload' onClick={()=>{this.templeteUrl = templateHrefs;this.jumpTo();}}>模板下载</Button>
  171. ]
  172. }
  173. if (type == 'dialog') {
  174. return [
  175. <Button type="primary" key='a' onClick={() => this.openModal()}>导入</Button>,
  176. // <Button style={{marginLeft: 10}} key='templateDowloadTwo' onClick={this.jumpTo}>模板下载</Button>,
  177. <Dropdown.Button onClick={this.handleButtonClick.bind(this)} overlay={this.renderMenu()}>
  178. 模板下载
  179. </Dropdown.Button>
  180. ]
  181. }
  182. return <></>
  183. }
  184. }
  185. // 定义参数类型
  186. CAUpload.propTypes = {
  187. // 模板下载地址
  188. templateHrefs: PropTypes.oneOfType([
  189. PropTypes.array,
  190. PropTypes.string,
  191. ]),
  192. // 上传地址
  193. url: PropTypes.string.isRequired,
  194. // 导入成功后的回调
  195. importSuccessCallback: PropTypes.func,
  196. //最终必需resolve一个值
  197. customRequestCallback: PropTypes.func,
  198. //上传功能类型 【‘normal’,'dialog'】
  199. type: PropTypes.string,
  200. //渲染节点
  201. content: PropTypes.node,
  202. };
  203. // const CAUpload = (props)=>{
  204. // return (
  205. // <Upload {...props}>
  206. // <Button type="primary" icon={<ImportOutlined />}>导入</Button>
  207. // </Upload>
  208. // )
  209. // }
  210. export default CAUpload;