index.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { UploadOutlined } from '@ant-design/icons';
  2. import SystemConst from '@/utils/const';
  3. import Token from '@/utils/token';
  4. import { useState } from 'react';
  5. import { connect } from '@formily/react';
  6. import { Input, Upload } from 'antd';
  7. import type { UploadChangeParam } from 'antd/lib/upload/interface';
  8. import './index.less';
  9. interface Props {
  10. value: any;
  11. onChange: (value: any) => void;
  12. placeholder: string;
  13. beforeUpload: any;
  14. }
  15. const FUpload = connect((props: Props) => {
  16. const [url, setUrl] = useState<any>(props?.value?.url);
  17. const handleChange = (info: UploadChangeParam) => {
  18. if (info.file.status === 'done') {
  19. const result = info.file.response?.result;
  20. const f = {
  21. ...result,
  22. url: `${location.protocol}//${SystemConst.API_BASE}/file/${result?.id}?accessKey=${result?.others?.accessKey}`,
  23. };
  24. setUrl(f.url);
  25. props.onChange(f);
  26. }
  27. };
  28. return (
  29. <Upload
  30. beforeUpload={props.beforeUpload}
  31. action={`/${SystemConst.API_BASE}/file/upload`}
  32. headers={{
  33. 'X-Access-Token': Token.get(),
  34. }}
  35. multiple={false}
  36. onChange={handleChange}
  37. progress={{}}
  38. >
  39. <Input
  40. placeholder={props.placeholder}
  41. value={url || ''}
  42. readOnly
  43. addonAfter={<UploadOutlined />}
  44. />
  45. </Upload>
  46. );
  47. });
  48. export default FUpload;