Commit e0e939e5 authored by 薛智杰's avatar 薛智杰

feat: 简单优化

parent cb57c3d6
...@@ -15,8 +15,8 @@ import { ...@@ -15,8 +15,8 @@ import {
notification, notification,
} from 'antd'; } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons'; import { ExclamationCircleOutlined } from '@ant-design/icons';
import { jsonToArray } from '@/utils/utils';
import moment from 'moment'; import moment from 'moment';
import { jsonToArray } from '@/utils/utils';
import { layout, mealType, boolOptions, hideOptions, mealSections } from '../staticData/index'; import { layout, mealType, boolOptions, hideOptions, mealSections } from '../staticData/index';
import { import {
apiEnterpriseInfo, apiEnterpriseInfo,
...@@ -33,15 +33,9 @@ const { confirm } = Modal; ...@@ -33,15 +33,9 @@ const { confirm } = Modal;
const CustomerInfo = props => { const CustomerInfo = props => {
const [form] = Form.useForm(); const [form] = Form.useForm();
const [meals, setMeals] = useState({}); const [meals, setMeals] = useState({});
const [mealTypes, setMealTypes] = useState([]); const [selectedMealTypes, setSelectedMealTypes] = useState([]);
const [pickSelfList, setPickSelfList] = useState([]); const [pickSelfList, setPickSelfList] = useState([]);
// 关闭分组信息弹窗
const handleCancel = () => {
props.reFresh();
props.handleClose(false);
};
// 校验时间 // 校验时间
const checkTime = (arr, curren, curName) => { const checkTime = (arr, curren, curName) => {
let valid = false; let valid = false;
...@@ -109,7 +103,7 @@ const CustomerInfo = props => { ...@@ -109,7 +103,7 @@ const CustomerInfo = props => {
limit: [], limit: [],
}; };
Object.keys(res.mealLimit[item]).forEach(t => { Object.keys(res.mealLimit[item]).forEach(t => {
if (mealTypes.includes(t)) { if (selectedMealTypes.includes(t)) {
json.limit.push({ json.limit.push({
mealType: t, mealType: t,
limit: res.mealLimit[item][t], limit: res.mealLimit[item][t],
...@@ -132,8 +126,8 @@ const CustomerInfo = props => { ...@@ -132,8 +126,8 @@ const CustomerInfo = props => {
} }
const resp = await api(params); const resp = await api(params);
if (resp && resp.data) { if (resp && resp.data) {
handleCancel(); // 保存成功后刷新列表
props.reFresh(); props.onClose && props.onClose(true);
notification.success({ message: '保存成功!' }); notification.success({ message: '保存成功!' });
} }
}; };
...@@ -158,30 +152,30 @@ const CustomerInfo = props => { ...@@ -158,30 +152,30 @@ const CustomerInfo = props => {
// 改变餐品类型 (选自助餐必选外卖) // 改变餐品类型 (选自助餐必选外卖)
const onChangeMealType = async ms => { const onChangeMealType = async ms => {
console.log('mealTypes', selectedMealTypes, ms);
try { try {
if (props.info && props.info.id && ms.length < mealTypes.length) { // 编辑时,取消餐段,提示确认
console.log('props.info :>> ', props.id);
if (props.id && ms.length < selectedMealTypes.length) {
await checkConfirm(); await checkConfirm();
} }
// 添加餐段 // 取消外卖,必须取消自助餐
if (mealTypes.length < ms.length) { if (selectedMealTypes.includes('1') && !ms.includes('1')) {
if (!ms.includes('1') && ms.includes('2')) { ms = ms.filter(item => item !== '2');
ms.push('1'); }
}
} else if (!ms.includes('1') && ms.includes('2')) { // 选择自助餐,必须选择外卖
// 去除餐段 if (!selectedMealTypes.includes('1') && ms.includes('2')) {
if (ms.includes('4')) { ms.push('1');
ms = ['4'];
} else {
ms = [];
}
} }
form.setFieldsValue({ form.setFieldsValue({
mealType: ms, mealType: ms,
}); });
setMealTypes(ms); setSelectedMealTypes(ms);
} catch { } catch {
form.setFieldsValue({ form.setFieldsValue({
mealType: mealTypes, mealType: selectedMealTypes,
}); });
} }
}; };
...@@ -201,55 +195,69 @@ const CustomerInfo = props => { ...@@ -201,55 +195,69 @@ const CustomerInfo = props => {
const getInfo = async () => { const getInfo = async () => {
const res = await apiEnterpriseInfo(props.id); const res = await apiEnterpriseInfo(props.id);
if (res && res.data) { if (res && res.data) {
const obj = Object.assign({}, res.data); const {
if (res.data.mealTimePeriod && res.data.mealTimePeriod.length) { hideImage,
const m = moment().format('YYYY-MM-DD'); hidePrice,
const arr = Object.keys(mealSections); id,
obj.mealTimePeriod = Object.keys(mealSections).map(() => ({})); name,
res.data.mealTimePeriod.forEach((item, i) => { mealLimit,
if (item) { mealTimePeriod = [],
const index = arr.indexOf(`${item.mealPeriodType}`); mealType: type,
if (index > -1) { weekPreview,
obj.mealTimePeriod[index] = { } = res.data;
mealPeriodType: `${item.mealPeriodType}`, const formData = {
time: [moment(`${m} ${item.beginTime}`), moment(`${m} ${item.endTime}`)], id,
}; name,
} weekPreview,
} mealType: type?.map(item => `${item}`) ?? [],
}); hideInfo: [],
} else { };
obj.mealTimePeriod = [];
} // 数据模型转换-隐藏信息
obj.mealLimit = {}; if (+hidePrice) {
if (res.data.mealLimit && res.data.mealLimit.length) { formData.hideInfo.push('hidePrice');
res.data.mealLimit.forEach(item => {
obj.mealLimit[`limit${item.mealPeriodType}`] = {};
item.limit.forEach(limit => {
obj.mealLimit[`limit${item.mealPeriodType}`][limit.mealType] = limit.limit;
});
});
}
obj.hideInfo = [];
if (+res.data.hidePrice) {
obj.hideInfo.push('hidePrice');
}
if (+res.data.hideImage) {
obj.hideInfo.push('hideImage');
} }
if (res.data.mealType) { if (+hideImage) {
obj.mealType = res.data.mealType.map(item => `${item}`); formData.hideInfo.push('hideImage');
} else {
obj.mealType = [];
} }
setMealTypes(obj.mealType);
const json = {}; // 数据模型转换-餐段配置,转为 {餐段:餐段名称}
if (res.data.mealTimePeriod) { // 把mealTimePeriod按mealPeriodType转为map
res.data.mealTimePeriod.forEach(item => { const mealTimePeriodMap = {};
json[item.mealPeriodType] = mealSections[item.mealPeriodType]; mealTimePeriod.forEach(item => {
mealTimePeriodMap[item.mealPeriodType] = mealSections[item.mealPeriodType];
});
setMeals(mealTimePeriodMap);
// 数据模型转换-餐段和时间配置, [{餐段, time}, {}, {}]
const mealTimePeriodArr = Object.keys(mealSections).map(() => ({}));
formData.mealTimePeriod = mealTimePeriodArr;
mealTimePeriod.forEach(item => {
if (!item) return;
const index = Object.keys(mealSections).indexOf(`${item.mealPeriodType}`);
if (index > -1) {
formData.mealTimePeriod[index] = {
mealPeriodType: `${item.mealPeriodType}`,
time: [
moment(`${moment().format('YYYY-MM-DD')} ${item.beginTime}`),
moment(`${moment().format('YYYY-MM-DD')} ${item.endTime}`),
],
};
}
});
// 数据模型转换-消费限额, 转为{餐段: {餐品类型: 限额}}
const mealLimitMap = {};
mealLimit.forEach(item => {
mealLimitMap[`limit${item.mealPeriodType}`] = {};
item.limit.forEach(t => {
mealLimitMap[`limit${item.mealPeriodType}`][t.mealType] = t.limit;
}); });
} });
setMeals(json); formData.mealLimit = mealLimitMap;
form.setFieldsValue(obj);
setSelectedMealTypes(formData.mealType);
form.setFieldsValue(formData);
} }
}; };
...@@ -272,13 +280,13 @@ const CustomerInfo = props => { ...@@ -272,13 +280,13 @@ const CustomerInfo = props => {
if (props.id) { if (props.id) {
getInfo(); getInfo();
} else { } else {
setMealTypes([]); setSelectedMealTypes([]);
setMeals({}); setMeals({});
form.resetFields(); form.resetFields();
getPickSelf(); getPickSelf();
} }
} else { } else {
setMealTypes([]); setSelectedMealTypes([]);
setMeals({}); setMeals({});
form.setFieldsValue({}); form.setFieldsValue({});
} }
...@@ -292,7 +300,7 @@ const CustomerInfo = props => { ...@@ -292,7 +300,7 @@ const CustomerInfo = props => {
maskClosable={false} maskClosable={false}
width="900px" width="900px"
onOk={handleConfirm} onOk={handleConfirm}
onCancel={handleCancel} onCancel={() => props.onClose(false)}
> >
<Form <Form
name="basicInfo" name="basicInfo"
...@@ -358,7 +366,7 @@ const CustomerInfo = props => { ...@@ -358,7 +366,7 @@ const CustomerInfo = props => {
> >
{fs => ( {fs => (
<Row key={`row${meal}`}> <Row key={`row${meal}`}>
{mealTypes.map((t, i) => ( {selectedMealTypes.map((t, i) => (
<Col span={7} offset={i ? 1 : 0} key={t}> <Col span={7} offset={i ? 1 : 0} key={t}>
<MealLimit value={t} label={mealType[t]} name={`${t}`} /> <MealLimit value={t} label={mealType[t]} name={`${t}`} />
</Col> </Col>
......
...@@ -29,6 +29,12 @@ const BusinessCustomer = () => { ...@@ -29,6 +29,12 @@ const BusinessCustomer = () => {
setVisible(true); setVisible(true);
}; };
const onClose = refresh => {
console.log(3);
setVisible(false);
refresh && refTable.current.reload();
};
return ( return (
<div className={utilStyle.formPageBox}> <div className={utilStyle.formPageBox}>
<ProTable <ProTable
...@@ -57,12 +63,7 @@ const BusinessCustomer = () => { ...@@ -57,12 +63,7 @@ const BusinessCustomer = () => {
</Button>, </Button>,
]} ]}
/> />
<CustomerInfo <CustomerInfo visible={visible} id={id} onClose={onClose} />
visible={visible}
id={id}
reFresh={() => refTable.current.reload()}
handleClose={setVisible}
/>
</div> </div>
); );
}; };
......
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