index.jsx 9.13 KB
Newer Older
TZW's avatar
TZW committed
1 2 3 4
/* 角色管理
 * @Author: Li Hanlin
 * @Date: 2022-11-09 14:44:44
 * @Last Modified by: Li Hanlin
TZW's avatar
TZW committed
5
 * @Last Modified time: 2022-12-30 14:10:29
TZW's avatar
TZW committed
6 7
 */

wuhao's avatar
wuhao committed
8 9 10 11 12 13
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
14 15 16 17
import { doFetch } from '@/utils/doFetch';
import { message } from 'antd';
import { Drawer } from 'antd';
import PermissionTree from '@/components/PermissionTree/Permissiontree';
wuhao's avatar
wuhao committed
18

TZW's avatar
TZW committed
19
function Role(props) {
wuhao's avatar
wuhao committed
20 21
  const actionRef = useRef(),
    formRef = useRef();
TZW's avatar
TZW committed
22
  const [drawer, setDrawer] = useState({
wuhao's avatar
wuhao committed
23
    visible: false,
TZW's avatar
TZW committed
24
    authorityOpen: false,
wuhao's avatar
wuhao committed
25 26
  });

TZW's avatar
TZW committed
27
  const urlParams = {
TZW's avatar
TZW committed
28 29 30
    save: '/auth/sysRole/save',
    remove: '/auth/sysRole/deleteById',
    list: '/auth/sysRole/queryList',
TZW's avatar
TZW committed
31
    detail: '/auth/sysFactory/getById',
TZW's avatar
TZW committed
32
    person: '/auth/sysUser/queryPageByRoleId',
TZW's avatar
TZW committed
33 34
  };

TZW's avatar
TZW committed
35
  const detailPerson = (text, row, _, action) => {
wuhao's avatar
wuhao committed
36 37 38 39
    return (
      <PremButton
        btn={{
          size: 'small',
TZW's avatar
TZW committed
40
          // type: 'link',
wuhao's avatar
wuhao committed
41
          onClick: () => {
TZW's avatar
TZW committed
42
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
43 44 45 46
              ...s,
              visible: true,
              item: row,
              title: '详情',
TZW's avatar
TZW committed
47 48 49
              val: 'only',
              type: 'person',
              title: row.roleName + ' - 人员列表',
wuhao's avatar
wuhao committed
50 51 52 53
            }));
          },
        }}
      >
TZW's avatar
TZW committed
54
        查看人员
wuhao's avatar
wuhao committed
55 56 57 58 59 60 61 62 63 64
      </PremButton>
    );
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
TZW's avatar
TZW committed
65
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
66 67 68 69 70
              ...s,
              visible: true,
              item: row,
              title: '编辑',
              val: 'edit',
TZW's avatar
TZW committed
71 72 73 74 75 76 77 78 79 80
              onFinish: async (vals) => {
                let params = {
                  ...vals,
                  id: row.id,
                };
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
TZW's avatar
TZW committed
81
                  message.success('编辑成功!');
TZW's avatar
TZW committed
82 83 84 85 86 87 88
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
wuhao's avatar
wuhao committed
89 90 91 92 93 94 95 96 97 98 99 100 101
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
TZW's avatar
TZW committed
102
          title: '是否删除该工厂?',
wuhao's avatar
wuhao committed
103 104
          okText: '确认',
          cancelText: '取消',
TZW's avatar
TZW committed
105 106 107 108 109 110 111 112 113 114
          onConfirm: async () => {
            let res = await doFetch({ url: urlParams.remove, params: { id: row.id } });
            if (res.code === '0000') {
              message.success('删除成功!');
              setDrawer((s) => ({
                ...s,
                visible: false,
              }));
              actionRef.current.reload();
            }
wuhao's avatar
wuhao committed
115 116 117 118 119 120 121 122 123 124 125 126
          },
        }}
        btn={{
          size: 'small',
          type: 'danger',
        }}
      >
        删除
      </PremButton>
    );
  };

TZW's avatar
TZW committed
127 128 129 130 131 132 133
  // 权限配置
  const authority = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
TZW's avatar
TZW committed
134
            // const { data } = doFetch({
TZW's avatar
TZW committed
135 136 137
            //   url: '/sysRolePermission/queryAll',
            //   params: { roleId: row.id },
            // });
TZW's avatar
TZW committed
138 139
            // //console.log(data);
            //console.log(row);
TZW's avatar
TZW committed
140
            setDrawer((s) => ({
TZW's avatar
TZW committed
141 142 143 144
              ...s,
              authorityOpen: true,
              item: row,
              onFinish: async (vals) => {
TZW's avatar
TZW committed
145
                //console.log(vals);
TZW's avatar
TZW committed
146
                let res = await doFetch({
TZW's avatar
TZW committed
147
                  url: '/auth/sysRolePermission/save',
TZW's avatar
TZW committed
148 149
                  params: { ...vals },
                });
TZW's avatar
TZW committed
150
                //console.log(res);
TZW's avatar
TZW committed
151 152
                if (res.code === '0000') {
                  message.success('修改成功!');
TZW's avatar
TZW committed
153
                  setDrawer((s) => ({
TZW's avatar
TZW committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
            }));
          },
        }}
      >
        权限配置
      </PremButton>
    );
  };

TZW's avatar
TZW committed
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
  const PersonTable = () => {
    return (
      <>
        <AutoTable
          pagetitle={false}
          columns={[
            {
              title: '用户名',
              dataIndex: 'username',
              key: 'username',
              formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
            },
            {
              title: '姓名',
              dataIndex: 'fullName',
              key: 'fullName',
              formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
            },
            {
              title: '公司名称',
              dataIndex: 'organizationName',
              key: 'organizationId',
              valueType: 'select',
              options: { path: '/auth/sysDepartment/query/organization/selectbox', params: {} },
              formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
            },
            {
              title: '部门名称',
              dataIndex: 'departmentName',
TZW's avatar
TZW committed
198
              valueType: 'treeSelect',
TZW's avatar
TZW committed
199 200
              key: 'departmentId',
              options: {
TZW's avatar
TZW committed
201
                path: '/auth/sysDepartment/query/children/tree',
TZW's avatar
TZW committed
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
                linkParams: {
                  organizationId: 'parentId',
                },
              },
              formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
            },
            {
              title: '工厂名称',
              dataIndex: 'factoryNames',
              key: 'factoryNames',
              formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
            },
            {
              title: '负责车间',
              dataIndex: 'shopNames',
              key: 'shopNames',
              formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
            },
          ]}
          path={urlParams.person}
          params={{ roleId: drawer?.item?.id }}
          actionRef={actionRef}
          resizeable={false}
        />
      </>
    );
  };
TZW's avatar
TZW committed
229 230 231
  // 数据分权
  const distribution = (text, row, _, action) => {};

wuhao's avatar
wuhao committed
232
  const columns = useMemo(() => {
TZW's avatar
TZW committed
233
    let defcolumn = getcolumns(setDrawer);
wuhao's avatar
wuhao committed
234 235 236 237
    return defcolumn.concat({
      title: '操作',
      valueType: 'option',
      width: 150,
TZW's avatar
TZW committed
238
      render: (text, row, _, action) => [
TZW's avatar
TZW committed
239
        authority(text, row, _, action),
TZW's avatar
TZW committed
240 241 242 243
        edit(text, row, _, action),
        remove(text, row, _, action),
        detailPerson(text, row, _, action),
      ],
wuhao's avatar
wuhao committed
244 245 246 247 248 249
    });
  }, []);

  return (
    <div style={{ position: 'relative' }}>
      <AutoTable
TZW's avatar
TZW committed
250
        pagetitle={<h3 className="page-title">角色管理</h3>}
wuhao's avatar
wuhao committed
251
        columns={columns}
TZW's avatar
TZW committed
252
        path={urlParams.list}
wuhao's avatar
wuhao committed
253 254
        actionRef={actionRef}
        pageextra={'add'}
TZW's avatar
TZW committed
255
        resizeable={false}
wuhao's avatar
wuhao committed
256 257 258
        addconfig={{
          // access: 'sysDepartment_save',
          btn: {
TZW's avatar
TZW committed
259
            type: 'primary',
wuhao's avatar
wuhao committed
260 261
            disabled: false,
            onClick: () => {
TZW's avatar
TZW committed
262
              setDrawer((s) => ({
wuhao's avatar
wuhao committed
263 264 265
                ...s,
                visible: true,
                item: null,
TZW's avatar
TZW committed
266
                detailpath: null,
wuhao's avatar
wuhao committed
267 268
                title: '新增',
                val: 'add',
TZW's avatar
TZW committed
269
                onFinish: async (vals) => {
TZW's avatar
TZW committed
270
                  //console.log(1);
TZW's avatar
TZW committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
                  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
287 288 289 290 291 292 293 294
              }));
            },
          },
        }}
      />

      <DrawerPro
        fields={columns}
TZW's avatar
TZW committed
295 296 297
        // detailpath={urlParams.detail}
        // params={{ id: drawer?.item?.id }}
        defaultFormValue={drawer?.item ?? {}}
wuhao's avatar
wuhao committed
298 299 300
        formRef={formRef}
        placement="right"
        onClose={() => {
TZW's avatar
TZW committed
301
          setDrawer((s) => ({
wuhao's avatar
wuhao committed
302 303 304 305 306
            ...s,
            visible: false,
          }));
        }}
        {...drawer}
TZW's avatar
TZW committed
307 308 309
      >
        {drawer.type == 'person' ? <PersonTable /> : null}
      </DrawerPro>
TZW's avatar
TZW committed
310 311 312 313

      <Drawer
        title="角色权限"
        onClose={() => {
TZW's avatar
TZW committed
314
          setDrawer((s) => ({
TZW's avatar
TZW committed
315 316 317 318 319
            ...s,
            authorityOpen: false,
          }));
        }}
        open={drawer.authorityOpen}
TZW's avatar
TZW committed
320
        destroyOnClose
TZW's avatar
TZW committed
321 322 323 324
      >
        <PermissionTree
          id={drawer?.item?.id}
          close={() => {
TZW's avatar
TZW committed
325
            setDrawer((s) => ({
TZW's avatar
TZW committed
326 327 328 329 330 331 332 333 334
              ...s,
              authorityOpen: false,
              item: null,
            }));
            actionRef.current.reload();
          }}
          treeType="auth"
        />
      </Drawer>
wuhao's avatar
wuhao committed
335 336 337 338
    </div>
  );
}

TZW's avatar
TZW committed
339
export default Role;