modal.jsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. import React from 'react';
  2. import { Tag, Modal, Input, Spin, Switch, Select, message, Popconfirm, Button, Cascader, Icon } from 'antd';
  3. import { provinceSelect, provinceList, getProvince ,citySelect} from '../../../NewDicProvinceList.js';
  4. import { companySearch } from '../../../tools.js';
  5. import ajax from 'jquery/src/ajax/xhr.js';
  6. import $ from 'jquery/src/ajax';
  7. import {customerStatus} from '../../../dataDic.js'
  8. const ProvinceAdd = React.createClass({
  9. getInitialState() {
  10. return {
  11. cityOption: [],
  12. theProvince: [],
  13. citys: [],
  14. departmentArr:[]
  15. };
  16. },
  17. componentWillMount() {
  18. let theArr = [];
  19. this.state.theProvince = [this.props.locations[this.props.index].province];
  20. this.state.citys = this.props.locations[this.props.index].city || [];
  21. this.props.provinceList.map((item) => {
  22. if(item.id == this.props.locations[this.props.index].province) {
  23. item.cityList.map((city) => {
  24. theArr.push({
  25. value: city.id,
  26. label: city.name
  27. })
  28. });
  29. };
  30. });
  31. this.state.cityOption = theArr;
  32. },
  33. componentWillReceiveProps(nextProps) {
  34. let theArr = [];
  35. this.state.theProvince = [nextProps.locations[nextProps.index].province];
  36. this.state.citys = nextProps.locations[nextProps.index].city || [];
  37. this.props.provinceList.map((item) => {
  38. if(item.id == nextProps.locations[nextProps.index].province) {
  39. item.cityList.map((city) => {
  40. theArr.push({
  41. value: city.id,
  42. label: city.name
  43. })
  44. });
  45. };
  46. });
  47. this.state.cityOption = theArr;
  48. },
  49. render() {
  50. return(
  51. <div style={{ display: 'inline-block', marginRight: '20px', marginBottom: '10px' }}>
  52. <Cascader placeholder="选择省份"
  53. style={{ width: 80, marginRight: '20px', verticalAlign: 'middle' }}
  54. options={this.props.provinceOption}
  55. value={this.state.theProvince}
  56. showSearch
  57. filterOption={companySearch}
  58. onChange={(e) => {
  59. let bool = true, theArr = [], _me = this;;
  60. this.props.locations.map((item, i) => {
  61. if (item.province == e[0] && this.props.index != i) {
  62. bool = false;
  63. };
  64. });
  65. if (!bool) {
  66. message.warning('请选择一个其他省份');
  67. this.setState({
  68. theProvince: undefined,
  69. citys: undefined,
  70. cityOption: []
  71. });
  72. return;
  73. };
  74. this.props.provinceList.map((item) => {
  75. if (item.id == e) {
  76. item.cityList.map((city) => {
  77. theArr.push({
  78. value: city.id,
  79. label: city.name
  80. })
  81. });
  82. };
  83. });
  84. this.props.getLocations(this.props.index, e[0])
  85. this.setState({
  86. theProvince: e,
  87. citys: undefined,
  88. cityOption: theArr,
  89. })
  90. }} />
  91. <Select style={{ verticalAlign: 'middle', width: 584 }}
  92. placeholder="选择城市" notFoundContent="请先选择一个省份" multiple={true}
  93. showSearch
  94. filterOption={companySearch}
  95. value={this.state.citys}
  96. onChange={(e) => { this.setState({ citys: e }) }}
  97. onBlur={() => {
  98. this.props.getLocations(this.props.index, this.state.theProvince ? this.state.theProvince[0] : null, this.state.citys)
  99. }}>
  100. {this.state.cityOption.map((item) => {
  101. return <Select.Option key={String(item.value)}>{item.label}</Select.Option>
  102. })}
  103. </Select>
  104. <Button style={{ verticalAlign: 'middle', marginLeft: '20px' }} type="dashed" size="small"
  105. onClick={() => { this.props.delLocations(this.props.index) }}>
  106. <Icon type="minus" />
  107. </Button>
  108. </div>
  109. );
  110. },
  111. })
  112. const TheModal = React.createClass({
  113. postData() {
  114. this.setState({
  115. loading: true
  116. });
  117. let theArr = [];
  118. let theLocations = this.state.locations.concat();
  119. theLocations.map((item, i) => {
  120. if(item.province) {
  121. theArr.push(item);
  122. };
  123. });
  124. theArr.map((item) => {
  125. if(item.city && item.city.length) {
  126. item.city = item.city.join(',');
  127. } else {
  128. item.city = null;
  129. };
  130. });
  131. let departmentIds='';
  132. if(!this.state.department){
  133. departmentIds=''
  134. }else if((this.state.department).length>30){
  135. departmentIds=this.state.department
  136. }else{
  137. departmentIds=this.state.departmentId
  138. }
  139. $.ajax({
  140. type: "POST",
  141. url: this.state.id ? globalConfig.context + "/api/admin/supervise/updateAdmin" : globalConfig.context + "/api/admin/supervise/insertAdmin",
  142. data: {
  143. data: JSON.stringify({
  144. 'id': this.state.id,
  145. 'name': this.state.name,
  146. 'email': this.state.email,
  147. 'mobile': this.state.mobile,
  148. 'position': this.state.position,
  149. //'locations': theArr,
  150. 'province':this.state.addressSearch.length?this.state.addressSearch[0]:'',
  151. 'city':this.state.addressSearch.length?this.state.addressSearch[1]:'',
  152. 'departmentId':departmentIds,
  153. 'superiorId': this.state.superiorId
  154. }),
  155. 'roles': this.state.bindroles
  156. }
  157. }).done((res) => {
  158. if(res.error.length) {
  159. message.error(res.error[0].message);
  160. } else {
  161. message.success("保存成功");
  162. this.setState({
  163. visible: false,
  164. });
  165. this.props.handleReturn(false, true);
  166. //第二个参数表示保存
  167. }
  168. }).always(() => {
  169. this.setState({
  170. loading: false
  171. })
  172. })
  173. this.props.postData;
  174. },
  175. getInitialState() {
  176. return {
  177. name: '',
  178. email: '',
  179. mobile: '',
  180. roles: [],
  181. visible: false,
  182. loading: false,
  183. theSwitch: true,
  184. bindroles: [],
  185. locations: [],
  186. cityOption: [],
  187. locationsObj: {},
  188. adminSelectObj: {}
  189. }
  190. },
  191. handleCancel() {
  192. this.setState({
  193. visible: false,
  194. locations: []
  195. });
  196. this.props.handleReturn(false, false);
  197. },
  198. loadAdminSelectList() {
  199. this.setState({
  200. loading: true
  201. });
  202. $.ajax({
  203. url: globalConfig.context + '/api/admin/supervise/adminSelectList',
  204. cache: false
  205. }).done((data) => {
  206. if(!data.data) {
  207. if(data.error && data.error.length) {
  208. message.warning(data.error[0].message);
  209. return;
  210. };
  211. };
  212. let theArr = [],
  213. i;
  214. for(i in data.data) {
  215. theArr.push(
  216. <Select.Option key={i}>{data.data[i]}</Select.Option>
  217. )
  218. };
  219. this.setState({
  220. adminSelectOption: theArr,
  221. adminSelectObj: data.data,
  222. loading: false
  223. });
  224. })
  225. },
  226. loadAdminOwnLocation() {
  227. this.setState({
  228. loading: true
  229. });
  230. $.ajax({
  231. url: globalConfig.context + '/api/admin/supervise/adminOwnLocation',
  232. cache: false
  233. }).done((data) => {
  234. if(!data.data) {
  235. if(data.error && data.error.length) {
  236. message.warning(data.error[0].message);
  237. return;
  238. };
  239. };
  240. let theArr = [],
  241. theList = [];
  242. if((typeof(data.data)).indexOf('string') == -1) {
  243. data.data.map((item) => {
  244. theArr.push({
  245. value: item.province,
  246. label: getProvince(item.province)
  247. });
  248. let cityList = [];
  249. if(item.city) {
  250. item.city.split(',').map((c) => {
  251. cityList.push({
  252. id: Number(c),
  253. name: getProvince(c)
  254. });
  255. });
  256. };
  257. theList.push({
  258. id: item.province,
  259. name: getProvince(item.province),
  260. cityList: cityList.length ? cityList : null
  261. });
  262. });
  263. } else {
  264. theArr = provinceSelect();
  265. theList = provinceList;
  266. }
  267. this.setState({
  268. provinceOption: theArr,
  269. provinceList: theList,
  270. loading: false
  271. });
  272. })
  273. },
  274. loadBindRoles(uid) {
  275. this.setState({
  276. loading: true
  277. });
  278. $.ajax({
  279. url: globalConfig.context + '/api/admin/role',
  280. cache: false,
  281. data: {
  282. "uid": uid
  283. }
  284. }).done((data) => {
  285. if(!data.data) {
  286. if(data.error && data.error.length) {
  287. message.warning(data.error[0].message);
  288. };
  289. return;
  290. } else if(data.data) {
  291. this.state.theSwitch = true;
  292. for(let i = 0; i < data.data.length; i++) {
  293. for(let n = 0; n < this.props.currentRoles.length; n++) {
  294. if(data.data[i] === this.props.currentRoles[n] && uid != 1) {
  295. this.state.theSwitch = false;
  296. }
  297. }
  298. };
  299. };
  300. this.setState({
  301. bindroles: data.data || [],
  302. theSwitch: this.state.theSwitch,
  303. loading: false
  304. });
  305. })
  306. },
  307. loadLocations(id) {
  308. this.setState({
  309. loading: true
  310. });
  311. $.ajax({
  312. url: globalConfig.context + '/api/admin/supervise/adminDetailLocation',
  313. cache: false,
  314. data: {
  315. "id": id
  316. }
  317. }).done((data) => {
  318. if(!data.data) {
  319. if(data.error && data.error.length) {
  320. message.warning(data.error[0].message);
  321. return;
  322. };
  323. };
  324. data.data.map((item) => {
  325. if(item.city) {
  326. item.city = item.city.split(',');
  327. }
  328. });
  329. this.setState({
  330. locations: data.data,
  331. loading: false
  332. });
  333. })
  334. },
  335. componentWillMount() {
  336. this.loadAdminSelectList();
  337. this.loadAdminOwnLocation();
  338. },
  339. componentWillReceiveProps(nextProps) {
  340. if(!this.state.visible && nextProps.show) {
  341. if(nextProps.data.id) {
  342. this.loadBindRoles(nextProps.data.id);
  343. this.loadLocations(nextProps.data.id);
  344. }
  345. };
  346. let proCity=[];
  347. let province=nextProps.data ? parseInt(nextProps.data.province) : '';
  348. let city = nextProps.data ? parseInt(nextProps.data.city) : '';
  349. proCity.push(province,city);
  350. let nextState = {
  351. visible: nextProps.show,
  352. id: nextProps.data ? nextProps.data.id : '',
  353. name: nextProps.data ? nextProps.data.name : '',
  354. email: nextProps.data ? nextProps.data.email : '',
  355. mobile: nextProps.data ? nextProps.data.mobile : '',
  356. position: nextProps.data ? nextProps.data.position : '',
  357. addressSearch:proCity,
  358. addressProvince:nextProps.data ? nextProps.data.province : '',
  359. department: nextProps.data ? nextProps.data.departmentName : '',
  360. departmentId:nextProps.data ? nextProps.data.departmentId : '',
  361. superiorId: nextProps.data ? nextProps.data.superiorId : '',
  362. };
  363. this.setState(nextState)
  364. },
  365. resetPwd(e) {
  366. this.setState({
  367. loading: true
  368. })
  369. $.ajax({
  370. type: 'post',
  371. url: globalConfig.context + "/api/admin/supervise/resetPwd",
  372. dataType: "json",
  373. data: {
  374. id: this.state.id
  375. }
  376. }).done((res) => {
  377. if(res.error && res.error.length) {
  378. message.error(res.error[0].message);
  379. } else {
  380. message.success("密码重置成功");
  381. }
  382. }).always(() => {
  383. this.setState({
  384. loading: false
  385. })
  386. });
  387. },
  388. getRolesSelection() {
  389. if(!this.state.id) {
  390. return <li></li>
  391. } else if(this.state.id != '1') {
  392. return <li className="list">
  393. <span className='modal-text'>角色</span>
  394. <Select
  395. multiple
  396. style={{ width: '60%' }}
  397. placeholder="选择用户关联角色"
  398. value={this.state.bindroles}
  399. onChange={this.bindRoles} >
  400. {this.getRolesOptions()}
  401. </Select>
  402. </li>
  403. } else if(this.state.id == "1") {
  404. return <li><span className='modal-text'>角色</span><span style={{width:'150px'}}>系统管理员</span></li>
  405. }
  406. },
  407. bindRoles(val) {
  408. this.setState({
  409. bindroles: val
  410. });
  411. },
  412. getRolesOptions() {
  413. let options = [];
  414. for(let i = 0; i < this.props.roles.length; i++) {
  415. options.push(<Select.Option key={String(this.props.roles[i].id)}>{this.props.roles[i].roleName}</Select.Option>);
  416. }
  417. return options;
  418. },
  419. getLocations(index, p, c) {
  420. this.state.locations.map((item, i) => {
  421. if(index == i) {
  422. item.province = p;
  423. item.city = c || [];
  424. };
  425. });
  426. },
  427. delLocations(index) {
  428. this.state.locations.splice(index, 1);
  429. this.setState({ locations: this.state.locations });
  430. },
  431. provinceAdd() {
  432. this.state.locations.push({
  433. province: null,
  434. city: null
  435. });
  436. this.setState({ locations: this.state.locations });
  437. },
  438. render() {
  439. let departmentArr=this.props.departmentArr||[];
  440. return(
  441. <div className="modal" >
  442. <Modal maskClosable={false} title="管理员详情"
  443. closable={false} width={1000}
  444. visible={this.state.visible}
  445. onOk={this.postData}
  446. onCancel={this.handleCancel} >
  447. <Spin spinning={this.state.loading} >
  448. {this.state.theSwitch ?
  449. <div className="modal-box">
  450. <ul className="modal-content clearfix">
  451. <li className="list">
  452. <span className='modal-text'>名字</span>
  453. <Input value={this.state.name} onChange={(e) => { this.state.name = e.target.value; this.setState({ name: this.state.name }); }} />
  454. </li>
  455. <li className="list">
  456. <span className='modal-text'>职位</span>
  457. <Input value={this.state.position} onChange={(e) => { this.state.position = e.target.value; this.setState({ position: this.state.position }); }} />
  458. </li>
  459. <li className="list">
  460. <span className='modal-text'>部门</span>
  461. <Select placeholder="选择部门" style={{width:'200px'}}
  462. value={this.state.department}
  463. onChange={(e) => { this.setState({ department: e }) }}>
  464. {
  465. departmentArr.map(function (item) {
  466. return <Select.Option key={item.id} >{item.name}</Select.Option>
  467. })
  468. }
  469. </Select>
  470. </li>
  471. <li className="list">
  472. <span className='modal-text'>上级</span>
  473. <Select style={{ verticalAlign: 'middle', width: 200 }}
  474. placeholder="选择一个上级" notFoundContent="没有获取到管理员列表"
  475. showSearch
  476. filterOption={companySearch}
  477. value={this.state.superiorId}
  478. onChange={(e) => { this.setState({ superiorId: e }) }} >
  479. {this.state.adminSelectOption}
  480. </Select>
  481. </li>
  482. <li className="list">
  483. <span className='modal-text'>登录账号</span>
  484. <Input value={this.state.mobile} onChange={(e) => { this.state.mobile = e.target.value; this.setState({ mobile: this.state.mobile }); }} />
  485. </li>
  486. <li className="list">
  487. <span className='modal-text'>邮箱</span>
  488. <Input value={this.state.email} onChange={(e) => { this.state.email = e.target.value; this.setState({ email: this.state.email }); }} />
  489. </li>
  490. {this.getRolesSelection()}
  491. {this.state.id ? <li className="list">
  492. <span></span>
  493. <Popconfirm
  494. title={"用户 [ " + this.state.name + " ] 的密码将会重置为123456,确认操作?"}
  495. onConfirm={this.resetPwd}
  496. okText="确认"
  497. cancelText="取消"
  498. placement="topLeft">
  499. <Button>重置密码</Button>
  500. </Popconfirm>
  501. </li> : <li></li>}
  502. </ul>
  503. <div>
  504. <span className='modal-text' style={{ verticalAlign: 'top' }}>地区</span>
  505. {/* {window.showPermissionList && window.showRoleList ? <div style={{ display: 'inline-block', width: '88%' }}>
  506. {this.state.locations.map((item, i) => {
  507. return <ProvinceAdd
  508. provinceList={this.state.provinceList}
  509. provinceOption={this.state.provinceOption}
  510. getLocations={this.getLocations}
  511. delLocations={this.delLocations}
  512. locations={this.state.locations}
  513. index={i} key={i} />
  514. })}
  515. <Button style={{ verticalAlign: 'middle' }} type="dashed" size="small"
  516. onClick={this.provinceAdd}>
  517. <Icon type="plus" />
  518. </Button>
  519. </div> : <div style={{ display: 'inline-block', width: '88%' }}>
  520. {this.state.locations.map((item, i) => {
  521. return <Tag key={i}>{getProvince(item.province) + ' ' + (item.city ? item.city.map((c) => {
  522. return getProvince(c) + ' '
  523. }) : '')}</Tag>
  524. })}
  525. </div>}*/}
  526. <Cascader options={citySelect()} value={this.state.addressSearch} placeholder="选择城市"
  527. onChange={(e,pre) => { this.setState({ addressSearch: e }) }} />
  528. </div>
  529. </div> : <div className="modal-box">
  530. <ul className="modal-content clearfix">
  531. <li className="list">
  532. <span className='modal-text'>名字</span>
  533. <span className="modal-det">{this.state.name}</span>
  534. </li>
  535. <li className="list">
  536. <span className='modal-text'>职位</span>
  537. <span className="modal-det">{this.state.position}</span>
  538. </li>
  539. <li className="list">
  540. <span className='modal-text'>上级</span>
  541. <span className="modal-det">{this.state.adminSelectObj[this.state.superiorId]}</span>
  542. </li>
  543. <li className="list">
  544. <span className='modal-text'>登录账号</span>
  545. <span className="modal-det">{this.state.mobile}</span>
  546. </li>
  547. <li className="list">
  548. <span className='modal-text'>邮箱</span>
  549. <span className="modal-det">{this.state.email}</span>
  550. </li>
  551. </ul>
  552. <div>
  553. <span className='modal-text' style={{ verticalAlign: 'top' }}>地区</span>
  554. <div style={{ display: 'inline-block', width: '88%' }}>
  555. {this.state.locations.map((item, i) => {
  556. return <Tag key={i}>{getProvince(item.province) + ' ' + (item.city ? item.city.map((c) => {
  557. return getProvince(c) + ' '
  558. }) : '')}</Tag>
  559. })}
  560. </div>
  561. </div>
  562. </div>}
  563. </Spin>
  564. </Modal>
  565. </div >
  566. );
  567. }
  568. })
  569. export default TheModal;