index.jsx 7.8 KB

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