index.js 1.79 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
import React from 'react';
import { Row, Col, Button, message, Upload } from 'antd';
import styles from './index.less';

import { uploadParams } from '@/pages//system/parameter/services/operation';

const beforeUpload = file => {
  // 检查文件类型
  const reg = /\.(xlsx|xls)$/;
  const isXls = reg.test(file.name);
  if (!isXls) {
    message.error('只能上传Excel文件');
    return false;
  }

  // 进行文件大小检查
  const isLt100M = file.size / 1024 / 1024 < 100;
  if (!isLt100M) {
    message.error('文件大小不能超过100M');
  }
  return isXls && isLt100M;
};

const customRequest = ({ file, onSuccess }) => {
  uploadParams({
    file,
    onSuccess: ({ code, data }) => {
      if (code === 'sys.success') {
        message.success('导入成功');
        onSuccess(data);
      }
    },
  });
};
const props = {
  name: 'file',
  headers: {
    authorization: 'authorization-text',
  },
  accept: '.xlsx, .xls',
  beforeUpload,
  customRequest,
};

const Action = ({
  onClickQuery,
  onClickReset,
  onClickExport,
  onClickAdd,
  onSuccessUpload,
  loading,
}) => (
  <Row className={styles.root}>
    <Col span={12} className={styles.left}>
      <Button type="primary" onClick={onClickQuery}>
        查询
      </Button>
      <Button type="primary" onClick={onClickReset}>
        重置
      </Button>
      <div className={styles.uploadBtn}>
        <Upload {...props} fileList={[]} onSuccess={onSuccessUpload}>
          <Button type="primary">导入</Button>
        </Upload>
      </div>
      <Button type="primary" onClick={onClickExport}>
        导出
      </Button>
    </Col>
    <Col span={12} className={styles.right}>
      <Button type="primary" onClick={onClickAdd} disabled={loading}>
        新建
      </Button>
    </Col>
  </Row>
);

export default Action;