index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { siteApi } from "@/api";
  2. import { MemoSpinLoding } from "@/components";
  3. import {
  4. DageUpload,
  5. DageUploadConsumer,
  6. DageUploadProvider,
  7. DageUploadType,
  8. } from "@dage/pc-components";
  9. import { App, Button, Form, FormInstance, Input } from "antd";
  10. import { cloneDeep } from "lodash";
  11. import { useEffect, useRef, useState } from "react";
  12. export default function SitePage() {
  13. const initialData = useRef<any>(null);
  14. const { message } = App.useApp();
  15. const formRef = useRef<FormInstance>(null);
  16. const [loading, setLoading] = useState(false);
  17. const [readOnly, setReadOnly] = useState(true);
  18. useEffect(() => {
  19. getDetail();
  20. }, []);
  21. const getDetail = async () => {
  22. setLoading(true);
  23. try {
  24. const str = await siteApi.get();
  25. const { fileName, filePath, ...rest } = JSON.parse(str);
  26. if (fileName) {
  27. rest.audio = [
  28. {
  29. uid: fileName,
  30. url: `${process.env.REACT_APP_API_URL}${filePath}`,
  31. name: fileName,
  32. status: "done",
  33. },
  34. ];
  35. }
  36. formRef.current?.setFieldsValue(rest);
  37. initialData.current = cloneDeep(rest);
  38. } finally {
  39. setLoading(false);
  40. }
  41. };
  42. const save = async () => {
  43. if (!formRef.current || !(await formRef.current.validateFields())) return;
  44. try {
  45. setLoading(true);
  46. const { audio, ...rest } = formRef.current.getFieldsValue();
  47. if (Array.isArray(audio) && audio.length) {
  48. Object.assign(rest, {
  49. filePath: audio[0].response.filePath,
  50. fileName: audio[0].response.fileName,
  51. });
  52. }
  53. await siteApi.save(rest);
  54. message.success("保存成功");
  55. initialData.current = cloneDeep(formRef.current.getFieldsValue());
  56. setReadOnly(true);
  57. } finally {
  58. setLoading(false);
  59. }
  60. };
  61. const cancel = () => {
  62. formRef.current?.setFieldsValue({
  63. ...initialData.current,
  64. });
  65. setReadOnly(true);
  66. };
  67. return (
  68. <DageUploadProvider>
  69. <DageUploadConsumer>
  70. {(data) => (
  71. <Form
  72. ref={formRef}
  73. labelCol={{ flex: "80px" }}
  74. labelAlign="left"
  75. colon={false}
  76. >
  77. {loading && <MemoSpinLoding />}
  78. <Form.Item label="背景音乐:" name="audio">
  79. <DageUpload
  80. maxCount={1}
  81. disabled={readOnly}
  82. action="/api/cms/site/upload"
  83. dType={DageUploadType.AUDIO}
  84. maxSize={10}
  85. tips="仅支持MP3格式的音频文件,大小不得超过10MB"
  86. />
  87. </Form.Item>
  88. <Form.Item label="联系方式:" name="phone">
  89. <Input
  90. readOnly={readOnly}
  91. className="w220"
  92. placeholder="请输入内容"
  93. />
  94. </Form.Item>
  95. <Form.Item
  96. label="电子邮箱:"
  97. name="email"
  98. rules={[{ type: "email" }]}
  99. >
  100. <Input
  101. readOnly={readOnly}
  102. className="w220"
  103. placeholder="请输入内容"
  104. />
  105. </Form.Item>
  106. <Form.Item label=" ">
  107. {readOnly ? (
  108. <Button onClick={() => setReadOnly(false)}>编辑</Button>
  109. ) : (
  110. <>
  111. <Button style={{ marginRight: "15px" }} onClick={cancel}>
  112. 取消
  113. </Button>
  114. <Button
  115. disabled={data?.uploading}
  116. type="primary"
  117. onClick={save}
  118. >
  119. 保存
  120. </Button>
  121. </>
  122. )}
  123. </Form.Item>
  124. </Form>
  125. )}
  126. </DageUploadConsumer>
  127. </DageUploadProvider>
  128. );
  129. }