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

  const onFinish = value => {};
  return (
    <SiteModal
      {...props}
      title="新增角色"
      bodyStyle={{ width: '100%', minHeight: '100px' }}
Maofei94's avatar
Maofei94 committed
169
      style={{ top: 100 }}
170 171 172
      width="600px"
      destroyOnClose
      cancelText="取消"
Maofei94's avatar
Maofei94 committed
173
      okText="确认"
174 175 176
      onOk={() => onSubmit()}
      confirmLoading={loading}
    >
Maofei94's avatar
Maofei94 committed
177 178 179 180 181
      <Form
        form={form}
        layout={formLayout}
        onFinish={onFinish}
        labelCol={{ span: 4 }}
Maofei94's avatar
Maofei94 committed
182 183 184
        // initialValues={{
        //   subSystemValue: itemObj.subSystemValue || itemObj.visibleValue,
        // }}
Maofei94's avatar
Maofei94 committed
185
      >
186 187
        <Item
          label="角色名称"
Maofei94's avatar
Maofei94 committed
188
          name="roleName"
189 190 191 192 193 194 195 196 197
          rules={[
            {
              required: true,
              message: '请输入角色名称',
            },
          ]}
        >
          <Input placeholder="请输入角色名称" />
        </Item>
Maofei94's avatar
Maofei94 committed
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
        <Item
          label="角色类别"
          name="subSystemValue"
          rules={[
            {
              required: true,
              message: '请选择角色类别',
            },
          ]}
        >
          {/* {itemObj.subSystemValue || itemObj.visibleValue} */}
          <Select
            placeholder="请选择角色类别"
            onFocus={() => {
              handleSelect();
            }}
            onChange={e => {
              handleChange(e);
            }}
          >
            {typeList.map(item => (
              <Option value={item.value} key={item.key}>
                {item.label}
              </Option>
            ))}
          </Select>
Maofei94's avatar
Maofei94 committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
        </Item>
        <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>
243 244 245 246 247 248 249 250 251
        <Item label="角色描述" name="description">
          <Input placeholder="请输入角色描述" />
        </Item>
      </Form>
    </SiteModal>
  );
};

export default AddModal;