page.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 { 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. dataIndex: 'isSystem',
  136. render: (text) => {
  137. return SystemSelectMap[text];
  138. },
  139. }, {
  140. title: '操作',
  141. dataIndex: 'handler',
  142. render: (text, record) => {
  143. return <div className="table-button">
  144. {(
  145. <a onClick={() => {
  146. this.editAction(record);
  147. }}>编辑</a>
  148. )}
  149. </div>;
  150. },
  151. }];
  152. this.changeSearch(this.filterForm, this, this.state.search.channel, this.state.search.position);
  153. }
  154. changeSearch(list, component, key, value, index = 1) {
  155. if (key === 'course-video' || key === 'course-vs' || key === 'course-package' || key === 'course_data') {
  156. bindSearch(list, 'position', component, (search) => {
  157. if (key === 'course-video') {
  158. return Course.list(Object.assign({ courseModule: 'video' }, search));
  159. } if (key === 'course-vs') {
  160. return Course.list(Object.assign({ courseModule: 'vs' }, search));
  161. } if (key === 'course-package') {
  162. return Course.listPackage(search);
  163. }
  164. return Course.listData(search);
  165. }, (row) => {
  166. return {
  167. title: row.title,
  168. value: row.id,
  169. };
  170. }, value ? Number(value) : null, null);
  171. list[index].disabled = false;
  172. } else {
  173. list[index].disabled = true;
  174. }
  175. component.setState({ load: false });
  176. }
  177. initData() {
  178. if (!this.state.search.order) {
  179. this.state.search.order = 'sort';
  180. this.state.search.direction = 'desc';
  181. }
  182. System.listComment(Object.assign({ isSpecial: true }, this.state.search)).then(result => {
  183. this.setTableData(result.list, result.total);
  184. });
  185. }
  186. changeModel(value) {
  187. const { search, page } = this.state;
  188. if (value) {
  189. search.size = page.total;
  190. search.order = 'sort';
  191. search.direction = 'desc';
  192. search.isSystem = null;
  193. this.setState({ mode: 'order', search });
  194. } else {
  195. search.size = 20;
  196. search.order = null;
  197. search.direction = null;
  198. this.setState({ mode: null, search });
  199. }
  200. this.initData();
  201. }
  202. addAction() {
  203. asyncForm('创建评价', this.itemList, {}, data => {
  204. data.isShow = 1;
  205. data.isSystem = 1;
  206. data.isSpecial = 1;
  207. data.channel = data.channel.join('-');
  208. return System.addComment(data).then(() => {
  209. asyncSMessage('添加成功!');
  210. this.refresh();
  211. });
  212. }).then(component => {
  213. this.formF = component;
  214. this.changeSearch(this.itemList, this.formF, null, null, 2);
  215. });
  216. }
  217. editAction(row) {
  218. let item = this.itemList;
  219. if (row.userId) {
  220. item = this.userItemList;
  221. }
  222. const info = Object.assign({}, row);
  223. info.channel = info.channel.split('-');
  224. asyncForm('编辑', item, info, data => {
  225. data.isShow = 1;
  226. data.isSystem = 1;
  227. data.isSpecial = 1;
  228. data.channel = data.channel.join('-');
  229. return System.editComment(data).then(() => {
  230. asyncSMessage('编辑成功!');
  231. this.refresh();
  232. });
  233. }).then(component => {
  234. this.formF = component;
  235. this.changeSearch(this.itemList, this.formF, row.channel, row.position, 2);
  236. });
  237. }
  238. special(row, isShow) {
  239. System.editComment({ id: row.id, isShow }).then(() => {
  240. asyncSMessage('操作成功!');
  241. this.refresh();
  242. });
  243. }
  244. order(oldIndex, newIndex) {
  245. const { list } = this.state;
  246. const tmp = list.splice(oldIndex, 1);
  247. if (newIndex === list.length) {
  248. list.push(tmp[0]);
  249. } else {
  250. list.splice(newIndex, 0, tmp[0]);
  251. }
  252. this.setState({ list });
  253. }
  254. submit() {
  255. const { list } = this.state;
  256. System.orderComment({ ids: list.map(row => row.id) }).then(() => {
  257. asyncSMessage('操作成功!');
  258. this.refresh();
  259. });
  260. }
  261. renderView() {
  262. const { search } = this.state;
  263. return <Block flex>
  264. <FilterLayout
  265. show
  266. ref={(ref) => { this.filterF = ref; }}
  267. itemList={this.filterForm}
  268. data={Object.assign({}, search, { channel: search.channel ? search.channel.split('-') : '' })}
  269. onChange={data => {
  270. data.channel = data.channel.join('-');
  271. data.page = 1;
  272. this.search(data);
  273. }} />
  274. <ActionLayout
  275. itemList={this.actionList}
  276. selectedKeys={this.state.selectedKeys}
  277. onAction={key => this.onAction(key)}
  278. />
  279. {!this.state.mode && <TableLayout
  280. columns={this.tableSort(this.columns)}
  281. list={this.state.list}
  282. pagination={this.state.page}
  283. loading={this.props.core.loading}
  284. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  285. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  286. selectedKeys={this.state.selectedKeys}
  287. />}
  288. {this.state.mode === 'order' && <DragList
  289. loading={this.props.core.loading}
  290. dataSource={this.state.list || []}
  291. handle={'.icon'}
  292. rowKey={'id'}
  293. onMove={(oldIndex, newIndex) => {
  294. this.order(oldIndex, newIndex);
  295. }}
  296. renderItem={(item) => (
  297. <List.Item actions={[<Icon type='bars' className='icon' />]}>
  298. <Row style={{ width: '100%' }}>
  299. <Col span={8}>昵称:{item.user ? item.user.nickname : item.nickname}</Col>
  300. <Col span={15} offset={1}>评论:{item.content}</Col>
  301. </Row>
  302. </List.Item>
  303. )}
  304. />}
  305. {this.state.mode === 'order' && <Row type="flex" justify="center">
  306. <Col>
  307. <Button type="primary" onClick={() => {
  308. this.submit();
  309. }}>保存</Button>
  310. </Col>
  311. </Row>}
  312. </Block>;
  313. }
  314. }