page.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import React from 'react';
  2. import { Row, Button, Switch, Col, List, Icon } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import FilterLayout from '@src/layouts/FilterLayout';
  7. import ActionLayout from '@src/layouts/ActionLayout';
  8. import TableLayout from '@src/layouts/TableLayout';
  9. import DragList from '@src/components/DragList';
  10. import { getMap, formatDate, bindSearch, formatTreeData, flattenTree } from '@src/services/Tools';
  11. import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
  12. import { FaqChannel, SystemSelect } from '../../../../Constant';
  13. import { System } from '../../../stores/system';
  14. import { Course } from '../../../stores/course';
  15. const SystemSelectMap = getMap(SystemSelect, 'value', 'label');
  16. const FaqChannelTree = formatTreeData(FaqChannel, 'value', 'label', 'parent');
  17. const FaqChannelFlatten = flattenTree(FaqChannelTree, (row, item) => {
  18. row = Object.assign({}, row);
  19. row.value = `${item.value}-${row.value}`;
  20. row.label = `${item.label}-${row.label}`;
  21. return row;
  22. }, 'children');
  23. const FaqChannelMap = getMap(FaqChannelFlatten, 'value', 'label');
  24. export default class extends Page {
  25. init() {
  26. this.actionList = [{
  27. key: 'add',
  28. type: 'primary',
  29. name: '创建',
  30. }, {
  31. key: 'switch',
  32. name: '切换模式',
  33. render: () => {
  34. const { channel, position } = this.state.search;
  35. let d = false;
  36. if (channel === 'course-video' || channel === 'course-vs' || channel === 'course-package' || channel === 'course_data') {
  37. d = !position;
  38. } else {
  39. d = !channel;
  40. }
  41. return <Switch disabled={d} checked={this.state.mode === 'order'} checkedChildren='排序模式' unCheckedChildren='列表模式' onChange={(value) => {
  42. this.changeModel(value);
  43. }} />;
  44. },
  45. }];
  46. this.formF = null;
  47. this.itemList = [{
  48. key: 'id',
  49. type: 'hidden',
  50. }, {
  51. key: 'channel',
  52. type: 'cascader',
  53. name: '频道',
  54. select: FaqChannelTree,
  55. placeholder: '请选择',
  56. onChange: (value) => {
  57. this.changeSearch(this.itemList, this.formF, value.join('-'), null, 2);
  58. },
  59. }, {
  60. key: 'position',
  61. type: 'select',
  62. name: '位置',
  63. select: [],
  64. placeholder: '请选择',
  65. }, {
  66. key: 'content',
  67. type: 'textarea',
  68. name: '用户留言',
  69. }, {
  70. key: 'answer',
  71. type: 'textarea',
  72. name: '编辑回复',
  73. }];
  74. this.filterF = null;
  75. this.filterForm = [{
  76. key: 'channel',
  77. type: 'cascader',
  78. allowClear: true,
  79. name: '频道',
  80. select: formatTreeData(FaqChannel, 'value', 'label', 'parent'),
  81. placeholder: '请选择',
  82. onChange: (value) => {
  83. this.changeSearch(this.filterForm, this, value.join('-'), null);
  84. },
  85. }, {
  86. key: 'position',
  87. type: 'select',
  88. allowClear: true,
  89. name: '位置',
  90. number: true,
  91. select: [],
  92. placeholder: '请选择',
  93. }, {
  94. key: 'isSystem',
  95. type: 'select',
  96. allowClear: true,
  97. number: true,
  98. name: '来源',
  99. select: SystemSelect,
  100. }];
  101. this.columns = [{
  102. title: '频道',
  103. dataIndex: 'channel',
  104. render: (text, record) => {
  105. return FaqChannelMap[record.channel];
  106. },
  107. }, {
  108. title: '位置',
  109. dataIndex: 'positionDetail.title',
  110. }, {
  111. title: '创建时间',
  112. sorter: true,
  113. dataIndex: 'createTime',
  114. render: (text) => {
  115. return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
  116. },
  117. }, {
  118. title: '来源',
  119. dataIndex: 'isSystem',
  120. render: (text) => {
  121. return SystemSelectMap[text];
  122. },
  123. }, {
  124. title: '操作',
  125. dataIndex: 'handler',
  126. render: (text, record) => {
  127. return <div className="table-button">
  128. {(
  129. <a onClick={() => {
  130. this.editAction(record);
  131. }}>编辑</a>
  132. )}
  133. </div>;
  134. },
  135. }];
  136. this.changeSearch(this.filterForm, this, this.state.search.channel, this.state.search.position);
  137. }
  138. changeSearch(list, component, key, value, index = 1) {
  139. if (key === 'course-video' || key === 'course-vs' || key === 'course-package' || key === 'course_data') {
  140. bindSearch(list, 'position', component, (search) => {
  141. if (key === 'course-video') {
  142. return Course.list(Object.assign({ courseModule: 'video' }, search));
  143. } if (key === 'course-vs') {
  144. return Course.list(Object.assign({ courseModule: 'vs' }, search));
  145. } if (key === 'course-package') {
  146. return Course.listPackage(search);
  147. }
  148. return Course.listData(search);
  149. }, (row) => {
  150. return {
  151. title: row.title,
  152. value: row.id,
  153. };
  154. }, value ? Number(value) : null, null);
  155. list[index].disabled = false;
  156. } else {
  157. list[index].disabled = true;
  158. }
  159. component.setState({ load: false });
  160. }
  161. initData() {
  162. if (!this.state.search.order) {
  163. this.state.search.order = 'sort';
  164. this.state.search.direction = 'desc';
  165. }
  166. System.listFAQ(Object.assign({ isSpecial: true }, this.state.search)).then(result => {
  167. this.setTableData(result.list, result.total);
  168. });
  169. }
  170. changeModel(value) {
  171. const { search, page } = this.state;
  172. if (value) {
  173. search.order = 'sort';
  174. search.direction = 'desc';
  175. search.size = page.total;
  176. search.isSystem = null;
  177. this.setState({ mode: 'order', search });
  178. } else {
  179. search.size = 20;
  180. search.order = null;
  181. search.direction = null;
  182. this.setState({ mode: null, search });
  183. }
  184. this.initData();
  185. }
  186. addAction() {
  187. asyncForm('创建', this.itemList, {}, data => {
  188. data.isShow = 1;
  189. data.isSystem = 1;
  190. data.isSpecial = 1;
  191. data.channel = data.channel.join('-');
  192. return System.addFAQ(data).then(() => {
  193. asyncSMessage('添加成功!');
  194. this.refresh();
  195. });
  196. }).then(component => {
  197. this.formF = component;
  198. this.changeSearch(this.itemList, this.formF, null, null, 2);
  199. });
  200. }
  201. editAction(row) {
  202. const info = Object.assign({}, row);
  203. info.channel = info.channel.split('-');
  204. asyncForm('编辑', this.itemList, info, data => {
  205. data.isShow = 1;
  206. data.isSystem = 1;
  207. data.isSpecial = 1;
  208. data.channel = data.channel.join('-');
  209. return System.editFAQ(data).then(() => {
  210. asyncSMessage('编辑成功!');
  211. this.refresh();
  212. });
  213. }).then(component => {
  214. this.formF = component;
  215. this.changeSearch(this.itemList, this.formF, row.channel, row.position, 2);
  216. });
  217. }
  218. show(row, isShow) {
  219. System.editFAQ({ id: row.id, isShow }).then(() => {
  220. asyncSMessage('编辑成功!');
  221. this.refresh();
  222. });
  223. }
  224. order(oldIndex, newIndex) {
  225. const { list } = this.state;
  226. const tmp = list.splice(oldIndex, 1);
  227. if (newIndex === list.length) {
  228. list.push(tmp[0]);
  229. } else {
  230. list.splice(newIndex, 0, tmp[0]);
  231. }
  232. this.setState({ list });
  233. }
  234. submit() {
  235. const { list } = this.state;
  236. System.orderFAQ({ ids: list.map(row => row.id) }).then(() => {
  237. asyncSMessage('操作成功!');
  238. this.refresh();
  239. });
  240. }
  241. renderView() {
  242. const { search } = this.state;
  243. return <Block flex>
  244. <FilterLayout
  245. show
  246. ref={(ref) => { this.filterF = ref; }}
  247. itemList={this.filterForm}
  248. data={Object.assign({}, search, { channel: search.channel ? search.channel.split('-') : '' })}
  249. onChange={data => {
  250. data.channel = data.channel.join('-');
  251. data.page = 1;
  252. this.search(data);
  253. }} />
  254. <ActionLayout
  255. itemList={this.actionList}
  256. selectedKeys={this.state.selectedKeys}
  257. onAction={key => this.onAction(key)}
  258. />
  259. {!this.state.mode && <TableLayout
  260. columns={this.tableSort(this.columns)}
  261. list={this.state.list}
  262. pagination={this.state.page}
  263. loading={this.props.core.loading}
  264. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  265. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  266. selectedKeys={this.state.selectedKeys}
  267. />}
  268. {this.state.mode === 'order' && <DragList
  269. loading={this.props.core.loading}
  270. dataSource={this.state.list || []}
  271. handle={'.icon'}
  272. rowKey={'id'}
  273. onMove={(oldIndex, newIndex) => {
  274. this.order(oldIndex, newIndex);
  275. }}
  276. renderItem={(item) => (
  277. <List.Item actions={[<Icon type='bars' className='icon' />]}>
  278. <Row style={{ width: '100%' }}>
  279. <Col span={8}>Q: {item.content}</Col>
  280. <Col span={15} offset={1}>A: {item.answer}</Col>
  281. </Row>
  282. </List.Item>
  283. )}
  284. />}
  285. {this.state.mode === 'order' && <Row type="flex" justify="center">
  286. <Col>
  287. <Button type="primary" onClick={() => {
  288. this.submit();
  289. }}>保存</Button>
  290. </Col>
  291. </Row>}
  292. </Block>;
  293. }
  294. }