1
0

page.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import React from 'react';
  2. import { Tabs, Button, Icon } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import TreeLayout from '@src/layouts/TreeLayout';
  7. import ActionLayout from '@src/layouts/ActionLayout';
  8. import { formatTreeData } from '@src/services/Tools';
  9. import { asyncDelConfirm, asyncForm, asyncSMessage } from '@src/services/AsyncTools';
  10. // import { System } from '../../../stores/system';
  11. import { Examination } from '../../../stores/examination';
  12. import { Exercise } from '../../../stores/exercise';
  13. export default class extends Page {
  14. constructor(props) {
  15. super(props);
  16. this.exerciseActionList = [{
  17. key: 'addChild',
  18. name: '增加子节点',
  19. }, {
  20. key: 'addBrother',
  21. name: '增加兄弟节点',
  22. selectNum: 1,
  23. }, {
  24. key: 'edit',
  25. name: '编辑',
  26. selectNum: 1,
  27. }, {
  28. key: 'delete',
  29. name: '批量删除',
  30. type: 'danger',
  31. needCheck: 1,
  32. }];
  33. this.exerciseItemList = [{
  34. key: 'id',
  35. type: 'hidden',
  36. }, {
  37. key: 'parentId',
  38. type: 'tree',
  39. name: '父节点',
  40. tree: [],
  41. fieldNames: { label: 'title', value: 'value', children: 'children' },
  42. notFoundContent: null,
  43. onChange: (value) => {
  44. console.log(value);
  45. },
  46. }, {
  47. key: 'titleZh',
  48. name: '中文名称',
  49. type: 'input',
  50. placeholder: '请输入中文名称',
  51. required: true,
  52. }, {
  53. key: 'titleEn',
  54. name: '英文名称',
  55. type: 'input',
  56. placeholder: '请输入英文名称',
  57. required: true,
  58. }, {
  59. key: 'description',
  60. name: '描述',
  61. type: 'textarea',
  62. placeholder: '请输入描述',
  63. required: true,
  64. }];
  65. this.examinationActionList = [{
  66. key: 'addChild',
  67. name: '增加子节点',
  68. }, {
  69. key: 'addBrother',
  70. name: '增加兄弟节点',
  71. selectNum: 1,
  72. }, {
  73. key: 'edit',
  74. name: '编辑',
  75. selectNum: 1,
  76. }, {
  77. key: 'delete',
  78. name: '批量删除',
  79. type: 'danger',
  80. needCheck: 1,
  81. }];
  82. this.examinationItemList = [{
  83. key: 'id',
  84. type: 'hidden',
  85. }, {
  86. key: 'parentId',
  87. type: 'tree',
  88. name: '父节点',
  89. tree: [],
  90. fieldNames: { label: 'title', value: 'value', children: 'children' },
  91. notFoundContent: null,
  92. onChange: (value) => {
  93. console.log(value);
  94. },
  95. }, {
  96. key: 'titleZh',
  97. name: '中文名称',
  98. type: 'input',
  99. placeholder: '请输入中文名称',
  100. required: true,
  101. }, {
  102. key: 'titleEn',
  103. name: '英文名称',
  104. type: 'input',
  105. placeholder: '请输入英文名称',
  106. required: true,
  107. }, {
  108. key: 'description',
  109. name: '描述',
  110. type: 'textarea',
  111. placeholder: '请输入描述',
  112. required: true,
  113. }];
  114. this.state.tab = 'exercise';
  115. }
  116. initData() {
  117. this.refresh();
  118. }
  119. refresh() {
  120. const { tab } = this.state;
  121. if (tab === 'exercise') {
  122. return this.refreshExercise();
  123. }
  124. return this.refreshExamination();
  125. }
  126. refreshExercise() {
  127. Exercise.allStruct().then(result => {
  128. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  129. this.exerciseItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list), 'id', 'title', 'parentId');
  130. this.setState({
  131. exerciseList: list,
  132. exerciseStruct: formatTreeData(list.map(row => {
  133. if (row.level < 4) return row;
  134. row = Object.assign({}, row);
  135. row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  136. e.preventDefault();
  137. row.questionStatus = row.questionStatus > 0 ? 0 : 1;
  138. Exercise.editStruct(row).then(() => {
  139. this.refresh();
  140. });
  141. }}>{row.questionStatus > 0 ? [<Icon type='pause' />, <span>提问中</span>] : [<Icon type="caret-right" />, <span>提问关闭</span>]}</Button></div>;
  142. return row;
  143. }), 'id', 'title', 'parentId'),
  144. });
  145. });
  146. }
  147. refreshExamination() {
  148. Examination.allStruct().then(result => {
  149. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  150. this.examinationItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list), 'id', 'title', 'parentId');
  151. this.setState({
  152. examinationList: list,
  153. examinationStruct: formatTreeData(list.map(row => {
  154. if (row.level !== 2) return row;
  155. row = Object.assign({}, row);
  156. row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  157. e.preventDefault();
  158. row.payStatus = row.payStatus > 0 ? 0 : 1;
  159. Examination.editStruct(row).then(() => {
  160. this.refresh();
  161. });
  162. }}>{row.payStatus > 0 ? [<Icon type="pay-circle" />, <span>付费</span>] : [<Icon type="eye" />, <span>免费</span>]}</Button></div>;
  163. return row;
  164. }), 'id', 'title', 'parentId'),
  165. });
  166. });
  167. }
  168. addChildAction() {
  169. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  170. let itemList;
  171. let node = 0;
  172. if (tab === 'exercise') {
  173. itemList = this.exerciseItemList;
  174. if (selectedKeys.length > 0) {
  175. node = exerciseList.filter(row => row.id === Number(selectedKeys[0]))[0].id;
  176. }
  177. } else {
  178. itemList = this.examinationItemList;
  179. if (selectedKeys.length > 0) {
  180. node = examinationList.filter(row => row.id === Number(selectedKeys[0]))[0].id;
  181. }
  182. }
  183. asyncForm('新增', itemList, { parentId: `${node}` }, data => {
  184. console.log(data);
  185. let handler;
  186. if (tab === 'exercise') {
  187. handler = Exercise.addStruct(data);
  188. } else {
  189. handler = Examination.addStruct(data);
  190. }
  191. return handler.then(() => {
  192. asyncSMessage('新增成功!');
  193. this.refresh();
  194. });
  195. });
  196. }
  197. addBrotherAction() {
  198. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  199. let itemList;
  200. let node;
  201. if (tab === 'exercise') {
  202. itemList = this.exerciseItemList;
  203. if (selectedKeys.length > 0) {
  204. node = exerciseList.filter(row => row.id === Number(selectedKeys[0]))[0].parentId;
  205. }
  206. } else {
  207. itemList = this.examinationItemList;
  208. if (selectedKeys.length > 0) {
  209. node = examinationList.filter(row => row.id === Number(selectedKeys[0]))[0].parentId;
  210. }
  211. }
  212. asyncForm('新增', itemList, { parentId: `${node}` }, data => {
  213. let handler;
  214. if (tab === 'exercise') {
  215. handler = Exercise.addStruct(data);
  216. } else {
  217. handler = Examination.addStruct(data);
  218. }
  219. return handler.then(() => {
  220. asyncSMessage('新增成功!');
  221. this.refresh();
  222. });
  223. });
  224. }
  225. editAction() {
  226. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  227. let itemList;
  228. let list;
  229. if (tab === 'exercise') {
  230. itemList = this.exerciseItemList;
  231. list = exerciseList;
  232. } else {
  233. itemList = this.examinationItemList;
  234. list = examinationList;
  235. }
  236. asyncForm('编辑', itemList, list.filter(row => row.id === Number(selectedKeys[0])).map(row => {
  237. row.parentId = `${row.parentId}`;
  238. return row;
  239. })[0], data => {
  240. let handler;
  241. if (tab === 'exercise') {
  242. handler = Exercise.editStruct(data);
  243. } else {
  244. handler = Examination.editStruct(data);
  245. }
  246. return handler.then(() => {
  247. asyncSMessage('编辑成功!');
  248. this.refresh();
  249. });
  250. });
  251. }
  252. deleteAction() {
  253. const { tab, checkedKeys } = this.state;
  254. asyncDelConfirm('删除确认', '是否删除选中节点?', () => {
  255. if (tab === 'exercise') {
  256. return Promise.all(checkedKeys.map(row => Examination.delStruct({ id: row })));
  257. }
  258. return Promise.all(checkedKeys.map(row => Examination.delStruct({ id: row })));
  259. }).then(() => {
  260. asyncSMessage('删除成功!');
  261. this.refresh();
  262. });
  263. }
  264. renderExamination() {
  265. const { loading } = this.state;
  266. return <Block loading={loading}>
  267. <ActionLayout
  268. itemList={this.exerciseActionList}
  269. selectedKeys={this.state.selectedKeys}
  270. checkedKeys={this.state.checkedKeys}
  271. onAction={key => this.onAction(key)} />
  272. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.examinationStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  273. this.setState({ selectedKeys });
  274. }} checkedKeys={this.state.checkedKeys} onCheck={(checkedKeys) => {
  275. this.setState({ checkedKeys });
  276. }} />
  277. </Block>;
  278. }
  279. renderExercise() {
  280. const { loading } = this.state;
  281. return <Block loading={loading}>
  282. <ActionLayout
  283. itemList={this.exerciseActionList}
  284. selectedKeys={this.state.selectedKeys}
  285. checkedKeys={this.state.checkedKeys}
  286. onAction={key => this.onAction(key)} />
  287. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.exerciseStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  288. this.setState({ selectedKeys });
  289. }} checkedKeys={this.state.checkedKeys} onCheck={(checkedKeys) => {
  290. this.setState({ checkedKeys });
  291. }} />
  292. </Block>;
  293. }
  294. renderView() {
  295. const { tab } = this.state;
  296. return <Block><Tabs activeKey={tab} onChange={(value) => {
  297. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  298. this.refresh();
  299. }}>
  300. <Tabs.TabPane tab="练习结构" key="exercise">
  301. {this.renderExercise()}
  302. </Tabs.TabPane>
  303. <Tabs.TabPane tab="模考结构" key="examination">
  304. {this.renderExamination()}
  305. </Tabs.TabPane>
  306. </Tabs></Block>;
  307. }
  308. }