Commit cd53255e authored by GuanHua's avatar GuanHua

fix: 商城下单页面收货地址编辑问题

parent eb48ad23
...@@ -10,7 +10,7 @@ import { ...@@ -10,7 +10,7 @@ import {
import { Input, Switch, Select, FormMegaLayout } from '@formily/antd-components' import { Input, Switch, Select, FormMegaLayout } from '@formily/antd-components'
import { PublicApi } from '@/services/api' import { PublicApi } from '@/services/api'
import { PATTERN_MAPS } from '@/constants/regExp' import { PATTERN_MAPS } from '@/constants/regExp'
import { GetLogisticsReceiverAddressPageResponseDetail, postLogisticsReceiverAddressUpdate } from '@/services/LogisticsApi' import { GetLogisticsReceiverAddressPageResponseDetail, postLogisticsReceiverAddressUpdate, GetLogisticsReceiverAddressGetResponse } from '@/services/LogisticsApi'
import styles from './index.less' import styles from './index.less'
import { isEmpty } from 'lodash' import { isEmpty } from 'lodash'
...@@ -46,13 +46,16 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => { ...@@ -46,13 +46,16 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => {
const [cityName, setCityName] = useState('') const [cityName, setCityName] = useState('')
const [districtName, setDistrictName] = useState('') const [districtName, setDistrictName] = useState('')
const [provinceList, setProvinceList] = useState([]) const [provinceList, setProvinceList] = useState([])
const [cityList] = useState([]) const [cityList, setCityList] = useState([])
const [districtList, setDistrictList] = useState([])
// const [addressDetail, setAddressDetail] = useState<GetLogisticsReceiverAddressGetResponse>()
const initAddressItemInfo = async () => { const initAddressItemInfo = async () => {
const param: any = { const param: any = {
id: editItem.id id: editItem.id
} }
const addressDetailRes = await PublicApi.getLogisticsReceiverAddressGet(param) const addressDetailRes = await PublicApi.getLogisticsReceiverAddressGet(param)
// setAddressDetail(addressDetailRes.data)
const addressDetail = addressDetailRes.data const addressDetail = addressDetailRes.data
Object.keys(addressDetail).forEach(key => { Object.keys(addressDetail).forEach(key => {
...@@ -60,11 +63,11 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => { ...@@ -60,11 +63,11 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => {
if (key == 'isDefault') { if (key == 'isDefault') {
state.value = addressDetail[key] === 0 ? false : true state.value = addressDetail[key] === 0 ? false : true
} else if (key == 'provinceCode') { } else if (key == 'provinceCode') {
state.value = `${addressDetail.provinceCode}-${addressDetail.provinceName}` state.value = `${addressDetail.provinceCode}`
} else if (key == 'cityCode') { } else if (key == 'cityCode') {
state.value = `${addressDetail.cityCode}-${addressDetail.cityName}` state.value = `${addressDetail.cityCode}`
} else if (key == 'districtCode') { } else if (key == 'districtCode') {
state.value = `${addressDetail.districtCode}-${addressDetail.districtName}` state.value = `${addressDetail.districtCode}`
} else { } else {
state.value = addressDetail[key] state.value = addressDetail[key]
} }
...@@ -76,16 +79,14 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => { ...@@ -76,16 +79,14 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => {
if(isEmpty(telCodeList)) { if(isEmpty(telCodeList)) {
PublicApi.getManageCountryAreaGetTelCode().then(res => { PublicApi.getManageCountryAreaGetTelCode().then(res => {
const result: any = [] const result: any = []
res.data.forEach((item, index) => { res.data.forEach((item) => {
result.push({ label: item, value: (index).toString() }) result.push({ label: item, value: item })
}) })
setTelCodeList(result) setTelCodeList(result)
}) })
} }
} }
useEffect(() => { useEffect(() => {
if(visible) { if(visible) {
fetchCountryAreaTelCode() fetchCountryAreaTelCode()
...@@ -94,15 +95,14 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => { ...@@ -94,15 +95,14 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => {
getAllCode = setTimeout(() => { getAllCode = setTimeout(() => {
PublicApi.getManageAreaByPcodeAll({ pcode: '100000' }).then(res => { PublicApi.getManageAreaByPcodeAll({ pcode: '100000' }).then(res => {
const list = [] const list = []
res.data.forEach((item: any, index: number) => { res.data.forEach((item: any) => {
list.push({ label: item.name, value: `${item.code}-${item.name}` }) list.push({ label: item.name, value: item.code })
}) })
setProvinceList(list) setProvinceList(list)
}) })
}, 1000) }, 1000)
} }
if (type === 'edit' && editItem) { if (type === 'edit' && editItem) {
initAddressItemInfo() initAddressItemInfo()
} }
...@@ -121,12 +121,10 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => { ...@@ -121,12 +121,10 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => {
const formSubmit = (values) => { const formSubmit = (values) => {
const value = { ...values } const value = { ...values }
value.isDefault = values.isDefault ? 1 : 0 value.isDefault = values.isDefault ? 1 : 0
value.provinceCode = value.provinceCode.split('-').length > 1 ? value.provinceCode.split('-')[0] : value.provinceCode
value.cityCode = value.cityCode.split('-').length > 1 ? value.cityCode.split('-')[0] : value.cityCode value.provinceName = getNameByCode(value.provinceCode, provinceList)
value.districtCode = value.districtCode.split('-').length > 1 ? value.districtCode.split('-')[0] : value.districtCode value.cityName = getNameByCode(value.cityCode, cityList)
value.provinceName = provinceName value.districtName = getNameByCode(value.districtCode, districtList)
value.cityName = cityName
value.districtName = districtName
setConfirmLoading(true) setConfirmLoading(true)
let postLogisticsFn let postLogisticsFn
if (type === 'edit') { if (type === 'edit') {
...@@ -146,6 +144,19 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => { ...@@ -146,6 +144,19 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => {
}) })
} }
const getNameByCode = (code, list) => {
if(list && code) {
let result = ""
list.forEach(item => {
if(item.value === code) {
result = item.label
}
});
return result
}
return ""
}
/** /**
* @description: 自定义HOOK * @description: 自定义HOOK
* @param {type} * @param {type}
...@@ -154,45 +165,37 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => { ...@@ -154,45 +165,37 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => {
const useAreaEffects = () => { const useAreaEffects = () => {
const { setFieldState } = createFormActions() const { setFieldState } = createFormActions()
onFieldValueChange$('provinceCode').subscribe(({ value }) => { onFieldValueChange$('provinceCode').subscribe(({ value }) => {
if(!value) return
setFieldState('districtCode', state => { setFieldState('districtCode', state => {
state.value = '' state.value = ''
}) })
const name = value && value.split('-').length > 1 ? value.split('-')[1] : '' setFieldState('*(cityCode)', (state) => {
setProvinceName(name)
setFieldState('*(cityCode)', state => {
state.value = '' state.value = ''
const list = [] const list = []
const pcode = value && value.split('-').length > 1 ? value.split('-')[0] : value const pcode = value
PublicApi.getManageAreaByPcodeAll({ pcode: pcode }).then(res => { PublicApi.getManageAreaByPcodeAll({ pcode: pcode }).then(res => {
res.data.forEach((item: any, index: number) => { res.data.forEach((item: any) => {
// list.push({ label: item.name, value: item.code }) list.push({ label: item.name, value: item.code })
list.push({ label: item.name, value: `${item.code}-${item.name}` })
}) })
setCityList(list)
}) })
FormPath.setIn(state, 'props.enum', list)
}) })
}) })
onFieldValueChange$('cityCode').subscribe(({ value }) => { onFieldValueChange$('cityCode').subscribe(({ value }) => {
const name = value && value.split('-').length > 1 ? value.split('-')[1] : '' if(!value) return
setCityName(name)
setFieldState('*(districtCode)', state => { setFieldState('*(districtCode)', state => {
state.value = '' state.value = ''
const list = [] const list = []
const pcode = value && value.split('-').length > 1 ? value.split('-')[0] : value const pcode = value
PublicApi.getManageAreaByPcodeAll({ pcode: pcode }).then(res => { PublicApi.getManageAreaByPcodeAll({ pcode: pcode }).then(res => {
res.data.forEach((item: any, index: number) => { res.data.forEach((item: any) => {
// list.push({ label: item.name, value: item.code }) list.push({ label: item.name, value: item.code })
list.push({ label: item.name, value: `${item.code}-${item.name}` })
}) })
setDistrictList(list)
}) })
FormPath.setIn(state, 'props.enum', list)
}) })
}) })
onFieldValueChange$('districtCode').subscribe(({ value }) => {
const name = value && value.split('-').length > 1 ? value.split('-')[1] : ''
setDistrictName(name)
})
} }
const handleOk = () => { const handleOk = () => {
...@@ -264,7 +267,7 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => { ...@@ -264,7 +267,7 @@ const AddAddress: React.FC<AddAddressPropsType> = (props) => {
<Field <Field
x-mega-props={{ span: 1 }} x-mega-props={{ span: 1 }}
x-component="Select" x-component="Select"
enum={[]} enum={districtList}
x-rules={{ x-rules={{
required: true, required: true,
message: '请选择区', message: '请选择区',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment