index.jsx 4.78 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6
import * as React from 'react';
import { useState, useMemo, useRef } from 'react';
import DrawerPro from '@/components/DrawerPro';
import AutoTable from '@/components/AutoTable';
import PremButton from '@/components/PremButton';
import getcolumns from './columns';
TZW's avatar
TZW committed
7 8
import { doFetch } from '@/utils/doFetch';
import { message } from 'antd';
wuhao's avatar
wuhao committed
9 10 11 12

function Factory(props) {
  const actionRef = useRef(),
    formRef = useRef();
TZW's avatar
TZW committed
13
  const [drawer, setDrawer] = useState({
wuhao's avatar
wuhao committed
14 15 16
    visible: false,
  });

TZW's avatar
TZW committed
17 18 19 20 21 22 23
  const urlParams = {
    save: '/auth/sysFactory/saveOrUpdate',
    remove: '/auth/sysFactory/delete',
    list: '/auth/sysFactory/queryPage',
    detail: '/auth/sysFactory/getById',
  };

wuhao's avatar
wuhao committed
24 25 26 27 28 29 30
  const detail = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          type: 'link',
          onClick: () => {
TZW's avatar
TZW committed
31
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
              ...s,
              visible: true,
              item: row,
              title: '详情',
              val: 'detail',
              title: row.userName + '的详细信息',
            }));
          },
        }}
      >
        详情
      </PremButton>
    );
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
TZW's avatar
TZW committed
53
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
54 55 56 57 58
              ...s,
              visible: true,
              item: row,
              title: '编辑',
              val: 'edit',
TZW's avatar
TZW committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
              onFinish: async (vals) => {
                console.log(1);
                let params = {
                  ...vals,
                  id: row.id,
                };
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
                  message.success('新增成功!');
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
wuhao's avatar
wuhao committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
          title: '是否删除该用户?',
          okText: '确认',
          cancelText: '取消',
TZW's avatar
TZW committed
94 95 96 97 98 99 100 101 102 103
          onConfirm: async () => {
            let res = await doFetch({ url: urlParams.remove, params: { id: row.id } });
            if (res.code === '0000') {
              message.success('删除成功!');
              setDraw((s) => ({
                ...s,
                visible: false,
              }));
              actionRef.current.reload();
            }
wuhao's avatar
wuhao committed
104 105 106 107 108 109 110 111 112 113 114 115 116
          },
        }}
        btn={{
          size: 'small',
          type: 'danger',
        }}
      >
        删除
      </PremButton>
    );
  };

  const columns = useMemo(() => {
TZW's avatar
TZW committed
117
    let defcolumn = getcolumns(setDrawer);
wuhao's avatar
wuhao committed
118 119 120 121
    return defcolumn.concat({
      title: '操作',
      valueType: 'option',
      width: 150,
TZW's avatar
TZW committed
122
      render: (text, row, _, action) => [edit(text, row, _, action), remove(text, row, _, action)],
wuhao's avatar
wuhao committed
123 124 125 126 127 128
    });
  }, []);

  return (
    <div style={{ position: 'relative' }}>
      <AutoTable
TZW's avatar
TZW committed
129
        pagetitle={<h3 className="page-title">工厂管理</h3>}
wuhao's avatar
wuhao committed
130
        columns={columns}
TZW's avatar
TZW committed
131
        path={urlParams.list}
wuhao's avatar
wuhao committed
132 133 134 135 136 137 138 139
        actionRef={actionRef}
        pageextra={'add'}
        resizeable={false}
        addconfig={{
          // access: 'sysDepartment_save',
          btn: {
            disabled: false,
            onClick: () => {
TZW's avatar
TZW committed
140
              setDrawer((s) => ({
wuhao's avatar
wuhao committed
141 142 143 144 145
                ...s,
                visible: true,
                item: null,
                title: '新增',
                val: 'add',
TZW's avatar
TZW committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
                onFinish: async (vals) => {
                  console.log(1);
                  let params = {
                    ...vals,
                  };
                  let res = await doFetch({
                    url: urlParams.save,
                    params,
                  });
                  if (res.code === '0000') {
                    message.success('新增成功!');
                    setDrawer((s) => ({
                      ...s,
                      visible: false,
                    }));
                    actionRef.current.reload();
                  }
                },
wuhao's avatar
wuhao committed
164 165 166 167 168 169 170 171
              }));
            },
          },
        }}
      />

      <DrawerPro
        fields={columns}
TZW's avatar
TZW committed
172 173 174
        // detailpath={urlParams.detail}
        // params={{ id: drawer?.item?.id }}
        defaultFormValue={drawer?.item ?? {}}
wuhao's avatar
wuhao committed
175 176 177
        formRef={formRef}
        placement="right"
        onClose={() => {
TZW's avatar
TZW committed
178
          setDrawer((s) => ({
wuhao's avatar
wuhao committed
179 180 181 182 183 184 185 186 187 188 189
            ...s,
            visible: false,
          }));
        }}
        {...drawer}
      />
    </div>
  );
}

export default Factory;