AddModal.jsx 7.6 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1
/* eslint-disable no-unused-expressions */
Maofei94's avatar
Maofei94 committed
2
import React, { useState, useEffect } from 'react';
皮倩雯's avatar
皮倩雯 committed
3
import { Form, Input, notification, Select, Radio, Tooltip } from 'antd';
4
import SiteModal from '@/components/Modal/SiteModa';
皮倩雯's avatar
皮倩雯 committed
5 6
import { addRole, GetRoleGroup, getWebConfigTypes } from '@/services/RoleManage/api';
import { InfoCircleOutlined } from '@ant-design/icons';
7
const { Item } = Form;
Maofei94's avatar
Maofei94 committed
8
const { Option } = Select;
9 10 11
const AddModal = props => {
  const [form] = Form.useForm();
  const [formLayout, setFormLayout] = useState('horizontal');
Maofei94's avatar
Maofei94 committed
12
  const [groupList, setGroupList] = useState([]);
13
  const [loading, setLoading] = useState(false);
Maofei94's avatar
Maofei94 committed
14 15
  const { confirmModal, itemObj, siteList } = props;
  const [typeList, setTypeList] = useState([]); // 应用类别
皮倩雯's avatar
皮倩雯 committed
16
  const [value, setValue] = useState(2);
Maofei94's avatar
Maofei94 committed
17 18 19 20 21
  useEffect(() => {
    itemObj.groupflag &&
      form.setFieldsValue({
        group: itemObj.groupflag,
      });
Maofei94's avatar
Maofei94 committed
22 23 24 25
    itemObj &&
      form.setFieldsValue({
        subSystemValue: itemObj.visibleValue,
      });
Maofei94's avatar
Maofei94 committed
26 27 28 29
    return () => {
      form.resetFields();
    };
  }, [itemObj]);
Maofei94's avatar
Maofei94 committed
30 31 32 33 34 35 36
  useEffect(() => {
    getWebConfigTypes({}).then(res => {
      if (res.code === 0) {
        const { data } = res;
        const arr = Object.entries(data);
        const list = [];
        arr.map((item, index) => {
Maofei94's avatar
Maofei94 committed
37 38 39 40 41 42 43
          console.log(item, 'item');
          if (item[1] === '小程序') {
            item[1] = '移动应用';
          }
          if (item[1] === '手持系统') {
            return;
          }
Maofei94's avatar
Maofei94 committed
44 45 46 47 48 49 50 51 52 53 54 55 56
          let obj = {
            key: index,
            value: item[0],
            label: item[1],
          };
          list.push(obj);
        });
        setTypeList(list);
        console.log(list);
      }
      console.log(res, 'resss');
    });
  }, [itemObj]);
57 58 59 60
  const onSubmit = () => {
    form
      .validateFields()
      .then(res => {
Maofei94's avatar
Maofei94 committed
61
        console.log(form.getFieldsValue());
62
        setLoading(true);
皮倩雯's avatar
皮倩雯 committed
63 64 65 66 67 68 69
        console.log(res.BuiltInRole);
        if (!res.BuiltInRole || res.BuiltInRole == 2) {
          res.BuiltInRole = false;
        } else if (res.BuiltInRole == 1) {
          res.BuiltInRole = true;
        }
        console.log(res.BuiltInRole);
Maofei94's avatar
Maofei94 committed
70 71
        addRole({
          roleName: res.roleName,
72
          description: res.description,
Maofei94's avatar
Maofei94 committed
73
          group: res.group,
Maofei94's avatar
Maofei94 committed
74
          subSystemValue: res.subSystemValue || '',
皮倩雯's avatar
皮倩雯 committed
75
          BuiltInRole: res.BuiltInRole,
76 77 78
        })
          .then(res => {
            setLoading(false);
皮倩雯's avatar
皮倩雯 committed
79
            if (res.msg === '') {
Maofei94's avatar
Maofei94 committed
80
              let id = res.roleID;
81 82
              form.resetFields();
              notification.success({
Maofei94's avatar
Maofei94 committed
83
                message: '提示',
84 85 86
                duration: 3,
                description: '新增成功',
              });
Maofei94's avatar
Maofei94 committed
87
              confirmModal(id);
88 89 90
            } else {
              notification.error({
                message: '提示',
Maofei94's avatar
Maofei94 committed
91
                duration: 15,
92
                description: res.msg,
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
              });
            }
          })
          .catch(err => {
            setLoading(false);
            notification.error({
              message: '提示',
              duration: 3,
              description: err,
            });
          });
      })
      .catch(err => {
        console.error(err);
      });
  };
Maofei94's avatar
Maofei94 committed
109 110 111 112 113 114 115 116 117 118
  const onChange = value => {
    console.log(value);
    const { length } = value;
    form.setFieldsValue({
      group: value[length - 1],
    });
  };
  // 获取分组列表
  const selectFocus = e => {
    setGroupList([]);
Maofei94's avatar
Maofei94 committed
119
    console.log(form.getFieldsValue().subSystemValue);
120
    GetRoleGroup({
Maofei94's avatar
Maofei94 committed
121 122 123 124
      // subSystemValue: itemObj.subSystemValue || itemObj.visibleValue || '',
      // subSystemName: itemObj.subSystemValue || itemObj.visibleValue || '',
      subSystemValue: form.getFieldsValue().subSystemValue || '',
      subSystemName: form.getFieldsValue().subSystemValue || '',
Maofei94's avatar
Maofei94 committed
125 126 127
    })
      .then(res => {
        if (res) {
128
          setGroupList(res.data);
Maofei94's avatar
Maofei94 committed
129 130 131 132 133 134 135 136 137 138 139 140 141
        } else {
          notification.error({
            message: '提示',
            duration: 15,
            description: res.message,
          });
          setGroupList([]);
        }
      })
      .catch(err => {
        console.error(err);
      });
  };
Maofei94's avatar
Maofei94 committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  const handleSelect = () => {
    // getWebConfigTypes({}).then(res => {
    //   if (res.code === 0) {
    //     const { data } = res;
    //     const arr = Object.entries(data);
    //     const list = [];
    //     arr.map((item, index) => {
    //       let obj = {
    //         key: index,
    //         value: item[0],
    //         label: item[1],
    //       };
    //       list.push(obj);
    //     });
    //     setTypeList(list);
    //     console.log(list);
    //   }
    //   console.log(res, 'resss');
    // });
  };
  const handleChange = e => {
    console.log(e);
  };
165

皮倩雯's avatar
皮倩雯 committed
166 167 168 169
  const onChangeRadio = e => {
    console.log(e.target.value);
    setValue(e.target.value);
  };
170 171 172 173 174 175
  const onFinish = value => {};
  return (
    <SiteModal
      {...props}
      title="新增角色"
      bodyStyle={{ width: '100%', minHeight: '100px' }}
Maofei94's avatar
Maofei94 committed
176
      style={{ top: 100 }}
177 178 179
      width="600px"
      destroyOnClose
      cancelText="取消"
Maofei94's avatar
Maofei94 committed
180
      okText="确认"
181 182 183
      onOk={() => onSubmit()}
      confirmLoading={loading}
    >
Maofei94's avatar
Maofei94 committed
184 185 186 187 188
      <Form
        form={form}
        layout={formLayout}
        onFinish={onFinish}
        labelCol={{ span: 4 }}
Maofei94's avatar
Maofei94 committed
189 190 191
        // initialValues={{
        //   subSystemValue: itemObj.subSystemValue || itemObj.visibleValue,
        // }}
Maofei94's avatar
Maofei94 committed
192
      >
193 194
        <Item
          label="角色名称"
Maofei94's avatar
Maofei94 committed
195
          name="roleName"
196 197 198 199 200 201 202
          rules={[
            {
              required: true,
              message: '请输入角色名称',
            },
          ]}
        >
203
          <Input placeholder="请输入角色名称" maxLength="20" />
204
        </Item>
Maofei94's avatar
Maofei94 committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        <Item
          label="角色类别"
          name="subSystemValue"
          rules={[
            {
              required: true,
              message: '请选择角色类别',
            },
          ]}
        >
          {/* {itemObj.subSystemValue || itemObj.visibleValue} */}
          <Select
            placeholder="请选择角色类别"
            onFocus={() => {
              handleSelect();
            }}
            onChange={e => {
              handleChange(e);
            }}
Maofei94's avatar
Maofei94 committed
224
            disabled
Maofei94's avatar
Maofei94 committed
225 226 227 228 229 230 231
          >
            {typeList.map(item => (
              <Option value={item.value} key={item.key}>
                {item.label}
              </Option>
            ))}
          </Select>
Maofei94's avatar
Maofei94 committed
232
        </Item>
皮倩雯's avatar
皮倩雯 committed
233 234 235 236 237 238 239 240 241 242 243 244 245
        <Item
          label={
            <div>
              <Tooltip title="内置角色不能配置菜单权限">
                <InfoCircleOutlined style={{ color: 'rgb(24, 144, 255)', marginRight: '5px' }} />
              </Tooltip>
              <span>内置角色</span>
            </div>
          }
          name="BuiltInRole"
        >
          <Radio.Group onChange={onChangeRadio} value={value} defaultValue={value}>
            <Radio value={2}></Radio>
246
            <Radio value={1}></Radio>
皮倩雯's avatar
皮倩雯 committed
247 248
          </Radio.Group>
        </Item>
Maofei94's avatar
Maofei94 committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        <Item label="角色分组" name="group">
          <Select
            mode="tags"
            placeholder="请选择分组"
            onFocus={() => {
              selectFocus();
            }}
            onChange={e => {
              onChange(e);
            }}
          >
            {groupList.map((item, index) => (
              <Option value={item.value} key={index}>
                {item.text}
              </Option>
            ))}
          </Select>
        </Item>
267
        <Item label="角色描述" name="description">
268
          <Input placeholder="请输入角色描述" maxLength="100" />
269 270 271 272 273 274 275
        </Item>
      </Form>
    </SiteModal>
  );
};

export default AddModal;