page.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 'special':
  65. this.refreshSpecial();
  66. break;
  67. case 'my':
  68. this.refreshMy();
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. refreshSpecial() {
  75. const { id } = this.params;
  76. this.setState({
  77. orderSelect: [
  78. { title: '无', key: '' },
  79. { title: '时间轴', key: 'position' },
  80. { title: '更新时间', key: 'updateTime' },
  81. { title: '查看次数', key: 'viewNumber' },
  82. ],
  83. });
  84. // paixu
  85. Course.listAsk(Object.assign({ courseId: id }, this.state.search)).then(result => {
  86. // 只显示单个提问
  87. if (this.state.search.askId) {
  88. const askId = Number(this.state.search.askId);
  89. result.list = result.list.filter(row => row.id === askId);
  90. }
  91. this.setState({ list: result.list, total: result.total });
  92. });
  93. }
  94. refreshMy() {
  95. const { id } = this.params;
  96. this.setState({
  97. orderSelect: [
  98. { title: '无', key: '' },
  99. { title: '时间轴', key: 'position' },
  100. { title: '更新时间', key: 'updateTime' },
  101. ],
  102. });
  103. My.listCourseAsk(Object.assign({ courseId: id }, this.state.search)).then(result => {
  104. this.setState({ list: result.list, total: result.total });
  105. });
  106. }
  107. onTabChange(tab) {
  108. const data = { tab };
  109. this.refreshQuery(data);
  110. }
  111. onFilter(value) {
  112. this.search(value, false);
  113. this.initData();
  114. }
  115. onSearch(value) {
  116. this.search({ keyword: value }, false);
  117. this.initData();
  118. }
  119. onAction() { }
  120. delAsk(id) {
  121. My.delCourseAsk(id).then(() => {
  122. this.refresh();
  123. });
  124. }
  125. viewAsk(id) {
  126. Course.askView(id);
  127. }
  128. renderView() {
  129. const { course = {}, courseNoMap = {} } = this.state;
  130. const { tab, courseNoSelect, answerSelect, timelineSelect, orderSelect, filterMap = {}, list = [] } = this.state;
  131. const { total, page } = this.state;
  132. const selectActionList = [
  133. {
  134. key: 'courseNoId',
  135. placeholder: '全部课时',
  136. select: courseNoSelect,
  137. },
  138. ];
  139. selectActionList.push({
  140. key: 'order',
  141. placeholder: '排序',
  142. select: orderSelect,
  143. });
  144. if (filterMap.order === 'position') {
  145. selectActionList.push({
  146. key: 'position',
  147. placeholder: '时间区间',
  148. select: timelineSelect,
  149. });
  150. }
  151. if (tab === 'my') {
  152. selectActionList.push({
  153. key: 'answerStatus',
  154. placeholder: '状态',
  155. select: answerSelect,
  156. });
  157. }
  158. return (
  159. <div>
  160. <div className="top content t-8">
  161. <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>
  162. <div className="f-r" onClick={() => linkTo(`/course/detail/${course.id}`)}>
  163. 返回课程
  164. </div>
  165. </div>
  166. <div className="center content">
  167. <div className="t-1 t-s-20 m-b-2">{course.title}</div>
  168. <Tabs
  169. border
  170. type="division"
  171. theme="theme"
  172. size="small"
  173. space={2.5}
  174. width={100}
  175. active={tab}
  176. tabs={[{ key: 'special', title: '精选问答' }, { key: 'my', title: '我的提问' }]}
  177. onChange={key => this.onTabChange(key)}
  178. />
  179. <UserAction
  180. search
  181. defaultSearch={filterMap.keyword}
  182. selectList={selectActionList}
  183. filterMap={filterMap}
  184. onFilter={value => this.onFilter(value)}
  185. onSearch={value => this.onSearch(value)}
  186. />
  187. {list.map(item => {
  188. return (
  189. <div className="answer-item">
  190. <div className="t-2">
  191. 课时{(courseNoMap[item.courseNoId] || {}).no} {item.position}:00~{item.position + 5}:00
  192. </div>
  193. <div className="t-2">课程内容: {(courseNoMap[item.courseNoId] || {}).title}</div>
  194. {tab === 'my' && item.answerStatus === 0 && (
  195. <div className="f-r">
  196. <Button radius size="small" onClick={() => this.delAsk(item.id)}>
  197. 删除
  198. </Button>
  199. </div>
  200. )}
  201. <div>
  202. <div className="small-tag">提问</div>
  203. <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>
  204. </div>
  205. <div className="desc">
  206. <OpenText>{item.content}</OpenText>
  207. </div>
  208. {item.answerStatus > 0 && (
  209. <div>
  210. <div className="small-tag">回答</div>
  211. <div className="f-r t-2 t-s-12">{formatDate(item.answerTime, 'YYYY-MM-DD HH:mm:ss')}</div>
  212. </div>
  213. )}
  214. {item.answerStatus > 0 && (
  215. <div className="desc">
  216. <OpenText onOpen={() => tab !== 'my' && this.viewAsk(item.id)}>
  217. {item.answerStatus === 2 ? '与课程内容无关,老师无法作出回答,敬请谅解。' : item.answer}
  218. </OpenText>
  219. </div>
  220. )}
  221. </div>
  222. );
  223. })}
  224. {total > 0 && list.length > 0 && (
  225. <UserPagination
  226. total={total}
  227. pageSize={this.state.search.size}
  228. current={page}
  229. onChange={p => this.onChangePage(p)}
  230. />
  231. )}
  232. </div>
  233. </div>
  234. );
  235. }
  236. }