index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import { Icon } from 'antd';
  4. import { getMap, formatDate, formatPercent } from '@src/services/Tools';
  5. import moment from 'moment';
  6. import { SpecialRadioGroup } from '../Radio';
  7. import Modal from '../Modal';
  8. import Button from '../Button';
  9. import TotalSort from '../TotalSort';
  10. import Date from '../Date';
  11. import Ratio from '../Ratio';
  12. import { My } from '../../stores/my';
  13. import { PrepareStatus, PrepareExaminationTime, PrepareScoreTime } from '../../../Constant';
  14. const PrepareStatusMap = getMap(PrepareStatus, 'value', 'short');
  15. const PrepareExaminationTimeMap = getMap(PrepareExaminationTime, 'value', 'label');
  16. const PrepareScoreTimeMap = getMap(PrepareScoreTime, 'value', 'label');
  17. console.log(PrepareStatusMap, PrepareExaminationTimeMap);
  18. export default class extends Component {
  19. constructor(props) {
  20. super(props);
  21. this.statusColors = ['#41A6F3', '#3F86EA', '#41A6F3', '#6DC0FF', '#9BD4FF'];
  22. this.examinationTimeColors = ['#6865FD', '#322EF2', '#8F65FF', '#8C8AFF'];
  23. this.goalColors = ['#2754E0', '#3F86EA', '#41A6F3', '#6DC0FF'];
  24. this.scoreTimeColors = ['#6865FD', '#322EF2', '#8F65FF', '#8C8AFF', '#8F65FF', '#C3C1D1'];
  25. this.stepProp = {
  26. 0: {
  27. title: '我的身份',
  28. },
  29. 1: {
  30. title: '下次考试时间',
  31. },
  32. 2: {
  33. title: '目标成绩',
  34. },
  35. 3: {
  36. title: '最晚出分时间(选填)',
  37. },
  38. 4: {
  39. title: '备考信息',
  40. onConfirm: props.onConfirm,
  41. onCancel: () => {
  42. this.setState({ step: 0 });
  43. },
  44. confirmText: '关闭',
  45. cancelText: '修改',
  46. },
  47. };
  48. this.state = { step: 0, data: { prepareGoal: 700 } };
  49. }
  50. componentWillReceiveProps(nextProps) {
  51. if (nextProps.show && !this.init) {
  52. this.init = true;
  53. My.getPrepare()
  54. .then(result => {
  55. result.stat = result.stat || {};
  56. if (!result.stat.status) {
  57. result.stat.status = [];
  58. }
  59. if (!result.stat.goal) {
  60. result.stat.goal = [];
  61. }
  62. if (!result.stat.examinationTime) {
  63. result.stat.examinationTime = [];
  64. }
  65. if (!result.stat.scoreTime) {
  66. result.stat.scoreTime = [];
  67. }
  68. const statusTotal = result.stat.status.reduce((p, n) => { return p + n.value; }, 0);
  69. const goalTotal = result.stat.goal.reduce((p, n) => { return p + n.value; }, 0);
  70. const examinationTimeTotal = result.stat.examinationTime.reduce((p, n) => { return p + n.value; }, 0);
  71. const scoreTimeTotal = result.stat.scoreTime.reduce((p, n) => { return p + n.value; }, 0);
  72. const stat = {
  73. status: result.stat.status.map((row, index) => {
  74. const a = {};
  75. a.value = formatPercent(row.value, statusTotal);
  76. a.label = `${PrepareStatusMap[row.key]}; ${a.value}%`;
  77. a.color = this.statusColors[index];
  78. return a;
  79. }),
  80. goal: result.stat.goal.map((row, index) => {
  81. const a = {};
  82. a.value = formatPercent(row.value, goalTotal);
  83. a.label = `${row.key}+; ${a.value}%`;
  84. a.color = this.goalColors[index];
  85. return a;
  86. }),
  87. examinationTime: result.stat.examinationTime.map((row, index) => {
  88. const a = {};
  89. a.value = formatPercent(row.value, examinationTimeTotal);
  90. a.label = `${PrepareExaminationTimeMap[row.key]}; ${a.value}%`;
  91. a.color = this.examinationTimeColors[index];
  92. return a;
  93. }),
  94. scoreTime: result.stat.scoreTime.map((row, index) => {
  95. const a = {};
  96. a.value = formatPercent(row.value, scoreTimeTotal);
  97. a.label = `${PrepareScoreTimeMap[row.key]}; ${a.value}%`;
  98. a.color = this.scoreTimeColors[index];
  99. return a;
  100. }),
  101. };
  102. result.prepareGoal = result.prepareGoal || 700;
  103. result.prepareScoreTime = result.prepareScoreTime ? moment(result.prepareScoreTime) : null;
  104. this.setState({ data: result, stat, first: !result.prepareStatus, step: !result.prepareStatus ? 0 : 4, info: result.info });
  105. });
  106. }
  107. }
  108. onChange(type, key) {
  109. const { data } = this.state;
  110. data[type] = key;
  111. this.setState({ data });
  112. }
  113. onPrev() {
  114. const { step } = this.state;
  115. this.setState({ step: step - 1 });
  116. }
  117. onNext() {
  118. const { step } = this.state;
  119. this.setState({ step: step + 1 });
  120. }
  121. submitPrepare() {
  122. const { data } = this.state;
  123. My.editPrepare(data).then(result => {
  124. this.setState({ result });
  125. this.onNext();
  126. });
  127. }
  128. onClose() {
  129. const { onClose } = this.props;
  130. if (onClose) onClose();
  131. this.setState({ step: 0 });
  132. }
  133. render() {
  134. const { step, info } = this.state;
  135. const { show } = this.props;
  136. return (
  137. <Modal
  138. className="examination-modal"
  139. show={show}
  140. width={460}
  141. {...this.stepProp[step]}
  142. onClose={() => this.onClose()}
  143. >
  144. {info && <div className="examination-modal-wrapper">{this[`renderStep${step}`]()}</div>}
  145. </Modal>
  146. );
  147. }
  148. renderStep0() {
  149. const { data } = this.state;
  150. const { prepareStatus } = data;
  151. return (
  152. <div className="step-0-layout">
  153. <SpecialRadioGroup
  154. list={PrepareStatus}
  155. value={prepareStatus}
  156. width={190}
  157. space={10}
  158. onChange={key => this.onChange('prepareStatus', key)}
  159. />
  160. <div className="action-layout">
  161. <div className="next" onClick={() => prepareStatus && this.onNext()}>
  162. 下一题
  163. <Icon type="right-circle" theme="filled" />
  164. </div>
  165. </div>
  166. </div>
  167. );
  168. }
  169. renderStep1() {
  170. const { data } = this.state;
  171. const { prepareExaminationTime } = data;
  172. return (
  173. <div className="step-1-layout">
  174. <SpecialRadioGroup
  175. list={PrepareExaminationTime}
  176. value={prepareExaminationTime}
  177. width={195}
  178. space={10}
  179. onChange={key => this.onChange('prepareExaminationTime', key)}
  180. />
  181. <div className="action-layout">
  182. <div className="prev" onClick={() => this.onPrev()}>
  183. <Icon type="left-circle" theme="filled" />
  184. 上一题
  185. </div>
  186. <div className="next" onClick={() => prepareExaminationTime && this.onNext()}>
  187. 下一题
  188. <Icon type="right-circle" theme="filled" />
  189. </div>
  190. </div>
  191. </div>
  192. );
  193. }
  194. renderStep2() {
  195. const { data } = this.state;
  196. const { prepareGoal } = data;
  197. return (
  198. <div className="step-2-layout">
  199. <TotalSort
  200. value={prepareGoal}
  201. onChange={goal => {
  202. this.onChange('prepareGoal', goal);
  203. }}
  204. />
  205. <div className="action-layout">
  206. <div className="prev" onClick={() => this.onPrev()}>
  207. <Icon type="left-circle" theme="filled" />
  208. 上一题
  209. </div>
  210. <div className="next" onClick={() => prepareGoal && this.onNext()}>
  211. 下一题
  212. <Icon type="right-circle" theme="filled" />
  213. </div>
  214. </div>
  215. </div>
  216. );
  217. }
  218. renderStep3() {
  219. const { data, info, step } = this.state;
  220. const { prepareScoreTime } = data;
  221. return (
  222. <div className="step-3-layout">
  223. {info && <a href={info.link} className="a-l" target='_blank'>{info.title}</a>}
  224. <div style={{ position: 'relative' }}>
  225. <Date
  226. show={step === 3}
  227. theme="filled"
  228. value={prepareScoreTime}
  229. onChange={date => {
  230. this.onChange('prepareScoreTime', date);
  231. }}
  232. />
  233. </div>
  234. <div className="action-layout">
  235. <div className="prev" onClick={() => this.onPrev()}>
  236. <Icon type="left-circle" theme="filled" />
  237. 上一题
  238. </div>
  239. <Button
  240. size="lager"
  241. radius
  242. onClick={() => {
  243. this.submitPrepare();
  244. }}
  245. >
  246. 提交
  247. </Button>
  248. </div>
  249. </div>
  250. );
  251. }
  252. renderStep4() {
  253. const { first, data, stat = {} } = this.state;
  254. console.log(data, stat);
  255. return (
  256. <div className="step-4-layout">
  257. {first && (
  258. <div className="tip">
  259. <Icon type="check" />
  260. 7天VIP权限已赠送至您的账户。
  261. </div>
  262. )}
  263. <Ratio text="身份" subtext={PrepareStatusMap[data.prepareStatus]} values={stat.status || []} />
  264. <Ratio
  265. text="考试时间"
  266. subtext={PrepareExaminationTimeMap[data.prepareExaminationTime]}
  267. values={stat.examinationTime || []}
  268. />
  269. <Ratio text="目标成绩" subtext={`${data.prepareGoal}分`} values={stat.goal || []} />
  270. <Ratio
  271. text="出分日期"
  272. subtext={`${formatDate(data.prepareScoreTime, 'YYYY-MM')}`}
  273. values={stat.scoreTime || []}
  274. />
  275. </div>
  276. );
  277. }
  278. }