role.data.ts 4.88 KB
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { setRoleStatus } from '/@/api/system/system';
import { useMessage } from '/@/hooks/web/useMessage';
import { getRoleListByPage } from '/@/api/system/system';
import { USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache } from '/@/utils/auth';

export const columns: BasicColumn[] = [
  {
    title: '角色名称',
    dataIndex: 'name',
    width: 200,
  },
  {
    title: '角色Code',
    dataIndex: 'code',
    width: 200,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        async onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          const data = await getRoleListByPage({
            page: 1,
            pageSize: 100,
            // roleType: 'CUSTOMER_USER',
          });
          const userInfo = (await getAuthCache(USER_INFO_KEY)) as unknown as any;
          const findById = data.items.find((f) => {
            if (f.id === userInfo.plainRoles[0].roleId) {
              return f.id;
            }
          });
          if (record.id == findById?.id) return createMessage.error(`该租户下面有此客户,无法禁用`);
          record.pendingStatus = true;
          setRoleStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改角色状态`);
            })
            .catch(() => {
              createMessage.error('修改角色状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },

  {
    title: '备注',
    dataIndex: 'remark',
    width: 240,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'roleName',
    label: '角色名称',
    component: 'Input',
    colProps: { span: 6 },
    componentProps: {
      maxLength: 255,
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '停用', value: 0 },
      ],
    },
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'name',
    label: '角色名称',
    required: true,
    component: 'Input',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入角色名称',
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '停用', value: 0 },
      ],
    },
    // componentProps({ formModel, formActionType }) {
    //   const initStatus = formModel.status;
    //   const { createMessage } = useMessage();
    //   let initBoolean = false;
    //   const { setFieldsValue } = formActionType;
    //   if (formModel.name !== undefined) {
    //     initBoolean = true;
    //   } else if (formModel.name == undefined) {
    //     initBoolean = false;
    //   }
    //   return {
    //     options: [
    //       { label: '启用', value: 1 },
    //       { label: '停用', value: 0 },
    //     ],
    //     async onChange(e) {
    //       // const data = await getAllRoleList();
    //       // const findById = data.find((f) => f.id);
    //       if (!initBoolean) {
    //         setFieldsValue({
    //           status: initStatus,
    //         });
    //       } else {
    //         if (e == 0 && formModel.name !== undefined) {
    //           createMessage.error(`该租户下面有此用户,无法停用`);
    //           setFieldsValue({
    //             status: Number(1),
    //           });
    //         } else {
    //           setFieldsValue({
    //             status: initStatus,
    //           });
    //         }
    //       }
    //       // if (e == 0) {
    //       //   if (findById?.id) return createMessage.error(`该租户下面有此用户,无法禁用`);
    //       //   setFieldsValue({
    //       //     status: 1,
    //       //   });
    //       // }
    //     },
    //   };
    // },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      maxLength: 255,
      placeholder: '请输入备注',
    },
  },
  {
    label: '',
    field: 'menu',
    slot: 'menu',
    component: 'Input',
  },
];