newRole.jsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 {...item} />;
  182. });
  183. },
  184. tableRowClick(record, index) {
  185. },
  186. render() {
  187. const FormItem = Form.Item
  188. const formItemLayout = {
  189. labelCol: { span: 8 },
  190. wrapperCol: { span: 14 },
  191. };
  192. return(
  193. <div>
  194. <Modal maskClosable={false} visible={this.state.visible}
  195. onOk={this.handleOk} onCancel={this.handleCancel}
  196. width='1000px'
  197. title={!this.props.userDetaile?'添加角色':'编辑角色'}
  198. footer=''
  199. className="admin-desc-content">
  200. <Form layout="horizontal" onSubmit={this.handleSubmit} id="demand-form" style={{paddingBottom:'40px'}} required="required">
  201. <Spin spinning={this.state.loading}>
  202. <div className="clearfix">
  203. <div className="clearfix">
  204. <FormItem className="half-item"
  205. {...formItemLayout}
  206. label="角色名称" >
  207. <Input placeholder="角色名称" value={this.state.roleName} onChange={(e)=>{this.setState({roleName:e.target.value})}} style={{width:'200px'}} required="required"/>
  208. <span className="mandatory">*</span>
  209. </FormItem>
  210. <FormItem className="half-item"
  211. {...formItemLayout}
  212. label="" >
  213. <Button className="set-submit" type="primary" htmlType="submit">保存</Button>
  214. <Button className="set-submit" type="ghost" onClick={this.handleCancel}>返回</Button>
  215. </FormItem>
  216. <FormItem className="half-item"
  217. {...formItemLayout}
  218. label="权限编号" >
  219. <Input placeholder="权限编号" value={this.state.roleType} onChange={(e)=>{this.setState({roleType:e.target.value})}} style={{width:'200px'}}/>
  220. </FormItem>
  221. </div>
  222. <div className="clearfix" style={{marginLeft:'90px'}}>
  223. <Tree
  224. checkable
  225. onExpand={this.onExpand}
  226. checkStrictly={true}
  227. expandedKeys={this.state.expandedKeys}
  228. autoExpandParent={this.state.autoExpandParent}
  229. onCheck={this.onCheck}
  230. checkedKeys={this.state.checkedKeys}
  231. onSelect={this.onSelect}
  232. selectedKeys={this.state.selectedKeys}
  233. >
  234. {this.renderTreeNodes(this.state.Interface)}
  235. </Tree>
  236. </div>
  237. </div>
  238. </Spin>
  239. </Form >
  240. </Modal>
  241. </div>
  242. )
  243. }
  244. }));
  245. export default Newrole;