page.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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, asyncDelConfirm } from '@src/services/AsyncTools';
  12. import { CommentChannel, SystemSelect } from '../../../../Constant';
  13. import { System } from '../../../stores/system';
  14. import { Course } from '../../../stores/course';
  15. const CommentChannelTree = formatTreeData(CommentChannel, 'value', 'label', 'parent');
  16. const CommentChannelFlatten = flattenTree(CommentChannelTree, (row, item) => {
  17. row = Object.assign({}, row);
  18. row.value = `${item.value}-${row.value}`;
  19. row.label = `${item.label}-${row.label}`;
  20. return row;
  21. }, 'children');
  22. const CommentChannelMap = getMap(CommentChannelFlatten, 'value', 'label');
  23. const SystemSelectMap = getMap(SystemSelect, '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. allowClear: true,
  54. name: '频道',
  55. select: CommentChannelTree,
  56. placeholder: '请选择',
  57. onChange: (value) => {
  58. this.changeSearch(this.itemList, this.formF, value.join('-'), null, 2);
  59. },
  60. }, {
  61. key: 'position',
  62. type: 'select',
  63. allowClear: true,
  64. name: '位置',
  65. select: [],
  66. placeholder: '请选择',
  67. }, {
  68. key: 'nickname',
  69. type: 'input',
  70. name: '学员昵称',
  71. }, {
  72. key: 'avatar',
  73. type: 'image',
  74. name: '学员头像',
  75. onUpload: ({ file }) => {
  76. return System.uploadImage(file).then(result => { return result; });
  77. },
  78. }, {
  79. key: 'content',
  80. type: 'textarea',
  81. name: '评价内容',
  82. }];
  83. this.userItemList = [{
  84. key: 'id',
  85. type: 'hidden',
  86. }, {
  87. key: 'content',
  88. type: 'textarea',
  89. name: '评价内容',
  90. }];
  91. this.filterF = null;
  92. this.filterForm = [{
  93. key: 'channel',
  94. type: 'cascader',
  95. allowClear: true,
  96. name: '频道',
  97. select: formatTreeData(CommentChannel, 'value', 'label', 'parent'),
  98. placeholder: '请选择',
  99. onChange: (value) => {
  100. this.changeSearch(this.filterForm, this, value.join('-'), null);
  101. },
  102. }, {
  103. key: 'position',
  104. type: 'select',
  105. allowClear: true,
  106. name: '位置',
  107. select: [],
  108. placeholder: '请选择',
  109. }, {
  110. key: 'isSystem',
  111. type: 'select',
  112. allowClear: true,
  113. number: true,
  114. name: '来源',
  115. select: SystemSelect,
  116. }];
  117. this.columns = [{
  118. title: '频道',
  119. dataIndex: 'channel',
  120. render: (text, record) => {
  121. return CommentChannelMap[record.channel];
  122. },
  123. }, {
  124. title: '商品名称',
  125. dataIndex: 'positionDetail.title',
  126. }, {
  127. title: '创建时间',
  128. sorter: true,
  129. dataIndex: 'createTime',
  130. render: (text) => {
  131. return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
  132. },
  133. }, {
  134. title: '更新时间',
  135. sorter: true,
  136. dataIndex: 'updateTime',
  137. render: (text) => {
  138. return formatDate(text, 'YYYY-MM-DD HH:mm:ss');
  139. },
  140. }, {
  141. title: '来源',
  142. dataIndex: 'isSystem',
  143. render: (text) => {
  144. return SystemSelectMap[text];
  145. },
  146. }, {
  147. title: '操作',
  148. dataIndex: 'handler',
  149. render: (text, record) => {
  150. return <div className="table-button">
  151. {(
  152. <a onClick={() => {
  153. this.editAction(record);
  154. }}>编辑</a>
  155. )}
  156. {(
  157. <a onClick={() => {
  158. this.deleteAction(record);
  159. }}>删除</a>
  160. )}
  161. </div>;
  162. },
  163. }];
  164. this.changeSearch(this.filterForm, this, this.state.search.channel, this.state.search.position);
  165. }
  166. changeSearch(list, component, key, value, index = 1) {
  167. if (key === 'course-video' || key === 'course-vs' || key === 'course-package' || key === 'course_data') {
  168. bindSearch(list, 'position', component, (search) => {
  169. if (key === 'course-video') {
  170. return Course.list(Object.assign({ courseModule: 'video' }, search));
  171. } if (key === 'course-vs') {
  172. return Course.list(Object.assign({ courseModule: 'vs' }, search));
  173. } if (key === 'course-package') {
  174. return Course.listPackage(search);
  175. }
  176. return Course.listData(search);
  177. }, (row) => {
  178. return {
  179. title: row.title,
  180. value: row.id,
  181. };
  182. }, value ? Number(value) : null, null);
  183. list[index].disabled = false;
  184. } else {
  185. list[index].disabled = true;
  186. }
  187. component.setState({ load: false });
  188. }
  189. initData() {
  190. if (!this.state.search.order) {
  191. this.state.search.order = 'updateTime';
  192. this.state.search.direction = 'desc';
  193. }
  194. System.listComment(Object.assign({ isSpecial: true }, this.state.search)).then(result => {
  195. this.setTableData(result.list, result.total);
  196. });
  197. }
  198. changeModel(value) {
  199. const { search, page } = this.state;
  200. if (value) {
  201. search.size = page.total;
  202. search.order = 'sort';
  203. search.direction = 'desc';
  204. search.isSystem = null;
  205. this.setState({ mode: 'order', search });
  206. } else {
  207. search.size = 20;
  208. search.order = 'updateTime';
  209. search.direction = 'desc';
  210. this.setState({ mode: null, search });
  211. }
  212. this.initData();
  213. }
  214. addAction() {
  215. asyncForm('创建评价', this.itemList, {}, data => {
  216. data.isShow = 1;
  217. data.isSystem = 1;
  218. data.isSpecial = 1;
  219. data.channel = data.channel.join('-');
  220. return System.addComment(data).then(() => {
  221. asyncSMessage('添加成功!');
  222. this.refresh();
  223. });
  224. }).then(component => {
  225. this.formF = component;
  226. this.changeSearch(this.itemList, this.formF, null, null, 2);
  227. });
  228. }
  229. editAction(row) {
  230. let item = this.itemList;
  231. if (row.userId) {
  232. item = this.userItemList;
  233. }
  234. const info = Object.assign({}, row);
  235. info.channel = info.channel.split('-');
  236. asyncForm('编辑', item, info, data => {
  237. data.isShow = 1;
  238. data.isSystem = 1;
  239. data.isSpecial = 1;
  240. data.channel = data.channel.join('-');
  241. return System.editComment(data).then(() => {
  242. asyncSMessage('编辑成功!');
  243. this.refresh();
  244. });
  245. }).then(component => {
  246. this.formF = component;
  247. this.changeSearch(this.itemList, this.formF, row.channel, row.position, 2);
  248. });
  249. }
  250. deleteAction(row) {
  251. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  252. const handler = System.delComment({ id: row.id });
  253. return handler.then(() => {
  254. asyncSMessage('删除成功!');
  255. this.refresh();
  256. });
  257. });
  258. }
  259. special(row, isShow) {
  260. System.editComment({ id: row.id, isShow }).then(() => {
  261. asyncSMessage('操作成功!');
  262. this.refresh();
  263. });
  264. }
  265. order(oldIndex, newIndex) {
  266. const { list } = this.state;
  267. const tmp = list.splice(oldIndex, 1);
  268. if (newIndex === list.length) {
  269. list.push(tmp[0]);
  270. } else {
  271. list.splice(newIndex, 0, tmp[0]);
  272. }
  273. this.setState({ list });
  274. }
  275. submit() {
  276. const { list } = this.state;
  277. System.orderComment({ ids: list.map(row => row.id) }).then(() => {
  278. asyncSMessage('操作成功!');
  279. this.refresh();
  280. });
  281. }
  282. renderView() {
  283. const { search } = this.state;
  284. return <Block flex>
  285. <FilterLayout
  286. show
  287. ref={(ref) => { this.filterF = ref; }}
  288. itemList={this.filterForm}
  289. data={Object.assign({}, search, { channel: search.channel ? search.channel.split('-') : '' })}
  290. onChange={data => {
  291. data.channel = data.channel ? data.channel.join('-') : data.channel;
  292. data.page = 1;
  293. this.search(data);
  294. }} />
  295. <ActionLayout
  296. itemList={this.actionList}
  297. selectedKeys={this.state.selectedKeys}
  298. onAction={key => this.onAction(key)}
  299. />
  300. {!this.state.mode && <TableLayout
  301. columns={this.tableSort(this.columns)}
  302. list={this.state.list}
  303. pagination={this.state.page}
  304. loading={this.props.core.loading}
  305. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  306. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  307. selectedKeys={this.state.selectedKeys}
  308. />}
  309. {this.state.mode === 'order' && <DragList
  310. loading={this.props.core.loading}
  311. dataSource={this.state.list || []}
  312. handle={'.icon'}
  313. rowKey={'id'}
  314. onMove={(oldIndex, newIndex) => {
  315. this.order(oldIndex, newIndex);
  316. }}
  317. renderItem={(item) => (
  318. <List.Item actions={[<Icon type='bars' className='icon' />]}>
  319. <Row style={{ width: '100%' }}>
  320. <Col span={8}>昵称:{item.user ? item.user.nickname : item.nickname}</Col>
  321. <Col span={15} offset={1}>评论:{item.content}</Col>
  322. </Row>
  323. </List.Item>
  324. )}
  325. />}
  326. {this.state.mode === 'order' && <Row type="flex" justify="center">
  327. <Col>
  328. <Button type="primary" onClick={() => {
  329. this.submit();
  330. }}>保存</Button>
  331. </Col>
  332. </Row>}
  333. </Block>;
  334. }
  335. }