index.jsx 4.1 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3 4 5
/**
 * 徐立
 * 2019年12月6日
 * 电子签章多功能封装
 */
6 7 8
import React, { Component } from 'react';
import SignatureCanvas from 'react-signature-canvas';
import { message, Button } from 'antd';
chscls@163.com's avatar
chscls@163.com committed
9
import config from '@/webPublic/one_stop_public/config';
徐立's avatar
徐立 committed
10
import reqwest from 'reqwest';
11

chscls@163.com's avatar
chscls@163.com committed
12
function dataURLtoBlob(toDataURL) {
钟是志's avatar
钟是志 committed
13 14 15 16 17 18 19 20 21
  var arr = toDataURL.split(','),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
22
}
钟是志's avatar
钟是志 committed
23

chscls@163.com's avatar
chscls@163.com committed
24
function blobToFile(Blob, fileName) {
钟是志's avatar
钟是志 committed
25 26 27
  Blob.lastModifiedDate = new Date();
  Blob.name = fileName;
  return Blob;
徐立's avatar
徐立 committed
28
}
chscls@163.com's avatar
chscls@163.com committed
29

30
export default class Index extends Component {
钟是志's avatar
钟是志 committed
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
  constructor(props) {
    super(props);
    const value = props.value;
    this.state = {
      url: value,
    };
  }

  triggerChange = (changedValue) => {
    // Should provide an event to pass value to Form.
    const onChange = this.props.onChange;
    if (onChange) {
      onChange(changedValue);
    }
  };

  componentWillReceiveProps(nextProps) {
    // Should be a controlled component.
    if ('value' in nextProps) {
      const value = nextProps.value;

      this.setState({ url: value });

      //
    }
  }
57

钟是志's avatar
钟是志 committed
58 59 60 61 62 63 64
  sigCanvas = {
    clear: () => {
    },
    toDataURL: (param) => {
      return '';
    },
  };
65

钟是志's avatar
钟是志 committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  clear = () => {
    this.sigCanvas.clear();
  };
  delete = () => {
    if (!('value' in this.props)) {
      this.setState({ url: null });
    }
    this.triggerChange(null);
  };
  trim = () => {
    const formData = new FormData();
    const xx = dataURLtoBlob(this.sigCanvas.toDataURL('image/png'));
    const file = blobToFile(xx, 'sign.png');
    if (file == null) {
      return;
    }
    formData.append('file', file, 'sign.png');
    reqwest({
      url: config.uploadUrl,
      method: 'post',
      processData: false,
      data: formData,
      success: (url) => {
        if (!('value' in this.props)) {
          this.setState({ url: url });
        }
        this.triggerChange(url);
93

钟是志's avatar
钟是志 committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        message.success('保存成功');
      },
      error: (e) => {
        if (e.status == 200) {
          message.success('保存成功');
          if (!('value' in this.props)) {
            this.setState({ url: e.response });
          }
          this.triggerChange(e.response);
        } else {
          message.error('保存失败');
        }
      },
    });
    //let trimmedCanvas = this.sigCanvas.getTrimmedCanvas();
    // this.setState({trimmedDataURL: this.sigCanvas.toDataURL('image/png')})
  };
111

钟是志's avatar
钟是志 committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  render() {
    const {
      width,
      height,
    } = this.props;
    const { url } = this.state;
    return (
      <div id='canvas_signature' style={{
        width: '100%',
        height: '100%',
        marginLeft: 5,
      }}>
        <div
          style={{
            width: width,
            textAlign: 'center',
            height: height,
            border: '1px  dashed gray',
            position: 'relative',
          }}>
          {url ? (
            <img src={config.httpServer + url} style={{
              width: width,
              height: height,
            }} />
          ) : (
            <SignatureCanvas
              penColor='black'
              ref={(ref) => {
                this.sigCanvas = ref;
              }}
              canvasProps={{
                width: width || 400,
                height: height || 200,
                className: 'sigCanvas',
              }}
            />
          )}
          <div style={{
            position: 'absolute',
            left: 5,
            bottom: 5,
          }}>
            {url ? (
              <Button type='danger' size='small' onClick={this.delete}>
                重写
              </Button>
            ) : (
              <>
                <Button style={{ marginLeft: 12 }} type='danger' size='small' onClick={this.clear}>
                  清除
                </Button>
                <Button style={{ marginLeft: 12 }} onClick={this.trim} size='small' type='primary'>
                  保存
                </Button>
              </>
            )}
          </div>
        </div>
      </div>
    );
  }
174
}