react签名+上传base64图片接口入参处理

好久没更博啦,最近写项目需要实现将签名生成的url(base64图片格式)上传到upload接口。在这简要的记录下~~

背景:1.实现签名功能; 2.上传base64图片(格式自定义)

实现:1.签名功能

引入react-signature-canvas中的SignatureCanvas绘制:

import React, { useState } from 'react';
import SignatureCanvas from 'react-signature-canvas';

const Signature = props => {
  const { resetImg } = props;
  const [sign, setsign] = useState(null);
  const end = () => {
    resetImg(sign);
  };

    return (
        <>
          <SignatureCanvas
            canvasProps={{ width: 1264, height: 400, className: 'sigCanvas' }}
            penColor="#181818"
            ref={ref => {
              setsign(ref);
            }}
            onEnd={end}
            throttle={20}
            minWidth={1}
            maxWidth={4}
          />
            
        </>
    )

};

export default Signature;

在b页面引入signature组件:

import React, { useState } from 'react';
import Signature from '@/pages/Order/FlowPage/components/Signature';
const SignModal = props => {
  const [sigPad, setsigPad] = useState(null);
    
  const reset = value => {
    setsigPad(value);
    dispatch({
      type: 'global/save',
      payload: {
        showSignUrl: value,
      },
    });
  };

  const confirm = () => {
    setUrl(sigPad.getTrimmedCanvas().toDataURL('image/png'));
    props.clickOK(sigPad.getTrimmedCanvas().toDataURL('image/png'));
  };

  const resetSign = () => {
    sigPad && sigPad.clear();
  };

return (
    <div>
      <Modal
        visible={props.visible}
        title='modal'
        width={1296}
        onOk={handleOk}
        onCancel={handleCancel}
        footer={[
          <div key="signModal">
            <Button onClick={() => handleCancel()}>CANCEL</Button>
            <Button type="primary" loading={loading} onClick={() => handleOk()}>OK</Button>
            <Button onClick={() => resetSign()} >RESET</Button>
          </div>,
        ]}
      >
        <Signature resetImg={reset} />
      </Modal>
    </div>
  );
};
export default SignModal;

点击ok如下图所示:

2.上传图片操作:

  const submitSign = () => {
    // 重要三步骤
    const formData = new FormData();
    const fileImg = dataURLtoFile(showSignUrl);
    formData.append('file', fileImg, `${Date.now()}.png`); 

    dispatch({
      type: 'global/saveSignature',
      payload: formData,
    }).then(({ success, data, errMessage }) => {
      if (success) {
        console.log(success)
        }).then(({ success, errMessage }) => {
          if (success) {
            message.error(errMessage);
          }
        });
      }
    });
  };

 Last: 希望能帮到需要的小伙伴们~越努力越幸运

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值