page.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 { timeRange, getMap, formatDate } from '@src/services/Tools';
  6. import UserLayout from '../../../layouts/User';
  7. import UserTable from '../../../components/UserTable';
  8. import UserAction from '../../../components/UserAction';
  9. import UserPagination from '../../../components/UserPagination';
  10. import { RealAuth } from '../../../components/OtherModal';
  11. import Examination from '../../../components/Examination';
  12. import VipRenew from '../../../components/VipRenew';
  13. import Modal from '../../../components/Modal';
  14. import menu, { refreshQuestionType, refreshStruct } from '../index';
  15. import Tabs from '../../../components/Tabs';
  16. import { TimeRange, QuestionType, AskTarget } from '../../../../Constant';
  17. import { My } from '../../../stores/my';
  18. import { OpenText } from '../../../components/Open';
  19. const QuestionTypeMap = getMap(QuestionType, 'value', 'label');
  20. const AskTargetMap = getMap(AskTarget, 'value', 'label');
  21. const questionColumns = [
  22. {
  23. key: 'questionType',
  24. width: 140,
  25. render(text, row) {
  26. return <div className="group">
  27. <Link to={row.userQuestionId ? `/paper/question/${row.userQuestionId}` : `/question/detail/${row.questionNoId}`}>{QuestionTypeMap[text]}</Link>
  28. </div>;
  29. },
  30. },
  31. {
  32. key: 'title',
  33. width: 100,
  34. render(text, row) {
  35. return <div className="group">
  36. <Link to={row.userQuestionId ? `/paper/question/${row.userQuestionId}` : `/question/detail/${row.questionNoId}`}>{text}</Link>
  37. </div>;
  38. },
  39. },
  40. {
  41. key: 'content',
  42. width: 540,
  43. render(text, row) {
  44. return <div className="group text-hidden"><Link to={row.userQuestionId ? `/paper/question/${row.userQuestionId}` : `/question/detail/${row.questionNoId}`}>{text}</Link></div>;
  45. },
  46. },
  47. ];
  48. const contentColumns = [
  49. {
  50. key: 'title',
  51. title: '笔记对象',
  52. width: 140,
  53. render(text) {
  54. return <div className="sub">{AskTargetMap[text]}</div>;
  55. },
  56. },
  57. {
  58. key: 'updateTime',
  59. title: '更新时间',
  60. width: 100,
  61. render(text) {
  62. return <div className="sub">
  63. <div className="t-2 t-s-12">{text.split(' ')[0]}</div>
  64. <div className="t-6 t-s-12">{text.split(' ')[1]}</div>
  65. </div>;
  66. },
  67. },
  68. {
  69. key: 'content',
  70. title: '内容',
  71. width: 540,
  72. render(text) {
  73. return <OpenText>{text}</OpenText>;
  74. },
  75. },
  76. ];
  77. export default class extends Page {
  78. constructor(props) {
  79. props.size = 10;
  80. super(props);
  81. }
  82. initState() {
  83. return {
  84. filterMap: {},
  85. sortMap: {},
  86. selectList: [],
  87. contentSelectList: [],
  88. allChecked: false,
  89. tab: 'exercise',
  90. timerange: 'today',
  91. };
  92. }
  93. initData() {
  94. const data = Object.assign(this.state, this.state.search);
  95. data.filterMap = this.state.search;
  96. if (data.order) {
  97. data.sortMap = { [data.order]: data.direction };
  98. }
  99. if (data.timerange) {
  100. data.filterMap.timerange = data.timerange;
  101. }
  102. const [startTime, endTime] = timeRange(data.timerange);
  103. refreshQuestionType(this, data.subject, data.questionType, {
  104. all: true,
  105. needSentence: false,
  106. allSubject: true,
  107. }).then(({ questionTypes }) => {
  108. return refreshStruct(this, data.tab, data.one, data.two, {
  109. all: true,
  110. needPreview: false,
  111. needTextbook: false,
  112. }).then(({ structIds, latest, year }) => {
  113. My.listQuestionNote(
  114. Object.assign(
  115. { module: data.tab, questionTypes, structIds, latest, year, startTime, endTime },
  116. this.state.search,
  117. {
  118. order: Object.keys(data.sortMap)
  119. .map(key => {
  120. return `${key} ${data.sortMap[key]}`;
  121. })
  122. .join(','),
  123. },
  124. ),
  125. ).then(result => {
  126. result.list = result.list.map(row => {
  127. row.key = row.questionNoId;
  128. row.group = true;
  129. row.questionType = row.question.questionType;
  130. row.title = row.questionNo.title;
  131. row.content = row.question.description;
  132. row.list = [];
  133. AskTarget.forEach((r) => {
  134. if (!row[`${r.value}Content`]) return;
  135. row.list.push({
  136. title: r.value,
  137. key: `${row.key}|${r.value}`,
  138. updateTime: formatDate(row[`${r.value}Time`], 'YYYY-MM-DD HH:mm:ss'),
  139. content: row[`${r.value}Content`],
  140. });
  141. });
  142. return row;
  143. });
  144. this.setState({ list: result.list, total: result.total, page: data.page });
  145. });
  146. });
  147. });
  148. }
  149. onTabChange(tab) {
  150. const data = { tab };
  151. this.refreshQuery(data);
  152. }
  153. onFilter(value) {
  154. this.search(value, false);
  155. this.initData();
  156. }
  157. onSearch(value) {
  158. this.search({ keyword: value }, false);
  159. this.initData();
  160. }
  161. onSort(value) {
  162. const keys = Object.keys(value);
  163. // this.search({ order: keys.length ? keys.join('|') : null, direction: keys.length ? Object.values(value).join('|') : null });
  164. const { sortMap } = this.state;
  165. const index = keys.length > 1 && sortMap[keys[0]] ? 1 : 0;
  166. this.search({ order: keys.length ? keys[index] : null, direction: keys.length ? value[keys[index]] : null }, false);
  167. }
  168. onChangePage(page) {
  169. this.search({ page }, false);
  170. this.initData();
  171. }
  172. onAll(checked) {
  173. const selectList = [];
  174. if (checked) {
  175. const { list = [] } = this.state;
  176. list.forEach(item => {
  177. if (selectList.indexOf(item.key) >= 0) return;
  178. selectList.push(item.key);
  179. });
  180. }
  181. this.setState({ selectList, allChecked: checked });
  182. }
  183. onSelect(selectList) {
  184. this.setState({ selectList });
  185. }
  186. onSelectContent(contentSelectList) {
  187. this.setState({ contentSelectList });
  188. }
  189. onAction(key) {
  190. const { info } = this.props.user;
  191. const { selectList, contentSelectList } = this.state;
  192. const questionNoMap = {};
  193. let questionNoIds;
  194. switch (key) {
  195. case 'help':
  196. this.setState({ showTips: true });
  197. return;
  198. case 'clear':
  199. if (selectList.length === 0 && contentSelectList === 0) {
  200. this.setState({ showWarn: true, warn: { title: '移除', content: '不可少于1题,请重新选择' } });
  201. return;
  202. }
  203. contentSelectList.forEach(row => {
  204. const [questionNoId, target] = row.split('|');
  205. if (selectList.indexOf(questionNoId) >= 0) return;
  206. if (!questionNoMap[questionNoId]) {
  207. questionNoMap[questionNoId] = {};
  208. }
  209. questionNoMap[questionNoId][target] = '';
  210. });
  211. questionNoIds = Object.keys(questionNoMap);
  212. if (questionNoIds.length > 0) {
  213. Promise.all(questionNoIds.map(row => {
  214. return My.updateQuestionNote(row, questionNoMap);
  215. }))
  216. .then(() => {
  217. if (selectList.length > 0) {
  218. this.clearNote();
  219. } else {
  220. this.refresh();
  221. }
  222. });
  223. } else {
  224. this.clearNote();
  225. }
  226. // this.setState({ showClearConfirm: true, clearInfo: { questionNoIds: selectList } });
  227. break;
  228. case 'export':
  229. if (!info.vip) {
  230. this.setState({ showVip: true });
  231. return;
  232. }
  233. if (selectList.length < 1) {
  234. this.setState({ showWarn: true, warn: { title: '导出', content: '不可小于1题,请重新选择' } });
  235. return;
  236. }
  237. if (selectList.length > 100) {
  238. this.setState({ showWarn: true, warn: { title: '导出', content: '不可多余100题,请重新选择' } });
  239. return;
  240. }
  241. this.setState({ showExportConfirm: true, exportInfo: { questionNoIds: selectList } });
  242. break;
  243. default:
  244. }
  245. }
  246. clearNote() {
  247. const { clearInfo } = this.state;
  248. My.clearQuestionNote(clearInfo.questionNoIds)
  249. .then(() => {
  250. this.refresh();
  251. })
  252. .catch(e => {
  253. this.setState({ warn: { title: '移除', content: e.message }, showClearConfirm: false });
  254. });
  255. }
  256. renderView() {
  257. const { config } = this.props;
  258. return <UserLayout active={config.key} menu={menu} center={this.renderTable()} />;
  259. }
  260. renderTable() {
  261. const {
  262. tab,
  263. questionSubjectSelect,
  264. questionSubjectMap = {},
  265. oneSelect,
  266. twoSelectMap = {},
  267. filterMap = {},
  268. sortMap = {},
  269. list = [],
  270. } = this.state;
  271. const { selectList = [], contentSelectList = [], allChecked, page, total } = this.state;
  272. const { info } = this.props.user;
  273. return (
  274. <div className="table-layout">
  275. <Tabs
  276. border
  277. type="division"
  278. theme="theme"
  279. size="small"
  280. space={2.5}
  281. width={100}
  282. active={tab}
  283. tabs={[{ key: 'exercise', title: '练习' }, { key: 'examination', title: '模考' }]}
  284. onChange={key => this.onTabChange(key)}
  285. />
  286. <UserAction
  287. search
  288. defaultSearch={filterMap.keyword}
  289. selectList={[
  290. {
  291. children: [
  292. {
  293. key: 'subject',
  294. placeholder: '学科',
  295. select: questionSubjectSelect,
  296. },
  297. {
  298. placeholder: '题型',
  299. key: 'questionType',
  300. be: 'subject',
  301. selectMap: questionSubjectMap,
  302. },
  303. ],
  304. },
  305. {
  306. label: '范围',
  307. children: [
  308. {
  309. key: 'one',
  310. placeholder: '全部',
  311. select: oneSelect,
  312. },
  313. {
  314. key: 'two',
  315. be: 'one',
  316. placeholder: '全部',
  317. selectMap: twoSelectMap,
  318. },
  319. ],
  320. },
  321. {
  322. right: true,
  323. key: 'timerange',
  324. select: TimeRange,
  325. },
  326. ]}
  327. filterMap={filterMap}
  328. onFilter={value => this.onFilter(value)}
  329. onSearch={value => this.onSearch(value)}
  330. />
  331. <UserAction
  332. allCheckbox
  333. allChecked={allChecked}
  334. help
  335. btnList={[
  336. { title: '删除', key: 'clear' },
  337. { title: '导出', key: 'export', tag: 'vip', disabled: !info.vip },
  338. ]}
  339. sortList={[{ right: true, label: '更新时间', key: 'update_time' }]}
  340. sortMap={sortMap}
  341. onSort={value => this.onSort(value)}
  342. onAll={checked => this.onAll(checked)}
  343. onAction={key => this.onAction(key)}
  344. />
  345. {list.map((item, index) => {
  346. return (
  347. <div className="group">
  348. <UserTable
  349. theme="dark"
  350. border={false}
  351. size="small"
  352. select
  353. selectList={selectList}
  354. columns={questionColumns}
  355. data={[item]}
  356. onSelect={l => this.onSelect(l)}
  357. header={false}
  358. />
  359. <UserTable
  360. border={false}
  361. size="small"
  362. select
  363. even="default"
  364. selectList={contentSelectList}
  365. columns={contentColumns}
  366. data={item.list}
  367. onSelect={l => this.onSelectContent(l)}
  368. header={index === 0}
  369. />
  370. </div>
  371. );
  372. })}
  373. {total > 0 && list.length > 0 && (
  374. <UserPagination total={total} current={page} pageSize={this.state.search.size} onChange={p => this.onChangePage(p)} />
  375. )}
  376. {this.renderModal()}
  377. </div>
  378. );
  379. }
  380. renderModal() {
  381. const { showWarn, warn = {}, showClearConfirm, clearInfo = {}, showVip, showExamination, showReal, showExportWait, showExportConfirm, exportInfo = {} } = this.state;
  382. const { info } = this.props.user;
  383. return [
  384. <Modal show={showWarn} title={warn.title} confirmText="好的,知道了" btnAlign="center" onConfirm={() => this.setState({ showWarn: false })}>
  385. <div className="t-2 t-s-18">{warn.content}</div>
  386. </Modal>,
  387. <Modal show={showClearConfirm} title="移出" onConfirm={() => this.clearNote()} onCancel={() => this.setState({ showClearConfirm: false })}>
  388. <div className="t-2 t-s-18">
  389. 当前选中的 <span className="t-4">{clearInfo.questionNoIds ? clearInfo.questionNoIds.length : 0}</span> 道题即将被移出笔记,移出后无法恢复,是否继续?
  390. </div>
  391. </Modal>,
  392. <Modal show={showExportConfirm} title="导出" confirmText="导出" onConfirm={() => this.export()} onCancel={() => this.setState({ showExportConfirm: false })}>
  393. <div className="t-2 t-s-18 m-b-5">
  394. 当前共选中 <span className="t-4">{exportInfo.questionNoIds ? exportInfo.questionNoIds.length : 0}</span> 道题笔记,是否开始导出:
  395. </div>
  396. </Modal>,
  397. <Modal show={showExportWait} title="导出" confirmText="好的,知道了" btnAlign="center" onConfirm={() => this.setState({ showExportWait: false })}>
  398. <div className="t-2 t-s-18">正在下载中,请不要关闭当前页面!</div>
  399. </Modal>,
  400. <Examination
  401. show={showExamination}
  402. data={info}
  403. onConfirm={() => this.setState({ showExamination: false })}
  404. onCancel={() => this.setState({ showExamination: false })}
  405. onClose={() => this.setState({ showExamination: false })}
  406. />,
  407. <RealAuth show={showReal} data={info} onConfirm={() => this.setState({ showReal: false })} />,
  408. <VipRenew
  409. show={showVip}
  410. data={info}
  411. onReal={() => this.setState({ showVip: false, showReal: true })}
  412. onPrepare={() => this.setState({ showVip: false, showExamination: true })}
  413. onClose={() => this.setState({ showVip: false })}
  414. />,
  415. ];
  416. }
  417. }