page.js 11 KB

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