page.js 6.3 KB

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