Result.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { ColorType } from "@/context/themes/color";
  2. import { View, Text } from "@tarojs/components";
  3. import { useRouter } from "@tarojs/taro";
  4. import { useEffect, useState } from "react";
  5. import './Result.scss';
  6. import Taro from "@tarojs/taro";
  7. import { useTranslation } from "react-i18next";
  8. import { deleteWorkoutRecord } from "@/services/workout";
  9. import { rpxToPx } from "@/utils/tools";
  10. export default function Component() {
  11. const router = useRouter();
  12. const [histories, setHistories] = useState<any>([])
  13. const [record, setRecord] = useState(null)
  14. const { t } = useTranslation()
  15. useEffect(() => {
  16. var data = JSON.parse(router.params.detail as any)
  17. console.log(data)
  18. setRecord(data)
  19. var list = data.items[0].groups
  20. setHistories(list)
  21. }, [])
  22. function twoTimeDuration(start, end) {
  23. var time = Math.floor((end - start) / 1000);
  24. const hours = Math.floor(time / 3600);
  25. const minutes = Math.floor((time % 3600) / 60);
  26. const seconds = Math.floor(time % 60);
  27. var strDuration = ''
  28. if (hours > 0) {
  29. strDuration = `${hours}时`
  30. }
  31. if (minutes > 0) {
  32. strDuration += `${minutes}分`
  33. }
  34. if (seconds > 0) {
  35. strDuration += `${seconds}秒`
  36. }
  37. return strDuration.length == 0 ? '1秒' : strDuration
  38. }
  39. function getCount() {
  40. var count = 0
  41. histories.map(item => {
  42. if (item.type == 'GROUP') {
  43. count++
  44. }
  45. })
  46. return count
  47. }
  48. function getIndex(index) {
  49. var count = 0
  50. for (var i = 0; i <= index; i++) {
  51. if (histories[i].type == 'GROUP') {
  52. count++
  53. }
  54. }
  55. return count;
  56. }
  57. function getValue(item) {
  58. var list = item.values
  59. var count = 1
  60. var unit = ''
  61. for (var i = 0; i < list.length; i++) {
  62. count = count * (parseInt(list[i].value + ''))
  63. unit = unit + list[i].unit + '·'
  64. }
  65. var result = count + unit
  66. return result.substring(0, result.length - 1)
  67. }
  68. function del() {
  69. Taro.showModal({
  70. title: t('feature.common.modal.delete_item_title'),
  71. content: t('feature.common.modal.delete_item_content'),
  72. success: function (res) {
  73. if (res.confirm) {
  74. delData()
  75. }
  76. }
  77. })
  78. }
  79. function delData() {
  80. deleteWorkoutRecord({ id: (record as any).id }).then(res => {
  81. global.delWorkoutRecord()
  82. global.refreshWorkout()
  83. Taro.navigateBack()
  84. })
  85. }
  86. return <View style={{ color: '#fff', display: 'flex', flexDirection: 'column' }}>
  87. <Text className="result_detail_text" style={{ color: router.params.themeColor }}>{router.params.title}</Text>
  88. <Text className="result_detail_text" style={{color:'#fff',opacity:0.4,marginTop:rpxToPx(10),marginBottom:rpxToPx(20)}}>计时训练</Text>
  89. <Text className="result_detail_text">训练统计</Text>
  90. <View style={{marginLeft:rpxToPx(32),display:'flex',flexDirection:'column'}}>
  91. <Text>总量</Text>
  92. <Text style={{ color: router.params.themeColor }}>111</Text>
  93. <Text>用时</Text>
  94. <View style={{flexDirection:'row',display:'flex',width:'100%'}}>
  95. <View style={{display:'flex',flexDirection:'column',alignItems:'center',flex:1}}>
  96. <Text style={{ color: router.params.themeColor }}>111</Text>
  97. <Text>实际</Text>
  98. </View>
  99. <View style={{display:'flex',flexDirection:'column',alignItems:'center',flex:1}}>
  100. <Text style={{ color: router.params.themeColor }}>111</Text>
  101. <Text>计划</Text>
  102. </View>
  103. </View>
  104. </View>
  105. {/* <View>
  106. <Text>用时</Text>
  107. <Text style={{ color: router.params.themeColor }}>111</Text>
  108. </View> */}
  109. <Text className="result_detail_text">训练详情</Text>
  110. {
  111. histories.length > 0 && <Text className="result_count">共{getCount()}组</Text>
  112. }
  113. {
  114. histories.map((item, index) => {
  115. return <View key={index} className={item.typ == 'GROUP' ? 'result_item' : 'result_item result_item_rest'}>
  116. <Text>{item.type == 'GROUP' ? `第${getIndex(index)}组` : '组间休息'}</Text>
  117. {item.type == 'GROUP' && <Text style={{ color: router.params.themeColor }}>{getValue(item)}</Text>}
  118. <Text className="result_time">{twoTimeDuration(item.start.timestamp, item.end.timestamp)}</Text>
  119. </View>
  120. })
  121. }
  122. <Text className="result_del" onClick={del}>删除训练记录</Text>
  123. </View>
  124. }