page.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 (!questionNoMap[questionNoId]) {
  265. questionNoMap[questionNoId] = { fields: 0 };
  266. }
  267. questionNoMap[questionNoId][target] = '';
  268. questionNoMap[questionNoId].fields += 1;
  269. });
  270. questionNoIds = Object.keys(questionNoMap).filter(row => questionNoMap[row].fields < AskTarget.length);
  271. if (questionNoIds.length > 0) {
  272. Promise.all(questionNoIds.map(row => {
  273. return My.updateQuestionNote(row, questionNoMap);
  274. }))
  275. .then(() => {
  276. const clearList = Object.keys(questionNoMap).filter(row => questionNoMap[row].fields === AskTarget.length);
  277. if (clearList.length > 0) {
  278. this.clearNote(clearList);
  279. } else {
  280. this.refresh();
  281. }
  282. });
  283. }
  284. // this.setState({ showClearConfirm: true, clearInfo: { questionNoIds: selectList } });
  285. break;
  286. case 'export':
  287. if (!info.vip) {
  288. this.setState({ showVip: true });
  289. return;
  290. }
  291. if (selectList.length < 1) {
  292. this.setState({ showWarn: true, warn: { title: '导出', content: '不可小于1题,请重新选择' } });
  293. return;
  294. }
  295. if (selectList.length > 100) {
  296. this.setState({ showWarn: true, warn: { title: '导出', content: '不可多余100题,请重新选择' } });
  297. return;
  298. }
  299. contentSelectList.forEach(row => {
  300. const [questionNoIdStr, target] = row.split('|');
  301. const questionNoId = Number(questionNoIdStr);
  302. if (!questionNoMap[questionNoId]) {
  303. questionNoMap[questionNoId] = { fields: 0 };
  304. }
  305. questionNoMap[questionNoId][target] = '';
  306. questionNoMap[questionNoId].fields += 1;
  307. });
  308. questionNoIds = Object.keys(questionNoMap);
  309. this.setState({ showExportConfirm: true, exportInfo: { questionNoIds, questionNoMap } });
  310. break;
  311. default:
  312. }
  313. }
  314. clearNote(list) {
  315. My.clearQuestionNote(list)
  316. .then(() => {
  317. this.refresh();
  318. })
  319. .catch(e => {
  320. this.setState({ warn: { title: '移除', content: e.message }, showClearConfirm: false });
  321. });
  322. }
  323. renderView() {
  324. const { config } = this.props;
  325. return <UserLayout active={config.key} menu={menu} center={this.renderTable()} />;
  326. }
  327. renderTable() {
  328. const {
  329. tab,
  330. questionSubjectSelect,
  331. questionSubjectMap = {},
  332. oneSelect,
  333. twoSelectMap = {},
  334. filterMap = {},
  335. sortMap = {},
  336. list = [],
  337. } = this.state;
  338. const { selectList = [], contentSelectList = [], allChecked, page, total } = this.state;
  339. const { info } = this.props.user;
  340. return (
  341. <div className="table-layout">
  342. <Tabs
  343. border
  344. type="division"
  345. theme="theme"
  346. size="small"
  347. space={2.5}
  348. width={100}
  349. active={tab}
  350. tabs={[{ key: 'exercise', title: '练习' }, { key: 'examination', title: '模考' }]}
  351. onChange={key => this.onTabChange(key)}
  352. />
  353. <UserAction
  354. search
  355. defaultSearch={filterMap.keyword}
  356. selectList={[
  357. {
  358. children: [
  359. {
  360. key: 'subject',
  361. placeholder: '学科',
  362. select: questionSubjectSelect,
  363. },
  364. {
  365. placeholder: '题型',
  366. key: 'questionType',
  367. be: 'subject',
  368. selectMap: questionSubjectMap,
  369. },
  370. ],
  371. },
  372. {
  373. label: '范围',
  374. children: [
  375. {
  376. key: 'one',
  377. placeholder: '全部',
  378. select: oneSelect,
  379. },
  380. {
  381. key: 'two',
  382. be: 'one',
  383. placeholder: '全部',
  384. selectMap: twoSelectMap,
  385. },
  386. ],
  387. },
  388. {
  389. right: true,
  390. key: 'timerange',
  391. select: TimeRange,
  392. },
  393. ]}
  394. filterMap={filterMap}
  395. onFilter={value => this.onFilter(value)}
  396. onSearch={value => this.onSearch(value)}
  397. />
  398. <UserAction
  399. allCheckbox
  400. allChecked={allChecked}
  401. help
  402. btnList={[
  403. { title: '删除', key: 'clear' },
  404. { title: '导出', key: 'export', tag: 'vip', disabled: !info.vip },
  405. ]}
  406. sortList={[{ right: true, label: '更新时间', key: 'update_time' }]}
  407. sortMap={sortMap}
  408. onSort={value => this.onSort(value)}
  409. onAll={checked => this.onAll(checked)}
  410. onAction={key => this.onAction(key)}
  411. />
  412. {list.map((item, index) => {
  413. return (
  414. <div className="group">
  415. <UserTable
  416. theme="dark"
  417. border={false}
  418. size="small"
  419. select
  420. selectList={selectList}
  421. columns={questionColumns}
  422. data={[item]}
  423. onSelect={(l, key, checked) => this.onSelect(l, key, checked)}
  424. header={false}
  425. />
  426. <UserTable
  427. border={false}
  428. size="small"
  429. select
  430. even="default"
  431. selectList={contentSelectList}
  432. columns={contentColumns}
  433. data={item.list}
  434. onSelect={(l, key, checked) => this.onSelectContent(l, key, checked)}
  435. header={index === 0}
  436. />
  437. </div>
  438. );
  439. })}
  440. {total > 0 && list.length > 0 && (
  441. <UserPagination total={total} current={page} pageSize={this.state.search.size} onChange={p => this.onChangePage(p)} />
  442. )}
  443. {this.renderModal()}
  444. </div>
  445. );
  446. }
  447. renderModal() {
  448. const { showWarn, warn = {}, showClearConfirm, clearInfo = {}, showVip, showExamination, showReal, showExportWait, showExportConfirm, exportInfo = {} } = this.state;
  449. const { info } = this.props.user;
  450. return [
  451. <Modal show={showWarn} title={warn.title} confirmText="好的,知道了" btnAlign="center" onConfirm={() => this.setState({ showWarn: false })}>
  452. <div className="t-2 t-s-18">{warn.content}</div>
  453. </Modal>,
  454. <Modal show={showClearConfirm} title="移出" onConfirm={() => this.clearNote()} onCancel={() => this.setState({ showClearConfirm: false })}>
  455. <div className="t-2 t-s-18">
  456. 当前选中的 <span className="t-4">{clearInfo.questionNoIds ? clearInfo.questionNoIds.length : 0}</span> 道题即将被移出笔记,移出后无法恢复,是否继续?
  457. </div>
  458. </Modal>,
  459. <Modal show={showExportConfirm} title="导出" confirmText="导出" onConfirm={() => this.export()} onCancel={() => this.setState({ showExportConfirm: false })}>
  460. <div className="t-2 t-s-18 m-b-5">
  461. 当前共选中 <span className="t-4">{exportInfo.questionNoIds ? exportInfo.questionNoIds.length : 0}</span> 道题笔记,是否开始导出:
  462. </div>
  463. </Modal>,
  464. <Modal show={showExportWait} title="导出" confirmText="好的,知道了" btnAlign="center" onConfirm={() => this.setState({ showExportWait: false })}>
  465. <div className="t-2 t-s-18">正在下载中,请不要关闭当前页面!</div>
  466. </Modal>,
  467. <Examination
  468. show={showExamination}
  469. data={info}
  470. onConfirm={() => this.setState({ showExamination: false })}
  471. onCancel={() => this.setState({ showExamination: false })}
  472. onClose={() => this.setState({ showExamination: false })}
  473. />,
  474. <RealAuth show={showReal} data={info} onConfirm={() => this.setState({ showReal: false })} />,
  475. <VipRenew
  476. show={showVip}
  477. data={info}
  478. onReal={() => this.setState({ showVip: false, showReal: true })}
  479. onPrepare={() => this.setState({ showVip: false, showExamination: true })}
  480. onClose={() => this.setState({ showVip: false })}
  481. />,
  482. ];
  483. }
  484. }