page.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import Block from '@src/components/Block';
  5. // import FilterLayout from '@src/layouts/FilterLayout';
  6. import ActionLayout from '@src/layouts/ActionLayout';
  7. import TableLayout from '@src/layouts/TableLayout';
  8. import { getMap, formatTreeData, flattenObject } from '@src/services/Tools';
  9. import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
  10. import { ServiceKey, ServiceParamMap, SwitchSelect } from '../../../../Constant';
  11. import { Course } from '../../../stores/course';
  12. import { Exercise } from '../../../stores/exercise';
  13. const SwitchSelectMap = getMap(SwitchSelect, 'value', 'label');
  14. export default class extends Page {
  15. constructor(props) {
  16. super(props);
  17. this.exerciseMap = {};
  18. this.actionList = [{
  19. key: 'add',
  20. type: 'primary',
  21. name: '创建',
  22. }];
  23. this.itemList = [{
  24. key: 'id',
  25. type: 'hidden',
  26. }, {
  27. key: 'title',
  28. type: 'input',
  29. name: '套餐名称',
  30. }, {
  31. key: 'structId',
  32. type: 'select',
  33. select: [],
  34. name: '套餐学科',
  35. }, {
  36. key: 'description',
  37. type: 'input',
  38. name: '套餐描述',
  39. }, {
  40. key: 'price',
  41. type: 'number',
  42. name: '套餐价格',
  43. }, {
  44. key: 'courseIds',
  45. type: 'multiple',
  46. name: '包含课程',
  47. }];
  48. ServiceKey.forEach((row) => {
  49. const list = ServiceParamMap[row.value];
  50. const item = {
  51. key: `gift.${row.value}`,
  52. type: 'number',
  53. name: `赠送${row.label}`,
  54. };
  55. if (list) {
  56. item.select = list;
  57. item.type = 'select';
  58. }
  59. this.itemList.push(item);
  60. });
  61. this.columns = [{
  62. title: '套餐名称',
  63. dataIndex: 'title',
  64. }, {
  65. title: '套餐学科',
  66. dataIndex: 'structId',
  67. render: (text) => {
  68. return this.exerciseMap[text];
  69. },
  70. }, {
  71. title: '套餐价格',
  72. dataIndex: 'price',
  73. }, {
  74. title: '销售数量',
  75. dataIndex: 'saleNumber',
  76. }, {
  77. title: '首页推荐',
  78. dataIndex: 'isSpecial',
  79. render: (text) => {
  80. return SwitchSelectMap[text] || text;
  81. },
  82. }, {
  83. title: '操作',
  84. dataIndex: 'handler',
  85. render: (text, record) => {
  86. return <div className="table-button">
  87. {(
  88. <a onClick={() => {
  89. this.editAction(record);
  90. }}>编辑</a>
  91. )}
  92. {!!record.isSpecial && (
  93. <a onClick={() => {
  94. this.special(record, 0);
  95. }}>取消推荐</a>
  96. )}
  97. {!record.isSpecial && (
  98. <a onClick={() => {
  99. this.special(record, 1);
  100. }}>首页推荐</a>
  101. )}
  102. </div>;
  103. },
  104. }];
  105. }
  106. init() {
  107. Exercise.courseStruct().then((result) => {
  108. const list = result.filter(row => row.level === 1).map(row => {
  109. row.title = `${row.titleZh}`;
  110. row.value = row.id;
  111. return row;
  112. });
  113. this.itemList[2].select = list;
  114. this.exerciseMap = getMap(list, 'id', 'title');
  115. this.setState({ exercise: formatTreeData(list, 'id', 'title', 'parentId') });
  116. });
  117. Course.list({ excludeVs: true, excludeOnline: true }).then((result) => {
  118. this.itemList[5].select = result.list.map(row => {
  119. row.value = row.id;
  120. return row;
  121. });
  122. });
  123. }
  124. initData() {
  125. Course.listPackage(this.state.search).then(result => {
  126. this.setTableData(result.list, result.total);
  127. });
  128. }
  129. detailAction(row) {
  130. this.setState({ detail: row });
  131. }
  132. addAction() {
  133. asyncForm('创建', this.itemList, {}, data => {
  134. return Course.addPackage(data).then(() => {
  135. asyncSMessage('添加成功!');
  136. this.refresh();
  137. });
  138. });
  139. }
  140. editAction(row) {
  141. row = flattenObject(row);
  142. asyncForm('编辑', this.itemList, row, data => {
  143. return Course.editPackage(data).then(() => {
  144. asyncSMessage('编辑成功!');
  145. this.refresh();
  146. });
  147. });
  148. }
  149. special(row, isSpecial) {
  150. Course.editPackage({ id: row.id, isSpecial }).then(() => {
  151. asyncSMessage('编辑成功!');
  152. this.refresh();
  153. });
  154. }
  155. renderView() {
  156. return <Block flex>
  157. {/* <FilterLayout
  158. show
  159. itemList={this.filterForm}
  160. data={this.state.search}
  161. onChange={data => {
  162. data.page = 1;
  163. this.search(data);
  164. }} /> */}
  165. <ActionLayout
  166. itemList={this.actionList}
  167. selectedKeys={this.state.selectedKeys}
  168. onAction={key => this.onAction(key)}
  169. />
  170. <TableLayout
  171. columns={this.tableSort(this.columns)}
  172. list={this.state.list}
  173. pagination={this.state.page}
  174. loading={this.props.core.loading}
  175. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  176. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  177. selectedKeys={this.state.selectedKeys}
  178. />
  179. </Block>;
  180. }
  181. }