1
0

page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.group = true;
  128. row.questionType = row.question.questionType;
  129. row.title = row.questionNo.title;
  130. row.content = row.question.description;
  131. row.list = [];
  132. AskTarget.forEach((r) => {
  133. if (!row[`${r.value}Content`]) return;
  134. row.list.push({
  135. title: r.value,
  136. updateTime: formatDate(row[`${r.value}Time`], 'YYYY-MM-DD HH:mm:ss'),
  137. content: row[`${r.value}Content`],
  138. });
  139. });
  140. return row;
  141. });
  142. this.setState({ list: result.list, total: result.total, page: data.page });
  143. });
  144. });
  145. });
  146. }
  147. onTabChange(tab) {
  148. const data = { tab };
  149. this.refreshQuery(data);
  150. }
  151. onFilter(value) {
  152. this.search(value);
  153. }
  154. onSort(value) {
  155. const keys = Object.keys(value);
  156. // this.search({ order: keys.length ? keys.join('|') : null, direction: keys.length ? Object.values(value).join('|') : null });
  157. const { sortMap } = this.state;
  158. const index = keys.length > 1 && sortMap[keys[0]] ? 1 : 0;
  159. this.search({ order: keys.length ? keys[index] : null, direction: keys.length ? value[keys[index]] : null });
  160. }
  161. onChangePage(page) {
  162. this.search({ page });
  163. }
  164. onAll(checked) {
  165. if (checked) {
  166. const { data = [] } = this.state;
  167. const list = [];
  168. data.forEach(item => {
  169. list.push(item.key);
  170. });
  171. this.setState({ selectList: list, allChecked: true });
  172. } else {
  173. this.setState({ selectList: [], allChecked: false });
  174. }
  175. }
  176. onSelect(selectList) {
  177. this.setState({ selectList });
  178. }
  179. onAction(key) {
  180. const { info } = this.props.user;
  181. const { selectList } = this.state;
  182. switch (key) {
  183. case 'help':
  184. this.setState({ showTips: true });
  185. return;
  186. case 'clear':
  187. if (selectList.length === 0) {
  188. this.setState({ showWarn: true, warn: { title: '移除', content: '不可少于1题,请重新选择' } });
  189. return;
  190. }
  191. // this.clearNote();
  192. this.setState({ showClearConfirm: true, clearInfo: { questionNoIds: selectList } });
  193. break;
  194. case 'export':
  195. if (!info.vip) {
  196. this.setState({ showVip: true });
  197. return;
  198. }
  199. if (selectList.length < 0) {
  200. this.setState({ showWarn: true, warn: { title: '导出', content: '不可小于10题,请重新选择' } });
  201. return;
  202. }
  203. if (selectList.length > 100) {
  204. this.setState({ showWarn: true, warn: { title: '导出', content: '不可多余100题,请重新选择' } });
  205. return;
  206. }
  207. this.setState({ showExportConfirm: true, exportInfo: { questionNoIds: selectList } });
  208. break;
  209. default:
  210. }
  211. }
  212. clearNote() {
  213. const { clearInfo } = this.state;
  214. My.clearQuestionNote(clearInfo.questionNoIds)
  215. .then(() => {
  216. this.refresh();
  217. })
  218. .catch(e => {
  219. this.setState({ warn: { title: '移除', content: e.message }, showClearConfirm: false });
  220. });
  221. }
  222. renderView() {
  223. const { config } = this.props;
  224. return <UserLayout active={config.key} menu={menu} center={this.renderTable()} />;
  225. }
  226. renderTable() {
  227. const {
  228. tab,
  229. questionSubjectSelect,
  230. questionSubjectMap = {},
  231. oneSelect,
  232. twoSelectMap = {},
  233. filterMap = {},
  234. sortMap = {},
  235. list = [],
  236. } = this.state;
  237. const { selectList = [], contentSelectList = [], allChecked, page, total } = this.state;
  238. const { info } = this.props.user;
  239. return (
  240. <div className="table-layout">
  241. <Tabs
  242. border
  243. type="division"
  244. theme="theme"
  245. size="small"
  246. space={2.5}
  247. width={100}
  248. active={tab}
  249. tabs={[{ key: 'exercise', title: '练习' }, { key: 'examination', title: '模考' }]}
  250. onChange={key => this.onTabChange(key)}
  251. />
  252. <UserAction
  253. search
  254. selectList={[
  255. {
  256. children: [
  257. {
  258. key: 'subject',
  259. placeholder: '学科',
  260. select: questionSubjectSelect,
  261. },
  262. {
  263. placeholder: '题型',
  264. key: 'questionType',
  265. be: 'subject',
  266. selectMap: questionSubjectMap,
  267. },
  268. ],
  269. },
  270. {
  271. label: '范围',
  272. children: [
  273. {
  274. key: 'one',
  275. placeholder: '全部',
  276. select: oneSelect,
  277. },
  278. {
  279. key: 'two',
  280. be: 'one',
  281. placeholder: '全部',
  282. selectMap: twoSelectMap,
  283. },
  284. ],
  285. },
  286. {
  287. right: true,
  288. key: 'timerange',
  289. select: TimeRange,
  290. },
  291. ]}
  292. filterMap={filterMap}
  293. onFilter={value => this.onFilter(value)}
  294. />
  295. <UserAction
  296. allCheckbox
  297. allChecked={allChecked}
  298. help
  299. btnList={[
  300. { title: '删除', key: 'clear' },
  301. { title: '导出', key: 'export', tag: 'vip', disabled: !info.vip },
  302. ]}
  303. sortList={[{ right: true, label: '更新时间', key: 'update_time' }]}
  304. sortMap={sortMap}
  305. onSort={value => this.onSort(value)}
  306. onAll={checked => this.onAll(checked)}
  307. onAction={key => this.onAction(key)}
  308. />
  309. {list.map((item, index) => {
  310. return (
  311. <div className="group">
  312. <UserTable
  313. theme="dark"
  314. border={false}
  315. size="small"
  316. select
  317. selectList={selectList}
  318. columns={questionColumns}
  319. data={[item]}
  320. header={false}
  321. />
  322. <UserTable
  323. border={false}
  324. size="small"
  325. select
  326. even="default"
  327. selectList={contentSelectList}
  328. columns={contentColumns}
  329. data={item.list}
  330. header={index === 0}
  331. />
  332. </div>
  333. );
  334. })}
  335. {total > 0 && list.length > 0 && (
  336. <UserPagination total={total} current={page} pageSize={this.state.search.size} onChange={p => this.onChangePage(p)} />
  337. )}
  338. {this.renderModal()}
  339. </div>
  340. );
  341. }
  342. renderModal() {
  343. const { showWarn, warn = {}, showClearConfirm, clearInfo = {}, showVip, showExamination, showReal, showExportWait, showExportConfirm, exportInfo = {} } = this.state;
  344. const { info } = this.props.user;
  345. return [
  346. <Modal show={showWarn} title={warn.title} confirmText="好的,知道了" btnAlign="center" onConfirm={() => this.setState({ showWarn: false })}>
  347. <div className="t-2 t-s-18">{warn.content}</div>
  348. </Modal>,
  349. <Modal show={showClearConfirm} title="移出" onConfirm={() => this.clearNote()} onCancel={() => this.setState({ showClearConfirm: false })}>
  350. <div className="t-2 t-s-18">
  351. 当前选中的 <span className="t-4">{clearInfo.questionNoIds ? clearInfo.questionNoIds.length : 0}</span> 道题即将被移出笔记,移出后无法恢复,是否继续?
  352. </div>
  353. </Modal>,
  354. <Modal show={showExportConfirm} title="导出" confirmText="导出" onConfirm={() => this.export()} onCancel={() => this.setState({ showExportConfirm: false })}>
  355. <div className="t-2 t-s-18 m-b-5">
  356. 当前共选中 <span className="t-4">{exportInfo.questionNoIds ? exportInfo.questionNoIds.length : 0}</span> 道题笔记,是否开始导出:
  357. </div>
  358. </Modal>,
  359. <Modal show={showExportWait} title="导出" confirmText="好的,知道了" btnAlign="center" onConfirm={() => this.setState({ showExportWait: false })}>
  360. <div className="t-2 t-s-18">正在下载中,请不要关闭当前页面!</div>
  361. </Modal>,
  362. <Examination
  363. show={showExamination}
  364. data={info}
  365. onConfirm={() => this.setState({ showExamination: false })}
  366. onCancel={() => this.setState({ showExamination: false })}
  367. onClose={() => this.setState({ showExamination: false })}
  368. />,
  369. <RealAuth show={showReal} data={info} onConfirm={() => this.setState({ showReal: false })} />,
  370. <VipRenew
  371. show={showVip}
  372. data={info}
  373. onReal={() => this.setState({ showVip: false, showReal: true })}
  374. onPrepare={() => this.setState({ showVip: false, showExamination: true })}
  375. onClose={() => this.setState({ showVip: false })}
  376. />,
  377. ];
  378. }
  379. }