modal.jsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. 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={provinceSelect()}
  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. 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. provinceList: [],
  158. locationsObj: {}
  159. }
  160. },
  161. handleCancel() {
  162. this.setState({
  163. visible: false,
  164. });
  165. this.props.handleReturn(false, false);
  166. },
  167. loadInitialData() {
  168. this.setState({
  169. loading: true
  170. });
  171. $.ajax({
  172. url: globalConfig.context + '/api/roles',
  173. cache: false
  174. }).done((rolesres) => {
  175. this.setState({
  176. roles: rolesres.data,
  177. loading: false
  178. });
  179. })
  180. },
  181. loadAdminSelectList() {
  182. this.setState({
  183. loading: true
  184. });
  185. $.ajax({
  186. url: globalConfig.context + '/api/admin/supervise/adminSelectList',
  187. cache: false
  188. }).done((data) => {
  189. if (!data.data) {
  190. if (data.error && data.error.length) {
  191. message.warning(data.error[0].message);
  192. return;
  193. };
  194. };
  195. let theArr = [], i;
  196. for (i in data.data) {
  197. theArr.push(
  198. <Select.Option key={i}>{data.data[i]}</Select.Option>
  199. )
  200. };
  201. this.setState({
  202. adminSelectOption: theArr,
  203. loading: false
  204. });
  205. })
  206. },
  207. loadBindRoles(uid) {
  208. this.setState({
  209. loading: true
  210. });
  211. $.ajax({
  212. url: globalConfig.context + '/api/admin/role',
  213. cache: false,
  214. data: {
  215. "uid": uid
  216. }
  217. }).done((data) => {
  218. this.setState({
  219. bindroles: data.data || [],
  220. loading: false
  221. });
  222. })
  223. },
  224. loadLocations(id) {
  225. this.setState({
  226. loading: true
  227. });
  228. $.ajax({
  229. url: globalConfig.context + '/api/admin/supervise/adminDetailLocation',
  230. cache: false,
  231. data: {
  232. "id": id
  233. }
  234. }).done((data) => {
  235. if (!data.data) {
  236. if (data.error && data.error.length) {
  237. message.warning(data.error[0].message);
  238. return;
  239. };
  240. };
  241. data.data.map((item) => {
  242. if (item.city) {
  243. item.city = item.city.split(',');
  244. }
  245. });
  246. this.setState({
  247. locations: data.data,
  248. loading: false
  249. });
  250. })
  251. },
  252. componentWillMount() {
  253. this.loadAdminSelectList();
  254. },
  255. componentWillReceiveProps(nextProps) {
  256. if (nextProps.show && !this.state.roles.length) {
  257. this.loadInitialData();
  258. };
  259. if (!this.state.visible && nextProps.show) {
  260. this.loadBindRoles(nextProps.data.id);
  261. this.loadLocations(nextProps.data.id);
  262. };
  263. let nextState = {
  264. visible: nextProps.show,
  265. id: nextProps.data ? nextProps.data.id : '',
  266. name: nextProps.data ? nextProps.data.name : '',
  267. email: nextProps.data ? nextProps.data.email : '',
  268. mobile: nextProps.data ? nextProps.data.mobile : '',
  269. position: nextProps.data ? nextProps.data.position : '',
  270. superiorId: nextProps.data ? nextProps.data.superiorId : '',
  271. };
  272. this.setState(nextState)
  273. },
  274. resetPwd(e) {
  275. this.setState({
  276. loading: true
  277. })
  278. $.ajax({
  279. type: 'post',
  280. url: globalConfig.context + "/api/admin/supervise/resetPwd",
  281. dataType: "json",
  282. data: {
  283. id: this.state.id
  284. }
  285. }).done((res) => {
  286. if (res.error && res.error.length) {
  287. message.error(res.error[0].message);
  288. } else {
  289. message.success("密码重置成功");
  290. }
  291. }).always(() => {
  292. this.setState({
  293. loading: false
  294. })
  295. });
  296. },
  297. getRolesSelection() {
  298. if (!this.state.id) {
  299. return <li></li>
  300. } else if (this.state.id != '1') {
  301. return <li className="list">
  302. <span className='modal-text'>角色</span>
  303. <Select
  304. multiple
  305. style={{ width: '60%' }}
  306. placeholder="选择用户关联角色"
  307. value={this.state.bindroles}
  308. onChange={this.bindRoles} >
  309. {this.getRolesOptions()}
  310. </Select>
  311. </li>
  312. } else if (this.state.id == "1") {
  313. return <li><span className='modal-text'>角色</span><span>系统管理员</span></li>
  314. }
  315. },
  316. getProvinceSelection() {
  317. if (window.showPermissionList && window.showRoleList) {
  318. return <li>
  319. <span className='modal-text'>省份</span>
  320. <div style={{ display: 'inline-block' }}>
  321. <Cascader placeholder="选择省份"
  322. style={{ width: 200 }}
  323. value={this.state.province}
  324. options={citySelect()}
  325. showSearch
  326. filterOption={companySearch}
  327. onChange={(e) => { this.setState({ province: e }) }} />
  328. </div>
  329. </li>
  330. } else {
  331. return <li>
  332. <span className='modal-text'>省份</span>
  333. <Cascader placeholder="选择省份"
  334. style={{ width: 200 }}
  335. value={this.state.province}
  336. options={citySelect()}
  337. showSearch
  338. filterOption={companySearch}
  339. onChange={(e) => { this.setState({ province: e }) }} />
  340. </li>
  341. }
  342. },
  343. bindRoles(val) {
  344. this.setState({
  345. bindroles: val
  346. });
  347. },
  348. getRolesOptions() {
  349. let options = [];
  350. for (let i = 0; i < this.state.roles.length; i++) {
  351. options.push(<Select.Option key={String(this.state.roles[i].id)}>{this.state.roles[i].roleName}</Select.Option>);
  352. }
  353. return options;
  354. },
  355. getLocations(index, p, c) {
  356. this.state.locations.map((item, i) => {
  357. if (index == i) {
  358. item.province = p;
  359. item.city = c || [];
  360. };
  361. });
  362. },
  363. delLocations(index) {
  364. this.state.locations.splice(index, 1);
  365. this.setState({ locations: this.state.locations });
  366. },
  367. provinceAdd() {
  368. this.state.locations.push({
  369. province: null,
  370. city: null
  371. });
  372. this.setState({ locations: this.state.locations });
  373. },
  374. render() {
  375. return (
  376. <div className="modal" >
  377. <Spin spinning={this.state.loading} >
  378. <Modal maskClosable={false} title="管理员详情"
  379. closable={false} width={1000}
  380. visible={this.state.visible}
  381. onOk={this.postData}
  382. onCancel={this.handleCancel} >
  383. <ul className="modal-content clearfix">
  384. <li className="list">
  385. <span className='modal-text'>名字</span>
  386. <Input value={this.state.name} onChange={(e) => { this.state.name = e.target.value; this.setState({ name: this.state.name }); }} />
  387. </li>
  388. <li className="list">
  389. <span className='modal-text'>职位</span>
  390. <Input value={this.state.position} onChange={(e) => { this.state.position = e.target.value; this.setState({ position: this.state.position }); }} />
  391. </li>
  392. <li className="list">
  393. <span className='modal-text'>上级</span>
  394. <Select style={{ verticalAlign: 'middle', width: 200 }}
  395. placeholder="选择一个上级" notFoundContent="没有获取到管理员列表"
  396. showSearch
  397. filterOption={companySearch}
  398. value={this.state.superiorId}
  399. onChange={(e) => { this.setState({ superiorId: e }) }} >
  400. {this.state.adminSelectOption}
  401. </Select>
  402. </li>
  403. <li className="list">
  404. <span className='modal-text'>登录账号</span>
  405. <Input value={this.state.mobile} onChange={(e) => { this.state.mobile = e.target.value; this.setState({ mobile: this.state.mobile }); }} />
  406. </li>
  407. <li className="list">
  408. <span className='modal-text'>邮箱</span>
  409. <Input value={this.state.email} onChange={(e) => { this.state.email = e.target.value; this.setState({ email: this.state.email }); }} />
  410. </li>
  411. {this.getRolesSelection()}
  412. {this.state.id ? <li className="list">
  413. <span></span>
  414. <Popconfirm
  415. title={"用户 [ " + this.state.name + " ] 的密码将会重置为123456,确认操作?"}
  416. onConfirm={this.resetPwd}
  417. okText="确认"
  418. cancelText="取消"
  419. placement="topLeft">
  420. <Button>重置密码</Button>
  421. </Popconfirm>
  422. </li> : <li></li>}
  423. </ul>
  424. <div>
  425. <span className='modal-text' style={{ verticalAlign: 'top' }}>地区</span>
  426. {!window.showPermissionList && window.showRoleList ? <div style={{ display: 'inline-block', width: '88%' }}>
  427. {this.state.locations.map((item, i) => {
  428. return <ProvinceAdd
  429. getLocations={this.getLocations}
  430. delLocations={this.delLocations}
  431. locations={this.state.locations}
  432. index={i} key={i} />
  433. })}
  434. <Button style={{ verticalAlign: 'middle' }} type="dashed" size="small"
  435. onClick={this.provinceAdd}>
  436. <Icon type="plus" />
  437. </Button>
  438. </div> : <div style={{ display: 'inline-block', width: '88%' }}>
  439. {this.state.locations.map((item, i) => {
  440. return <Tag key={i}>{getProvince(item.province) + ' ' + (item.city ? item.city.map((item) => {
  441. return getProvince(item) + ' '
  442. }) : '')}</Tag>
  443. })}
  444. </div>}
  445. </div>
  446. </Modal>
  447. </Spin>
  448. </div >
  449. );
  450. }
  451. })
  452. export default TheModal;