modal.jsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. });
  182. this.props.handleReturn(false, false);
  183. },
  184. loadAdminSelectList() {
  185. this.setState({
  186. loading: true
  187. });
  188. $.ajax({
  189. url: globalConfig.context + '/api/admin/supervise/adminSelectList',
  190. cache: false
  191. }).done((data) => {
  192. if (!data.data) {
  193. if (data.error && data.error.length) {
  194. message.warning(data.error[0].message);
  195. return;
  196. };
  197. };
  198. let theArr = [], i;
  199. for (i in data.data) {
  200. theArr.push(
  201. <Select.Option key={i}>{data.data[i]}</Select.Option>
  202. )
  203. };
  204. this.setState({
  205. adminSelectOption: theArr,
  206. adminSelectObj: data.data,
  207. loading: false
  208. });
  209. })
  210. },
  211. loadAdminOwnLocation() {
  212. this.setState({
  213. loading: true
  214. });
  215. $.ajax({
  216. url: globalConfig.context + '/api/admin/supervise/adminOwnLocation',
  217. cache: false
  218. }).done((data) => {
  219. if (!data.data) {
  220. if (data.error && data.error.length) {
  221. message.warning(data.error[0].message);
  222. return;
  223. };
  224. };
  225. let theArr = [], theList = [];
  226. if ((typeof (data.data)).indexOf('string') == -1) {
  227. data.data.map((item) => {
  228. theArr.push({
  229. value: item.province,
  230. label: getProvince(item.province)
  231. });
  232. let cityList = [];
  233. if (item.city) {
  234. item.city.split(',').map((c) => {
  235. cityList.push({
  236. id: Number(c),
  237. name: getProvince(c)
  238. });
  239. });
  240. };
  241. theList.push({
  242. id: item.province,
  243. name: getProvince(item.province),
  244. cityList: cityList.length ? cityList : null
  245. });
  246. });
  247. } else {
  248. theArr = provinceSelect();
  249. theList = provinceList;
  250. }
  251. this.setState({
  252. provinceOption: theArr,
  253. provinceList: theList,
  254. loading: false
  255. });
  256. })
  257. },
  258. loadBindRoles(uid) {
  259. this.setState({
  260. loading: true
  261. });
  262. $.ajax({
  263. url: globalConfig.context + '/api/admin/role',
  264. cache: false,
  265. data: {
  266. "uid": uid
  267. }
  268. }).done((data) => {
  269. if (!data.data) {
  270. if (data.error && data.error.length) {
  271. message.warning(data.error[0].message);
  272. };
  273. return;
  274. } else if (data.data) {
  275. this.state.theSwitch = true;
  276. for (let i = 0; i < data.data.length; i++) {
  277. for (let n = 0; n < this.props.currentRoles.length; n++) {
  278. if (data.data[i] === this.props.currentRoles[n]) {
  279. this.state.theSwitch = false;
  280. }
  281. }
  282. };
  283. };
  284. this.setState({
  285. bindroles: data.data || [],
  286. theSwitch: this.state.theSwitch,
  287. loading: false
  288. });
  289. })
  290. },
  291. loadLocations(id) {
  292. this.setState({
  293. loading: true
  294. });
  295. $.ajax({
  296. url: globalConfig.context + '/api/admin/supervise/adminDetailLocation',
  297. cache: false,
  298. data: {
  299. "id": id
  300. }
  301. }).done((data) => {
  302. if (!data.data) {
  303. if (data.error && data.error.length) {
  304. message.warning(data.error[0].message);
  305. return;
  306. };
  307. };
  308. data.data.map((item) => {
  309. if (item.city) {
  310. item.city = item.city.split(',');
  311. }
  312. });
  313. this.setState({
  314. locations: data.data,
  315. loading: false
  316. });
  317. })
  318. },
  319. componentWillMount() {
  320. this.loadAdminSelectList();
  321. this.loadAdminOwnLocation();
  322. },
  323. componentWillReceiveProps(nextProps) {
  324. if (!this.state.visible && nextProps.show) {
  325. if (nextProps.data.id) {
  326. this.loadBindRoles(nextProps.data.id);
  327. this.loadLocations(nextProps.data.id);
  328. }
  329. };
  330. let nextState = {
  331. visible: nextProps.show,
  332. id: nextProps.data ? nextProps.data.id : '',
  333. name: nextProps.data ? nextProps.data.name : '',
  334. email: nextProps.data ? nextProps.data.email : '',
  335. mobile: nextProps.data ? nextProps.data.mobile : '',
  336. position: nextProps.data ? nextProps.data.position : '',
  337. superiorId: nextProps.data ? nextProps.data.superiorId : '',
  338. };
  339. this.setState(nextState)
  340. },
  341. resetPwd(e) {
  342. this.setState({
  343. loading: true
  344. })
  345. $.ajax({
  346. type: 'post',
  347. url: globalConfig.context + "/api/admin/supervise/resetPwd",
  348. dataType: "json",
  349. data: {
  350. id: this.state.id
  351. }
  352. }).done((res) => {
  353. if (res.error && res.error.length) {
  354. message.error(res.error[0].message);
  355. } else {
  356. message.success("密码重置成功");
  357. }
  358. }).always(() => {
  359. this.setState({
  360. loading: false
  361. })
  362. });
  363. },
  364. getRolesSelection() {
  365. if (!this.state.id) {
  366. return <li></li>
  367. } else if (this.state.id != '1') {
  368. return <li className="list">
  369. <span className='modal-text'>角色</span>
  370. <Select
  371. multiple
  372. style={{ width: '60%' }}
  373. placeholder="选择用户关联角色"
  374. value={this.state.bindroles}
  375. onChange={this.bindRoles} >
  376. {this.getRolesOptions()}
  377. </Select>
  378. </li>
  379. } else if (this.state.id == "1") {
  380. return <li><span className='modal-text'>角色</span><span>系统管理员</span></li>
  381. }
  382. },
  383. bindRoles(val) {
  384. this.setState({
  385. bindroles: val
  386. });
  387. },
  388. getRolesOptions() {
  389. let options = [];
  390. for (let i = 0; i < this.props.roles.length; i++) {
  391. options.push(<Select.Option key={String(this.props.roles[i].id)}>{this.props.roles[i].roleName}</Select.Option>);
  392. }
  393. return options;
  394. },
  395. getLocations(index, p, c) {
  396. this.state.locations.map((item, i) => {
  397. if (index == i) {
  398. item.province = p;
  399. item.city = c || [];
  400. };
  401. });
  402. },
  403. delLocations(index) {
  404. this.state.locations.splice(index, 1);
  405. this.setState({ locations: this.state.locations });
  406. },
  407. provinceAdd() {
  408. this.state.locations.push({
  409. province: null,
  410. city: null
  411. });
  412. this.setState({ locations: this.state.locations });
  413. },
  414. render() {
  415. return (
  416. <div className="modal" >
  417. <Modal maskClosable={false} title="管理员详情"
  418. closable={false} width={1000}
  419. visible={this.state.visible}
  420. onOk={this.postData}
  421. onCancel={this.handleCancel} >
  422. <Spin spinning={this.state.loading} >
  423. {this.state.theSwitch ? <div className="modal-box">
  424. <ul className="modal-content clearfix">
  425. <li className="list">
  426. <span className='modal-text'>名字</span>
  427. <Input value={this.state.name} onChange={(e) => { this.state.name = e.target.value; this.setState({ name: this.state.name }); }} />
  428. </li>
  429. <li className="list">
  430. <span className='modal-text'>职位</span>
  431. <Input value={this.state.position} onChange={(e) => { this.state.position = e.target.value; this.setState({ position: this.state.position }); }} />
  432. </li>
  433. <li className="list">
  434. <span className='modal-text'>上级</span>
  435. <Select style={{ verticalAlign: 'middle', width: 200 }}
  436. placeholder="选择一个上级" notFoundContent="没有获取到管理员列表"
  437. showSearch
  438. filterOption={companySearch}
  439. value={this.state.superiorId}
  440. onChange={(e) => { this.setState({ superiorId: e }) }} >
  441. {this.state.adminSelectOption}
  442. </Select>
  443. </li>
  444. <li className="list">
  445. <span className='modal-text'>登录账号</span>
  446. <Input value={this.state.mobile} onChange={(e) => { this.state.mobile = e.target.value; this.setState({ mobile: this.state.mobile }); }} />
  447. </li>
  448. <li className="list">
  449. <span className='modal-text'>邮箱</span>
  450. <Input value={this.state.email} onChange={(e) => { this.state.email = e.target.value; this.setState({ email: this.state.email }); }} />
  451. </li>
  452. {this.getRolesSelection()}
  453. {this.state.id ? <li className="list">
  454. <span></span>
  455. <Popconfirm
  456. title={"用户 [ " + this.state.name + " ] 的密码将会重置为123456,确认操作?"}
  457. onConfirm={this.resetPwd}
  458. okText="确认"
  459. cancelText="取消"
  460. placement="topLeft">
  461. <Button>重置密码</Button>
  462. </Popconfirm>
  463. </li> : <li></li>}
  464. </ul>
  465. <div>
  466. <span className='modal-text' style={{ verticalAlign: 'top' }}>地区</span>
  467. {window.showPermissionList && window.showRoleList ? <div style={{ display: 'inline-block', width: '88%' }}>
  468. {this.state.locations.map((item, i) => {
  469. return <ProvinceAdd
  470. provinceList={this.state.provinceList}
  471. provinceOption={this.state.provinceOption}
  472. getLocations={this.getLocations}
  473. delLocations={this.delLocations}
  474. locations={this.state.locations}
  475. index={i} key={i} />
  476. })}
  477. <Button style={{ verticalAlign: 'middle' }} type="dashed" size="small"
  478. onClick={this.provinceAdd}>
  479. <Icon type="plus" />
  480. </Button>
  481. </div> : <div style={{ display: 'inline-block', width: '88%' }}>
  482. {this.state.locations.map((item, i) => {
  483. return <Tag key={i}>{getProvince(item.province) + ' ' + (item.city ? item.city.map((c) => {
  484. return getProvince(c) + ' '
  485. }) : '')}</Tag>
  486. })}
  487. </div>}
  488. </div>
  489. </div> : <div className="modal-box">
  490. <ul className="modal-content clearfix">
  491. <li className="list">
  492. <span className='modal-text'>名字</span>
  493. <span className="modal-det">{this.state.name}</span>
  494. </li>
  495. <li className="list">
  496. <span className='modal-text'>职位</span>
  497. <span className="modal-det">{this.state.position}</span>
  498. </li>
  499. <li className="list">
  500. <span className='modal-text'>上级</span>
  501. <span className="modal-det">{this.state.adminSelectObj[this.state.superiorId]}</span>
  502. </li>
  503. <li className="list">
  504. <span className='modal-text'>登录账号</span>
  505. <span className="modal-det">{this.state.mobile}</span>
  506. </li>
  507. <li className="list">
  508. <span className='modal-text'>邮箱</span>
  509. <span className="modal-det">{this.state.email}</span>
  510. </li>
  511. </ul>
  512. <div>
  513. <span className='modal-text' style={{ verticalAlign: 'top' }}>地区</span>
  514. <div style={{ display: 'inline-block', width: '88%' }}>
  515. {this.state.locations.map((item, i) => {
  516. return <Tag key={i}>{getProvince(item.province) + ' ' + (item.city ? item.city.map((c) => {
  517. return getProvince(c) + ' '
  518. }) : '')}</Tag>
  519. })}
  520. </div>
  521. </div>
  522. </div>}
  523. </Spin>
  524. </Modal>
  525. </div >
  526. );
  527. }
  528. })
  529. export default TheModal;