index.jsx 4.83 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
import React, { useEffect, useRef, useReducer } from "react";
import { Button } from "antd";
import AutoTable from "@/components/AutoTable";
import getPrem from "@/utils/getPrem"; //权限判断fn
import InitForm from "@/components/InitForm";
import { useRequest } from "umi";
import defaultFields from "./fields";
import { doFetch } from "@/utils/doFetch";
import DrawInitForm from "@/components/DrawInitForm";
const initState = {
  vs: false,
  fields: {},
  iftype: {},
  curitem: {},
};
function reducer(state, action) {
  let { type } = action,
    newState = {};
  switch (type) {
    case "add":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: action.title,
          val: type,
        },
        fields: { ...action.fields },
        curitem: action.curitem,
      };
      break;
    case "edit":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: action.title,
          val: type,
        },
        fields: { ...action.fields },
        curitem: action.curitem,
      };
      break;
    case "close":
      newState = {
        vs: false,
        fields: {},
        iftype: {},
        curitem: {},
      };
      break;
  }

  return newState;
}

const Dic = (props) => {
  let actionRef = useRef(),
    ChildRef = null;
  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }
  const { run, loading } = useRequest(doFetch, {
      manual: true,
      formatResult: (res) => res,
      onSuccess: (result, params) => {
        if (result.code == "0000") {
          reload();
          dispatch({ type: "close" });
        }
      },
    }),
    [state, dispatch] = useReducer(reducer, initState),
    { vs, fields, iftype, curitem } = state,
    columns = [
      {
        title: "数据编号",
        dataIndex: "no",
        key: "dicKey",
      },
      {
        title: "数据名称",
        dataIndex: "title",
        key: "dicName",
      },
      {
        title: "描述",
        dataIndex: "description",
        key: "description",
        search: false,
      },
      {
        title: "操作",
        dataIndex: "option_dataindex",
        key: "option_dataindex",
        valueType: "option",
        width: 135,
        render: (text, row, _, action) => extraAction(text, row, _, action),
      },
    ];

  function extraAction(text, record, _, action) {
    /**
     * type   1初始化   2自定义
     * isBasis 有无新增  1没有   2有
     */
    let ifs = record.type == 1,
      ifsc = record.isBasis == 2;
    return [
      ifs &&
      ifsc&&
        getPrem("sysDepartment_save", action, "新增", () => {
          for (let i in defaultFields) {
            defaultFields[i].value = null;
          }
          dispatch({
            type: "add",
            fields: defaultFields,
            curitem: record,
            title: `新增 ${record.title} 下的数据字典`,
          });
        }),
      !ifs &&
        getPrem("sysDepartment_save", action, "修改", () => {
          for (let i in defaultFields) {
            defaultFields[i].value = record[i];
            if (i == "dicName") {
              defaultFields[i].value = record.title;
            }
            if (i == "dicDescription") {
              defaultFields[i].value = record.description;
            }
          }
          dispatch({
            type: "edit",
            fields: defaultFields,
            curitem: record,
            title: `修改${record.title}`,
          });
        }),
      !ifs &&
        getPrem("sysDepartment_deleteById", action, "删除", null, {
          title: "确认删除该数据字典?",
          onConfirm: () => {
            run({
              url: "/ngic-base-business/sysDic/deleteById",
              params: { id: record.key },
            });
          },
        }),
    ];
  }
  let saveData = (values, fn) => {
    let newfields = JSON.parse(JSON.stringify(values));
    //新增&修改
    let difrid =
      iftype.val == "edit" ? { id: curitem.key } : { parentId: curitem.key };
    run({
      url: "/ngic-base-business/sysDic/save",
      params: { ...newfields, ...difrid },
    });
  };
  return (
    <div>
      <AutoTable
        pagetitle={props.route.name} //页面标题
        columns={columns}
        path="/ngic-base-business/sysDic/queryTreeList"
        actionRef={actionRef}
        onRef={(node) => (ChildRef = node)}
        rowKey="key"
      ></AutoTable>

      <DrawInitForm
        title={iftype.title}
        visible={vs}
        onClose={() => dispatch({ type: "close" })}
        footer={false}
        destroyOnClose={true}
        fields={fields}
        submitData={(values) => {
          saveData(values);
        }}
        onChange={(changedValues, allValues) => {
          //联动操作
        }}
        submitting={loading || !vs}
      ></DrawInitForm>
    </div>
  );
};
export default Dic;