page.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import React from 'react';
  2. // import { Link } from 'react-router-dom';
  3. // import { Button } from 'antd';
  4. import './index.less';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. // import FilterLayout from '@src/layouts/FilterLayout';
  8. import ActionLayout from '@src/layouts/ActionLayout';
  9. import TableLayout from '@src/layouts/TableLayout';
  10. import { asyncSMessage, asyncForm, asyncDelConfirm } from '@src/services/AsyncTools';
  11. import { formatDate, getMap, formatTreeData, flattenTree } from '@src/services/Tools';
  12. import { AdChannel, AdPlace } from '../../../../Constant';
  13. import { System } from '../../../stores/system';
  14. const AdChannelTree = formatTreeData(AdChannel, 'value', 'label', 'parent');
  15. const AdChannelFlatten = flattenTree(AdChannelTree, (row, item) => {
  16. row = Object.assign({}, row);
  17. row.value = `${item.value}-${row.value}`;
  18. row.label = `${item.label}-${row.label}`;
  19. return row;
  20. }, 'children');
  21. const AdChannelMap = getMap(AdChannelFlatten, 'value', 'label');
  22. const AdPlaceTree = formatTreeData([].concat(AdChannelFlatten.map(row => Object.assign({}, row)), AdPlace), 'value', 'label', 'parent');
  23. const AdPlaceChannelMap = getMap(AdPlaceTree, 'value', 'children');
  24. const AdPlaceMap = getMap(AdPlaceTree, 'value', 'label', 'children');
  25. export default class extends Page {
  26. init() {
  27. this.formF = null;
  28. this.itemList = [{
  29. key: 'id',
  30. type: 'hidden',
  31. }, {
  32. key: 'channel',
  33. type: 'cascader',
  34. allowClear: true,
  35. name: '频道',
  36. select: AdChannelTree,
  37. placeholder: '请选择',
  38. onChange: (value) => {
  39. this.changeSearch(this.itemList, this.formF, value.join('-'), null, 2);
  40. },
  41. }, {
  42. key: 'place',
  43. type: 'select',
  44. allowClear: true,
  45. name: '位置',
  46. select: [],
  47. placeholder: '请选择',
  48. }, {
  49. key: 'title',
  50. type: 'input',
  51. name: '广告名称',
  52. }, {
  53. key: 'time',
  54. type: 'daterange',
  55. name: '广告时间',
  56. }, {
  57. key: 'image',
  58. type: 'image',
  59. name: '广告图片',
  60. onUpload: ({ file }) => {
  61. return System.uploadImage(file).then(result => { return result; });
  62. },
  63. }, {
  64. key: 'link',
  65. type: 'input',
  66. name: '链接地址',
  67. }];
  68. this.filterForm = [{
  69. key: 'channel',
  70. type: 'cascader',
  71. allowClear: true,
  72. name: '频道',
  73. select: AdChannelTree,
  74. placeholder: '请选择',
  75. }];
  76. this.actionList = [{
  77. key: 'add',
  78. type: 'primary',
  79. name: '创建',
  80. }];
  81. this.columns = [{
  82. title: '频道',
  83. dataIndex: 'channel',
  84. render: (text) => {
  85. return AdChannelMap[text] || '';
  86. },
  87. }, {
  88. title: '位置',
  89. dataIndex: 'place',
  90. render: (text, record) => {
  91. return (AdPlaceMap[record.channel] || {})[text];
  92. },
  93. }, {
  94. title: '广告名称',
  95. dataIndex: 'title',
  96. }, {
  97. title: '展示时间',
  98. dataIndex: 'time',
  99. render: (text, record) => {
  100. return record.startTime && record.endTime ? `${formatDate(record.startTime, 'YYYY-MM-DD')}-${formatDate(record.endTime, 'YYYY-MM-DD')}` : '长期';
  101. },
  102. }, {
  103. title: '操作',
  104. dataIndex: 'handler',
  105. render: (text, record) => {
  106. return <div className="table-button">
  107. {(
  108. <a onClick={() => {
  109. this.editAction(record);
  110. }}>编辑</a>
  111. )}
  112. {(
  113. <a onClick={() => {
  114. this.deleteAction(record);
  115. }}>删除</a>
  116. )}
  117. </div>;
  118. },
  119. }];
  120. }
  121. changeSearch(list, component, key, value, index = 1) {
  122. if (key) {
  123. list[index].select = AdPlaceChannelMap[key];
  124. list[index].disabled = false;
  125. } else {
  126. list[index].disabled = true;
  127. }
  128. component.setFieldsValue({ [list[index].key]: value });
  129. component.setState({ load: false });
  130. }
  131. initData() {
  132. System.listAd(Object.assign({}, this.state.search)).then(result => {
  133. this.setTableData(result.list, result.total);
  134. });
  135. }
  136. addAction() {
  137. asyncForm('创建广告', this.itemList, {}, data => {
  138. if (data.time && data.time.length > 0) {
  139. data.time[0].local();
  140. data.time[1].local();
  141. data.startTime = data.time[0].format('YYYY-MM-DDT00:00:00Z');
  142. data.endTime = data.time[1].format('YYYY-MM-DDT00:00:00Z');
  143. }
  144. data.channel = data.channel.join('-');
  145. return System.addAd(data).then(() => {
  146. asyncSMessage('添加成功!');
  147. this.refresh();
  148. });
  149. }).then(component => {
  150. this.formF = component;
  151. this.changeSearch(this.itemList, this.formF, null, null, 2);
  152. });
  153. }
  154. editAction(row) {
  155. const info = Object.assign({}, row);
  156. info.channel = info.channel.split('-');
  157. info.time = [info.startTime, info.endTime];
  158. asyncForm('编辑广告', this.itemList, info, data => {
  159. if (data.time && data.time.length > 0) {
  160. data.time[0].local();
  161. data.time[1].local();
  162. data.startTime = data.time[0].format('YYYY-MM-DDT00:00:00Z');
  163. data.endTime = data.time[1].format('YYYY-MM-DDT00:00:00Z');
  164. }
  165. data.channel = data.channel.join('-');
  166. return System.editAd(data).then(() => {
  167. asyncSMessage('编辑成功!');
  168. this.refresh();
  169. });
  170. }).then(component => {
  171. this.formF = component;
  172. this.changeSearch(this.itemList, this.formF, row.channel, row.place, 2);
  173. });
  174. }
  175. deleteAction(row) {
  176. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  177. const handler = System.delAd({ id: row.id });
  178. return handler.then(() => {
  179. asyncSMessage('删除成功!');
  180. this.refresh();
  181. });
  182. });
  183. }
  184. renderView() {
  185. return <Block flex>
  186. {/* <FilterLayout
  187. show
  188. itemList={this.filterForm}
  189. data={this.state.search}
  190. onChange={data => {
  191. data.page = 1;
  192. this.search(data);
  193. }} /> */}
  194. <ActionLayout
  195. itemList={this.actionList}
  196. selectedKeys={this.state.selectedKeys}
  197. onAction={key => this.onAction(key)}
  198. />
  199. <TableLayout
  200. columns={this.tableSort(this.columns)}
  201. list={this.state.list}
  202. pagination={this.state.page}
  203. loading={this.props.core.loading}
  204. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  205. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  206. selectedKeys={this.state.selectedKeys}
  207. />
  208. </Block>;
  209. }
  210. }