newRole.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import React from 'react';
  2. import { Icon, Table, Modal, message, Spin, Input, Select, Button, Form, Tree } from 'antd';
  3. import ajax from 'jquery/src/ajax/xhr.js';
  4. import $ from 'jquery/src/ajax';
  5. import './userMangagement.less';
  6. const TreeNodet = Tree.TreeNode;
  7. const Newrole = Form.create()(React.createClass({
  8. addloadData() {
  9. this.setState({
  10. loading: true
  11. });
  12. $.ajax({
  13. method: "post",
  14. dataType: "json",
  15. crossDomain: false,
  16. url: globalConfig.context + '/api/admin/permissions',
  17. data: {
  18. },
  19. success: function (data) {
  20. let theArr = [];
  21. if (!data.data || !data.data.one) {
  22. if (data.error && data.error.length) {
  23. message.warning(data.error[0].message);
  24. };
  25. } else {
  26. this.setState({
  27. Interface: data.data.one,
  28. });
  29. };
  30. }.bind(this),
  31. }).always(function () {
  32. this.setState({
  33. loading: false
  34. });
  35. }.bind(this));
  36. },
  37. getInitialState() {
  38. return {
  39. loading: false,
  40. visible: false,
  41. expandedKeys: [],
  42. autoExpandParent: true,
  43. checkedKeys: [],
  44. selectedKeys: [],
  45. renderTreeNodes: undefined,
  46. Interface: []
  47. };
  48. },
  49. handleSubmit(e) {
  50. e.preventDefault();
  51. this.props.form.validateFields((err, values) => {
  52. let selectAllId = this.state.checkedKeys || [];
  53. if (!err) {
  54. this.setState({
  55. loading: true
  56. });
  57. // if(selectAllId){
  58. // if(selectAllId[0]==null){
  59. // selectAllId.splice(0,1)
  60. // }
  61. // }
  62. $.ajax({
  63. method: "POST",
  64. dataType: "json",
  65. crossDomain: false,
  66. url: globalConfig.context + '/api/admin/role',
  67. data: {
  68. data: JSON.stringify({
  69. 'id': this.state.ids ? this.state.ids : null,
  70. 'roleName': this.state.roleName,
  71. 'roleType': this.state.roleType,
  72. 'permissions': selectAllId.checked ? selectAllId.checked : selectAllId
  73. })
  74. }
  75. }).done(function (data) {
  76. this.setState({
  77. loading: false
  78. });
  79. if (!data.error.length) {
  80. message.success('保存成功!');
  81. this.handleOk()
  82. } else {
  83. message.warning(data.error[0].message);
  84. }
  85. }.bind(this));
  86. }
  87. });
  88. },
  89. //查看基本详情基本信息
  90. loaduser(record) {
  91. if (record) {
  92. $.ajax({
  93. method: "post",
  94. dataType: "json",
  95. crossDomain: false,
  96. url: globalConfig.context + '/api/admin/role/rolePermission',
  97. data: {
  98. id: record.id
  99. },
  100. success: function (data) {
  101. let thisData = data.data;
  102. if (!thisData) {
  103. if (data.error && data.error.length) {
  104. message.warning(data.error[0].message);
  105. };
  106. thisData = {};
  107. };
  108. let idList = [];
  109. thisData.map(function (item) {
  110. idList.push(
  111. item.pid
  112. )
  113. })
  114. this.setState({
  115. ids: record.id,
  116. roleName: thisData[0].rname,
  117. roleType: thisData[0].roleType,
  118. checkedKeys: idList,
  119. });
  120. }.bind(this),
  121. }).always(function () {
  122. this.setState({
  123. loading: false
  124. });
  125. }.bind(this));
  126. }
  127. },
  128. handleOk(e) {
  129. this.setState({
  130. visible: false,
  131. });
  132. this.props.closeDesc(false, true);
  133. },
  134. handleCancel(e) {
  135. this.setState({
  136. visible: false,
  137. });
  138. this.props.closeDesc(false);
  139. },
  140. componentWillMount() {
  141. this.addloadData()
  142. },
  143. componentWillReceiveProps(nextProps) { //props改变时触发
  144. this.state.visible = nextProps.showDesc;
  145. if (nextProps.userDetaile && nextProps.showDesc) {
  146. if (nextProps.datauser && nextProps.datauser.id) {
  147. this.loaduser(nextProps.datauser)
  148. }
  149. } else {
  150. this.setState({
  151. checkedKeys: [],
  152. datauser: {},
  153. roleName: '',
  154. roleType: '',
  155. ids: '',
  156. })
  157. }
  158. },
  159. //权限多选
  160. onExpand(expandedKeys) {
  161. this.setState({
  162. expandedKeys,
  163. autoExpandParent: false,
  164. });
  165. },
  166. onCheck(checkedKeys) {
  167. this.setState({ checkedKeys: checkedKeys });
  168. },
  169. onSelect(selectedKeys, info) {
  170. this.setState({ selectedKeys });
  171. },
  172. renderTreeNodes(data) {
  173. return data.map((item) => {
  174. if (item.children) {
  175. return (
  176. <TreeNodet title={item.name} key={item.id} dataRef={item}>
  177. {this.renderTreeNodes(item.children)}
  178. </TreeNodet>
  179. );
  180. }
  181. return <TreeNodet title={item.name} key={item.id} dataRef={item} />
  182. // return <TreeNodet {...item} />;
  183. });
  184. },
  185. tableRowClick(record, index) {
  186. },
  187. render() {
  188. const FormItem = Form.Item
  189. const formItemLayout = {
  190. labelCol: { span: 8 },
  191. wrapperCol: { span: 14 },
  192. };
  193. return (
  194. <div>
  195. <Modal maskClosable={false} visible={this.state.visible}
  196. onOk={this.handleOk} onCancel={this.handleCancel}
  197. width='1200px'
  198. title={!this.props.userDetaile ? '添加角色' : '编辑角色'}
  199. footer=''
  200. className="admin-desc-content">
  201. <Form layout="horizontal" onSubmit={this.handleSubmit} id="demand-form" style={{ paddingBottom: '40px' }} required="required">
  202. <Spin spinning={this.state.loading}>
  203. <div className="clearfix">
  204. <div className="clearfix">
  205. <FormItem className="half-item"
  206. {...formItemLayout}
  207. label="角色名称" >
  208. <Input placeholder="角色名称" value={this.state.roleName} onChange={(e) => { this.setState({ roleName: e.target.value }) }} style={{ width: '200px' }} required="required" />
  209. <span className="mandatory">*</span>
  210. </FormItem>
  211. <FormItem className="half-item"
  212. {...formItemLayout}
  213. label="" >
  214. <Button className="set-submit" type="primary" htmlType="submit">保存</Button>
  215. <Button className="set-submit" type="ghost" onClick={this.handleCancel}>返回</Button>
  216. </FormItem>
  217. <FormItem className="half-item"
  218. {...formItemLayout}
  219. label="权限编号" >
  220. <Input placeholder="权限编号" value={this.state.roleType} onChange={(e) => { this.setState({ roleType: e.target.value }) }} style={{ width: '200px' }} />
  221. </FormItem>
  222. </div>
  223. <div className="clearfix" style={{ marginLeft: '90px' }}>
  224. <Tree
  225. checkable
  226. onExpand={this.onExpand}
  227. checkStrictly={true}
  228. expandedKeys={this.state.expandedKeys}
  229. autoExpandParent={this.state.autoExpandParent}
  230. onCheck={this.onCheck}
  231. checkedKeys={this.state.checkedKeys}
  232. onSelect={this.onSelect}
  233. selectedKeys={this.state.selectedKeys}
  234. >
  235. {this.renderTreeNodes(this.state.Interface)}
  236. </Tree>
  237. </div>
  238. </div>
  239. </Spin>
  240. </Form >
  241. </Modal>
  242. </div>
  243. )
  244. }
  245. }));
  246. export default Newrole;