orgAdd.js 20.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
import React from 'react';
import './orgIndex.less'
import axios from '@src/axios/index'
import Utils from '@src/utils/utils'
import { NavLink } from 'react-router-dom'
import UserManageBar from '@components/Common/WhiteBar/index'
import OrgOptionsForm from '@components/Common/OrgOptions/codeIndex'
import { API_SYSORORG_MANAGE } from '@src/Api'
import { Form, Button, Input, Icon, message, Modal, Radio,Row, Col, Spin } from 'antd'

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const TextArea = Input.TextArea


class OrgAdd extends React.Component {

  state = {
    setDisabled:false,//设置机构code是否禁止输入
    orgVisible: false,//机构弹窗显示
    orgOptionsArr: [],//机构弹窗页面树形数据
    checkedObj: {},//选中的机构信息
    orgInfo:{},//根据id获取机构信息
    titleParams: '新增机构',
    showOrg:{display : 'block'},
    hideOrg:{display : 'none'},
    spinStatus: false, // 是否显示加载中
  }
   componentWillMount() {
    document.getElementById('root').scrollIntoView(true);//为ture返回顶部,false为底部
  }
  async componentDidMount() {
    //获取机构树状图
    await this.getOrgTreeList()
    //判断跳转
    this.getRouterParams()
  }
  //根据菜单ID查询机构信息
  getOrgById = () => {
    this.setState({
      spinStatus: true,
    })  
    return new Promise((resolve, reject) => {
      const { optTag,oid } = this.props.match.params
        if (!oid) {
          message.warn('请重新选择,或者刷新页面')
          this.props.history.push({
            pathname: '/home/orgManage/orgIndex',
          });
          return
        }else {
          axios.ajax({
            url: API_SYSORORG_MANAGE.getOrgById,
            data: {
              id:oid
            }
          }).then(res => {
            this.setState({
              spinStatus: false,
            })  
            if (res && Object.keys(res).length > 0) {
               //根据修改还是新增下级机构来渲染上级机构显示数据
               if (optTag === 'updateItem') {
                  this.props.form.setFieldsValue({ 'parentCode':res.parentCode })
                  if(res.parentCode==='0'){
                    this.props.form.setFieldsValue({ 'parentName': res.name  })
                    this.props.form.setFieldsValue({ 'upParentName': res.name  })
                  }else{
                    this.props.form.setFieldsValue({ 'parentName': res.parentName  })
                    this.props.form.setFieldsValue({ 'upParentName': res.parentName  })
                  }
                  this.setState({ setDisabled: true })
                }else if (optTag === 'addNextItem') {
                  this.props.form.setFieldsValue({ 'parentCode': res.code  })
                   this.props.form.setFieldsValue({ 'parentName': res.name  })
                   this.props.form.setFieldsValue({ 'upParentName': res.name  })
                }
              resolve(this.setState({
                orgInfo: res,
                checkedObj: {
                  key: res.parentCode,
                  val: res.name,
                },
                parentCode:res.code
              }))
            }
          })
        }
     })
  }
  //获取跳转过来的传参判断是新增、修改、新增下级机构
  getRouterParams = () => {
    const params = this.props.match.params
    if (!params || !params.optTag) {
      message.warn('请重新选择,或者刷新页面')
      this.props.history.push({
        pathname: '/home/orgManage/orgIndex',
      });
      return
    }

    if (params.optTag === 'addItem') {
      this.setState({
        titleParams: '新增机构'
      })
    } else if (params.optTag === 'updateItem') {

      this.setState({
        titleParams: '修改机构',
      })
      this.getOrgById()
    }else if (params.optTag === 'addNextItem') {
      this.getOrgById()
      this.setState({
        titleParams: '新增下级机构',
        showOrg:{display : 'none'},
        hideOrg:{display : 'block'},
      })
    }
  }
  //弹窗根据机构名称查询
  getOrgByName = (orgName) => {
    axios.ajax({
      url: API_SYSORORG_MANAGE.getOrgByName,
      data: {
        name: orgName
      }

    }).then(res => {
      if (res && res.length > 0) {
        this.setState({
          orgOptionsArr: res
        })
      } else {
        this.setState({
          orgOptionsArr: []
        })
      }
    })
  }
   // 机构清空
  onRef = (ref) => {
    this.child = ref;
  }
  //获取机构树形机构
  getOrgTreeList = (orgName) => {
    axios.ajax({
      url: API_SYSORORG_MANAGE.getOrgTreeList,

    }).then(res => {
      if (res && res.length > 0) {
        this.setState({
          orgOptionsArr: res
        })
      } else {
        this.setState({
          orgOptionsArr: []
        })
      }
    })
  }
  //获取弹窗选中项
  getCheckedObj = (obj) => {
    this.setState({
      checkedObj: obj
    })
  }


  //打开所属机构弹窗
  showOrgModal = () => {
    let orgInfo = Utils.trim(this.props.form.getFieldsValue());
    this.setState({
      orgVisible: true,
      checkedObj:  {
          key: orgInfo.parentCode,
          val: orgInfo.parentName
        }
    })

  }
  
  //机构增加 
  handleOperator = (e) => {
    e && e.preventDefault();
    let data = Utils.trim(this.props.form.getFieldsValue());
    const { optTag } = this.props.match.params
    const { orgInfo } = this.state
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        this.setState({
          spinStatus: true,
        })  

        axios.optAjax({
          url: optTag && optTag === 'updateItem' ? API_SYSORORG_MANAGE.updateOrg:API_SYSORORG_MANAGE.addOrg,
          data: {
            ...data,
            id: optTag && optTag === 'updateItem' ? orgInfo.id:""
          }
        }).then(res=>{
          if(res && res.code && res.code !== 0){
            this.setState({
              spinStatus: false,
            }) 
            return
          }
          message.success(optTag && optTag === 'updateItem' ? '修改成功' : '新增成功')
          this.props.history.push({
            pathname: '/home/orgManage/orgIndex',
          });
        })
      }
    });
  }
  //确定选中机构
  handleOrgOptions = () => {
    const checkedObj = this.state.checkedObj
    if (Object.keys(checkedObj).length === 0 || !checkedObj) {
      message.warn('请选择对应的机构')
      return
    } else {
      this.setState({
        checkedObj
      })
      this.props.form.setFieldsValue({ 'parentName': checkedObj.val })
      this.props.form.setFieldsValue({ 'upParentName': checkedObj.val  })
      this.props.form.setFieldsValue({ 'parentCode': checkedObj.key })
      this.setState({
        orgVisible: false
      })
    }
  }
  //验证传真号码
  validFax = (rule, value, callback) => {
    var reg = /^(\s*||\s*(\d{5})||\s*(\d{3,4})?(-)?\d{5,8}\s*)$/;
    if (!value || value === undefined || value === null) {
      callback()
    }
    let num = value ? Utils.trim(value):value
    if (reg.test(num) === false) {
      callback('请输入正确的传真号码')
      return false;
    }
    callback()
  }
  //验证邮编
  validzipCode = (rule, value, callback) => {
    var reg = /^((\s)*[0-9]\d{5}(\s)*||(\s)*)$/;
    if (!value || value === undefined || value === null ) {
      callback()
    }
    if (reg.test(value) === false) {
      callback('请输入正确的邮编,邮编长度为6位')
      return false;
    }else if(Utils.trim(value).length>6){
      callback('请输入正确的邮编,邮编长度为6位')
      return false;
    }
    callback()
  }

  //电话号码
  validmobile = (rule, value, callback) => {
    var reg =  /^(\s*||\s*(\d{5})||\s*(\d{3,4})?(-)?\d{5,8}\s*)$/;
    if (!value || value === undefined || value === null) {
      callback()
    }
    if (reg.test(value) === false) {
      callback('请输入正确的电话号码')
      return false;
    }
    
    callback()
  }
  //手机号
  validphone = (rule, value, callback) => {
    var reg = /^(\s*||\s*(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[57])[0-9]{8}\s*)$/;
    if (!value || value === undefined || value === null) {
      callback()
    }
    if (reg.test(value) === false) {
      callback('请输入正确的手机号码')
      return false;
    }
    callback()
  }
  //城市验证
  validcity = (rule, value, callback) => {
    var reg = /^(\s*||\s*([\u4e00-\u9fa5A-Za-z])+\s*)$/;
    if (!value || value === undefined || value === null) {
      callback()
    }
    if(value){
      if (reg.test(value) === false || value.length>16) {
        callback('城市名为中英文,最长为16位字符')
        return false;
      }
    }
    
    callback()
  }

  render() {
     const { getFieldDecorator } = this.props.form;
     const { optTag } = this.props.match.params
     const { orgInfo } = this.state
     const { setDisabled} = this.state

     const formItemLayout = {
      labelCol: {
        md: { span: 12 },
        lg: { span: 6 },
      },
      wrapperCol: {
        md: { span: 24 },
        lg: { span: 18 },
      },
    };
    const formItemLayout2 = {
      labelCol: {
        md: { span: 12 },
        lg: { span: 3 },
      },
      wrapperCol: {
        md: { span: 24 },
        lg: { span: 21 },
      },
    };
    const formItemLayout3 = {
      labelCol: {
        md: { span: 12 },
        lg: { span: 14 },
      },
      wrapperCol: {
        md: { span: 24 },
        lg: { span: 10 },
      },
    };

    return (
      <div className="orgAdd">
        <Spin tip="加载中..." spinning = {this.state.spinStatus}>
          <div className="nav">
            <NavLink replace to={'/home/orgManage/orgIndex'} >机构管理</NavLink> > <span>{this.state.titleParams}</span>
          </div>
          <UserManageBar title="基本信息" />

          <div className="userInfo ">
            <Form layout="horizontal" className = "publicForm">

            <Row gutter={24}>
              <Col span="8" style={this.state.showOrg}>  
                <FormItem label="上级机构" {...formItemLayout} >
                  {
                    getFieldDecorator('parentName', {
                    rules: [
                        { required: true, message: '请选择上级机构', }
                      ]
                    })(
                      <Input  disabled addonAfter={<Icon className="chooseRole" onClick={() => { this.showOrgModal() }} type="search" />} placeholder="上级机构" />
                    )
                  }
                </FormItem>
              </Col>
              <Col span="8" style={this.state.hideOrg}>  
                <FormItem label="上级机构" {...formItemLayout} >
                  {
                    getFieldDecorator('upParentName', {
                    rules: [
                        { required: true, message: '请选择上级机构', }
                      ]
                    })(
                      <Input  disabled  placeholder="上级机构" />
                    )
                  }
                </FormItem>     
              </Col>
              <Col span="8" style={{ display: 'none' }}>  
                <FormItem>
                  {
                    getFieldDecorator('parentCode', {
                    })(
                      <Input type="number" placeholder="上级机构" min="0"/>
                    )
                  }
                </FormItem>      
              </Col>
              <Col span="8"> 
                <FormItem label={<span>管理机构和销售机构的标记</span>} {...formItemLayout3}>
                  {
                    getFieldDecorator('orgFlag', {
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.orgFlag:1,
                    })(
                      <RadioGroup>
                        <Radio value={1}>管理</Radio>
                        <Radio value={2}>销售</Radio>
                      </RadioGroup>
                    )
                  }
                </FormItem>
              </Col>
              <Col span="8">
                <FormItem label="机构code" {...formItemLayout}>
                    {
                      getFieldDecorator('code', {
                        initialValue: optTag && optTag === 'updateItem' ?   orgInfo.code+"":null,
                        rules: [
                        {required: true,  message: '请输入机构code' },
                        {pattern: /^(\s)*([0-9]{1,18})(\s)*$/, message: '机构code必须为正整数,最长为18位字符'},
                        ]
                      })(
                        <Input disabled={setDisabled} placeholder="机构code" min="1" className = "formInput" />
                      )
                    }
                  </FormItem>
              </Col>
            </Row>
            <Row gutter={24}>
              <Col span="8">  
                <FormItem label="机构名称" {...formItemLayout}>
                  {
                    getFieldDecorator('name', {
                    initialValue: optTag && optTag === 'updateItem' ?   orgInfo.name:null,
                    validateFirst: true,
                    rules: [
                        { required: true, message: '请输入机构名称', },
                        {pattern: /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/, message: '机构名称不能有特殊字符'},
                        { max: 64, whitespace: true, message: '机构名称最长为64位字符'  },
                      ]
                    })(
                      <Input placeholder="机构名称" />
                    )
                  }
                </FormItem>
              </Col>
              <Col span="8">  
                <FormItem label="机构级别"  {...formItemLayout}>
                  {
                    getFieldDecorator('orgGrade', {
                    initialValue: optTag && optTag === 'updateItem' ?   orgInfo.orgGrade+"":"",
                    rules: [
                      {required: true,  message: '请输入机构级别' },
                      {pattern: /^(\s)*([0-9]{1,4})(\s)*$/, message: '机构级别必须为正整数,最长为4位字符'},
                    ]
                    })(
                      <Input  placeholder="机构级别" min="1" />
                    )
                  }
                </FormItem>
              </Col>
            </Row>             
            <div className="detail"  >
              <UserManageBar title="其他信息" />
            </div>
            <Row gutter={24} style={{marginTop: 20}}>
              <Col span="8">  
                <FormItem label="城&emsp;&emsp;市" {...formItemLayout}>
                  {
                    getFieldDecorator('city',{
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.city:null,
                        rules: [{ validator: this.validcity },
                        ]
                    })(
                      <Input placeholder="城市" />
                    )
                  }
                </FormItem>
              </Col>
              <Col span="16">  
                <FormItem label="详细地址" {...formItemLayout2}>
                  {
                    getFieldDecorator('address',{
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.address:null,
                      rules: [
                        { max: 128, message: '详细地址最长为128位字符'  },
                        ]
                    })(
                    <Input placeholder="详细地址" />
                    )
                  }
                </FormItem>
              </Col>
            </Row>
            <Row gutter={24}>
              <Col span="8">  
                <FormItem label="电话号码"  {...formItemLayout}>
                  {
                    getFieldDecorator('mobile',{
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.mobile:'',
                      rules: [
                        { validator: this.validmobile }
                      ]
                    })(
                      <Input  placeholder="电话号码"  />
                    )
                  }
                </FormItem>
              </Col>
              <Col span="8">  
                <FormItem label="传真号码"  {...formItemLayout}>
                              {
                    getFieldDecorator('fax',{
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.fax:null,
                      rules: [{
                        validator: this.validFax
                      }]
                    })(
                      <Input  placeholder="传真号码"  />
                    )
                  }
                </FormItem>
              </Col>
              <Col span="8">  
                <FormItem label="手机号码"  {...formItemLayout}>
                  {
                    getFieldDecorator('telephone',{
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.telephone:null,
                      rules: [
                        { validator: this.validphone }
                      ]
                    })(
                      <Input placeholder="手机号码"  />
                    )
                  }
                </FormItem>
              </Col>
            </Row> 
            <Row gutter={24}>
              <Col span="8">  
                <FormItem label="邮&emsp;&emsp;编" {...formItemLayout}>
                  {
                    getFieldDecorator('zipcode',{
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.zipcode:null,
                      rules: [
                        { validator: this.validzipCode }
                      ]
                    })(
                      <Input placeholder="请输入6位邮编" />
                    )
                  }
                </FormItem>
              </Col>
              <Col span="8">  
                <FormItem label="负&nbsp; 责 &nbsp;人" {...formItemLayout} >
                  {
                    getFieldDecorator('leader',{
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.leader:null,
                      rules: [
                        { max: 32, message: '负责人最长为32位字符'  },
                        ]
                    })(
                      <Input placeholder="负责人"/>
                    )
                  }
                </FormItem>
              </Col>
              <Col span="8">  

              </Col>
            </Row>   
            <Row gutter={24}>
              <Col span="16">  
                <FormItem {...formItemLayout2}  label="备&emsp;&emsp;注:" >
                  {
                    getFieldDecorator('remark',{
                      initialValue: optTag && optTag === 'updateItem' ?   orgInfo.remark:null,
                      rules: [ {max: 300, message: '备注最长为300位字符'}, ]
                    })(
                      <TextArea placeholder="备注" rows={8} columns={10} />
                    )
                  }
                </FormItem>
              </Col>           
            </Row> 
            </Form>
          </div>
          <div className="submitBtn" >
          <Button type="primary" disabled = {this.state.spinStatus} onClick={this.handleOperator} >保存</Button>
          <NavLink to='/home/orgManage/orgIndex'><Button> 返回 </Button></NavLink>
        </div>
        </Spin>
        {/* 所属机构弹窗 */}
        <Modal
          title="所属机构"
          visible={this.state.orgVisible}
          width={800}
          className="modal"
          cancelText="取消"
          okText="确定"
          onOk={this.handleOrgOptions}
          onCancel={() => {
            this.child.reset();
            this.setState({
              orgVisible: false
            })
          }}
        >
          <OrgOptionsForm
            getOrgByName={(orgName) => this.getOrgByName(orgName)}
            getOrgTreeList={this.getOrgTreeList}
            orgOptionsArr={this.state.orgOptionsArr}
            getCheckedObj={this.getCheckedObj}
            checkedObj={this.state.checkedObj}
            parentCode={this.state.parentCode}
            onRef={this.onRef}
          ></OrgOptionsForm>
        </Modal>
        
      </div>
    )
  }
}

export default Form.create()(OrgAdd);