page.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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, contentSelectList } = this.state;
  174. const { list = [] } = this.state;
  175. if (checked) {
  176. list.forEach(item => {
  177. if (selectList.indexOf(item.key) < 0) {
  178. selectList.push(item.key);
  179. }
  180. AskTarget.forEach((r) => {
  181. if (!item[`${r.value}Content`]) return;
  182. if (contentSelectList.indexOf(`${item.key}|${r.value}`) < 0) {
  183. contentSelectList.push(`${item.key}|${r.value}`);
  184. }
  185. });
  186. });
  187. } else {
  188. list.forEach(item => {
  189. const index = selectList.indexOf(item.key);
  190. if (index >= 0) {
  191. selectList.splice(index, 1);
  192. }
  193. AskTarget.forEach((r) => {
  194. if (!item[`${r.value}Content`]) return;
  195. const i = contentSelectList.indexOf(`${item.key}|${r.value}`);
  196. if (i >= 0) {
  197. contentSelectList.splice(i, 1);
  198. }
  199. });
  200. });
  201. }
  202. this.setState({ selectList, contentSelectList, allChecked: checked });
  203. }
  204. onSelect(selectList, key, checked) {
  205. const { contentSelectList, list = [] } = this.state;
  206. if (checked) {
  207. const [item] = list.filter(row => row.key === key);
  208. if (item) {
  209. // 选中下面所有
  210. AskTarget.forEach((r) => {
  211. if (!item[`${r.value}Content`]) return;
  212. if (contentSelectList.indexOf(`${item.key}|${r.value}`) < 0) {
  213. contentSelectList.push(`${item.key}|${r.value}`);
  214. }
  215. });
  216. }
  217. } else {
  218. const [item] = list.filter(row => row.key === key);
  219. if (item) {
  220. // 取消下面所有
  221. AskTarget.forEach((r) => {
  222. if (!item[`${r.value}Content`]) return;
  223. const index = contentSelectList.indexOf(`${item.key}|${r.value}`);
  224. if (index >= 0) {
  225. contentSelectList.splice(index, 1);
  226. }
  227. });
  228. }
  229. }
  230. this.setState({ selectList, contentSelectList, allCheckbox: false });
  231. }
  232. onSelectContent(contentSelectList, key, checked) {
  233. const { selectList, list = [] } = this.state;
  234. if (checked) {
  235. const [questionNoIdStr] = key.split('|');
  236. const questionNoId = Number(questionNoIdStr);
  237. const [item] = list.filter(row => row.key === questionNoId);
  238. if (item) {
  239. // 选中上级
  240. if (selectList.indexOf(item.key) < 0) {
  241. selectList.push(item.key);
  242. }
  243. }
  244. }
  245. this.setState({ selectList, contentSelectList });
  246. }
  247. onAction(key) {
  248. const { info } = this.props.user;
  249. const { selectList, contentSelectList } = this.state;
  250. const questionNoMap = {};
  251. let questionNoIds;
  252. switch (key) {
  253. case 'help':
  254. this.setState({ showTips: true });
  255. return;
  256. case 'clear':
  257. if (selectList.length === 0 && contentSelectList === 0) {
  258. this.setState({ showWarn: true, warn: { title: '移除', content: '不可少于1题,请重新选择' } });
  259. return;
  260. }
  261. contentSelectList.forEach(row => {
  262. const [questionNoIdStr, target] = row.split('|');
  263. const questionNoId = Number(questionNoIdStr);
  264. if (selectList.indexOf(questionNoId) >= 0) return;
  265. if (!questionNoMap[questionNoId]) {
  266. questionNoMap[questionNoId] = {};
  267. }
  268. questionNoMap[questionNoId][target] = '';
  269. });
  270. questionNoIds = Object.keys(questionNoMap);
  271. if (questionNoIds.length > 0) {
  272. Promise.all(questionNoIds.map(row => {
  273. return My.updateQuestionNote(row, questionNoMap);
  274. }))
  275. .then(() => {
  276. if (selectList.length > 0) {
  277. this.clearNote();
  278. } else {
  279. this.refresh();
  280. }
  281. });
  282. } else {
  283. this.clearNote();
  284. }
  285. // this.setState({ showClearConfirm: true, clearInfo: { questionNoIds: selectList } });
  286. break;
  287. case 'export':
  288. if (!info.vip) {
  289. this.setState({ showVip: true });
  290. return;
  291. }
  292. if (selectList.length < 1) {
  293. this.setState({ showWarn: true, warn: { title: '导出', content: '不可小于1题,请重新选择' } });
  294. return;
  295. }
  296. if (selectList.length > 100) {
  297. this.setState({ showWarn: true, warn: { title: '导出', content: '不可多余100题,请重新选择' } });
  298. return;
  299. }
  300. this.setState({ showExportConfirm: true, exportInfo: { questionNoIds: selectList } });
  301. break;
  302. default:
  303. }
  304. }
  305. clearNote() {
  306. const { clearInfo } = this.state;
  307. My.clearQuestionNote(clearInfo.questionNoIds)
  308. .then(() => {
  309. this.refresh();
  310. })
  311. .catch(e => {
  312. this.setState({ warn: { title: '移除', content: e.message }, showClearConfirm: false });
  313. });
  314. }
  315. renderView() {
  316. const { config } = this.props;
  317. return <UserLayout active={config.key} menu={menu} center={this.renderTable()} />;
  318. }
  319. renderTable() {
  320. const {
  321. tab,
  322. questionSubjectSelect,
  323. questionSubjectMap = {},
  324. oneSelect,
  325. twoSelectMap = {},
  326. filterMap = {},
  327. sortMap = {},
  328. list = [],
  329. } = this.state;
  330. const { selectList = [], contentSelectList = [], allChecked, page, total } = this.state;
  331. const { info } = this.props.user;
  332. return (
  333. <div className="table-layout">
  334. <Tabs
  335. border
  336. type="division"
  337. theme="theme"
  338. size="small"
  339. space={2.5}
  340. width={100}
  341. active={tab}
  342. tabs={[{ key: 'exercise', title: '练习' }, { key: 'examination', title: '模考' }]}
  343. onChange={key => this.onTabChange(key)}
  344. />
  345. <UserAction
  346. search
  347. defaultSearch={filterMap.keyword}
  348. selectList={[
  349. {
  350. children: [
  351. {
  352. key: 'subject',
  353. placeholder: '学科',
  354. select: questionSubjectSelect,
  355. },
  356. {
  357. placeholder: '题型',
  358. key: 'questionType',
  359. be: 'subject',
  360. selectMap: questionSubjectMap,
  361. },
  362. ],
  363. },
  364. {
  365. label: '范围',
  366. children: [
  367. {
  368. key: 'one',
  369. placeholder: '全部',
  370. select: oneSelect,
  371. },
  372. {
  373. key: 'two',
  374. be: 'one',
  375. placeholder: '全部',
  376. selectMap: twoSelectMap,
  377. },
  378. ],
  379. },
  380. {
  381. right: true,
  382. key: 'timerange',
  383. select: TimeRange,
  384. },
  385. ]}
  386. filterMap={filterMap}
  387. onFilter={value => this.onFilter(value)}
  388. onSearch={value => this.onSearch(value)}
  389. />
  390. <UserAction
  391. allCheckbox
  392. allChecked={allChecked}
  393. help
  394. btnList={[
  395. { title: '删除', key: 'clear' },
  396. { title: '导出', key: 'export', tag: 'vip', disabled: !info.vip },
  397. ]}
  398. sortList={[{ right: true, label: '更新时间', key: 'update_time' }]}
  399. sortMap={sortMap}
  400. onSort={value => this.onSort(value)}
  401. onAll={checked => this.onAll(checked)}
  402. onAction={key => this.onAction(key)}
  403. />
  404. {list.map((item, index) => {
  405. return (
  406. <div className="group">
  407. <UserTable
  408. theme="dark"
  409. border={false}
  410. size="small"
  411. select
  412. selectList={selectList}
  413. columns={questionColumns}
  414. data={[item]}
  415. onSelect={(l, key, checked) => this.onSelect(l, key, checked)}
  416. header={false}
  417. />
  418. <UserTable
  419. border={false}
  420. size="small"
  421. select
  422. even="default"
  423. selectList={contentSelectList}
  424. columns={contentColumns}
  425. data={item.list}
  426. onSelect={(l, key, checked) => this.onSelectContent(l, key, checked)}
  427. header={index === 0}
  428. />
  429. </div>
  430. );
  431. })}
  432. {total > 0 && list.length > 0 && (
  433. <UserPagination total={total} current={page} pageSize={this.state.search.size} onChange={p => this.onChangePage(p)} />
  434. )}
  435. {this.renderModal()}
  436. </div>
  437. );
  438. }
  439. renderModal() {
  440. const { showWarn, warn = {}, showClearConfirm, clearInfo = {}, showVip, showExamination, showReal, showExportWait, showExportConfirm, exportInfo = {} } = this.state;
  441. const { info } = this.props.user;
  442. return [
  443. <Modal show={showWarn} title={warn.title} confirmText="好的,知道了" btnAlign="center" onConfirm={() => this.setState({ showWarn: false })}>
  444. <div className="t-2 t-s-18">{warn.content}</div>
  445. </Modal>,
  446. <Modal show={showClearConfirm} title="移出" onConfirm={() => this.clearNote()} onCancel={() => this.setState({ showClearConfirm: false })}>
  447. <div className="t-2 t-s-18">
  448. 当前选中的 <span className="t-4">{clearInfo.questionNoIds ? clearInfo.questionNoIds.length : 0}</span> 道题即将被移出笔记,移出后无法恢复,是否继续?
  449. </div>
  450. </Modal>,
  451. <Modal show={showExportConfirm} title="导出" confirmText="导出" onConfirm={() => this.export()} onCancel={() => this.setState({ showExportConfirm: false })}>
  452. <div className="t-2 t-s-18 m-b-5">
  453. 当前共选中 <span className="t-4">{exportInfo.questionNoIds ? exportInfo.questionNoIds.length : 0}</span> 道题笔记,是否开始导出:
  454. </div>
  455. </Modal>,
  456. <Modal show={showExportWait} title="导出" confirmText="好的,知道了" btnAlign="center" onConfirm={() => this.setState({ showExportWait: false })}>
  457. <div className="t-2 t-s-18">正在下载中,请不要关闭当前页面!</div>
  458. </Modal>,
  459. <Examination
  460. show={showExamination}
  461. data={info}
  462. onConfirm={() => this.setState({ showExamination: false })}
  463. onCancel={() => this.setState({ showExamination: false })}
  464. onClose={() => this.setState({ showExamination: false })}
  465. />,
  466. <RealAuth show={showReal} data={info} onConfirm={() => this.setState({ showReal: false })} />,
  467. <VipRenew
  468. show={showVip}
  469. data={info}
  470. onReal={() => this.setState({ showVip: false, showReal: true })}
  471. onPrepare={() => this.setState({ showVip: false, showExamination: true })}
  472. onClose={() => this.setState({ showVip: false })}
  473. />,
  474. ];
  475. }
  476. }