AllDayRings.tsx 21 KB

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