weiximei пре 5 година
родитељ
комит
5889a2b136
1 измењених фајлова са 50 додато и 43 уклоњено
  1. 50
    43
      src/pages/building/list/add/components/base.jsx

+ 50
- 43
src/pages/building/list/add/components/base.jsx Прегледај датотеку

@@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
2 2
 import { Form, Icon, Input, Button, DatePicker, Select, Card, Row, Col, Pagination, Alert, Radio, Tag, Tooltip, Tabs, InputNumber, notification } from 'antd';
3 3
 import moment from 'moment';
4 4
 import { router } from 'umi';
5
+import { ifError } from 'assert';
5 6
 import request from '../../../../../utils/request';
6 7
 import apis from '../../../../../services/apis';
7 8
 import Styles from '../style.less';
@@ -14,7 +15,6 @@ import BudildingProjectType from './buildingProjectType'
14 15
 import SelectCity from '../../../../../components/SelectButton/CitySelect'
15 16
 import FileUpload from '@/components/XForm/FileUpload';
16 17
 import { POI_TYPES_KETY, getAroundData, getPOIType, POI_TYPES } from './amapAroundData'
17
-import { ifError } from 'assert';
18 18
 
19 19
 const { Option } = Select
20 20
 const { TabPane } = Tabs;
@@ -46,6 +46,9 @@ function AddBuilding(props) {
46 46
   const [poi, setPoi] = useState([])
47 47
   const [videoImage, setVideoImage] = useState(false)
48 48
 
49
+  // 存放所以 buildingData 基础信息
50
+  const [buildingData, setBuildingData] = useState({})
51
+
49 52
   // console.log('props.building: ', props.building)
50 53
   const { buildingId } = props.building
51 54
      // eslint-disable-next-line react-hooks/rules-of-hooks
@@ -64,8 +67,8 @@ function AddBuilding(props) {
64 67
   function setPoiTagValue() {
65 68
     if (poi) {
66 69
       console.log('poi: ', poi)
67
-   
68
-      
70
+
71
+
69 72
       poi.map(x => {
70 73
         const ty = Object.prototype.toString
71 74
         // console.log('x.data: ', JSON.parse(x.data))
@@ -91,7 +94,7 @@ function AddBuilding(props) {
91 94
       }
92 95
 
93 96
       res.avatarImage = res.buildingImg.map(item => item.url)
94
-      
97
+
95 98
       if (res.buildingListImg) {
96 99
         // res.listImage = res.buildingListImg.map(item => item.url)
97 100
         res.listImage = res.buildingListImg.map(item => item.url)[0]
@@ -104,32 +107,15 @@ function AddBuilding(props) {
104 107
       if (res.videoImage) {
105 108
         res.videoImage = res.videoImage[0].url
106 109
       }
107
-      if (res.buildingTransport) {
108
-        const arr = res.buildingTransport.split(',')
109
-        res.buildingTransport = arr.map(x => ({ tagName: x, delete: true }))
110
-      }
111
-      if (res.buildingMall) {
112
-        const arr = res.buildingMall.split(',')
113
-        res.buildingMall = arr.map(x => ({ tagName: x, delete: true }))
114
-      }
115
-      if (res.buildingEdu) {
116
-        const arr = res.buildingEdu.split(',')
117
-        res.buildingEdu = arr.map(x => ({ tagName: x, delete: true }))
118
-      }
119
-      if (res.buildingHospital) {
120
-        const arr = res.buildingHospital.split(',')
121
-        res.buildingHospital = arr.map(x => ({ tagName: x, delete: true }))
122
-      }
123
-      if (res.buildingBank) {
124
-        const arr = res.buildingBank.split(',')
125
-        res.buildingBank = arr.map(x => ({ tagName: x, delete: true }))
126
-      }
127
-      if (res.buildingRestaurant) {
128
-        const arr = res.buildingRestaurant.split(',')
129
-        res.buildingRestaurant = arr.map(x => ({ tagName: x, delete: true }))
130
-      }
131 110
 
132
-      
111
+      res.buildingTransport = stringHandleTag(res.buildingTransport)
112
+      res.buildingMall = stringHandleTag(res.buildingMall)
113
+      res.buildingEdu = stringHandleTag(res.buildingEdu)
114
+      res.buildingHospital = stringHandleTag(res.buildingHospital)
115
+      res.buildingBank = stringHandleTag(res.buildingBank)
116
+      res.buildingRestaurant = stringHandleTag(res.buildingRestaurant)
117
+
118
+      setBuildingData(res)
133 119
       props.form.setFieldsValue(res)
134 120
       // console.log('mapJson: ', JSON.parse(res.mapJson))
135 121
       setPoi((res.mapJson && JSON.parse(res.mapJson)) || [])
@@ -137,6 +123,28 @@ function AddBuilding(props) {
137 123
     })
138 124
   }
139 125
 
126
+  /**
127
+   * 把 xxx,xxx,xxx  这样的格式转换成 [{ tagName: xxx, delete: true }, { tagName: xxx, delete: true }]
128
+   * @param {*} str
129
+   * @return 如果返回 str 为空 或者 非 string,则返回 str 本身,否则返回转换成功的数组
130
+   */
131
+  function stringHandleTag(str) {
132
+    if (typeof str === 'string' && str !== '') {
133
+      const arr = str.split(',')
134
+      return arrayTransition(arr)
135
+    }
136
+
137
+    return str
138
+  }
139
+
140
+  /**
141
+   * 把 [xxx,xxx,xxx]  这样的格式转换成 [{ tagName: xxx, delete: true }, { tagName: xxx, delete: true }]
142
+   * @param {*} arr
143
+   */
144
+  function arrayTransition(arr) {
145
+    return arr.map(x => ({ tagName: x, delete: true }))
146
+  }
147
+
140 148
   function handleSubmit(e) {
141 149
     e.preventDefault();
142 150
     props.form.validateFieldsAndScroll((err, values) => {
@@ -151,7 +159,7 @@ function AddBuilding(props) {
151 159
     if (poi.length !== 0) {
152 160
       data.mapJson = poi
153 161
     }
154
-    
162
+
155 163
     data.openingDate = moment(data.openingDate, 'yyyy-MM-dd HH:mm:ss')
156 164
     data.receivedDate = moment(data.receivedDate, 'yyyy-MM-dd HH:mm:ss')
157 165
     // 项目主图
@@ -162,7 +170,7 @@ function AddBuilding(props) {
162 170
       // console.log(data.videoUrl[0])
163 171
       data.videoUrl = data.videoUrl[0]
164 172
     }
165
-    
173
+
166 174
     if (data.tag) {
167 175
       data.tag = data.tag.map((item, _) => ({ tagName: item }))
168 176
     }
@@ -235,8 +243,7 @@ function AddBuilding(props) {
235 243
   }
236 244
 
237 245
   function setFormMapScopeValue(key, pois) {
238
-    const poiArray = pois.map(p => {
239
-      return {
246
+    const poiArray = pois.map(p => ({
240 247
         address: p.address,
241 248
         adname: p.adname,
242 249
         cityname: p.cityname,
@@ -248,9 +255,8 @@ function AddBuilding(props) {
248 255
         shopinfo: p.shopinfo,
249 256
         type: p.type,
250 257
         typecode: p.typecode,
251
-      }
252
-    })
253
-    
258
+      }))
259
+
254 260
     // 设置表单值
255 261
     setFormMapScopeTagValue(key, { data: poiArray })
256 262
 
@@ -261,7 +267,7 @@ function AddBuilding(props) {
261 267
       }
262 268
       return m
263 269
     })
264
-    
270
+
265 271
     setPoi(newPoi)
266 272
     return newPoi
267 273
   }
@@ -270,30 +276,31 @@ function AddBuilding(props) {
270 276
     const tag = item.data.map(t => ({ tagName: t.name, delete: false }))
271 277
     switch (keyType) {
272 278
       case POI_TYPES_KETY.Transport:
273
-        const buildingTransportValue = (props.form.getFieldValue('buildingTransport') || [])
279
+        const buildingTransportValue = (stringHandleTag(buildingData.buildingTransport) || [])
274 280
         props.form.setFieldsValue({ buildingTransport: tag.concat(buildingTransportValue) })
275 281
         return 'buildingTransport';
276 282
       case POI_TYPES_KETY.Bank:
277
-        const buildingBankValue = (props.form.getFieldValue('buildingBank') || [])
283
+        const buildingBankValue = (stringHandleTag(buildingData.buildingBank) || [])
278 284
         props.form.setFieldsValue({ buildingBank: tag.concat(buildingBankValue) })
279 285
         return 'buildingBank';
280 286
       case POI_TYPES_KETY.Edu:
281
-        const buildingEduValue = (props.form.getFieldValue('buildingEdu') || [])
287
+        const buildingEduValue = (stringHandleTag(buildingData.buildingEdu) || [])
282 288
         props.form.setFieldsValue({ buildingEdu: tag.concat(buildingEduValue) })
283 289
         return 'buildingEdu';
284 290
       case POI_TYPES_KETY.Hospital:
285
-        const buildingHospitalValue = (props.form.getFieldValue('buildingHospital') || [])
291
+        const buildingHospitalValue = (stringHandleTag(buildingData.buildingHospital) || [])
286 292
         props.form.setFieldsValue({ buildingHospital: tag.concat(buildingHospitalValue) })
287 293
         return 'buildingHospital';
288 294
       case POI_TYPES_KETY.Restaurant:
289
-        const buildingRestaurantValue = (props.form.getFieldValue('buildingRestaurant') || [])
295
+        const buildingRestaurantValue = (stringHandleTag(buildingData.buildingRestaurant) || [])
290 296
         props.form.setFieldsValue({ buildingRestaurant: tag.concat(buildingRestaurantValue) })
291 297
         return 'buildingRestaurant';
292 298
       case POI_TYPES_KETY.Mall:
293
-        const buildingMallValue = (props.form.getFieldValue('buildingMall') || [])
299
+        const buildingMallValue = (stringHandleTag(buildingData.buildingMall) || [])
294 300
         props.form.setFieldsValue({ buildingMall: tag.concat(buildingMallValue) })
295 301
         return 'buildingMall';
296 302
       default:
303
+        return ''
297 304
     }
298 305
   }
299 306