page.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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)} />}</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. changeCategoryData(id, checked) {
  169. const category = this.categoryMap[id];
  170. if (category.isData) {
  171. if (checked) return;
  172. } else if (!checked) return;
  173. Ready.editCategory({ id, isData: checked ? 1 : 0 })
  174. .then(() => {
  175. this.refreshCategory();
  176. });
  177. }
  178. changeCategoryOfficial(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, isOfficial: checked ? 1 : 0 })
  184. .then(() => {
  185. this.refreshCategory();
  186. });
  187. }
  188. changeCategoryOrder(from, to) {
  189. if (from.id === to.id) return;
  190. if (from.parentId !== to.parentId) {
  191. asyncSMessage('只允许同层排序', 'warn');
  192. return;
  193. }
  194. let parent = [];
  195. if (to.parentId === 0) {
  196. parent = this.categoryTree;
  197. } else {
  198. parent = this.categoryMap[to.parentId].children;
  199. }
  200. let oldIndex = -1;
  201. let newIndex = -1;
  202. parent.forEach((row, i) => {
  203. if (row.id === from.id) oldIndex = i;
  204. if (row.id === to.id) newIndex = i;
  205. });
  206. const others = parent.map(row => row.id);
  207. const tmp = others.splice(oldIndex, 1);
  208. if (newIndex === parent.length) {
  209. others.push(tmp[0]);
  210. } else {
  211. others.splice(newIndex, 0, tmp[0]);
  212. }
  213. Ready.editCategory({ id: from.id, index: newIndex === parent.length ? parent.length : newIndex })
  214. .then(() => {
  215. this.refreshCategory();
  216. });
  217. }
  218. categoryAction() {
  219. this.open({});
  220. }
  221. renderView() {
  222. return <Block flex>
  223. <FilterLayout
  224. show
  225. itemList={this.filterForm}
  226. data={this.state.search}
  227. onChange={data => {
  228. this.search(data);
  229. }} />
  230. <ActionLayout
  231. itemList={this.actionList}
  232. selectedKeys={this.state.selectedKeys}
  233. onAction={key => this.onAction(key)}
  234. />
  235. <TableLayout
  236. columns={this.tableSort(this.columns)}
  237. list={this.state.list}
  238. pagination={this.state.page}
  239. loading={this.props.core.loading}
  240. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  241. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  242. selectedKeys={this.state.selectedKeys}
  243. />
  244. {this.state.detail && <Modal visible closable title='目录结构' footer={null} onCancel={() => {
  245. this.close(false, 'detail');
  246. }} onOk={() => {
  247. this.close(true, 'detail');
  248. }}>
  249. <TreeLayout
  250. autoExpandParent
  251. defaultExpandAll
  252. itemList={this.state.categoryTree}
  253. selectable={false}
  254. draggable
  255. // onDragStart={({ event, node }) => console.log('start', event, node.props)}
  256. // onDragOver={({ event, node }) => console.log('over', event, node.props)}
  257. // onDragLeave={({ event, node }) => console.log('leave', event, node.kepropsy)}
  258. // onDragEnd={({ event, node }) => console.log('end', event, node.props)}
  259. // onDragEnter={({ event, node, expandedKeys }) => console.log('enter', event, node.props, expandedKeys)}
  260. onDrop={({ event, node, dragNode, dragNodesKeys }) => {
  261. console.log('drop', event, node.props, dragNode.props, dragNodesKeys);
  262. this.changeCategoryOrder(dragNode.props, node.props);
  263. }
  264. }
  265. />
  266. {/* <TableLayout
  267. rowKey={'id'}
  268. columns={this.categoryColumns}
  269. list={this.state.categoryList}
  270. pagination={false}
  271. /> */}
  272. {/* <DragList
  273. loading={this.props.core.loading}
  274. dataSource={this.state.categoryTree || []}
  275. handle={'.icon'}
  276. onMove={(oldIndex, newIndex) => {
  277. this.orderQuestion(oldIndex, newIndex);
  278. }}
  279. renderItem={(item) => (
  280. <List.Item actions={[<Icon type='bars' className='icon' />]}>
  281. <Row style={{ width: '100%' }}>
  282. <Col span={11}>标题: {item.title}</Col>
  283. </Row>
  284. <Row style={{ width: '100%' }}>
  285. <DragList
  286. loading={false}
  287. dataSource={item.children}
  288. handle={`.icon${item.id}`}
  289. onMove={(oldIndex, newIndex) => {
  290. this.orderQuestion(oldIndex, newIndex);
  291. }}
  292. renderItem={(row) => (
  293. <List.Item actions={[<Icon type='bars' className={`.icon${item.id}`} />]}>
  294. <Row style={{ width: '100%' }}>
  295. <Col span={11}>标题: {row.title}</Col>
  296. <Col span={5}><Switch checked={row.isData} checkedChildren='资料节点' unCheckedChildren='基本节点' onChange={() => this.changeCategoryData(row.id, !row.isData)} /></Col>
  297. <Col span={5}>{row.isData > 0 && <Switch checked={row.isOfficial} checkedChildren='官方资料' unCheckedChildren='非官方资料' onChange={() => this.changeCategoryOfficial(row.id, !row.isOfficial)} />}</Col>
  298. </Row>
  299. </List.Item>
  300. )} />
  301. </Row>
  302. </List.Item>
  303. )} /> */}
  304. </Modal>}
  305. </Block>;
  306. }
  307. }