page.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import { formatDate, formatSeconds, formatPercent } from '@src/services/Tools';
  5. import Icon from '../../../components/Icon';
  6. import { My } from '../../../stores/my';
  7. export default class extends Page {
  8. init() { }
  9. initData() {
  10. My.getStudyTotal().then(total => {
  11. this.setState({ total });
  12. });
  13. My.getStudyWeek(0).then(latest => {
  14. const diff = latest.time - latest.avgTime;
  15. const diffPercent = diff > 0 ? formatPercent(latest.time - latest.avgTime, latest.avgTime, true) : formatPercent(latest.avgTime - latest.time, latest.avgTime, true);
  16. this.setState({ latest, diff, diffPercent });
  17. My.getStudyWeek(1).then(last => {
  18. const diffLast = latest.time - last.time;
  19. const diffLastPercent = diffLast > 0 ? formatPercent(latest.time - last.time, last.time, true) : formatPercent(last.time - latest.time, last.time, true);
  20. this.setState({ last, diffLast, diffLastPercent });
  21. });
  22. });
  23. }
  24. renderView() {
  25. const { total = {}, latest = {}, diff = 0, diffPercent = 0, diffLast = 0, diffLastPercent = 0 } = this.state;
  26. return (
  27. <div>
  28. <div className="block">
  29. <div className="text">
  30. 自{total.createTime && formatDate(total.createTime, 'YYYY-MM-DD')},您已在千行学习<span>{total.days}</span>天
  31. </div>
  32. <div className="text">
  33. 累积<span dangerouslySetInnerHTML={{ __html: formatSeconds(total.time).replace(/([0-9]+)(min|m|hour|h|s)/g, '<span class="s">$1</span>$2') }} />
  34. </div>
  35. </div>
  36. <div className="title">本周数据</div>
  37. <div className="t-c">
  38. <div className="item">
  39. <div className="text">学习时间</div>
  40. <div className="value" dangerouslySetInnerHTML={{ __html: formatSeconds(latest.time).replace(/([0-9]+)(min|m|hour|h|s)/g, '<span>$1</span>$2') }} />
  41. </div>
  42. <div className="item">
  43. <div className="text">同比上周</div>
  44. <div className="value">
  45. {diffLast > 0 ? <Icon type="caret-up" theme="filled" color="#6EC64B" /> : <Icon type="caret-down" theme="filled" color="#F36565" />}
  46. <span>{diffLastPercent}</span>%
  47. </div>
  48. </div>
  49. <div className="item">
  50. <div className="text">同比全站</div>
  51. <div className="value">
  52. {diff > 0 ? <Icon type="caret-up" theme="filled" color="#6EC64B" /> : <Icon type="caret-down" theme="filled" color="#F36565" />}
  53. <span>{diffPercent}</span>%
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. );
  59. }
  60. }