modal.jsx 18 KB

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