permission.jsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import React from 'react';
  2. import { Table, Button, Input, Spin, Message, Icon } from 'antd';
  3. import ajax from 'jquery/src/ajax/xhr.js';
  4. import $ from 'jquery/src/ajax';
  5. const Permission = React.createClass({
  6. getInitialState() {
  7. return {
  8. data: [],
  9. selectedRowKeys: [],
  10. selectedRows: [],
  11. loading: false
  12. }
  13. },
  14. componentDidMount() {
  15. this.loadData();
  16. },
  17. loadData() {
  18. this.setState({
  19. loading: true
  20. });
  21. $.ajax({
  22. url: globalConfig.context + '/api/permissions',
  23. method: 'get',
  24. cache: false
  25. }).done((res) => {
  26. if (!res.error.length) {
  27. this.setState({
  28. data: res.data
  29. })
  30. }
  31. }).always(() => {
  32. this.setState({
  33. loading: false
  34. });
  35. });
  36. },
  37. save() {
  38. this.setState({
  39. loading: true
  40. });
  41. $.ajax({
  42. url: globalConfig.context + '/api/permission',
  43. method: 'post',
  44. data: {
  45. data: JSON.stringify(this.state.data)
  46. }
  47. }).done((res) => {
  48. if (!res.error.length) {
  49. Message.success("保存成功");
  50. this.loadData();
  51. } else {
  52. Message.error(res.error[0].message);
  53. }
  54. }).always(() => {
  55. this.setState({
  56. loading: false
  57. });
  58. })
  59. },
  60. remove() {
  61. let deletedIds = [];
  62. for (let idx = 0; idx < this.state.selectedRows.length; idx++) {
  63. let rowItem = this.state.selectedRows[idx];
  64. if (rowItem.id) {
  65. deletedIds.push(rowItem.id)
  66. }
  67. };
  68. this.setState({
  69. loading: deletedIds.length > 0
  70. });
  71. if (deletedIds.length) {
  72. $.ajax({
  73. url: globalConfig.context + '/api/permission/delete',
  74. method: 'post',
  75. data: {
  76. data: JSON.stringify(deletedIds)
  77. }
  78. }).done((res) => {
  79. if (!res.error.length) {
  80. Message.success("删除成功");
  81. this.state.selectedRowKeys.sort((a, b) => { return b - a });
  82. for (let idx = 0; idx < this.state.selectedRowKeys.length; idx++) {
  83. let dataIndex = this.state.selectedRowKeys[idx];
  84. this.state.data.splice(dataIndex, 1);
  85. };
  86. this.setState({
  87. data: this.state.data,
  88. selectedRowKeys: []
  89. });
  90. } else {
  91. Message.error(res.error[0].message);
  92. }
  93. }).always(() => {
  94. this.setState({
  95. loading: false
  96. });
  97. })
  98. }
  99. },
  100. addNew() {
  101. this.state.data.push({
  102. id: null,
  103. name: '',
  104. url: ''
  105. });
  106. this.setState({
  107. data: this.state.data
  108. })
  109. },
  110. render() {
  111. const columns = [{
  112. title: '权限名字',
  113. dataIndex: 'name',
  114. key: 'name',
  115. render: (text, record, index) => {
  116. return <Input value={record.name} onChange={(e) => { record.name = e.target.value; this.setState({ data: this.state.data }); }} />
  117. }
  118. }, {
  119. title: '接口路径',
  120. dataIndex: 'url',
  121. key: 'url',
  122. render: (text, record, index) => {
  123. return <Input value={record.url} onChange={(e) => { record.url = e.target.value; this.setState({ data: this.state.data }); }} />
  124. }
  125. }];
  126. const rowSelection = {
  127. type: 'checkbox',
  128. selectedRowKeys: this.state.selectedRowKeys,
  129. onChange: (selectedRowKeys, selectedRows) => {
  130. this.setState({
  131. selectedRows: selectedRows,
  132. selectedRowKeys: selectedRowKeys
  133. });
  134. }
  135. };
  136. const hasSelected = this.state.selectedRowKeys.length > 0;
  137. return (
  138. <Spin spinning={this.state.loading}>
  139. <div className="set-content">
  140. <div className="set-title">
  141. <span>权限控制</span>
  142. <Button style={{ background: "#ea0862", border: "none", color: "#fff" }}
  143. onClick={this.addNew}>添加<Icon type="plus" /></Button>
  144. <Button style={{ background: "#3fcf9e", border: "none", color: "#fff" }}
  145. disabled={!hasSelected}
  146. onClick={this.remove}>删除<Icon type="minus" /></Button>
  147. <Button type="primary" onClick={this.save}>保存修改</Button>
  148. </div>
  149. <Table size="middle" className="member-table"
  150. columns={columns}
  151. dataSource={this.state.data}
  152. pagination={false}
  153. rowSelection={rowSelection} />
  154. </div>
  155. </Spin>
  156. );
  157. }
  158. });
  159. export default Permission;