张延森 преди 3 години
родител
ревизия
809018e654

+ 65
- 7
src/components/Poster/Form.jsx Целия файл

@@ -1,6 +1,7 @@
1
-import React from 'react'
2
-import { Button, Form, Input } from 'antd'
1
+import React, { useEffect, useState } from 'react'
2
+import { Button, Form, Input, message } from 'antd'
3 3
 import ImageUpload from '@/components/XForm/ImageUpload'
4
+import { fetch, apis } from '@/utils/request'
4 5
 
5 6
 const formItemLayout = {
6 7
   labelCol: {
@@ -13,16 +14,73 @@ const formItemLayout = {
13 14
   },
14 15
 }
15 16
 
17
+const getData = fetch(apis.activity.poster)
18
+const saveData = fetch(apis.activity.addPoster)
19
+const updateData = fetch(apis.activity.updatePoster)
20
+
16 21
 const PosterForm = (props) => {
17
-  const { form } = props
18
-  const { getFieldDecorator } = form
22
+  const { form, targetId, targetType } = props
23
+  const { getFieldDecorator, resetFields, setFieldsValue, validateFields } = form
24
+
25
+  const [formData, setFormData] = useState()
26
+  const [loading, setLoading] = useState(false)
19 27
 
20 28
   const handleImageChange = (img) => {
21 29
     props.onImageChange(img)
22 30
   }
23 31
 
32
+  const handleSubmit = (e) => {
33
+    e.preventDefault();
34
+    validateFields((err, values) => {
35
+      if (err) {
36
+        console.error(err)
37
+        return
38
+      }
39
+
40
+      const request = formData.posterId ? updateData : saveData
41
+      const urlData = formData.posterId ? { id: formData.posterId } : undefined
42
+      const data = {
43
+        targetId,
44
+        targetType,
45
+        ...(formData || {}),
46
+        ...values,
47
+      }
48
+
49
+      setLoading(true)
50
+      request({ urlData, data }).then((res) => {
51
+        setLoading(false)
52
+        setFormData(res)
53
+        message.success('操作成功')
54
+      }).catch((err) => {
55
+        setLoading(false)
56
+        console.error(err)
57
+      })
58
+    })
59
+  }
60
+
61
+  useEffect(() => {
62
+    if (targetId && targetType) {
63
+      setLoading(true)
64
+      getData({ params: { targetId, targetType } }).then((res) => {
65
+        setFormData(res ? res[0] : undefined)
66
+        setLoading(false)
67
+      }).catch((err) => {
68
+        console.error(err)
69
+        setLoading(false)
70
+      })
71
+    }
72
+  }, [targetId, targetType])
73
+
74
+  useEffect(() => {
75
+    const { posterImg, posterTitle, posterDescription } = formData || {}
76
+
77
+    resetFields()
78
+    setFieldsValue({ posterImg, posterTitle, posterDescription })
79
+    handleImageChange(posterImg)
80
+  }, [formData])
81
+
24 82
   return (
25
-    <Form {...formItemLayout}>
83
+    <Form {...formItemLayout} onSubmit={handleSubmit}>
26 84
       <Form.Item label="海报图" help="图片建议尺寸:1080*1925px,比例9:16,格式:jpg">
27 85
       {getFieldDecorator('posterImg', {
28 86
           rules: [{ required: true, message: '请楼盘海报图' }],
@@ -35,8 +93,8 @@ const PosterForm = (props) => {
35 93
       {getFieldDecorator('posterDescription')(<Input.TextArea row={4} />)}
36 94
       </Form.Item>
37 95
       <Form.Item label=" " colon={false} style={{marginTop: '2em'}}>
38
-        <Button style={{marginLeft: '4em'}} type="primary" htmlType="submit">保存</Button>
39
-        <Button style={{marginLeft: '2em'}} onClick={props.onCancel}>取消</Button>
96
+        <Button loading={loading} style={{marginLeft: '4em'}} type="primary" htmlType="submit">保存</Button>
97
+        {/* <Button style={{marginLeft: '2em'}} onClick={props.onCancel}>取消</Button> */}
40 98
       </Form.Item>
41 99
     </Form>
42 100
   )

+ 12
- 7
src/components/Poster/index.jsx Целия файл

@@ -1,24 +1,29 @@
1
-import React, { useMemo, useState } from 'react'
1
+import React, { useState } from 'react'
2 2
 import { getImgURL } from '@/utils/image'
3 3
 import Preview from './Preview'
4 4
 import Form from './Form'
5 5
 import Styles from './style.less'
6 6
 
7
+const noop = x => x;
8
+
7 9
 export default (props) => {
8
-  const { target } = props
10
+  const { target, onCancel = noop } = props
9 11
   const { id: targetId, type: targetType } = target
10 12
 
11
-  const [poster, setPoster] = useState()
12
-
13
-  const posterImage = useMemo(() => (poster ? getImgURL(poster) : undefined), [poster]);
13
+  const [posterImg, setPosterImg] = useState()
14 14
 
15 15
   return (
16 16
     <div className={Styles['building-poster']}>
17 17
       <div className={Styles['poster-preview']}>
18
-        <Preview img={posterImage} />
18
+        <Preview img={getImgURL(posterImg)} />
19 19
       </div>
20 20
       <div className={Styles['poster-form']}>
21
-        <Form onImageChange={setPoster} />
21
+        <Form
22
+          targetId={targetId}
23
+          targetType={targetType}
24
+          onImageChange={setPosterImg}
25
+          onCancel={onCancel}
26
+        />
22 27
       </div>
23 28
     </div>
24 29
   )

+ 64
- 8
src/components/Share/index.jsx Целия файл

@@ -1,6 +1,7 @@
1
-import React from 'react'
1
+import React, { useState, useEffect } from 'react'
2 2
 import { Button, Form, Input } from 'antd'
3 3
 import ImageUpload from '@/components/XForm/ImageUpload'
4
+import { fetch, apis } from '@/utils/request'
4 5
 
5 6
 const formItemLayout = {
6 7
   labelCol: {
@@ -12,27 +13,82 @@ const formItemLayout = {
12 13
     sm: { span: 19 },
13 14
   },
14 15
 }
16
+const getData = fetch(apis.activity.shareContent)
17
+const saveData = fetch(apis.activity.addShareContent)
18
+const updateData = fetch(apis.activity.updateShareContent)
15 19
 
16 20
 const ShareForm = (props) => {
17 21
   const { target, form } = props
18
-  const { id: targetId, type: shareContentType } = target
19
-  const { getFieldDecorator } = form
22
+  const { id: targetId, type: targetType } = target
23
+  const { getFieldDecorator, resetFields, setFieldsValue, validateFields } = form
24
+
25
+  const [formData, setFormData] = useState()
26
+  const [loading, setLoading] = useState(false)
27
+
28
+  const handleSubmit = (e) => {
29
+    e.preventDefault();
30
+    validateFields((err, values) => {
31
+      if (err) {
32
+        console.error(err)
33
+        return
34
+      }
35
+
36
+      const request = formData.shareContentId ? updateData : saveData
37
+      const urlData = formData.shareContentId ? { id: formData.shareContentId } : undefined
38
+      const data = {
39
+        targetId,
40
+        shareContentType: targetType,
41
+        ...(formData || {}),
42
+        ...values,
43
+      }
44
+
45
+      setLoading(true)
46
+      request({ urlData, data }).then((res) => {
47
+        setLoading(false)
48
+        setFormData(res)
49
+        message.success('操作成功')
50
+      }).catch((err) => {
51
+        setLoading(false)
52
+        console.error(err)
53
+      })
54
+    })
55
+  }
56
+
57
+  useEffect(() => {
58
+    if (targetId && targetType) {
59
+      setLoading(true)
60
+      getData({ params: { targetId, targetType } }).then((res) => {
61
+        setFormData(res ? res[0] : undefined)
62
+        setLoading(false)
63
+      }).catch((err) => {
64
+        console.error(err)
65
+        setLoading(false)
66
+      })
67
+    }
68
+  }, [targetId, targetType])
69
+
70
+  useEffect(() => {
71
+    const { shareContentImg, shareContentTitle } = formData || {}
72
+
73
+    resetFields()
74
+    setFieldsValue({ shareContentImg, shareContentTitle })
75
+  }, [formData])
20 76
 
21 77
   return (
22
-    <Form {...formItemLayout}>
78
+    <Form {...formItemLayout} onSubmit={handleSubmit}>
23 79
       <Form.Item label="分享标题">
24 80
       {getFieldDecorator('shareContentTitle', {
25 81
         rules: [{required: true, message: '请填写分享标题'}]
26
-      })(<Input />)}
82
+      })(<Input placeholder="建议不要超过20字" />)}
27 83
       </Form.Item>
28 84
       <Form.Item label="分享图" help="建议图片尺寸:750*600px,比例5:4,格式:jpg">
29
-      {getFieldDecorator('posterImg', {
85
+      {getFieldDecorator('shareContentImg', {
30 86
           rules: [{ required: true, message: '请上传分享图' }],
31 87
       })(<ImageUpload />)}
32 88
       </Form.Item>
33 89
       <Form.Item label=" " colon={false} style={{marginTop: '2em'}}>
34
-        <Button style={{marginLeft: '4em'}} type="primary" htmlType="submit">保存</Button>
35
-        <Button style={{marginLeft: '2em'}} onClick={props.onCancel}>取消</Button>
90
+        <Button loading={loading} style={{marginLeft: '4em'}} type="primary" htmlType="submit">保存</Button>
91
+        {/* <Button style={{marginLeft: '2em'}} onClick={props.onCancel}>取消</Button> */}
36 92
       </Form.Item>
37 93
     </Form>
38 94
   )

+ 6
- 9
src/pages/building/Edit/Apartment/Form.jsx Целия файл

@@ -12,7 +12,7 @@ const saveData = fetch(apis.building.buildingApartmentAdd)
12 12
 const updateData = fetch(apis.building.buildingApartmentUpdate)
13 13
 
14 14
 const AMForm = (props) => {
15
-  const { visible, onCancel, form, onSubmit } = props
15
+  const { visible, onCancel, form, formData, onSuccess } = props
16 16
   const { getFieldDecorator, setFieldsValue, resetFields, validateFields } = form
17 17
 
18 18
   const [loading, setLoading] = useState(false)
@@ -25,9 +25,9 @@ const AMForm = (props) => {
25 25
         return
26 26
       }
27 27
 
28
-      const request = props.formData.apartmentId ? updateData : saveData
28
+      const request = formData && formData.apartmentId ? updateData : saveData
29 29
       const data = {
30
-        ...(props.formData),
30
+        ...(formData || {}),
31 31
         ...values,
32 32
       }
33 33
 
@@ -35,7 +35,7 @@ const AMForm = (props) => {
35 35
       request({ data }).then((res) => {
36 36
         setLoading(false)
37 37
         message.success('操作成功')
38
-        props.onSuccess(res)
38
+        onSuccess(res)
39 39
       }).catch((err) => {
40 40
         setLoading(false)
41 41
         console.error(err)
@@ -45,8 +45,8 @@ const AMForm = (props) => {
45 45
 
46 46
   useEffect(() => {
47 47
     resetFields()
48
-    setFieldsValue(props.formData || {})
49
-  }, [props.formData])
48
+    setFieldsValue(formData || {})
49
+  }, [formData])
50 50
 
51 51
   return (
52 52
     <ModalForm
@@ -55,9 +55,6 @@ const AMForm = (props) => {
55 55
       onCancel={onCancel}
56 56
       onSubmit={handleSubmit}
57 57
     >
58
-      <Form.Item label="编号" style={{ display: 'none' }}>
59
-          {getFieldDecorator('apartmentId')(<Input />)}
60
-      </Form.Item>
61 58
       <Form.Item label="户型名称">
62 59
         {getFieldDecorator('apartmentName', {
63 60
           rules: [

+ 1
- 1
src/pages/building/Edit/Apartment/index.jsx Целия файл

@@ -62,7 +62,7 @@ export default (props) => {
62 62
   return (
63 63
     <div>
64 64
       <div>
65
-        <Button type="primary" onClick={handleEdit}>新增户型</Button>
65
+        <Button type="primary" onClick={() => handleEdit()}>新增户型</Button>
66 66
       </div>
67 67
       <Form visible={visible} onCancel={() => setVisible(false)} formData={apartmentRef.current} onSuccess={handleSuccess} />
68 68
       <List loading={loading} dataSource={list} onEdit={handleEdit} onDelete={handleDelete} />

+ 5
- 1
src/pages/building/Edit/index.jsx Целия файл

@@ -15,7 +15,11 @@ import styles from './style.less'
15 15
 const { TabPane } = Tabs
16 16
 
17 17
 export default (props) => {
18
-  const target = { id: '', type: 'building' }
18
+  const { history } = props
19
+  const { query } = history.location
20
+  const { id } = query
21
+
22
+  const target = { id, type: 'building' }
19 23
 
20 24
   return (
21 25
     <Card>

+ 4
- 2
src/utils/image.js Целия файл

@@ -1,7 +1,9 @@
1 1
 
2
+const host = process.env.NODE_ENV !== 'production' ? 'https://xlk.njyz.tech' : ''
3
+
2 4
 export function getImgURL(img) {
3 5
     if (!img) return undefined;
6
+    if (img.indexOf('http') === 0) return img;
4 7
 
5
-    const isProd = process.env.NODE_ENV === 'production'
6
-    return isProd ? img : `https://xlk.njyz.tech${img}`;
8
+    return `${host}${img}`;
7 9
 }