page.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { Button, Modal, Checkbox, Row, Col, Switch } from 'antd';
  4. import './index.less';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. // import DragList from '@src/components/DragList';
  8. import FilterLayout from '@src/layouts/FilterLayout';
  9. import TreeLayout from '@src/layouts/TreeLayout';
  10. import ActionLayout from '@src/layouts/ActionLayout';
  11. import TableLayout from '@src/layouts/TableLayout';
  12. import { formatDate, getMap, formatTreeData } from '@src/services/Tools';
  13. import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
  14. // import { ArticleChannel } from '../../../../Constant';
  15. import { Ready } from '../../../stores/ready';
  16. // const ArticleChannelMap = getMap(ArticleChannel, 'value', 'label');
  17. export default class extends Page {
  18. init() {
  19. this.categoryTitleMap = {};
  20. this.categoryMap = {};
  21. this.categoryList = [];
  22. this.categoryTree = [];
  23. this.categoryColumns = [{
  24. dataIndex: 'parentId',
  25. title: '一级标题',
  26. render: (text, record) => {
  27. if (text) {
  28. return this.categoryMap[text];
  29. }
  30. return record.title;
  31. },
  32. }, {
  33. dataIndex: 'title',
  34. title: '二级标题',
  35. render: (text, record) => {
  36. if (record.parentId) {
  37. return record.title;
  38. }
  39. return '-';
  40. },
  41. }, {
  42. dataIndex: 'isData',
  43. title: '资料节点',
  44. render: (text, record) => {
  45. return record.parentId > 0 && <Checkbox onChange={(e) => {
  46. this.changeCategoryData(record.id, e.target.checked);
  47. }} checked={!!text} />;
  48. },
  49. }, {
  50. dataIndex: 'isOfficial',
  51. title: '官方资料',
  52. render: (text, record) => {
  53. return record.parentId > 0 && record.isData > 0 && <Checkbox onChange={(e) => {
  54. this.changeCategoryOfficial(record.id, e.target.checked);
  55. }} checked={!!text} />;
  56. },
  57. }];
  58. this.filterForm = [{
  59. key: 'parentCategoryId',
  60. type: 'select',
  61. allowClear: true,
  62. name: '一级标题',
  63. placeholder: '请选择',
  64. select: [],
  65. number: true,
  66. onChange: (value) => {
  67. this.changeSearch(this.filterForm, this, value);
  68. },
  69. }, {
  70. key: 'categoryId',
  71. type: 'select',
  72. allowClear: true,
  73. name: '二级标题',
  74. select: [],
  75. number: true,
  76. placeholder: '请选择',
  77. }];
  78. this.actionList = [{
  79. key: 'add',
  80. type: 'primary',
  81. name: '创建',
  82. render: (item) => {
  83. return <Link to='/ready/article/detail'><Button>{item.name}</Button></Link>;
  84. },
  85. }, {
  86. key: 'category',
  87. name: '目录',
  88. }];
  89. this.columns = [{
  90. title: '一级标题',
  91. dataIndex: 'parentCategoryId',
  92. render: (text) => {
  93. return this.categoryTitleMap[text];
  94. },
  95. }, {
  96. title: '二级标题',
  97. dataIndex: 'categoryId',
  98. render: (text) => {
  99. return this.categoryTitleMap[text];
  100. },
  101. }, {
  102. title: '文章标题',
  103. dataIndex: 'title',
  104. }, {
  105. title: '更新时间',
  106. dataIndex: 'updateTime',
  107. render: (text) => {
  108. return formatDate(text);
  109. },
  110. }, {
  111. title: '操作',
  112. dataIndex: 'handler',
  113. render: (text, record) => {
  114. return <div className="table-button">
  115. {<Link to={`/ready/article/detail/${record.id}`}>编辑</Link>}
  116. {<a onClick={() => {
  117. this.deleteAction(record);
  118. }}>删除</a>}
  119. </div>;
  120. },
  121. }];
  122. this.refreshCategory();
  123. }
  124. refreshCategory() {
  125. Ready.allCategory().then(result => {
  126. this.categoryTitleMap = getMap(result, 'id', 'title');
  127. this.categoryList = result.map(row => {
  128. row.value = row.id;
  129. row.title = <Row style={{ width: 400 }}>
  130. <Col span={11}>{row.title}</Col>
  131. <Col span={5}>{row.parentId > 0 && <Switch checked={row.isData} checkedChildren='资料节点' unCheckedChildren='基本节点' onChange={() => this.changeCategoryData(row.id, !row.isData)} />}{row.parentId === 0 && <Switch checked={row.isRoom} checkedChildren='考场节点' unCheckedChildren='非考场' onChange={() => this.changeCategoryRoom(row.id, !row.isRoom)} />}</Col>
  132. <Col span={5}>{row.parentId > 0 && row.isData > 0 && <Switch checked={row.isOfficial} checkedChildren='官方资料' unCheckedChildren='非官方资料' onChange={() => this.changeCategoryOfficial(row.id, !row.isOfficial)} />}</Col>
  133. </Row>;
  134. return row;
  135. });
  136. this.categoryTree = formatTreeData(result, 'id', 'title', 'parentId');
  137. this.categoryMap = getMap(result, 'id');
  138. this.filterForm[0].select = this.categoryList.filter(row => row.parentId === 0);
  139. this.changeSearch(this.filterForm, this, this.state.search.parentCategoryId);
  140. this.initData();
  141. this.setState({ categoryList: this.categoryList, categoryTree: this.categoryTree });
  142. });
  143. }
  144. changeSearch(list, component, value) {
  145. if (value) {
  146. list[1].disabled = false;
  147. list[1].select = this.categoryList.filter(row => row.parentId === value);
  148. } else {
  149. list[1].disabled = true;
  150. list[1].select = [];
  151. }
  152. component.setState({ load: false });
  153. }
  154. initData() {
  155. Ready.listArticle(this.state.search).then(result => {
  156. this.setTableData(result.list, result.total);
  157. });
  158. }
  159. deleteAction(row) {
  160. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  161. const handler = Ready.delArticle({ id: row.id });
  162. return handler.then(() => {
  163. asyncSMessage('删除成功!');
  164. this.refresh();
  165. });
  166. });
  167. }
  168. changeCategoryRoom(id, checked) {
  169. const category = this.categoryMap[id];
  170. if (category.isRoom) {
  171. if (checked) return;
  172. } else if (!checked) return;
  173. Ready.editCategory({ id, isRoom: checked ? 1 : 0, isData: 0 })
  174. .then(() => {
  175. this.refreshCategory();
  176. });
  177. }
  178. changeCategoryData(id, checked) {
  179. const category = this.categoryMap[id];
  180. if (category.isData) {
  181. if (checked) return;
  182. } else if (!checked) return;
  183. Ready.editCategory({ id, isData: checked ? 1 : 0, isRoom: 0 })
  184. .then(() => {
  185. this.refreshCategory();
  186. });
  187. }
  188. changeCategoryOfficial(id, checked) {
  189. const category = this.categoryMap[id];
  190. if (category.isData) {
  191. if (checked) return;
  192. } else if (!checked) return;
  193. Ready.editCategory({ id, isOfficial: checked ? 1 : 0 })
  194. .then(() => {
  195. this.refreshCategory();
  196. });
  197. }
  198. changeCategoryOrder(from, to) {
  199. if (from.id === to.id) return;
  200. if (from.parentId !== to.parentId) {
  201. asyncSMessage('只允许同层排序', 'warn');
  202. return;
  203. }
  204. let parent = [];
  205. if (to.parentId === 0) {
  206. parent = this.categoryTree;
  207. } else {
  208. parent = this.categoryMap[to.parentId].children;
  209. }
  210. let oldIndex = -1;
  211. let newIndex = -1;
  212. parent.forEach((row, i) => {
  213. if (row.id === from.id) oldIndex = i;
  214. if (row.id === to.id) newIndex = i;
  215. });
  216. const others = parent.map(row => row.id);
  217. const tmp = others.splice(oldIndex, 1);
  218. if (newIndex === parent.length) {
  219. others.push(tmp[0]);
  220. } else {
  221. others.splice(newIndex, 0, tmp[0]);
  222. }
  223. Ready.editCategory({ id: from.id, index: newIndex === parent.length ? parent.length : newIndex })
  224. .then(() => {
  225. this.refreshCategory();
  226. });
  227. }
  228. categoryAction() {
  229. this.open({});
  230. }
  231. renderView() {
  232. return <Block flex>
  233. <FilterLayout
  234. show
  235. itemList={this.filterForm}
  236. data={this.state.search}
  237. onChange={data => {
  238. this.search(data);
  239. }} />
  240. <ActionLayout
  241. itemList={this.actionList}
  242. selectedKeys={this.state.selectedKeys}
  243. onAction={key => this.onAction(key)}
  244. />
  245. <TableLayout
  246. columns={this.tableSort(this.columns)}
  247. list={this.state.list}
  248. pagination={this.state.page}
  249. loading={this.props.core.loading}
  250. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  251. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  252. selectedKeys={this.state.selectedKeys}
  253. />
  254. {this.state.detail && <Modal visible closable title='目录结构' footer={null} onCancel={() => {
  255. this.close(false, 'detail');
  256. }} onOk={() => {
  257. this.close(true, 'detail');
  258. }}>
  259. <TreeLayout
  260. autoExpandParent
  261. defaultExpandAll
  262. itemList={this.state.categoryTree}
  263. selectable={false}
  264. draggable
  265. // onDragStart={({ event, node }) => console.log('start', event, node.props)}
  266. // onDragOver={({ event, node }) => console.log('over', event, node.props)}
  267. // onDragLeave={({ event, node }) => console.log('leave', event, node.kepropsy)}
  268. // onDragEnd={({ event, node }) => console.log('end', event, node.props)}
  269. // onDragEnter={({ event, node, expandedKeys }) => console.log('enter', event, node.props, expandedKeys)}
  270. onDrop={({ event, node, dragNode, dragNodesKeys }) => {
  271. console.log('drop', event, node.props, dragNode.props, dragNodesKeys);
  272. this.changeCategoryOrder(dragNode.props, node.props);
  273. }
  274. }
  275. />
  276. {/* <TableLayout
  277. rowKey={'id'}
  278. columns={this.categoryColumns}
  279. list={this.state.categoryList}
  280. pagination={false}
  281. /> */}
  282. {/* <DragList
  283. loading={this.props.core.loading}
  284. dataSource={this.state.categoryTree || []}
  285. handle={'.icon'}
  286. onMove={(oldIndex, newIndex) => {
  287. this.orderQuestion(oldIndex, newIndex);
  288. }}
  289. renderItem={(item) => (
  290. <List.Item actions={[<Icon type='bars' className='icon' />]}>
  291. <Row style={{ width: '100%' }}>
  292. <Col span={11}>标题: {item.title}</Col>
  293. </Row>
  294. <Row style={{ width: '100%' }}>
  295. <DragList
  296. loading={false}
  297. dataSource={item.children}
  298. handle={`.icon${item.id}`}
  299. onMove={(oldIndex, newIndex) => {
  300. this.orderQuestion(oldIndex, newIndex);
  301. }}
  302. renderItem={(row) => (
  303. <List.Item actions={[<Icon type='bars' className={`.icon${item.id}`} />]}>
  304. <Row style={{ width: '100%' }}>
  305. <Col span={11}>标题: {row.title}</Col>
  306. <Col span={5}><Switch checked={row.isData} checkedChildren='资料节点' unCheckedChildren='基本节点' onChange={() => this.changeCategoryData(row.id, !row.isData)} /></Col>
  307. <Col span={5}>{row.isData > 0 && <Switch checked={row.isOfficial} checkedChildren='官方资料' unCheckedChildren='非官方资料' onChange={() => this.changeCategoryOfficial(row.id, !row.isOfficial)} />}</Col>
  308. </Row>
  309. </List.Item>
  310. )} />
  311. </Row>
  312. </List.Item>
  313. )} /> */}
  314. </Modal>}
  315. </Block>;
  316. }
  317. }