index.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { DeleteOutlined, UploadOutlined } from '@ant-design/icons';
  2. import SystemConst from '@/utils/const';
  3. import Token from '@/utils/token';
  4. import { useEffect, 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. import { service } from '@/pages/device/Firmware';
  10. interface Props {
  11. value: any;
  12. onChange: (value: any) => void;
  13. placeholder: string;
  14. beforeUpload: any;
  15. }
  16. const FUpload = connect((props: Props) => {
  17. const [url, setUrl] = useState<any>(props?.value?.url);
  18. const handleChange = async (info: UploadChangeParam) => {
  19. if (info.file.status === 'done') {
  20. const result = info.file.response?.result;
  21. const api = await service.querySystemApi(['paths']);
  22. const f = {
  23. ...result,
  24. url: `${api?.result[0]?.properties['base-path']}/file/${result?.id}?accessKey=${result?.others?.accessKey}`,
  25. };
  26. setUrl(f.url);
  27. props.onChange(f);
  28. }
  29. };
  30. useEffect(() => {
  31. setUrl(props.value?.url);
  32. }, [props.value]);
  33. return (
  34. <Upload
  35. beforeUpload={props.beforeUpload}
  36. action={`/${SystemConst.API_BASE}/file/upload`}
  37. headers={{
  38. 'X-Access-Token': Token.get(),
  39. }}
  40. multiple={false}
  41. onChange={handleChange}
  42. progress={{
  43. format: (percent) => percent && `${parseFloat(percent.toFixed(2))}%`,
  44. }}
  45. showUploadList={{
  46. removeIcon: (
  47. <DeleteOutlined
  48. onClick={() => {
  49. setUrl('');
  50. props.onChange(undefined);
  51. }}
  52. />
  53. ),
  54. }}
  55. >
  56. <Input
  57. placeholder={props.placeholder}
  58. value={url || ''}
  59. readOnly
  60. addonAfter={<UploadOutlined />}
  61. />
  62. </Upload>
  63. );
  64. });
  65. export default FUpload;