page.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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);
  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 = '`order`';
  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 = '`order`';
  174. search.direction = 'desc';
  175. search.size = page.total;
  176. this.setState({ mode: 'order', search });
  177. } else {
  178. search.size = 20;
  179. search.order = null;
  180. search.direction = null;
  181. this.setState({ mode: null, search });
  182. }
  183. this.initData();
  184. }
  185. addAction() {
  186. asyncForm('创建', this.itemList, {}, data => {
  187. data.isShow = 1;
  188. data.isSystem = 1;
  189. data.isSpecial = 1;
  190. data.channel = data.channel.join('-');
  191. return System.addFAQ(data).then(() => {
  192. asyncSMessage('添加成功!');
  193. this.refresh();
  194. });
  195. }).then(component => {
  196. this.formF = component;
  197. this.changeSearch(this.itemList, this.formF, null, null, 2);
  198. });
  199. }
  200. editAction(row) {
  201. const info = Object.assign({}, row);
  202. info.channel = info.channel.split('-');
  203. asyncForm('编辑', this.itemList, info, data => {
  204. data.channel = data.channel.join('-');
  205. return System.editFAQ(data).then(() => {
  206. asyncSMessage('编辑成功!');
  207. this.refresh();
  208. });
  209. }).then(component => {
  210. this.formF = component;
  211. this.changeSearch(this.itemList, this.formF, row.channel, row.position, 2);
  212. });
  213. }
  214. show(row, isShow) {
  215. System.editFAQ({ id: row.id, isShow }).then(() => {
  216. asyncSMessage('编辑成功!');
  217. this.refresh();
  218. });
  219. }
  220. order(oldIndex, newIndex) {
  221. const { list } = this.state;
  222. const tmp = list.splice(oldIndex, 1);
  223. if (newIndex === list.length) {
  224. list.push(tmp[0]);
  225. } else {
  226. list.splice(newIndex, 0, tmp[0]);
  227. }
  228. this.setState({ list });
  229. }
  230. submit() {
  231. const { list } = this.state;
  232. System.orderComment({ ids: list.map(row => row.id) }).then(() => {
  233. asyncSMessage('操作成功!');
  234. this.refresh();
  235. });
  236. }
  237. renderView() {
  238. const { search } = this.state;
  239. return <Block flex>
  240. <FilterLayout
  241. show
  242. ref={(ref) => { this.filterF = ref; }}
  243. itemList={this.filterForm}
  244. data={Object.assign({}, search, { channel: search.channel ? search.channel.split('-') : '' })}
  245. onChange={data => {
  246. data.channel = data.channel.join('-');
  247. this.search(data);
  248. }} />
  249. <ActionLayout
  250. itemList={this.actionList}
  251. selectedKeys={this.state.selectedKeys}
  252. onAction={key => this.onAction(key)}
  253. />
  254. {!this.state.mode && <TableLayout
  255. columns={this.tableSort(this.columns)}
  256. list={this.state.list}
  257. pagination={this.state.page}
  258. loading={this.props.core.loading}
  259. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  260. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  261. selectedKeys={this.state.selectedKeys}
  262. />}
  263. {this.state.mode === 'order' && <DragList
  264. loading={this.props.core.loading}
  265. dataSource={this.state.list || []}
  266. handle={'.icon'}
  267. rowKey={'id'}
  268. onMove={(oldIndex, newIndex) => {
  269. this.order(oldIndex, newIndex);
  270. }}
  271. renderItem={(item) => (
  272. <List.Item actions={[<Icon type='bars' className='icon' />]}>
  273. <Row style={{ width: '100%' }}>
  274. <Col span={8}>{item.user ? item.user.nickname : item.nickname}</Col>
  275. <Col span={15} offset={1}>{item.content}</Col>
  276. </Row>
  277. </List.Item>
  278. )}
  279. />}
  280. {this.state.mode === 'order' && <Row type="flex" justify="center">
  281. <Col>
  282. <Button type="primary" onClick={() => {
  283. this.submit();
  284. }}>保存</Button>
  285. </Col>
  286. </Row>}
  287. </Block>;
  288. }
  289. }