modal.jsx 22 KB

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