page.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import { formatDate, getMap } from '@src/services/Tools';
  6. import UserAction from '../../../components/UserAction';
  7. import UserPagination from '../../../components/UserPagination';
  8. import Tabs from '../../../components/Tabs';
  9. import { OpenText } from '../../../components/Open';
  10. import { Button } from '../../../components/Button';
  11. import { Course } from '../../../stores/course';
  12. import { My } from '../../../stores/my';
  13. export default class extends Page {
  14. constructor(props) {
  15. props.size = 10;
  16. super(props);
  17. }
  18. initState() {
  19. return {
  20. filterMap: {},
  21. list: [],
  22. tab: 'special',
  23. answerSelect: [{ title: '全部', key: '' }, { title: '已回答', key: '1' }, { title: '未回答', key: '0' }],
  24. };
  25. }
  26. init() {
  27. const { id } = this.params;
  28. Course.get(id).then(result => {
  29. const courseNoSelect = result.courseNos.map(row => {
  30. return {
  31. title: row.title,
  32. key: `${row.id}`,
  33. };
  34. });
  35. courseNoSelect.unshift({
  36. title: '全部课时',
  37. key: '',
  38. });
  39. const courseNoMap = getMap(result.courseNos, 'id');
  40. const timelineSelect = [{ title: '全部区间', value: '' }];
  41. const max = Math.max(result.courseNos.map(row => row.time));
  42. let start = 0;
  43. let end = start + 5;
  44. while (start < max) {
  45. timelineSelect.push({
  46. title: `${start}:00~${end}:00`,
  47. key: `${start}`,
  48. });
  49. start += 5;
  50. end = Math.min(start + 5, max);
  51. }
  52. this.setState({ course: result, courseNoMap, courseNoSelect, timelineSelect });
  53. });
  54. }
  55. initData() {
  56. const data = Object.assign(this.state, this.state.search);
  57. if (data.order) {
  58. data.sortMap = { [data.order]: data.direction };
  59. }
  60. data.filterMap = this.state.search;
  61. this.setState(data);
  62. const { tab } = this.state;
  63. switch (tab) {
  64. case 'my':
  65. this.refreshMy();
  66. break;
  67. case 'special':
  68. default:
  69. this.refreshSpecial();
  70. break;
  71. }
  72. }
  73. refreshSpecial() {
  74. const { id } = this.params;
  75. this.setState({
  76. orderSelect: [
  77. { title: '无', key: '' },
  78. { title: '时间轴', key: 'position' },
  79. { title: '更新时间', key: 'updateTime' },
  80. { title: '查看次数', key: 'viewNumber' },
  81. ],
  82. });
  83. // paixu
  84. Course.listAsk(Object.assign({ courseId: id }, this.state.search)).then(result => {
  85. // 只显示单个提问
  86. if (this.state.search.askId) {
  87. const askId = Number(this.state.search.askId);
  88. result.list = result.list.filter(row => row.id === askId);
  89. }
  90. this.setState({ list: result.list, total: result.total });
  91. });
  92. }
  93. refreshMy() {
  94. const { id } = this.params;
  95. this.setState({
  96. orderSelect: [
  97. { title: '无', key: '' },
  98. { title: '时间轴', key: 'position' },
  99. { title: '更新时间', key: 'updateTime' },
  100. ],
  101. });
  102. My.listCourseAsk(Object.assign({ courseId: id }, this.state.search)).then(result => {
  103. this.setState({ list: result.list, total: result.total });
  104. });
  105. }
  106. onTabChange(tab) {
  107. const data = { tab };
  108. this.refreshQuery(data);
  109. }
  110. onFilter(value) {
  111. this.search(value, false);
  112. this.initData();
  113. }
  114. onSearch(value) {
  115. this.search({ keyword: value }, false);
  116. this.initData();
  117. }
  118. onChangePage(page) {
  119. this.search({ page }, false);
  120. this.initData();
  121. }
  122. onAction() { }
  123. delAsk(id) {
  124. My.delCourseAsk(id).then(() => {
  125. this.refresh();
  126. });
  127. }
  128. viewAsk(id) {
  129. Course.askView(id);
  130. }
  131. renderView() {
  132. const { course = {}, courseNoMap = {} } = this.state;
  133. const { tab, courseNoSelect, answerSelect, timelineSelect, orderSelect, filterMap = {}, list = [] } = this.state;
  134. const { total, page } = this.state;
  135. const selectActionList = [
  136. {
  137. key: 'courseNoId',
  138. placeholder: '全部课时',
  139. select: courseNoSelect,
  140. },
  141. ];
  142. selectActionList.push({
  143. key: 'order',
  144. placeholder: '排序',
  145. select: orderSelect,
  146. });
  147. if (filterMap.order === 'position') {
  148. selectActionList.push({
  149. key: 'position',
  150. placeholder: '时间区间',
  151. select: timelineSelect,
  152. });
  153. }
  154. if (tab === 'my') {
  155. selectActionList.push({
  156. key: 'answerStatus',
  157. placeholder: '状态',
  158. select: answerSelect,
  159. });
  160. }
  161. return (
  162. <div>
  163. <div className="top content t-8">
  164. <Link to="/course">千行课堂</Link> > <Link to="/course/online?tab=single">全部课程</Link> > <Link to={`/course/detail/${course.id}`}>{course.title}</Link> > <span className="t-1">全部问答</span>
  165. <div className="f-r" onClick={() => linkTo(`/course/detail/${course.id}`)}>
  166. 返回课程
  167. </div>
  168. </div>
  169. <div className="center content">
  170. <div className="t-1 t-s-20 m-b-2">{course.title}</div>
  171. <Tabs
  172. border
  173. type="division"
  174. theme="theme"
  175. size="small"
  176. space={2.5}
  177. width={100}
  178. active={tab}
  179. tabs={[{ key: 'special', title: '精选问答' }, { key: 'my', title: '我的提问' }]}
  180. onChange={key => this.onTabChange(key)}
  181. />
  182. <UserAction
  183. search
  184. defaultSearch={filterMap.keyword}
  185. selectList={selectActionList}
  186. filterMap={filterMap}
  187. onFilter={value => this.onFilter(value)}
  188. onSearch={value => this.onSearch(value)}
  189. />
  190. {list.map(item => {
  191. return (
  192. <div className="answer-item">
  193. <div className="t-2">
  194. 课时{(courseNoMap[item.courseNoId] || {}).no} {item.position}:00~{item.position + 5}:00
  195. </div>
  196. <div className="t-2">课程内容: {(courseNoMap[item.courseNoId] || {}).title}</div>
  197. {tab === 'my' && item.answerStatus === 0 && (
  198. <div className="f-r">
  199. <Button radius size="small" onClick={() => this.delAsk(item.id)}>
  200. 删除
  201. </Button>
  202. </div>
  203. )}
  204. <div>
  205. <div className="small-tag">提问</div>
  206. <div className="f-r t-2 t-s-12">{formatDate(item.createTime, 'YYYY-MM-DD HH:mm:ss')} <span hidden={tab === 'my'}>阅读 {item.viewNumber}</span></div>
  207. </div>
  208. <div className="desc">
  209. <OpenText>{item.content}</OpenText>
  210. </div>
  211. {item.answerStatus > 0 && (
  212. <div>
  213. <div className="small-tag">回答</div>
  214. <div className="f-r t-2 t-s-12">{formatDate(item.answerTime, 'YYYY-MM-DD HH:mm:ss')}</div>
  215. </div>
  216. )}
  217. {item.answerStatus > 0 && (
  218. <div className="desc">
  219. <OpenText onOpen={() => tab !== 'my' && this.viewAsk(item.id)}>
  220. {item.answerStatus === 2 ? '与课程内容无关,老师无法作出回答,敬请谅解。' : item.answer}
  221. </OpenText>
  222. </div>
  223. )}
  224. </div>
  225. );
  226. })}
  227. {total > 0 && list.length > 0 && (
  228. <UserPagination
  229. total={total}
  230. pageSize={this.state.search.size}
  231. current={page}
  232. onChange={p => this.onChangePage(p)}
  233. />
  234. )}
  235. </div>
  236. </div>
  237. );
  238. }
  239. }