Discovery.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import { View, Text, Image, Switch } from "@tarojs/components";
  2. import { dotIsOuterRange, getBgRing, getCommon, getDot, getReal, getSchedule, getTarget } from "@/features/trackTimeDuration/hooks/RingData";
  3. import { RealRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  4. import { ColorType } from "@/context/themes/color";
  5. import { useSelector } from "react-redux";
  6. import Rings from "@/features/trackTimeDuration/components/Rings";
  7. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  8. import { rpxToPx } from "@/utils/tools";
  9. import { useEffect, useState } from "react";
  10. import Taro from "@tarojs/taro";
  11. import './Discovery.scss'
  12. import { useTranslation } from "react-i18next";
  13. import { TimeFormatter } from "@/utils/time_format";
  14. import { getPerm, uploadPerm } from "@/services/user";
  15. import Box from "@/components/layout/Box";
  16. let useNavigation;
  17. if (process.env.TARO_ENV == 'rn') {
  18. useNavigation = require("@react-navigation/native").useNavigation
  19. }
  20. export default function Discovery() {
  21. const user = useSelector((state: any) => state.user);
  22. const dayNight = useSelector((state: any) => state.dayNight);
  23. const [showRing, setShowRing] = useState(false)
  24. const [schedule, setSchedule] = useState(null)
  25. const { t } = useTranslation()
  26. let navigation;
  27. let timeStamp = new Date().getTime()
  28. if (useNavigation) {
  29. navigation = useNavigation()
  30. }
  31. useEffect(() => {
  32. getContent()
  33. }, [])
  34. useEffect(() => {
  35. setSchedule(global.homeData.scenarios)
  36. }, [global.homeData])
  37. useEffect(() => {
  38. if (user.isLogin) {
  39. getPerm({}).then(res => {
  40. setShowRing((res as any).show_day_ring)
  41. })
  42. }
  43. }, [user.isLogin])
  44. async function getStorage(key: string) {
  45. try {
  46. const res = await Taro.getStorage({ key });
  47. return res.data;
  48. } catch {
  49. return '';
  50. }
  51. }
  52. async function getContent() {
  53. const isShow = await getStorage('showDayRing') || false
  54. setShowRing(isShow)
  55. }
  56. function getFoodTime() {
  57. var obj;
  58. (schedule as any).map(item => {
  59. if (item.name == 'FAST') {
  60. obj = item
  61. }
  62. })
  63. return [obj.schedule.fast.end_time, obj.schedule.fast.start_time]
  64. }
  65. function getActivityTime() {
  66. var obj;
  67. (schedule as any).map(item => {
  68. if (item.name == 'SLEEP') {
  69. obj = item
  70. }
  71. })
  72. return [obj.schedule.sleep.end_time, obj.schedule.sleep.start_time]
  73. }
  74. function bigRing() {
  75. var common = getCommon(null, true)
  76. common.radius = 42;
  77. common.lineWidth = 9;
  78. var bgRing = getBgRing()
  79. const realRingBig: RealRing = {
  80. color: ColorType.food,
  81. startArc: 0,
  82. durationArc: 2
  83. }
  84. var list = getFoodTime()
  85. var start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
  86. var end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
  87. if (end < start) {
  88. end += 24 * 60
  89. }
  90. var duration = end - start
  91. realRingBig.startArc = (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  92. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  93. var currentDot = getDot(null, false)
  94. var date = new Date()
  95. var minutes = date.getHours() * 60 + date.getMinutes()
  96. if (minutes < start) {
  97. minutes += 1440
  98. }
  99. if (start <= minutes && end > minutes) {
  100. currentDot.color = ColorType.food
  101. }
  102. else {
  103. currentDot.color = ColorType.ring
  104. }
  105. return <Rings common={common} bgRing={bgRing} realRing={realRingBig} currentDot={currentDot} canvasId={timeStamp + '_big'} />
  106. }
  107. function smallRing() {
  108. var common = getCommon(null, false)
  109. common.radius = 28;
  110. common.lineWidth = 9;
  111. var bgRing = getBgRing()
  112. const realRingBig: RealRing = {
  113. color: ColorType.activity,
  114. startArc: 0,
  115. durationArc: 2
  116. }
  117. var list = getActivityTime()
  118. var start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
  119. var end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
  120. if (end < start) {
  121. end += 24 * 60
  122. }
  123. var duration = end - start
  124. realRingBig.startArc = (start * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  125. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  126. var currentDot = getDot(null, false)
  127. var date = new Date()
  128. var minutes = date.getHours() * 60 + date.getMinutes()
  129. if (minutes < start) {
  130. minutes += 1440
  131. }
  132. if (start <= minutes && end > minutes) {
  133. currentDot.color = ColorType.activity
  134. }
  135. else {
  136. currentDot.color = ColorType.ring
  137. }
  138. return <Rings common={common} bgRing={bgRing} realRing={realRingBig} currentDot={currentDot} canvasId={timeStamp + '_small'} />
  139. }
  140. function dayRing() {
  141. var common = getCommon(null, true)
  142. common.radius = 56;
  143. common.lineWidth = 9;
  144. var bgRing = getBgRing()
  145. const realRingBig: RealRing = {
  146. color: ColorType.day,
  147. startArc: 0,
  148. durationArc: 2
  149. }
  150. var sunRise = 6 * 60
  151. var sunSet = 18 * 60
  152. if (dayNight.gpsInfo && user.test_user) {
  153. var sunRiseObj = dayNight.gpsInfo.daylights[0].sunrise
  154. var sunSetObj = dayNight.gpsInfo.daylights[0].sunset
  155. sunRise = parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  156. sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  157. if (sunSetObj.indexOf('PM') != -1) {
  158. sunSet += 12 * 60
  159. }
  160. }
  161. var duration = sunSet - sunRise
  162. realRingBig.startArc = (sunRise * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  163. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  164. var currentDot = getDot(null, false)
  165. var date = new Date()
  166. var minutes = date.getHours() * 60 + date.getMinutes()
  167. if (minutes < sunRise) {
  168. minutes += 1440
  169. }
  170. if (sunRise <= minutes && sunSet > minutes) {
  171. currentDot.color = ColorType.day
  172. }
  173. else {
  174. currentDot.color = ColorType.ring
  175. }
  176. return <Rings common={common} bgRing={bgRing} realRing={realRingBig} currentDot={currentDot} canvasId={timeStamp + '_day'} />
  177. }
  178. function goAcitivity() {
  179. if (user.isLogin) {
  180. jumpPage('/pages/workout/Workout', 'Workout', navigation)
  181. return
  182. }
  183. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  184. }
  185. function goFood() {
  186. if (user.isLogin) {
  187. jumpPage('/pages/food/Food', 'Food', navigation)
  188. return
  189. }
  190. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  191. }
  192. function dayDuration() {
  193. if (dayNight.gpsInfo && dayNight.isMember) {
  194. var sunRiseObj = dayNight.gpsInfo.daylights[0].sunrise
  195. var sunSetObj = dayNight.gpsInfo.daylights[0].sunset
  196. var sunRise = parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  197. var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  198. if (sunSetObj.indexOf('PM') != -1) {
  199. sunSet += 12 * 60
  200. }
  201. var duration = (sunSet - sunRise) * 60 * 1000
  202. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  203. } else {
  204. return '12小时'
  205. }
  206. }
  207. function eatDuration() {
  208. var list = getFoodTime()
  209. var start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
  210. var end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
  211. if (end < start) {
  212. end += 24 * 60
  213. }
  214. var duration = (end - start) * 60 * 1000
  215. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  216. }
  217. function activityDuration() {
  218. var list = getActivityTime()
  219. var start = parseInt(list[0].split(':')[0]) * 60 + parseInt(list[0].split(':')[1])
  220. var end = parseInt(list[1].split(':')[0]) * 60 + parseInt(list[1].split(':')[1])
  221. if (end < start) {
  222. end += 24 * 60
  223. }
  224. var duration = (end - start) * 60 * 1000
  225. return TimeFormatter.calculateTimeDifference(new Date().getTime(), new Date().getTime() + duration);
  226. }
  227. function rings() {
  228. return <View style={{ position: 'relative', zIndex: 1 }}>
  229. {
  230. bigRing()
  231. }
  232. {
  233. <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
  234. {
  235. smallRing()
  236. }
  237. </View>
  238. }
  239. {
  240. showRing && <View style={{ display: 'flex', position: 'absolute', left: -14, top: -14, right: -14, bottom: -14 }}>
  241. {
  242. dayRing()
  243. }
  244. </View>
  245. }
  246. </View>
  247. }
  248. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  249. <Box>
  250. <View>
  251. <View style={{ display: 'flex', flexDirection: 'row', marginBottom: rpxToPx(60), alignItems: 'center' }}>
  252. {
  253. schedule && rings()
  254. }
  255. {schedule && <View className="duration_bg">
  256. {
  257. showRing && <Text className="duration_title">{t('feature.common.day')}</Text>
  258. }
  259. {
  260. showRing &&
  261. <Text className="duration_value" style={{ color: ColorType.day }}>{dayDuration()}</Text>
  262. }
  263. {
  264. <Text className="duration_title">{t('feature.common.eat')}</Text>
  265. }
  266. {
  267. <Text className="duration_value" style={{ color: ColorType.food }}>{eatDuration()}</Text>
  268. }
  269. {
  270. <Text className="duration_title">{t('feature.common.move')}</Text>
  271. }
  272. {
  273. <Text className="duration_value" style={{ color: ColorType.activity }}>{activityDuration()}</Text>
  274. }
  275. </View>
  276. }
  277. </View>
  278. <View className="food_btn1" style={{ backgroundColor: ColorType.food }} onClick={goFood}>
  279. <Text style={{ fontWeight: 'bold' }}>食物日记</Text>
  280. </View>
  281. <View className="food_btn1" style={{ backgroundColor: ColorType.activity,marginBottom:0 }} onClick={goAcitivity}>
  282. <Text style={{ fontWeight: 'bold' }}>运动训练</Text>
  283. </View>
  284. </View>
  285. </Box>
  286. <View className="cell_bg" style={{ marginTop: 20 }}>
  287. <View className="cell_full">
  288. <Text className="cell_title">{t('feature.common.day')}</Text>
  289. <Switch checked={showRing}
  290. color={ColorType.day}
  291. onChange={(e) => {
  292. setShowRing(e.detail.value)
  293. uploadPerm({ show_day_ring: e.detail.value })
  294. Taro.setStorage({
  295. key: 'showDayRing',
  296. data: e.detail.value
  297. })
  298. }}
  299. />
  300. </View>
  301. {
  302. showRing && <View className="cell_line" style={{ height: 1 }} />
  303. }
  304. {
  305. showRing && <View className="cell_full">
  306. <Text className="cell_title">{t('feature.track_time_duration.third_ring.sunrise_today')}</Text>
  307. <Text className="cell_value" style={{ color: '#fff' }}>{
  308. dayNight.isMember && dayNight.gpsInfo ? dayNight.gpsInfo.daylights[0].sunrise : '06:00'
  309. }</Text>
  310. </View>
  311. }
  312. {
  313. showRing && <View className="cell_line" style={{ height: 1 }} />
  314. }
  315. {
  316. showRing && <View className="cell_full">
  317. <Text className="cell_title">{t('feature.track_time_duration.third_ring.sunset_today')}</Text>
  318. <Text className="cell_value" style={{ color: '#fff' }}>{
  319. dayNight.isMember && dayNight.gpsInfo ? dayNight.gpsInfo.daylights[0].sunset : '18:00'
  320. }</Text>
  321. </View>
  322. }
  323. {/* <View className="cell_line" style={{ height: 1 }} />
  324. <View className="cell_full">
  325. <Text className="cell_title">{t('feature.track_time_duration.third_ring.sunset_today')}</Text>
  326. <Text className="cell_value" style={{ color: '#fff' }}>{
  327. dayNight.isMember && dayNight.gpsInfo ? dayNight.gpsInfo.daylights[0].sunset : '18:00'
  328. }</Text>
  329. </View> */}
  330. </View>
  331. </View>
  332. }