page.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import React from 'react';
  2. import moment from 'moment';
  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 { formatDate, bindSearch, getMap } from '@src/services/Tools';
  10. import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
  11. import { CourseSource, CourseStatus, CourseVsType } from '../../../../Constant';
  12. import { Course } from '../../../stores/course';
  13. import { User } from '../../../stores/user';
  14. const CourseSourceMap = getMap(CourseSource, 'value', 'label');
  15. const CourseStatusMap = getMap(CourseStatus, 'value', 'label');
  16. const CourseVsTypeMap = getMap(CourseVsType, 'value', 'label');
  17. export default class extends Page {
  18. init() {
  19. this.onlineAction = [{
  20. key: 'info',
  21. name: '课程',
  22. render: () => {
  23. const { data } = this.state;
  24. return `${data.title}`;
  25. },
  26. }, {
  27. key: 'addOnlineStudent',
  28. name: '添加学员',
  29. }];
  30. this.onlineFilter = [{
  31. key: 'id',
  32. type: 'hidden',
  33. }, {
  34. key: 'timeId',
  35. type: 'select',
  36. allowClear: true,
  37. select: [],
  38. number: true,
  39. name: '时间段',
  40. }, {
  41. key: 'userId',
  42. type: 'select',
  43. name: '学员',
  44. allowClear: true,
  45. select: [],
  46. number: true,
  47. placeholder: '请输入',
  48. }];
  49. this.onlineList = [{
  50. key: 'timeId',
  51. type: 'select',
  52. select: [],
  53. name: '时间段',
  54. }, {
  55. key: 'userIds',
  56. type: 'multiple',
  57. name: '学员',
  58. allowClear: true,
  59. select: [],
  60. number: true,
  61. placeholder: '请输入',
  62. }];
  63. this.onlineColumns = [{
  64. title: '时间段',
  65. dataIndex: 'time',
  66. render: (text) => {
  67. return `${formatDate(text.startTime, 'YYYY-MM-DD')}~${formatDate(text.endTime, 'YYYY-MM-DD')}`;
  68. },
  69. }, {
  70. title: '学员id',
  71. dataIndex: 'userId',
  72. }, {
  73. title: '学员名称',
  74. dataIndex: 'user.nickname',
  75. }, {
  76. title: '手机号',
  77. dataIndex: 'user.mobile',
  78. }, {
  79. title: '操作',
  80. dataIndex: 'handler',
  81. render: (text, record) => {
  82. return <div className="table-button">
  83. {<a onClick={() => {
  84. this.deleteOnlineStudent(record);
  85. }}>删除</a>}
  86. </div>;
  87. },
  88. }];
  89. this.vsAction = [{
  90. key: 'info',
  91. name: '课程',
  92. render: () => {
  93. const { data } = this.state;
  94. return `${data.title}(${CourseVsTypeMap[data.vsType]})`;
  95. },
  96. }, {
  97. key: 'addVsStudent',
  98. name: '添加学员',
  99. }];
  100. this.vsList = [{
  101. key: 'id',
  102. type: 'hidden',
  103. }, {
  104. key: 'userId',
  105. type: 'select',
  106. name: '学员',
  107. select: [],
  108. number: true,
  109. placeholder: '请输入',
  110. // }, {
  111. // key: 'time',
  112. // type: 'daterange',
  113. // name: '有效期',
  114. }, {
  115. key: 'teacherId',
  116. type: 'select',
  117. select: [],
  118. name: '老师',
  119. }, {
  120. key: 'vsNumber',
  121. type: 'number',
  122. name: '课时数',
  123. }];
  124. this.vsColumns = [{
  125. title: '学员名称',
  126. dataIndex: 'user.nickname',
  127. }, {
  128. title: '有效期',
  129. dataIndex: 'time',
  130. render: (text, record) => {
  131. return record.isUsed ? `${formatDate(record.useStartTime, 'YYYY-MM-DD')}~${formatDate(record.useEndTime, 'YYYY-MM-DD')}` : '';
  132. },
  133. }, {
  134. title: '添加方式',
  135. dataIndex: 'source',
  136. render: (text) => {
  137. return CourseSourceMap[text] || text;
  138. },
  139. }, {
  140. title: '授课老师',
  141. dataIndex: 'teacher.realname',
  142. }, {
  143. title: '状态',
  144. dataIndex: 'status',
  145. render: (text, record) => {
  146. let status = 0;
  147. if (record.isUsed) {
  148. const end = new Date(record.useEndTime);
  149. status = 1;
  150. if (new Date().getTime() > end.getTime()) {
  151. status = 2;
  152. }
  153. }
  154. return CourseStatusMap[status] || status;
  155. },
  156. // }, {
  157. // title: '操作',
  158. // dataIndex: 'handler',
  159. // render: (text, record) => {
  160. // return <div className="table-button">
  161. // {<a onClick={() => {
  162. // this.changeVs(record);
  163. // }}>修改</a>}
  164. // </div>;
  165. // },
  166. }];
  167. }
  168. initData() {
  169. const { id } = this.params;
  170. let handler;
  171. if (id) {
  172. handler = Course.get({ id });
  173. }
  174. handler
  175. .then(result => {
  176. this.setState({ module: result.courseModule });
  177. this.setState({ data: result });
  178. this.refresh();
  179. });
  180. }
  181. refresh() {
  182. const { id } = this.params;
  183. const { module } = this.state;
  184. switch (module) {
  185. case 'video':
  186. break;
  187. case 'online':
  188. bindSearch(this.onlineFilter, 'timeId', this, (search) => {
  189. return Course.listTime(Object.assign({ courseId: id }, search));
  190. }, (row) => {
  191. return {
  192. title: `${formatDate(row.startTime, 'YYYY-MM-DD')}~${formatDate(row.endTime, 'YYYY-MM-DD')}`,
  193. value: row.id,
  194. };
  195. }, this.state.search.timeId ? Number(this.state.search.timeId) : null, null);
  196. bindSearch(this.onlineFilter, 'userId', this, (search) => {
  197. return User.list(search);
  198. }, (row) => {
  199. return {
  200. title: `${row.nickname}(${row.mobile})`,
  201. value: row.id,
  202. };
  203. }, null, null);
  204. bindSearch(this.onlineList, 'timeId', this, (search) => {
  205. return Course.listTime(Object.assign({ courseId: id }, search));
  206. }, (row) => {
  207. return {
  208. title: `${formatDate(row.startTime, 'YYYY-MM-DD')}~${formatDate(row.endTime, 'YYYY-MM-DD')}`,
  209. value: row.id,
  210. };
  211. }, null, null);
  212. bindSearch(this.onlineList, 'userIds', this, (search) => {
  213. return User.list(search);
  214. }, (row) => {
  215. return {
  216. title: `${row.nickname}(${row.mobile})`,
  217. value: row.id,
  218. };
  219. }, [], null);
  220. this.refreshOnline();
  221. break;
  222. case 'vs':
  223. this.refreshVs();
  224. break;
  225. default:
  226. }
  227. }
  228. refreshOnline() {
  229. const { id } = this.params;
  230. Course.listStudentOnline(Object.assign({ courseId: id }, this.state.search)).then(result => {
  231. this.setTableData(result.list, result.total);
  232. });
  233. }
  234. refreshVs() {
  235. const { id } = this.params;
  236. Course.listStudentVs(Object.assign({ courseId: id }, this.state.search)).then(result => {
  237. this.setTableData(result.list, result.total);
  238. });
  239. }
  240. addOnlineStudentAction() {
  241. const { id } = this.params;
  242. asyncForm('添加', this.onlineList, {}, data => {
  243. return Promise.all(data.userIds.map(userId => Course.addStudentOnline({ courseId: id, timeId: data.timeId, userId }))).then(() => {
  244. asyncSMessage('添加成功!');
  245. this.refresh();
  246. });
  247. }).catch(err => {
  248. console.log(err);
  249. });
  250. }
  251. deleteOnlineStudent(row) {
  252. Course.delStudentOnline({ id: row.id }).then(() => {
  253. asyncSMessage('删除成功!');
  254. this.refresh();
  255. });
  256. }
  257. changeVs(record) {
  258. const { id } = this.params;
  259. const { data } = this.state;
  260. record.time = [moment(record.useStartTime), moment(record.useEndTime)];
  261. this.vsList[4].type = data.vsType === 'novice' ? 'hidden' : 'number';
  262. asyncForm('修改', this.vsList, record, info => {
  263. // ([info.useStartTime, info.useEndTime] = info.time);
  264. return Course.editStudentVs(Object.assign({ courseId: id }, info)).then(() => {
  265. asyncSMessage('添加成功!');
  266. this.refresh();
  267. });
  268. }).then(component => {
  269. bindSearch(this.vsList, 'userId', component, (search) => {
  270. return User.list(search);
  271. }, (row) => {
  272. return {
  273. title: `${row.nickname}(${row.mobile})`,
  274. value: row.id,
  275. };
  276. }, record.userId, null);
  277. bindSearch(this.vsList, 'teacherId', component, (search) => {
  278. return Course.listTeacher(Object.assign({ courseId: id }, search));
  279. }, (row) => {
  280. return {
  281. title: row.realname,
  282. value: row.id,
  283. };
  284. }, record.teacherId, null);
  285. });
  286. }
  287. addVsStudentAction() {
  288. const { id } = this.params;
  289. const { data } = this.state;
  290. this.vsList[4].type = data.vsType === 'novice' ? 'hidden' : 'number';
  291. asyncForm('添加', this.vsList, {}, info => {
  292. if (data.vsType === 'novice') {
  293. // 写死:新手每次1课时
  294. info.vsNumber = 1;
  295. }
  296. // ([info.useStartTime, info.useEndTime] = info.time);
  297. return Course.addStudentVs(Object.assign({ courseId: id }, info)).then(() => {
  298. asyncSMessage('添加成功!');
  299. this.refresh();
  300. });
  301. }).then(component => {
  302. bindSearch(this.vsList, 'userId', component, (search) => {
  303. return User.list(search);
  304. }, (row) => {
  305. return {
  306. title: `${row.nickname}(${row.mobile})`,
  307. value: row.id,
  308. };
  309. }, null, null);
  310. bindSearch(this.vsList, 'teacherId', component, (search) => {
  311. return Course.listTeacher(Object.assign({ courseId: id }, search));
  312. }, (row) => {
  313. return {
  314. title: row.realname,
  315. value: row.id,
  316. };
  317. }, null, null);
  318. });
  319. }
  320. renderOnline() {
  321. return <Block flex>
  322. {<FilterLayout
  323. show
  324. itemList={this.onlineFilter}
  325. data={this.state.search}
  326. onChange={data => {
  327. this.search(data);
  328. }} />}
  329. <ActionLayout
  330. itemList={this.onlineAction}
  331. selectedKeys={this.state.selectedKeys}
  332. onAction={key => this.onAction(key)}
  333. />
  334. <TableLayout
  335. columns={this.onlineColumns}
  336. list={this.state.list}
  337. pagination={this.state.page}
  338. loading={this.props.core.loading}
  339. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  340. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  341. selectedKeys={this.state.selectedKeys}
  342. />
  343. </Block>;
  344. }
  345. renderVs() {
  346. return <Block flex>
  347. {<ActionLayout
  348. itemList={this.vsAction}
  349. selectedKeys={this.state.selectedKeys}
  350. onAction={key => this.onAction(key)}
  351. />}
  352. <TableLayout
  353. columns={this.vsColumns}
  354. list={this.state.list}
  355. pagination={this.state.page}
  356. loading={this.props.core.loading}
  357. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  358. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  359. selectedKeys={this.state.selectedKeys}
  360. />
  361. </Block>;
  362. }
  363. renderView() {
  364. switch (this.state.module) {
  365. case 'online':
  366. return [this.renderOnline()];
  367. case 'vs':
  368. return [this.renderVs()];
  369. case 'video':
  370. return [];
  371. default:
  372. return <div />;
  373. }
  374. }
  375. }