Formpage.jsx 20.2 KB
Newer Older
krysent's avatar
krysent committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
import React, { memo, useEffect, useRef, useState, useReducer } from "react";
import {
  Image,
  Divider,
  Menu,
  Dropdown,
  Button,
  Modal,
  Row,
  Col,
  Tooltip,
  Drawer,
  Form,
  Input,
  message,
  Typography,
} from "antd";
import { connect, useRequest } from "umi";
import AutoTable from "@/components/Tableform";
import getPrem from "@/utils/getPrem"; //权限判断fn
import InitForm from "@/components/InitForm";
import { doFetch } from "@/utils/doFetch";
import { tableField } from "./fields";
import moment from "moment";
import {
  factorySelect,
  zuzhi,
  allDepartment,
  allShop,
  depart,
  selectionBoxAll,
  roleList,
  currentUserList,
} from "@/services/system";
const { TextArea } = Input,
  { Paragraph } = Typography;

const initState = {
    vs: false,
    tableFields: {},
    iftype: {},
    tableData: [],
    hidesTitle: true,
    hidesContent: true,
    formFields: {},
    tableLoading: false,
    variableVisible: false,
    variableExtraparams: "",
  },
  sendToArr = [
    {
      label: "可操作人员",
      value: 1,
    },
    {
      label: "创建人",
      value: 2,
    },
    {
      label: "(上个通知人员)上级领导",
      value: 3,
    },
    {
      label: "选择角色",
      value: 4,
    },
    {
      label: "选择人员",
      value: 5,
    },
  ],
  unitArr = [
    {
      label: "分钟",
      value: 1,
    },
    {
      label: "小时",
      value: 2,
    },
    {
      label: "天",
      value: 3,
    },
  ],
  sendProcessArr = [
    {
      label: "一级通知",
      value: 1,
    },
    {
      label: "二级通知",
      value: 2,
    },
    {
      label: "三级通知",
      value: 3,
    },
    {
      label: "四级通知",
      value: 4,
    },
    {
      label: "五级通知",
      value: 5,
    },
    {
      label: "六级通知",
      value: 6,
    },
    {
      label: "七级通知",
      value: 7,
    },
    {
      label: "八级通知",
      value: 8,
    },
  ];
function reducer(state, action) {
  if (action.type == "add") {
    return {
      ...state,
      vs: true,
      tableFields: { ...action.tableFields },
      iftype: {
        val: "add",
        title: "新增基础设置",
        id: "",
      },
    };
  } else if (action.type == "edit") {
    return {
      ...state,
      vs: true,
      tableFields: { ...action.tableFields },
      iftype: {
        val: "edit",
        title: "修改基础设置",
        id: action.curitem.id,
      },
    };
  } else if (action.type == "changeHideTitle") {
    return {
      ...state,
      hidesTitle: action.hidesTitle,
    };
  } else if (action.type == "changeHideContent") {
    return {
      ...state,
      hidesContent: action.hidesContent,
    };
  } else if (action.type == "changeFormFields") {
    return {
      ...state,
      formFields: { ...action.formFields },
    };
  } else if (action.type == "start") {
    return {
      ...state,
      formFields: { ...action.formFields },
      tableData: [...action.tableData],
    };
  } else if (action.type == "changeTableFields") {
    return {
      ...state,
      tableFields: { ...action.tableFields },
    };
  } else if (action.type == "changeTableData") {
    return {
      ...state,
      tableData: [...action.tableData],
      vs: false,
      tableLoading: false,
    };
  } else if (action.type == "changeLoading") {
    return {
      ...state,
      tableLoading: true,
    };
  } else if (action.type == "changeShown") {
    return {
      ...state,
      shown: action.shown,
    };
  } else if (action.type == "openVariable") {
    return {
      ...state,
      variableVisible: true,
      variableExtraparams: action.variableExtraparams,
    };
  } else if (action.type == "close") {
    return {
      ...state,
      vs: false,
      iftype: {},
      tableFields: {},
      variableVisible: false,
      variableExtraparams: "",
    };
  }
}

const Variable = (props) => {
  const { businessMsgSceneId, dispatch } = props;
  return (
    <div>
      <AutoTable
        withCard={false}
        columns={[
          {
            title: "变量名",
            dataIndex: "variableName",
            key: "variableName",
            width: 120,
          },
          {
            title: "变量代码",
            dataIndex: "variableCode",
            key: "variableCode",
            search: false,
          },
          {
            title: "复制",
            valueType: "option",
            search: false,
            render: (_, row) => {
              return (
                <Paragraph
                  copyable={{
                    text: row.variableCode,
                    onCopy: () => {
                      dispatch({ type: "close" });
                    },
                  }}
                >
                  复制
                </Paragraph>
              );
            },
          },
        ]}
        bordered={false}
        path="/ngic-base-business/paBusinessMsgSceneVariable/queryPageBySceneId"
        extraparams={{ businessMsgSceneId }}
        x={"100%"}
      ></AutoTable>
    </div>
  );
};
const Formpage = (props) => {
  const { rowMessage, reset, iftypeParent } = props;
  let actionRef = useRef(),
    ChildRef = null;
  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }

  useEffect(() => {
    if (iftypeParent.val == "add") {
      for (let i in fields) {
        fields[i].hides = false;
        fields[i].value = null;
      }
    } else if (iftypeParent.val == "edit") {
      let database = getdatabase(rowMessage["fitField"]);
      for (let i in fields) {
        fields[i].value = rowMessage[i];
        if (i == "sendMethodType") {
          showTitleorContent(rowMessage[i]);
          form.setFieldsValue({
            sendTitle: rowMessage.sendTitle,
            sendContent: rowMessage.sendContent,
          });
        }
        if (i == "fieldId" && rowMessage["fitField"] == 1) {
          fields[i].hides = true;
        } else if (i == "fieldId" && rowMessage["fitField"] != 1) {
          fields[i].hides = false;
          fields[i].options = {
            database,
            params: {},
          };
        }
      }
    }
    dispatch({
      type: "start",
      formFields: { ...fields },
      tableData: iftypeParent.val == "add" ? [] : rowMessage?.detailList,
    });
  }, [fields]);

  const { fields } = props,
    [form] = Form.useForm(),
    [forms] = Form.useForm(),
    [tableFormRef] = Form.useForm(),
    [state, dispatch] = useReducer(reducer, initState),
    {
      vs,
      tableFields,
      iftype,
      tableData,
      hidesTitle,
      hidesContent,
      formFields,
      tableLoading,
      variableVisible,
      shown,
      variableExtraparams,
    } = state,
    columns = [
      {
        title: "推送流程",
        dataIndex: "sendProcessName",
        key: "sendProcessName",
        search: false,
      },
      {
        title: "推送对象",
        dataIndex: "sendToName",
        key: "sendToName",
        search: false,
      },
      {
        title: "选择对象",
        dataIndex: "targetNames",
        key: "targetNames",
        search: false,
      },
      {
        title: "初始触发时长",
        dataIndex: "initialTime",
        key: "initialTime",
        search: false,
      },
      {
        title: "初始触发时长单位",
        dataIndex: "initialUnitName",
        key: "initialUnitName",
        search: false,
      },
      {
        title: "间隔时长",
        dataIndex: "intervalTime",
        key: "intervalTime",
        search: false,
      },
      {
        title: "间隔时长单位",
        dataIndex: "intervalUnitName",
        key: "intervalUnitName",
        search: false,
      },
      {
        title: "触发次数",
        dataIndex: "sendNum",
        key: "sendNum",
        search: false,
      },
      {
        title: "操作",
        valueType: "option",
        width: 150,
        render: (text, row, _, action) => extraAction(text, row, _, action),
      },
    ],
    { run, loading } = useRequest(doFetch, {
      manual: true,
      formatResult: (res) => res,
      onSuccess: (result, params) => {
        if (result.code == "0000") {
          reset && reset();
        }
      },
    });

  let extrarender = [
    <Button
      size={"middle"}
      type="primary"
      onClick={() => {
        for (let i in tableField) {
          tableField[i].value = null;
          if (i == "targetIdList") {
            tableField[i].hides = true;
            tableField[i].value = [];
          }
          if (i == "initialTime") {
            tableField[i].value = 0;
          }
          if (i == "intervalTime") {
            tableField[i].value = 1;
          }
          if (i == "sendNum") {
            tableField[i].value = 1;
          }
        }
        dispatch({ type: "add", tableFields: tableField });
      }}
    >
      新增
    </Button>,
  ];
  function extraAction(text, record, _, action) {
    return (
      <div>
        {getPrem(true, null, "修改", () => {
          for (let j in tableField) {
            tableField[j].value = record[j];
            if (j == "targetIdList") {
              if (record.sendTo != 4 && record.sendTo != 5) {
                tableField[j].hides = true;
              } else {
                let database = getformdatabase(record.sendTo);
                tableField[j].hides = false;
                tableField[j].options = {
                  database,
                  params: {},
                };
              }
            }
            dispatch({
              type: "edit",
              tableFields: tableField,
              curitem: record,
            });
          }
        })}
        <Divider type="vertical" />
        {getPrem(true, "red", "删除", null, {
          title: "确认删除该基础配置?",
          onConfirm: () => {
            let newData = tableData.filter((it) => it.id != record.id);
            dispatch({ type: "changeTableData", tableData: newData });
          },
        })}
      </div>
    );
  }
  let saveData = (values, fn) => {
      dispatch({ type: "changeLoading" });
      const {
        sendTo,
        targetIdList,
        sendProcess,
        initialUnit,
        initialTime,
        intervalTime,
        intervalUnit,
        sendNum,
      } = values;
      let targetNames = [];
      let all = async () => {
        if (sendTo == 4) {
          let data = await roleList({});
          return {
            newArr: data?.data?.dataList || [],
          };
        } else if (sendTo == 5) {
          let data = await currentUserList({});
          return {
            newArr: data?.data?.dataList || [],
          };
        } else {
          return {
            newArr: [],
          };
        }
      };
      all().then((res) => {
        let dataArr = res.newArr;
        targetNames =
          dataArr
            ?.filter((it) => targetIdList?.indexOf(it.value) > -1)
            ?.map((it) => it.label)
            ?.join(",") || "";
        let params = {
            sendTo,
            targetNames,
            sendProcess,
            sendProcessName:
              sendProcessArr.filter((it) => it.value == sendProcess).length > 0
                ? sendProcessArr.filter((it) => it.value == sendProcess)[0]
                    .label
                : "",
            initialUnit,
            initialTime,
            intervalTime,
            intervalUnit,
            sendNum,
            targetIdList,
            id: iftype.id
              ? iftype.id
              : moment().valueOf() +
                Math.floor(Math.random() * (9999 - 1000)) +
                1000,
            sendToName:
              sendToArr.filter((it) => it.value == sendTo).length > 0
                ? sendToArr.filter((it) => it.value == sendTo)[0].label
                : "",
            initialUnitName:
              unitArr.filter((it) => it.value == initialUnit).length > 0
                ? unitArr.filter((it) => it.value == initialUnit)[0].label
                : "",
            intervalUnitName:
              unitArr.filter((it) => it.value == intervalUnit).length > 0
                ? unitArr.filter((it) => it.value == intervalUnit)[0].label
                : "",
          },
          newTable;
        if (iftype.val == "edit") {
          newTable = tableData.map((it) => {
            if (it.id == iftype.id) {
              it = { ...params };
            }
            return it;
          });
        } else {
          newTable = [...tableData];
          newTable.unshift(params);
        }
        dispatch({ type: "changeTableData", tableData: newTable });
      });
    },
    saveForm = () => {
      let editId = rowMessage ? { id: rowMessage.id } : {},
        all = async () => {
          let values = await forms.validateFields(),
            valuesc = await form.validateFields();
          return {
            values,
            valuesc,
          };
        };
      all().then((res) => {
        if (!tableData.length) {
          return message.warning("基础配置不可为空!");
        }
        let detailList = tableData.map((it) => {
            if (iftypeParent.val == "edit") {
              return {
                sendProcess: it.sendProcess,
                sendTo: it.sendTo,
                targetIdList: it.targetIdList,
                initialTime: it.initialTime,
                initialUnit: it.initialUnit,
                intervalTime: it.intervalTime,
                intervalUnit: it.intervalUnit,
                sendNum: it.sendNum,
                id: it.id && typeof it.id == "number" ? "" : it.id,
              };
            } else {
              return {
                sendProcess: it.sendProcess,
                sendTo: it.sendTo,
                targetIdList: it.targetIdList,
                initialTime: it.initialTime,
                initialUnit: it.initialUnit,
                intervalTime: it.intervalTime,
                intervalUnit: it.intervalUnit,
                sendNum: it.sendNum,
              };
            }
          }),
          params = {
            ...res.values,
            ...res.valuesc,
            detailList,
            ...editId,
          };
        run({
          url: "/ngic-base-business/paBusinessMsgOption/save",
          params: { ...params },
        });
      });
    };

  function showTitleorContent(val) {
    if (val == 4) {
      dispatch({ type: "changeHideTitle", hidesTitle: false });
    } else {
      dispatch({ type: "changeHideTitle", hidesTitle: false });
    }
    if (val == 2) {
      dispatch({ type: "changeHideContent", hidesContent: false });
    } else {
      dispatch({ type: "changeHideContent", hidesContent: false });
    }
  }
  function getdatabase(val) {
    let database;
    switch (val) {
      case 2:
        database = factorySelect;
        break;
      case 3:
        database = zuzhi;
        break;
      case 4:
        database = selectionBoxAll;
        break;
    }
    return database;
  }
  function getformdatabase(val) {
    let database;
    switch (val) {
      case 4:
        database = roleList;
        break;
      case 5:
        database = currentUserList;
        break;
    }
    return database;
  }

  return (
    <div>
      <InitForm
        fields={formFields}
        onChange={(changedValues, allValues) => {
          for (let i in changedValues) {
            if (i == "sendMethodType") {
              showTitleorContent(changedValues[i]);
            }
            if (i == "fitField") {
              let database = getdatabase(changedValues[i]);
              for (let j in formFields) {
                if (j == "fieldId" && changedValues[i] == 1) {
                  formFields[j].hides = true;
                } else if (j == "fieldId" && changedValues[i] != 1) {
                  formFields[j].hides = false;
                  formFields[j].options = {
                    database,
                    params: {},
                  };
                  formFields[j].type =
                    changedValues[i] == 3 ? "treeselect" : "select";

                  forms.setFieldsValue({ fieldId: "" });
                }
              }
              dispatch({ type: "changeFormFields", formFields: formFields });
            }
          }
        }}
        actions={(form, submitBtn) => {
          return null;
        }}
        formRef={forms}
      ></InitForm>
      <Form form={form}>
        {!hidesTitle && (
          <Form.Item
            name="sendTitle"
            rules={[
              {
                required: true,
                message: "请输入推送标题",
              },
            ]}
          >
            <div>
              <div>
                <span style={{ color: "#ff4d4f" }}>*</span> 推送标题
              </div>
              <TextArea
                rows={4}
                defaultValue={
                  iftypeParent.val == "edit" ? rowMessage.sendTitle : ""
                }
              />
            </div>
          </Form.Item>
        )}
        {(!hidesTitle || !hidesContent) && (
          <Button
            type="primary"
            style={{ marginBottom: 15 }}
            onClick={() => {
              let params = forms.getFieldValue("businessMsgSceneId");
              dispatch({ type: "openVariable", variableExtraparams: params });
            }}
          >
            查看变量
          </Button>
        )}
        {!hidesContent && (
          <Form.Item
            name="sendContent"
            rules={[
              {
                required: true,
                message: "请输入推送内容",
              },
            ]}
          >
            <div>
              <div>
                <span style={{ color: "#ff4d4f" }}>*</span> 推送内容
              </div>
              <TextArea
                rows={4}
                defaultValue={
                  iftypeParent.val == "edit" ? rowMessage.sendContent : ""
                }
              />
            </div>
          </Form.Item>
        )}
      </Form>
      {!tableLoading && (
        <AutoTable
          pagetitle="基础设置"
          pageextra={extrarender}
          columns={columns}
          actionRef={actionRef}
          onRef={(node) => (ChildRef = node)}
          dataSource={tableData}
        ></AutoTable>
      )}
      <div style={{ display: "flex", marginTop: 15 }}>
        <Button
          loading={loading}
          type="primary"
          size="large"
          style={{ flex: 1 }}
          onClick={saveForm}
        >
          提交
        </Button>
      </div>
      <Modal
        maskClosable={false}
        title={iftype.title}
        visible={vs}
        onCancel={() => dispatch({ type: "close" })}
        style={{ top: 20 }}
        footer={false}
        width="1000px"
        destroyOnClose={true}
      >
        <InitForm
          fields={tableFields}
          submitData={(values, fn) => {
            saveData(values, fn);
          }}
          formRef={tableFormRef}
          onChange={(changedValues, allValues) => {
            for (let i in changedValues) {
              if (i == "sendTo") {
                for (let j in tableField) {
                  if (j == "targetIdList") {
                    tableFormRef.setFieldsValue({ targetIdList: [] });
                    tableField[j].value = [];
                    if (changedValues[i] != 4 && changedValues[i] != 5) {
                      tableField[j].hides = true;
                    } else {
                      let database = getformdatabase(changedValues[i]);
                      tableField[j].hides = false;
                      tableField[j].options = {
                        database,
                        params: {},
                      };
                    }
                  }
                }
                dispatch({
                  type: "changeTableFields",
                  tableFields: tableField,
                });
              }
            }
          }}
        ></InitForm>
      </Modal>
      <Drawer
        title={"查看变量"}
        closable={true}
        visible={variableVisible}
        onClose={() => dispatch({ type: "close" })}
        destroyOnClose={true}
        afterVisibleChange={(v) => {
          dispatch({ type: "changeShown", shown: v });
        }}
        width="40%"
      >
        {shown && (
          <Variable
            businessMsgSceneId={variableExtraparams}
            dispatch={dispatch}
          />
        )}
      </Drawer>
    </div>
  );
};

export default Formpage;