AllDayRings.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import Box from '@/components/layout/Box'
  2. import './AllRings.scss'
  3. import { PageContainer, View, Text, Image } from '@tarojs/components'
  4. import { useEffect, useState } from 'react'
  5. import Modal from '@/components/layout/Modal.weapp'
  6. import { rpxToPx } from '@/utils/tools'
  7. import { ColorType } from '@/context/themes/color'
  8. import Rings from "@/features/trackTimeDuration/components/Rings";
  9. import { bigRingRadius, getBgRing, getCommon, getDot, ringWidth, smallRingRadius, thirdRingRadius } from '../trackTimeDuration/hooks/RingData'
  10. import { RealRing } from '@/features/trackTimeDuration/components/Rings'
  11. import { useSelector } from 'react-redux'
  12. import { useTranslation } from 'react-i18next'
  13. import { TimeFormatter } from '@/utils/time_format'
  14. import '@/utils/ring_card.scss';
  15. import DayNightRing from './DayNightRing'
  16. // import GradientText from '@/components/basic/GradientText'
  17. let sunriseA = new Date()
  18. sunriseA.setHours(6)
  19. sunriseA.setMinutes(0)
  20. sunriseA.setSeconds(0)
  21. sunriseA.setMilliseconds(0)
  22. const sunsetA = new Date()
  23. sunsetA.setHours(18)
  24. sunsetA.setMinutes(0)
  25. sunsetA.setSeconds(0)
  26. sunsetA.setMilliseconds(0)
  27. const sunriseB = new Date()
  28. sunriseB.setHours(6)
  29. sunriseB.setMinutes(0)
  30. sunriseB.setSeconds(0)
  31. sunriseB.setMilliseconds(0)
  32. let GradientText
  33. if (process.env.TARO_ENV == 'rn') {
  34. GradientText = require('@/components/basic/GradientText').default
  35. }
  36. export default function AllDayRings(props: { schedule: any }) {
  37. const [authInfo, setAuthInfo] = useState(global.locationDetail ? global.locationDetail : null)
  38. const [showRing, setShowRing] = useState(false)
  39. const user = useSelector((state: any) => state.user);
  40. const ring = useSelector((state: any) => state.ring);
  41. const [schedule] = useState(props.schedule)
  42. const { t } = useTranslation()
  43. useEffect(() => {
  44. setAuthInfo(global.locationDetail)
  45. }, [global.locationDetail])
  46. global.tapShowDay = () => {
  47. setShowRing(true)
  48. }
  49. function getFoodTime() {
  50. if (ring.schedule) {
  51. return [ring.schedule.fast.end_time, ring.schedule.fast.start_time]
  52. }
  53. var obj;
  54. (schedule as any).map(item => {
  55. if (item.name == 'FAST') {
  56. obj = item
  57. }
  58. })
  59. if (!obj) {
  60. console.log('报错', schedule)
  61. return ['16:00', '08:00'];
  62. }
  63. return [obj.schedule.fast.end_time, obj.schedule.fast.start_time]
  64. }
  65. function getActivityTime() {
  66. if (ring.schedule) {
  67. return [ring.schedule.sleep.end_time, ring.schedule.sleep.start_time]
  68. }
  69. var obj;
  70. (schedule as any).map(item => {
  71. if (item.name == 'SLEEP') {
  72. obj = item
  73. }
  74. })
  75. return [obj.schedule.sleep.end_time, obj.schedule.sleep.start_time]
  76. }
  77. function bigRing() {
  78. var common = getCommon(null, true)
  79. common.radius = bigRingRadius;
  80. common.lineWidth = ringWidth;
  81. var bgRing = getBgRing()
  82. const targetRing: RealRing = {
  83. color: (ring.current_record && ring.current_record.scenario == 'SLEEP') ? ColorType.activity + '66' : ColorType.food + '66',
  84. startArc: 0,
  85. durationArc: 2
  86. }
  87. var list = getFoodTime()
  88. var start;
  89. var end;
  90. if (ring.current_record && ring.current_record.scenario == 'SLEEP') {
  91. var list2 = getActivityTime()
  92. start = parseInt(list2[0].split(':')[0]) * 60 + parseInt(list2[0].split(':')[1])
  93. end = parseInt(list2[1].split(':')[0]) * 60 + parseInt(list2[1].split(':')[1])
  94. if (ring.current_record.status != 'WAIT_FOR_START') {
  95. var startDate = new Date(ring.current_record.sleep.target_start_time)
  96. var endDate = new Date(ring.current_record.sleep.target_end_time)
  97. end = startDate.getHours() * 60 + startDate.getMinutes()
  98. start = endDate.getHours() * 60 + endDate.getMinutes()
  99. }
  100. }
  101. else {
  102. start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
  103. end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
  104. if (ring.current_record && ring.current_record.status != 'WAIT_FOR_START') {
  105. var startDate = new Date(ring.current_record.fast.target_start_time)
  106. var endDate = new Date(ring.current_record.fast.target_end_time)
  107. end = startDate.getHours() * 60 + startDate.getMinutes()
  108. start = endDate.getHours() * 60 + endDate.getMinutes()
  109. }
  110. }
  111. if (end < start) {
  112. end += 24 * 60
  113. }
  114. var duration = end - start
  115. targetRing.startArc = (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  116. targetRing.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  117. var currentDot = getDot(null, false)
  118. var date = new Date()
  119. var minutes = date.getHours() * 60 + date.getMinutes()
  120. if (minutes < start) {
  121. minutes += 1440
  122. }
  123. currentDot.color = ColorType.food
  124. var now = new Date()
  125. var t = now.getHours() * 60 + now.getMinutes()
  126. var duration2 = t - start
  127. if (duration2 < 0) {
  128. duration2 += 24 * 60
  129. }
  130. let realRing: RealRing = {
  131. color: ColorType.food,
  132. startArc: (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  133. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  134. }
  135. if (ring.current_record && ring.current_record.scenario == 'SLEEP') {
  136. realRing.color = ColorType.activity
  137. currentDot.color = ColorType.activity
  138. }
  139. if (!user.isLogin) {
  140. currentDot = null
  141. realRing = null
  142. }
  143. if (ring.current_record.status == 'ONGOING1') {
  144. realRing = null
  145. }
  146. return <Rings common={common} bgRing={bgRing} targetRing={targetRing} realRing={duration2 <= duration ? realRing : null} currentDot={currentDot} canvasId={'pop_day_big'} />
  147. }
  148. function smallRing() {
  149. var common = getCommon(null, false)
  150. common.radius = smallRingRadius;
  151. common.lineWidth = ringWidth;
  152. var bgRing = getBgRing()
  153. const realRingBig: RealRing = {
  154. color: ColorType.activity + '66',
  155. startArc: 0,
  156. durationArc: 2
  157. }
  158. var list = getActivityTime()
  159. var start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
  160. var end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
  161. if (ring.current_record) {
  162. if (ring.current_record.scenario == 'SLEEP') {
  163. if (ring.current_record.status == 'ONGOING') {
  164. var startDate = new Date(ring.current_record.sleep.target_start_time)
  165. var endDate = new Date(ring.current_record.sleep.target_end_time)
  166. end = startDate.getHours() * 60 + startDate.getMinutes()
  167. start = endDate.getHours() * 60 + endDate.getMinutes()
  168. }
  169. }
  170. else {
  171. if (ring.current_record.status == 'ONGOING2') {
  172. var startDate = new Date(ring.current_record.sleep.target_start_time)
  173. var endDate = new Date(ring.current_record.sleep.target_end_time)
  174. end = startDate.getHours() * 60 + startDate.getMinutes()
  175. start = endDate.getHours() * 60 + endDate.getMinutes()
  176. }
  177. else if (ring.current_record.status == 'ONGOING3') {
  178. //睡眠已完成时,睡眠小于24小时,使用真实的real_end_time-real_start_time
  179. //大于24小时,使用real_end_time+sleep_duration
  180. if (ring.current_record.sleep.real_end_time - ring.current_record.sleep.real_start_time < 24 * 3600 * 1000) {
  181. var startDate = new Date(ring.current_record.sleep.real_start_time)
  182. var endDate = new Date(ring.current_record.sleep.real_end_time)
  183. end = startDate.getHours() * 60 + startDate.getMinutes()
  184. start = endDate.getHours() * 60 + endDate.getMinutes()
  185. }
  186. else {
  187. var startDate = new Date(ring.current_record.sleep.real_end_time)
  188. var durationTemp = end - start//ring.current_record.sleep.target_end_time-ring.current_record.sleep.target_start_time
  189. if (durationTemp < 0) {
  190. durationTemp += 24 * 60
  191. }
  192. var endDate = new Date(startDate.getTime() + durationTemp * 60 * 1000)
  193. start = startDate.getHours() * 60 + startDate.getMinutes()
  194. end = endDate.getHours() * 60 + endDate.getMinutes()
  195. }
  196. }
  197. }
  198. }
  199. if (end < start) {
  200. end += 24 * 60
  201. }
  202. var duration = end - start
  203. realRingBig.startArc = (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  204. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  205. var currentDot = getDot(null, false)
  206. var date = new Date()
  207. var minutes = date.getHours() * 60 + date.getMinutes()
  208. if (minutes < start) {
  209. minutes += 1440
  210. }
  211. currentDot.color = ColorType.activity
  212. var now = new Date()
  213. var t = now.getHours() * 60 + now.getMinutes()
  214. var duration2 = t - start
  215. if (duration2 < 0) {
  216. duration2 += 24 * 60
  217. }
  218. let realRing: RealRing = {
  219. color: ColorType.activity,
  220. startArc: (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  221. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  222. }
  223. if (!user.isLogin) {
  224. currentDot = null
  225. realRing = null
  226. }
  227. if (ring.current_record.status == 'ONGOING2') {
  228. realRing = null
  229. }
  230. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={duration2 <= duration ? realRing : null} currentDot={currentDot} canvasId={'pop_day_small'} />
  231. }
  232. function dayRing() {
  233. return <DayNightRing
  234. isNight={false}
  235. isThirdRing={true}
  236. canvasId={'day_night_card_big_day_ring111'}
  237. authInfo={authInfo}
  238. />
  239. }
  240. function rings() {
  241. return <View style={{
  242. position: 'relative', zIndex: 1,
  243. marginLeft: -6,
  244. }}>
  245. {
  246. bigRing()
  247. }
  248. {
  249. ring.current_record && ring.current_record.scenario == 'FAST_SLEEP' &&
  250. <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
  251. {
  252. smallRing()
  253. }
  254. </View>
  255. }
  256. {
  257. <View style={{ display: 'flex', position: 'absolute', left: -14, top: -14, right: -14, bottom: -14 }}>
  258. {
  259. dayRing()
  260. }
  261. </View>
  262. }
  263. </View>
  264. }
  265. function dayDuration() {
  266. return global.sunriseDuration
  267. }
  268. function eatDuration() {
  269. var list = getFoodTime()
  270. var start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
  271. var end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
  272. if (ring.current_record && ring.current_record.status != 'WAIT_FOR_START' && ring.current_record.scenario != 'SLEEP') {
  273. var startDate = new Date(ring.current_record.fast.target_start_time)
  274. var endDate = new Date(ring.current_record.fast.target_end_time)
  275. end = startDate.getHours() * 60 + startDate.getMinutes()
  276. start = endDate.getHours() * 60 + endDate.getMinutes()
  277. }
  278. if (end < start) {
  279. end += 24 * 60
  280. }
  281. var duration = (end - start) * 60 * 1000
  282. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  283. }
  284. function activityDuration() {
  285. var list = getActivityTime()
  286. var start = parseInt(list[0].split(':')[0]) * 3600 + parseInt(list[0].split(':')[1]) * 60
  287. var end = parseInt(list[1].split(':')[0]) * 3600 + parseInt(list[1].split(':')[1]) * 60
  288. if (ring.current_record) {
  289. if (ring.current_record.scenario == 'SLEEP') {
  290. if (ring.current_record.status == 'ONGOING') {
  291. var startDate = new Date(ring.current_record.sleep.target_start_time)
  292. var endDate = new Date(ring.current_record.sleep.target_end_time)
  293. end = startDate.getHours() * 3600 + startDate.getMinutes() * 60 + startDate.getSeconds()
  294. start = endDate.getHours() * 3600 + endDate.getMinutes() * 60 + endDate.getSeconds()
  295. }
  296. }
  297. else {
  298. if (ring.current_record.status == 'ONGOING2') {
  299. var startDate = new Date(ring.current_record.sleep.target_start_time)
  300. var endDate = new Date(ring.current_record.sleep.target_end_time)
  301. end = startDate.getHours() * 3600 + startDate.getMinutes() * 60 + startDate.getSeconds()
  302. start = endDate.getHours() * 3600 + endDate.getMinutes() * 60 + endDate.getSeconds()
  303. }
  304. else if (ring.current_record.status == 'ONGOING3') {
  305. //睡眠已完成时,睡眠小于24小时,使用真实的real_end_time-real_start_time
  306. //大于24小时,使用real_end_time+sleep_duration
  307. if (ring.current_record.sleep.real_end_time - ring.current_record.sleep.real_start_time < 24 * 3600 * 1000) {
  308. var startDate = new Date(ring.current_record.sleep.real_start_time)
  309. var endDate = new Date(ring.current_record.sleep.real_end_time)
  310. end = startDate.getHours() * 3600 + startDate.getMinutes() * 60 + startDate.getSeconds()
  311. start = endDate.getHours() * 3600 + endDate.getMinutes() * 60 + endDate.getSeconds()
  312. }
  313. else {
  314. var startDate = new Date(ring.current_record.sleep.real_end_time)
  315. var durationTemp = start - end//ring.current_record.sleep.target_end_time-ring.current_record.sleep.target_start_time
  316. if (durationTemp < 0) {
  317. durationTemp += 24 * 3600
  318. }
  319. var endDate = new Date(startDate.getTime() + durationTemp * 1000)
  320. end = startDate.getHours() * 3600 + startDate.getMinutes() * 60 + startDate.getSeconds()
  321. start = endDate.getHours() * 3600 + endDate.getMinutes() * 60 + endDate.getSeconds()
  322. debugger
  323. console.log('active', end, start, durationTemp)
  324. }
  325. }
  326. }
  327. }
  328. if (end < start) {
  329. end += 24 * 3600
  330. }
  331. var duration = (end - start) * 1000
  332. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  333. }
  334. function popDetail() {
  335. return <Box>
  336. <View className="ring_full_container">
  337. <View className="time_operate_item1">
  338. <View className='fast_sleep_item three_ring_card_detail'>
  339. {
  340. rings()
  341. }
  342. <View className="duration_bg3"
  343. style={{
  344. display:'flex',
  345. flexDirection:'column',
  346. marginLeft: rpxToPx(68),
  347. height: bigRingRadius * 2,
  348. overflow: 'visible' }}>
  349. <Text className="duration_title2" style={{ color: '#fff', opacity: 0.4 }}>{t('feature.common.day')}</Text>
  350. <Text className="duration_value2" style={{ color: ColorType.day }}>{dayDuration()}</Text>
  351. {
  352. ring.current_record && ring.current_record.scenario != 'SLEEP' && <Text className="duration_title2" style={{ color: '#fff', opacity: 0.4 }}>进食</Text>
  353. }
  354. {
  355. ring.current_record && ring.current_record.scenario != 'SLEEP' && <Text className="duration_value2" style={{ color: ColorType.food }}>{eatDuration()}</Text>
  356. }
  357. {
  358. ring.current_record && ring.current_record.scenario != 'FAST' && <Text className="duration_title2" style={{ color: '#fff', opacity: 0.4 }}>活动</Text>
  359. }
  360. {
  361. ring.current_record && ring.current_record.scenario != 'FAST' && <Text className="duration_value2" style={{ color: ColorType.activity, marginBottom: 0 }}>{activityDuration()}</Text>
  362. }
  363. </View>
  364. </View>
  365. </View>
  366. </View>
  367. </Box>
  368. }
  369. function modalContent() {
  370. return <Modal
  371. testInfo={null}
  372. dismiss={() => {
  373. setShowRing(false)
  374. }}
  375. confirm={() => { }}>
  376. {popDetail()}
  377. </Modal>
  378. }
  379. return <View>
  380. {
  381. showRing && modalContent()
  382. }
  383. </View>
  384. // return <Box onClick={tapShow}>
  385. // <View onClick={tapShow} style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
  386. // <Image className='three_ring_icon' src={require('@assets/images/day_three_ring.png')} />
  387. // {/* <View className='day_show_full'>{t('feature.day_night.show_all_rings')}</View> */}
  388. // {
  389. // process.env.TARO_ENV == 'weapp' ? <Text className='day_show_full'>{t('feature.day_night.show_all_rings')}</Text> :
  390. // <GradientText
  391. // colors={[ColorType.day, ColorType.food, ColorType.activity]}
  392. // style={{
  393. // fontSize: rpxToPx(32),
  394. // fontWeight: 'bold',
  395. // }}>{t('feature.day_night.show_all_rings')}</GradientText>
  396. // }
  397. // </View>
  398. // {
  399. // showRing && modalContent()
  400. // }
  401. // </Box>
  402. }