role.jsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import React from 'react';
  2. import { Table, Button, Input, Spin, Message, Select, Modal, Tag, Icon } from 'antd';
  3. import ajax from 'jquery/src/ajax/xhr.js'
  4. import $ from 'jquery/src/ajax';
  5. const Role = React.createClass({
  6. getInitialState() {
  7. return {
  8. visible: false,
  9. disabled: false,
  10. name: '',
  11. selectOption: [],
  12. data: [],
  13. selectedRowKeys: [],
  14. selectedRows: [],
  15. loading: false,
  16. permissions: {
  17. },
  18. modelRecord: {
  19. id: '',
  20. name: '',
  21. permissions: []
  22. }
  23. }
  24. },
  25. componentWillMount() {
  26. this.loadData();
  27. },
  28. loadData() {
  29. let _me = this;
  30. this.setState({
  31. loading: true
  32. });
  33. $.when($.ajax({
  34. url: globalConfig.context + '/api/roles',
  35. method: 'get',
  36. cache: false
  37. }), $.ajax({
  38. url: globalConfig.context + '/api/permissions',
  39. method: 'get',
  40. cache: false
  41. })).done((roles, permissions) => {
  42. if (!roles[0].error.length && roles[0].data) {
  43. this.state.data = [];
  44. for (let i = 0; i < roles[0].data.length; i++) {
  45. let thisdata = roles[0].data[i];
  46. this.state.data.push({
  47. key: i,
  48. id: thisdata.id,
  49. roleName: thisdata.roleName,
  50. permissions: thisdata.permissions.map((p) => { return String(p.id) }) || []
  51. });
  52. };
  53. }
  54. if (!permissions[0].error.length && permissions[0].data) {
  55. _me.state.selectOption = [];
  56. permissions[0].data.map(function (item) {
  57. _me.state.selectOption.push(
  58. <Select.Option key={item.id}>{item.name}</Select.Option>
  59. )
  60. _me.state.permissions[String(item.id)] = item.name;
  61. });
  62. }
  63. }).always(() => {
  64. this.setState({
  65. loading: false
  66. });
  67. });
  68. },
  69. save() {
  70. this.setState({
  71. loading: true
  72. });
  73. $.ajax({
  74. url: globalConfig.context + '/api/role',
  75. method: 'post',
  76. data: { data: JSON.stringify(this.state.modelRecord) },
  77. }).done((res) => {
  78. if (!res.error.length) {
  79. Message.success("保存成功");
  80. this.loadData();
  81. } else {
  82. Message.error(res.error[0].message);
  83. }
  84. }).always(() => {
  85. this.setState({
  86. loading: false,
  87. visible: false,
  88. disabled: false
  89. });
  90. })
  91. },
  92. remove() {
  93. let deletedIds = [];
  94. for (let idx = 0; idx < this.state.selectedRows.length; idx++) {
  95. let rowItem = this.state.selectedRows[idx];
  96. if (rowItem.id) {
  97. deletedIds.push(rowItem.id)
  98. }
  99. };
  100. this.setState({
  101. loading: deletedIds.length > 0
  102. });
  103. if (deletedIds.length) {
  104. $.ajax({
  105. url: globalConfig.context + '/api/role/delete',
  106. method: 'post',
  107. data: {
  108. data: JSON.stringify(deletedIds)
  109. }
  110. }).done((res) => {
  111. if (!res.error.length) {
  112. Message.success("删除成功");
  113. this.state.selectedRowKeys.sort((a, b) => { return b - a });
  114. for (let idx = 0; idx < this.state.selectedRowKeys.length; idx++) {
  115. let dataIndex = this.state.selectedRowKeys[idx];
  116. this.state.data.splice(dataIndex, 1);
  117. };
  118. this.setState({
  119. data: this.state.data,
  120. selectedRowKeys: []
  121. });
  122. } else {
  123. Message.error(res.error[0].message);
  124. }
  125. }).always(() => {
  126. this.setState({
  127. loading: false
  128. });
  129. })
  130. }
  131. },
  132. addNew() {
  133. this.setState({
  134. visible: true,
  135. modelRecord: {
  136. id: null,
  137. roleName: '',
  138. permissions: []
  139. }
  140. })
  141. },
  142. edit(e) {
  143. this.setState({
  144. modelRecord: {
  145. id: e.id,
  146. roleName: e.roleName,
  147. permissions: e.permissions
  148. },
  149. visible: true,
  150. disabled: false
  151. })
  152. },
  153. handleCancel() {
  154. this.setState({
  155. visible: false,
  156. disabled: false
  157. })
  158. },
  159. render() {
  160. const columns = [{
  161. title: '名字',
  162. dataIndex: 'roleName',
  163. key: 'roleName'
  164. }, {
  165. title: '绑定权限',
  166. dataIndex: 'permissions',
  167. key: 'permissions',
  168. render: (text, record, index) => {
  169. let _me = this;
  170. if (record.permissions) {
  171. return <div>
  172. {record.permissions.map((tag, i) => {
  173. if (i < 3) {
  174. return <Tag key={tag} closable={false}>
  175. {_me.state.permissions[tag]}
  176. </Tag>
  177. } else if (i == 3) {
  178. return <Tag key={tag} closable={false}>
  179. ...
  180. </Tag>
  181. } else {
  182. return false;
  183. }
  184. }
  185. )}
  186. </div>
  187. } else {
  188. return <div></div>;
  189. }
  190. },
  191. }];
  192. const rowSelection = {
  193. type: 'checkbox',
  194. selectedRowKeys: this.state.selectedRowKeys,
  195. onChange: (selectedRowKeys, selectedRows) => {
  196. this.setState({
  197. selectedRows: selectedRows,
  198. selectedRowKeys: selectedRowKeys
  199. });
  200. }
  201. };
  202. const hasSelected = this.state.selectedRowKeys.length > 0;
  203. return (
  204. <Spin spinning={this.state.loading}>
  205. <Modal maskClosable={false} title="角色编辑"
  206. closable={false}
  207. visible={this.state.visible}
  208. onOk={this.save}
  209. width={1000}
  210. onCancel={this.handleCancel}>
  211. <ul className="modal-content">
  212. <li>
  213. <span className='modal-text'>名字</span>
  214. <Input value={this.state.modelRecord.roleName} onChange={(e) => {
  215. this.state.modelRecord.roleName = e.target.value;
  216. this.setState({
  217. modelRecord: this.state.modelRecord
  218. })
  219. }} />
  220. </li>
  221. <li>
  222. <span className='modal-text'>权限</span>
  223. <Select
  224. multiple
  225. style={{ width: '80%' }}
  226. placeholder="选择绑定权限"
  227. disabled={!this.state.modelRecord.id}
  228. filterOption={(input, option) => { return option.props.children.indexOf(input) >= 0 }}
  229. value={this.state.modelRecord.permissions}
  230. onChange={(pids) => {
  231. this.state.modelRecord.permissions = pids;
  232. this.setState({
  233. modelRecord: this.state.modelRecord
  234. })
  235. }}
  236. >
  237. {this.state.selectOption}
  238. </Select>
  239. </li>
  240. </ul>
  241. </Modal>
  242. <div className="set-content">
  243. <div className="set-title">
  244. <span>角色控制</span>
  245. <Button style={{ background: "#ea0862", border: "none", color: "#fff" }}
  246. onClick={this.addNew}>添加<Icon type="plus" /></Button>
  247. <Button style={{ background: "#3fcf9e", border: "none", color: "#fff" }}
  248. disabled={!hasSelected}
  249. onClick={this.remove}>删除<Icon type="minus" /></Button>
  250. </div>
  251. <Table size="middle" className='member-table'
  252. columns={columns}
  253. dataSource={this.state.data}
  254. onRowClick={this.edit}
  255. style={{
  256. cursor: 'pointer',
  257. }}
  258. pagination={false}
  259. rowSelection={rowSelection} />
  260. </div>
  261. </Spin>
  262. );
  263. }
  264. });
  265. export default Role;