move_schedule.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { View, Text, Image, Switch } from "@tarojs/components";
  2. import './move_schedule.scss'
  3. import { useRouter } from "@tarojs/taro";
  4. import { useEffect, useState } from "react";
  5. import { createSchedule, getActiveMovesCurrent, getMoveSchedules } from "@/services/health";
  6. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  7. import { getThemeColor } from "@/features/health/hooks/health_hooks";
  8. import { rpxToPx } from "@/utils/tools";
  9. import NewHeader, { NewHeaderType } from "../components/new_header";
  10. import StatusIndicator, { StatusType } from "../base/status_indicator";
  11. import { MainColorType } from "@/context/themes/color";
  12. import { IconNotification, IconNotificationOff } from "@/components/basic/Icons";
  13. import ConsoleCell from "../components/console_cell";
  14. let useRoute;
  15. let useNavigation;
  16. let scenario = '';
  17. if (process.env.TARO_ENV == 'rn') {
  18. useRoute = require("@react-navigation/native").useRoute
  19. useNavigation = require("@react-navigation/native").useNavigation
  20. }
  21. /*
  22. https://fast-dev.oss-cn-beijing.aliyuncs.com/users/b13170f6916a27713c118a5ce75ea74a/food-journal/2024/88620d96-c898-472f-8783-d425bb91333a_20240910114628_698.png
  23. https://fast-dev.oss-cn-beijing.aliyuncs.com/users/b13170f6916a27713c118a5ce75ea74a/food-journal/2024/88620d96-c898-472f-8783-d425bb91333a_20240910114628_698.png?x-oss-process=image/resize,w_50,limit_0
  24. */
  25. let timer;
  26. export default function MoveSchedule() {
  27. let router
  28. let navigation;
  29. if (useNavigation) {
  30. navigation = useNavigation()
  31. }
  32. if (process.env.TARO_ENV == 'rn') {
  33. router = useRoute()
  34. }
  35. else {
  36. router = useRouter()
  37. }
  38. const [hours, setHours] = useState<any>([])
  39. const [loaded, setLoaded] = useState(false)
  40. const [count, setCount] = useState(1)
  41. useEffect(() => {
  42. getData()
  43. timer = setInterval(() => {
  44. setCount((count => count + 1))
  45. var now = new Date()
  46. if (now.getMinutes() == 10 && now.getSeconds() == 0) {
  47. getData()
  48. }
  49. }, 1000)
  50. // getMoveSchedules().then(res => {
  51. // console.log(hours)
  52. // setLoaded(true)
  53. // })
  54. return () => {
  55. clearInterval(timer)
  56. }
  57. }, [])
  58. function getData() {
  59. getActiveMovesCurrent().then(res => {
  60. setLoaded(true)
  61. setHours((res as any).hours)
  62. })
  63. }
  64. // console.log(JSON.parse(router.params.hours))
  65. if (!loaded) return <View />
  66. function formatTimeInterval(startTimestamp, endTimestamp) {
  67. // 计算时间间隔(以秒为单位)
  68. const intervalSeconds = Math.floor((endTimestamp - startTimestamp) / 1000);
  69. // 处理负值的情况
  70. if (intervalSeconds < 0) {
  71. throw new Error("End timestamp must be greater than start timestamp");
  72. }
  73. // 如果时间间隔大于1小时
  74. if (intervalSeconds >= 3600) {
  75. const hours = Math.floor(intervalSeconds / 3600);
  76. const minutes = Math.floor((intervalSeconds % 3600) / 60);
  77. const seconds = intervalSeconds % 60;
  78. return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
  79. } else {
  80. // 如果时间间隔小于1小时
  81. const minutes = Math.floor(intervalSeconds / 60);
  82. const seconds = intervalSeconds % 60;
  83. return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
  84. }
  85. }
  86. function firstTime(index) {
  87. var firstIndex = -1;
  88. var selHour = -1;
  89. for (var i = 0; i < hours.length; i++) {
  90. if (hours[i].status == 'WFS') {
  91. firstIndex = i;
  92. selHour = hours[i].hour
  93. break;
  94. }
  95. }
  96. if (firstIndex == index) {
  97. var endDate = new Date()
  98. endDate.setHours(selHour)
  99. endDate.setMinutes(10);
  100. endDate.setSeconds(0);
  101. endDate.setMilliseconds(0);
  102. var endTimestamp = endDate.getTime() + 3600 * 1000;
  103. if (new Date().getTime() > endTimestamp) {
  104. endTimestamp += 24 * 3600 * 1000
  105. }
  106. if (endTimestamp - new Date().getTime() > 20 * 60 * 1000) {
  107. return <Text>countdown {formatTimeInterval(new Date().getTime(), endTimestamp)} </Text>
  108. }
  109. else if (endTimestamp - new Date().getTime() < 10 * 60 * 1000) {
  110. <Text style={{ color: getThemeColor('ACTIVE') }}>Closing soon in {formatTimeInterval(new Date().getTime(), endTimestamp)} </Text>
  111. }
  112. return <Text>available </Text>
  113. }
  114. return <Text></Text>
  115. }
  116. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  117. <NewHeader type={NewHeaderType.left} title="Upcoming Check Ins" />
  118. {
  119. hours.length == 0 && <View style={{ display: 'flex', flex: 1, flexDirection: 'column', alignItems: 'center', marginTop: rpxToPx(110) }}>
  120. <Image src={require('@assets/_health/sleep2.png')} style={{ width: rpxToPx(64), height: rpxToPx(64) }} />
  121. <Text style={{ marginTop: rpxToPx(24), fontSize: rpxToPx(50), fontWeight: 'bold', color: '#B2B2B2' }}>睡个好觉</Text>
  122. </View>
  123. }
  124. {
  125. hours.map((item, index) => {
  126. var start = item.hour
  127. var open: any = start
  128. var end = start + 1
  129. start = (start + '').padStart(2, '0')
  130. end = (end + '').padStart(2, '0')
  131. open = (open + '').padStart(2, '0')
  132. if (item.status != 'WFS') return <View key={index} />
  133. return <ConsoleCell key={index}
  134. status={<StatusIndicator type={StatusType.normal}
  135. color={MainColorType.active}
  136. text={'WAKING HOUR ' + (index + 1)} />}
  137. title={`Check in at ${open}:50`}
  138. description={`${parseInt(start) < new Date().getHours() ? '' : ''}${start}:00-${end}:00`}
  139. subDesc={<Text className="schedule_item_time"> {firstTime(index)}</Text>}
  140. right={<View style={{
  141. display: 'flex',
  142. flexDirection: 'row',
  143. alignItems: 'center'
  144. }}>
  145. {
  146. item.reminder ? <IconNotification color={MainColorType.g03} width={rpxToPx(28)} /> : <IconNotificationOff color={MainColorType.g03} width={rpxToPx(28)} />
  147. }
  148. <View style={{ width: rpxToPx(12) }} />
  149. <Switch checked={item.reminder} color={getThemeColor('ACTIVE')} onChange={e => {
  150. console.log(item)
  151. createSchedule({
  152. schedules: [{
  153. id: item.schedule_id,
  154. reminder: e.detail.value
  155. }],
  156. }).then(res => {
  157. getData()
  158. if (global.refreshWindow) {
  159. global.refreshWindow()
  160. }
  161. if (global.refreshSchedules) {
  162. global.refreshSchedules()
  163. }
  164. if (global.updateMove) {
  165. global.updateMove()
  166. }
  167. })
  168. }} />
  169. </View>}
  170. disable={true}
  171. onClick={() => { }}
  172. showLine={true}
  173. fullLine={false}
  174. />
  175. })
  176. }
  177. <Text className="no_more" style={{ color: '#5C7099' }} onClick={() => {
  178. jumpPage('./move_setting_reminder')
  179. }}>Full schedules</Text>
  180. </View>
  181. }