page.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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: 'number',
  121. type: 'number',
  122. name: '课时数',
  123. }, {
  124. key: 'cctalkName',
  125. type: 'input',
  126. name: 'CCTalk用户名',
  127. }];
  128. this.vsColumns = [{
  129. title: '学员名称',
  130. dataIndex: 'user.nickname',
  131. }, {
  132. title: '有效期',
  133. dataIndex: 'time',
  134. render: (text, record) => {
  135. return record.isUsed ? `${formatDate(record.useStartTime, 'YYYY-MM-DD')}~${formatDate(record.useEndTime, 'YYYY-MM-DD')}` : '';
  136. },
  137. }, {
  138. title: '添加方式',
  139. dataIndex: 'source',
  140. render: (text) => {
  141. return CourseSourceMap[text] || text;
  142. },
  143. }, {
  144. title: '授课老师',
  145. dataIndex: 'teacher.realname',
  146. }, {
  147. title: '状态',
  148. dataIndex: 'status',
  149. render: (text, record) => {
  150. let status = 0;
  151. if (record.isUsed) {
  152. const end = new Date(record.useEndTime);
  153. status = 1;
  154. if (new Date().getTime() > end.getTime()) {
  155. status = 2;
  156. }
  157. }
  158. return CourseStatusMap[status] || status;
  159. },
  160. }, {
  161. title: 'CCTalk用户名',
  162. dataIndex: 'cctalkName',
  163. }, {
  164. title: '操作',
  165. dataIndex: 'handler',
  166. render: (text, record) => {
  167. return <div className="table-button">
  168. {<a onClick={() => {
  169. this.changeVs(record);
  170. }}>修改</a>}
  171. </div>;
  172. },
  173. }];
  174. }
  175. initData() {
  176. const { id } = this.params;
  177. let handler;
  178. if (id) {
  179. handler = Course.get({ id });
  180. }
  181. handler
  182. .then(result => {
  183. this.setState({ module: result.courseModule });
  184. this.setState({ data: result });
  185. this.refresh();
  186. });
  187. }
  188. refresh() {
  189. const { id } = this.params;
  190. const { module } = this.state;
  191. switch (module) {
  192. case 'video':
  193. break;
  194. case 'online':
  195. bindSearch(this.onlineFilter, 'timeId', this, (search) => {
  196. return Course.listTime(Object.assign({ courseId: id }, search));
  197. }, (row) => {
  198. return {
  199. title: `${formatDate(row.startTime, 'YYYY-MM-DD')}~${formatDate(row.endTime, 'YYYY-MM-DD')}`,
  200. value: row.id,
  201. };
  202. }, this.state.search.timeId ? Number(this.state.search.timeId) : null, null);
  203. bindSearch(this.onlineFilter, 'userId', this, (search) => {
  204. return User.list(search);
  205. }, (row) => {
  206. return {
  207. title: `${row.nickname}(${row.mobile})`,
  208. value: row.id,
  209. };
  210. }, null, null);
  211. bindSearch(this.onlineList, 'timeId', this, (search) => {
  212. return Course.listTime(Object.assign({ courseId: id }, search));
  213. }, (row) => {
  214. return {
  215. title: `${formatDate(row.startTime, 'YYYY-MM-DD')}~${formatDate(row.endTime, 'YYYY-MM-DD')}`,
  216. value: row.id,
  217. };
  218. }, null, null);
  219. bindSearch(this.onlineList, 'userIds', this, (search) => {
  220. return User.list(search);
  221. }, (row) => {
  222. return {
  223. title: `${row.nickname}(${row.mobile})`,
  224. value: row.id,
  225. };
  226. }, [], null);
  227. this.refreshOnline();
  228. break;
  229. case 'vs':
  230. this.refreshVs();
  231. break;
  232. default:
  233. }
  234. }
  235. refreshOnline() {
  236. const { id } = this.params;
  237. Course.listStudentOnline(Object.assign({ courseId: id }, this.state.search)).then(result => {
  238. this.setTableData(result.list, result.total);
  239. });
  240. }
  241. refreshVs() {
  242. const { id } = this.params;
  243. Course.listStudentVs(Object.assign({ courseId: id }, this.state.search)).then(result => {
  244. this.setTableData(result.list, result.total);
  245. });
  246. }
  247. addOnlineStudentAction() {
  248. const { id } = this.params;
  249. asyncForm('添加', this.onlineList, {}, data => {
  250. return Promise.all(data.userIds.map(userId => Course.addStudentOnline({ courseId: id, timeId: data.timeId, userId }))).then(() => {
  251. asyncSMessage('添加成功!');
  252. this.refresh();
  253. });
  254. }).catch(err => {
  255. console.log(err);
  256. });
  257. }
  258. deleteOnlineStudent(row) {
  259. Course.delStudentOnline({ id: row.id }).then(() => {
  260. asyncSMessage('删除成功!');
  261. this.refresh();
  262. });
  263. }
  264. changeVs(record) {
  265. const { id } = this.params;
  266. const { data } = this.state;
  267. record.time = [moment(record.useStartTime), moment(record.useEndTime)];
  268. this.vsList[4].type = data.vsType === 'novice' ? 'hidden' : 'number';
  269. asyncForm('修改', this.vsList, record, info => {
  270. // ([info.useStartTime, info.useEndTime] = info.time);
  271. return Course.editStudentVs(Object.assign({ courseId: id }, info)).then(() => {
  272. asyncSMessage('添加成功!');
  273. this.refresh();
  274. });
  275. }).then(component => {
  276. bindSearch(this.vsList, 'userId', component, (search) => {
  277. return User.list(search);
  278. }, (row) => {
  279. return {
  280. title: `${row.nickname}(${row.mobile})`,
  281. value: row.id,
  282. };
  283. }, record.userId, null);
  284. bindSearch(this.vsList, 'teacherId', component, (search) => {
  285. return Course.listTeacher(Object.assign({ courseId: id }, search));
  286. }, (row) => {
  287. return {
  288. title: row.realname,
  289. value: row.id,
  290. };
  291. }, record.teacherId, null);
  292. });
  293. }
  294. addVsStudentAction() {
  295. const { id } = this.params;
  296. const { data } = this.state;
  297. this.vsList[4].type = data.vsType === 'novice' ? 'hidden' : 'number';
  298. asyncForm('添加', this.vsList, {}, info => {
  299. if (data.vsType === 'novice') {
  300. // 写死:新手每次1课时
  301. info.number = 1;
  302. }
  303. // ([info.useStartTime, info.useEndTime] = info.time);
  304. return Course.addStudentVs(Object.assign({ courseId: id }, info)).then(() => {
  305. asyncSMessage('添加成功!');
  306. this.refresh();
  307. });
  308. }).then(component => {
  309. bindSearch(this.vsList, 'userId', component, (search) => {
  310. return User.list(search);
  311. }, (row) => {
  312. return {
  313. title: `${row.nickname}(${row.mobile})`,
  314. value: row.id,
  315. };
  316. }, null, null);
  317. bindSearch(this.vsList, 'teacherId', component, (search) => {
  318. return Course.listTeacher(Object.assign({ courseId: id }, search));
  319. }, (row) => {
  320. return {
  321. title: row.realname,
  322. value: row.id,
  323. };
  324. }, null, null);
  325. });
  326. }
  327. renderOnline() {
  328. return <Block flex>
  329. {<FilterLayout
  330. show
  331. itemList={this.onlineFilter}
  332. data={this.state.search}
  333. onChange={data => {
  334. this.search(data);
  335. }} />}
  336. <ActionLayout
  337. itemList={this.onlineAction}
  338. selectedKeys={this.state.selectedKeys}
  339. onAction={key => this.onAction(key)}
  340. />
  341. <TableLayout
  342. columns={this.onlineColumns}
  343. list={this.state.list}
  344. pagination={this.state.page}
  345. loading={this.props.core.loading}
  346. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  347. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  348. selectedKeys={this.state.selectedKeys}
  349. />
  350. </Block>;
  351. }
  352. renderVs() {
  353. return <Block flex>
  354. {<ActionLayout
  355. itemList={this.vsAction}
  356. selectedKeys={this.state.selectedKeys}
  357. onAction={key => this.onAction(key)}
  358. />}
  359. <TableLayout
  360. columns={this.vsColumns}
  361. list={this.state.list}
  362. pagination={this.state.page}
  363. loading={this.props.core.loading}
  364. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  365. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  366. selectedKeys={this.state.selectedKeys}
  367. />
  368. </Block>;
  369. }
  370. renderView() {
  371. switch (this.state.module) {
  372. case 'online':
  373. return [this.renderOnline()];
  374. case 'vs':
  375. return [this.renderVs()];
  376. case 'video':
  377. return [];
  378. default:
  379. return <div />;
  380. }
  381. }
  382. }