index.jsx 14 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12
import React, { useEffect, useRef, useReducer } from "react";
import { Button, Tooltip, Row, Divider, message } from "antd";
import AutoTable from "@/components/AutoTable";
import getPrem from "@/utils/getPrem"; //权限判断fn
import { useRequest } from "umi";
import defaultFields from "./fields";
import { doFetch } from "@/utils/doFetch";
import DrawInitForm from "@/components/DrawInitForm";
import { userDetails, userCheckDetails } from "@/utils/detailTotalCard";
import Details from "@/components/Details";
import { userSelect } from "@/services/system";
const initState = {
wuhao's avatar
wuhao committed
13 14 15 16 17 18 19 20 21 22 23
  vs: false,
  fields: {},
  iftype: {},
  curitem: {},
  detail: {
    dataSource: {},
    totalCard: [],
  },
  visible: false,
  extraparams: {
    orderType: "1",
wuhao's avatar
wuhao committed
24
  },
wuhao's avatar
wuhao committed
25
},
wuhao's avatar
wuhao committed
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
  tabList = [
    {
      key: "1",
      tab: "正式用户",
    },
    {
      key: "2",
      tab: "用户申请",
    },
  ];
function reducer(state, action) {
  let { type } = action,
    newState = {};
  switch (type) {
    case "add":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: "新增用户",
          val: type,
        },
        fields: { ...action.fields },
      };
      break;
    case "edit":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: "编辑用户",
          val: type,
        },
        fields: { ...action.fields },
        curitem: action.curitem,
      };
      break;
    case "audit":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: "审批用户",
          val: type,
        },
        fields: { ...action.fields },
        curitem: action.curitem,
      };
      break;
    case "see":
      newState = {
        ...state,
        detail: {
          dataSource: action.dataSource,
          totalCard: action.totalCard,
        },
        visible: true,
      };
      break;
    case "start":
      newState = {
        ...state,
        extraparams: {
          orderType: action.tabKey,
        },
      };
      break;
    case "close":
      newState = {
        ...state,
        vs: false,
        fields: {},
        iftype: {},
        curitem: {},
        detail: {
          dataSource: {},
          totalCard: [],
        },
        visible: false,
      };
      break;
  }

  return newState;
}
const Staff = (props) => {
  let actionRef = useRef(),
    ChildRef = null;
  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }
  const { run, loading } = useRequest(doFetch, {
wuhao's avatar
wuhao committed
119 120 121 122 123 124 125 126 127
    manual: true,
    formatResult: (res) => res,
    onSuccess: (result, params) => {
      if (result.code == "0000") {
        reload();
        dispatch({ type: "close" });
      }
    },
  }),
wuhao's avatar
wuhao committed
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
    [state, dispatch] = useReducer(reducer, initState),
    { vs, fields, iftype, curitem, detail, visible, extraparams } = state,
    columns = [
      {
        title: "用户名",
        dataIndex: "accountName",
        key: "accountName",
      },
      {
        title: "姓名",
        dataIndex: "userName",
        key: "userName",
        render: (_, row) => {
          return (
            <Tooltip title={row.userName}>
              <a
                className="table-cell"
                onClick={() => {
                  doFetch({
                    url: "/ngic-auth/sysUser/query/detail",
                    params: { id: row.id },
                  }).then((res) => {
                    if (res.code == "0000") {
                      let dataSource = res?.data?.data ?? {},
                        totalCard = [...userDetails];
                      dataSource.genderName =
                        dataSource.gender == 1
                          ? "男"
                          : dataSource.gender == 2
wuhao's avatar
wuhao committed
157 158
                            ? "女"
                            : "-";
wuhao's avatar
wuhao committed
159 160 161 162 163 164 165 166 167 168 169
                      dispatch({ type: "see", dataSource, totalCard });
                    }
                  });
                }}
              >
                {row.userName}
              </a>
            </Tooltip>
          );
        },
      },
wuhao's avatar
wuhao committed
170 171 172 173 174
      {
        title: "组织",
        dataIndex: "departmentName",
        key: "departmentName",
      },
wuhao's avatar
wuhao committed
175 176 177 178 179 180
      {
        title: "工厂",
        dataIndex: "factoryName",
        key: "factoryName",
      },
      {
wuhao's avatar
wuhao committed
181 182 183
        title: "负责仓库",
        dataIndex: "chargeStoreName",
        key: "chargeStoreName",
wuhao's avatar
wuhao committed
184 185 186
      },
      {
        title: "角色",
wuhao's avatar
wuhao committed
187 188
        dataIndex: "roleName",
        key: "roleName",
wuhao's avatar
wuhao committed
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
      },
      {
        title: "联系电话",
        dataIndex: "telephone",
        key: "telephone",
      },
      {
        title: "操作",
        dataIndex: "option_dataindex",
        key: "option_dataindex",
        valueType: "option",
        width: 225,
        render: (text, row, _, action) => extraAction(text, row, _, action),
      },
    ],
    columnsc = [
      {
        title: "用户名",
        dataIndex: "accountName",
        key: "accountName",
      },
      {
        title: "姓名",
        dataIndex: "userName",
        key: "userName",
        render: (_, row) => {
          return (
            <Tooltip title={row.userName}>
              <a
                className="table-cell"
                onClick={() => {
                  doFetch({
                    url: "/ngic-auth/sysAccountApply/queryById",
                    params: { id: row.id },
                  }).then((res) => {
                    if (res.code == "0000") {
                      let dataSource = res?.data?.data ?? {},
                        totalCard = [...userCheckDetails];
                      dataSource.genderName =
                        dataSource.gender == 1
                          ? "男"
                          : dataSource.gender == 2
wuhao's avatar
wuhao committed
231 232
                            ? "女"
                            : "-";
wuhao's avatar
wuhao committed
233 234 235 236 237 238 239 240 241 242 243 244
                      dispatch({ type: "see", dataSource, totalCard });
                    }
                  });
                }}
              >
                {row.userName}
              </a>
            </Tooltip>
          );
        },
      },
      {
wuhao's avatar
wuhao committed
245
        title: "组织",
wuhao's avatar
wuhao committed
246 247 248 249
        dataIndex: "departmentName",
        key: "departmentName",
      },
      {
wuhao's avatar
wuhao committed
250 251 252
        title: "工厂",
        dataIndex: "factoryName",
        key: "factoryName",
wuhao's avatar
wuhao committed
253 254
      },
      {
wuhao's avatar
wuhao committed
255 256 257
        title: "负责仓库",
        dataIndex: "chargeStoreName",
        key: "chargeStoreName",
wuhao's avatar
wuhao committed
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
      },

      {
        title: "角色",
        dataIndex: "roleName",
        key: "roleName",
      },
      {
        title: "联系电话",
        dataIndex: "telephone",
        key: "telephone",
      },
      {
        title: "审批结果",
        dataIndex: "auditResultName",
        key: "auditResultName",
        valueType: "select",
        options: [
          {
            label: "通过",
            value: 1,
          },
          {
            label: "不通过",
            value: 2,
          },
        ],
        formItemProps: {
          name: "auditResult",
        },
      },
      {
        title: "工单状态",
        dataIndex: "statusName",
        key: "statusName",
        valueType: "select",
        options: [
          {
            label: "待审批",
            value: 1,
          },
          {
            label: "已审批",
            value: 2,
          },
        ],
        formItemProps: {
          name: "status",
        },
      },
      {
        title: "操作",
        dataIndex: "option_dataindex",
        key: "option_dataindex",
        valueType: "option",
        width: 150,
        render: (text, row, _, action) => extraAction(text, row, _, action),
      },
    ];
  function extraAction(text, record, _, action) {
    return [
      extraparams.orderType == "1" &&
wuhao's avatar
wuhao committed
320 321 322
      getPrem("sysUser_restPassword", action, "重置密码", null, {
        title: "确认重置密码?",
        onConfirm: () => {
wuhao's avatar
wuhao committed
323
          doFetch({
wuhao's avatar
wuhao committed
324
            url: "/ngic-auth/sysUser/restPassword",
wuhao's avatar
wuhao committed
325 326 327
            params: { id: record.id },
          }).then((res) => {
            if (res.code == "0000") {
wuhao's avatar
wuhao committed
328 329
              message.success(res.msg);
              reload();
wuhao's avatar
wuhao committed
330 331
            }
          });
wuhao's avatar
wuhao committed
332 333
        },
      }),
wuhao's avatar
wuhao committed
334
      extraparams.orderType == "1" &&
wuhao's avatar
wuhao committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      getPrem("sysDepartment_save", action, "修改", () => {
        doFetch({
          url: "/ngic-auth/sysUser/query/detail",
          params: { id: record.id },
        }).then((res) => {
          if (res.code == "0000") {
            let data = res?.data?.data ?? {};
            for (let i in defaultFields) {
              defaultFields[i].value = data[i];
              if (i == "parentId") {
                defaultFields[i].belinked = {
                  options: {
                    database: userSelect,
                    params: { id: record.id },
                  },
                };
              }
            }
            dispatch({
              type: "edit",
              fields: { ...defaultFields },
              curitem: data,
wuhao's avatar
wuhao committed
357
            });
wuhao's avatar
wuhao committed
358 359 360 361 362 363 364 365 366
          }
        });
      }),
      extraparams.orderType == "1" &&
      getPrem("sysDepartment_deleteById", action, "删除", null, {
        title: "确认删除该用户?",
        onConfirm: () => {
          run({
            url: "/ngic-auth/sysUser/deleteById",
wuhao's avatar
wuhao committed
367
            params: { id: record.id },
wuhao's avatar
wuhao committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
          });
        },
      }),
      extraparams.orderType == "2" &&
      record.status == 1 &&
      getPrem("sysDepartment_save", action, "审批", () => {
        doFetch({
          url: "/ngic-auth/sysAccountApply/queryById",
          params: { id: record.id },
        }).then((res) => {
          if (res.code == "0000") {
            let data = res?.data?.data ?? {};
            for (let i in defaultFields) {
              defaultFields[i].value = data[i];
              if (i == "parentId") {
                defaultFields[i].belinked = {
                  options: {
                    database: userSelect,
                    params: { id: record.id },
                  },
                };
wuhao's avatar
wuhao committed
389
              }
wuhao's avatar
wuhao committed
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
            }
            let newParams = {
              remark: {
                value: data.remark,
                type: "textarea",
                title: "备注",
                name: ["remark"],
                required: false,
                col: { span: 24 },
              },
              applyTime: {
                value: data.applyTime,
                type: "datepicker",
                title: "申请时间",
                name: ["applyTime"],
                required: false,
                disabled: true,
              },
              auditResult: {
                value: null,
                type: "select",
                title: "审批结果",
                name: ["auditResult"],
                required: true,
                options: [
                  {
                    label: "通过",
                    value: 1,
                  },
                  {
                    label: "不通过",
                    value: 2,
                  },
                ],
                linked: true,
              },
              auditOpinion: {
                value: null,
                type: "textarea",
                title: "审批意见",
                name: ["auditOpinion"],
                required: true,
                col: { span: 24 },
                belinked: {
                  hides: [
wuhao's avatar
wuhao committed
435
                    {
wuhao's avatar
wuhao committed
436 437 438
                      name: "auditResult",
                      equalvalue: [1, null],
                      required: true,
wuhao's avatar
wuhao committed
439 440 441
                    },
                  ],
                },
wuhao's avatar
wuhao committed
442 443 444 445 446 447 448 449 450 451
              },
            };
            dispatch({
              type: "audit",
              fields: { ...defaultFields, ...newParams },
              curitem: data,
            });
          }
        });
      }),
wuhao's avatar
wuhao committed
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
    ];
  }
  let saveData = (values, fn) => {
    let newfields = JSON.parse(JSON.stringify(values));
    //新增&修改
    let difrid = iftype.val != "add" ? { id: curitem.id } : {};
    run({
      url:
        iftype.val == "audit"
          ? "/ngic-auth/sysAccountApply/audit"
          : "/ngic-auth/sysUser/save",
      params: { ...newfields, ...difrid },
    });
  };
  let extrarender = [
    <Button
      disabled={!getPrem("sysDepartment_save", "ifs")}
      type="primary"
      onClick={() => {
        for (let i in defaultFields) {
          defaultFields[i].value = i.indexOf("List") != -1 ? [] : "";
        }
        dispatch({ type: "add", fields: defaultFields });
      }}
    >
      新增
    </Button>,
  ];
  return (
    <div>
      {extraparams.orderType == "1" && (
        <AutoTable
          pagetitle={props.route.name} //页面标题
          pageextra={extrarender} //页面操作 新增or批量删除
          columns={columns}
          actionRef={actionRef}
          onRef={(node) => (ChildRef = node)}
          tabList={tabList}
          activeTabKey={extraparams.orderType}
          onTabChange={(key) => {
            dispatch({ type: "start", tabKey: key });
          }}
          path={"/ngic-auth/sysUser/query/page"}
        ></AutoTable>
      )}
      {extraparams.orderType == "2" && (
        <AutoTable
          pagetitle={props.route.name} //页面标题
          columns={columnsc}
          actionRef={actionRef}
          onRef={(node) => (ChildRef = node)}
          tabList={tabList}
          activeTabKey={extraparams.orderType}
          onTabChange={(key) => {
            dispatch({ type: "start", tabKey: key });
          }}
          path={"/ngic-auth/sysAccountApply/queryList"}
        ></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}
        width={"60%"}
      ></DrawInitForm>
      <Details
        title="用户详情"
        visible={visible}
        onClose={() => dispatch({ type: "close" })}
        footer={false}
        destroyOnClose={true}
        width={"100%"}
        {...detail}
      ></Details>
    </div>
  );
};
export default Staff;