Index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. import { View, Text, Image, ScrollView, PageContainer, Swiper, SwiperItem, Switch } from "@tarojs/components";
  2. import Tabbar from "@/components/navigation/TabBar";
  3. import IndexItem from '@/features/trackTimeDuration/components/IndexItem';
  4. import Rings from "@/features/trackTimeDuration/components/Rings";
  5. import './Index.scss'
  6. import { useDispatch, useSelector } from "react-redux";
  7. import { useDidShow, useReady } from "@tarojs/taro";
  8. import Taro from "@tarojs/taro";
  9. import { getInfoSuccess } from "@/store/user";
  10. import { clockHome, clockSummaryRecords, clockSummaryStats, getClockRecords, getClocks } from "@/services/trackTimeDuration";
  11. import { updateScenario } from "@/store/time";
  12. import { setConfigs } from "@/store/common";
  13. import { setScenario, setStep } from "@/store/scenario";
  14. import { useEffect, useState } from "react";
  15. import { IconPlus, IconRadioCheck, IconRadioCross } from "@/components/basic/Icons";
  16. import { ColorType } from "@/context/themes/color";
  17. import { bigRingRadius, getBgRing, getCommon, getDot, getSchedule, ringWidth, smallRingRadius } from "@/features/trackTimeDuration/hooks/RingData";
  18. import { RealRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  19. import IndexConsole from "@/features/trackTimeDuration/components/IndexConsole";
  20. import Modal from '@/components/layout/Modal'
  21. import { rpxToPx } from "@/utils/tools";
  22. import RecordFastSleep from "@/features/trackTimeDuration/components/RecordFastSleep";
  23. import DayLight from "@/features/trackTimeDuration/components/DayLight";
  24. import { getInfo } from "@/services/user";
  25. import { TimeFormatter } from "@/utils/time_format";
  26. import WeekCalendar from "@/features/trackTimeDuration/components/WeekCalendar";
  27. import { useTranslation } from "react-i18next";
  28. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  29. import Layout from "@/components/layout/layout";
  30. import { ModalType, NaviBarTitleShowType, TemplateType } from "@/utils/types";
  31. import TitleView from "@/features/trackTimeDuration/components/TitleView";
  32. import ClockHeader from "@/features/trackTimeDuration/components/ClockHeader";
  33. import DurationPicker from "@/features/trackTimeDuration/components/DurationPicker";
  34. import SegmentPop from "@/features/trackTimeDuration/components/SegmentPop";
  35. import Box from "@/components/layout/Box";
  36. import DayNightCard from "@/features/trackSomething/components/DayNightCard";
  37. import StageSelector from "@/features/trackTimeDuration/components/StageSelector";
  38. let GradientText
  39. let useNavigation;
  40. let isPause = false;
  41. if (process.env.TARO_ENV == 'rn') {
  42. GradientText = require('@/components/basic/GradientText').default
  43. useNavigation = require("@react-navigation/native").useNavigation
  44. }
  45. export default function Page() {
  46. const dispatch = useDispatch();
  47. global.dispatch = dispatch;
  48. let navigation;
  49. if (useNavigation) {
  50. navigation = useNavigation()
  51. }
  52. const { t } = useTranslation()
  53. const user = useSelector((state: any) => state.user);
  54. const time = useSelector((state: any) => state.time);
  55. const [count, setCount] = useState(0)
  56. const [homeData, setHomeData] = useState(null)
  57. const [selIndex, setSelIndex] = useState(0)
  58. const [showModal, setShowModal] = useState(false)
  59. const [modalDetail, setModalDetail] = useState<any>(null)
  60. const [showModal2, setShowModal2] = useState(false)
  61. const [modalDetail2, setModalDetail2] = useState<any>(null)
  62. const [debugInfo, setDebugInfo] = useState(null)
  63. const [isMulti, setIsMulti] = useState(false)
  64. const [records, setRecords] = useState([])
  65. const [multiData, setMultiData] = useState([
  66. {
  67. title: '睡前断食',
  68. checked: false
  69. },
  70. {
  71. title: '睡眠中断食',
  72. checked: false
  73. },
  74. {
  75. title: '起床后断食',
  76. checked: false
  77. },
  78. ])
  79. useEffect(() => {
  80. global.showNightRing = false;
  81. setInterval(() => {
  82. if (isPause) {
  83. return
  84. }
  85. setCount((prevCounter) => prevCounter + 1)
  86. }, 1000)
  87. }, [])
  88. useEffect(() => {
  89. getCheckData()
  90. }, [user.isLogin, time.status])
  91. useReady(async () => {
  92. const userData = await getStorage('userData');
  93. debugger
  94. if (userData) {
  95. dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
  96. // setTimeout(() => {
  97. // // checkWXPubFollow()
  98. // getCheckData()
  99. // }, 200)
  100. getHistory()
  101. }
  102. })
  103. useDidShow(() => {
  104. // if (user.isLogin)
  105. // refresh()
  106. })
  107. global.refreshIndex = () => {
  108. setCount((prevCounter) => prevCounter + 1)
  109. }
  110. function refresh() {
  111. getInfo().then(res => {
  112. dispatch(getInfoSuccess(res))
  113. }).catch(e => {
  114. })
  115. }
  116. function getCheckData() {
  117. // clockHome().then(res => {
  118. // setHomeData(res as any)
  119. // global.homeData = res
  120. // if (user.isLogin) {
  121. // isPause = (res as any).fast_sleep.current_record.status == 'WAIT_FOR_START'
  122. // dispatch(updateScenario((res as any).fast_sleep.current_record))
  123. // dispatch(setConfigs((res as any).time_input_schema));
  124. // dispatch(setScenario((res as any).fast_sleep.scenario));
  125. // }
  126. // })
  127. // if (!user.isLogin){
  128. // return;
  129. // }
  130. getClocks().then(res => {
  131. setHomeData(res as any)
  132. global.homeData = res
  133. if (user.isLogin) {
  134. isPause = (res as any).current_record.status == 'WAIT_FOR_START'
  135. dispatch(updateScenario((res as any).current_record))
  136. dispatch(setConfigs((res as any).time_input_schema));
  137. dispatch(setScenario((res as any).scenario));
  138. global.scenario = (res as any).scenario.name;
  139. }
  140. })
  141. getHistory()
  142. }
  143. function getHistory() {
  144. if (user.isLogin)
  145. getClockRecords({
  146. page: 1,
  147. limit: 1,
  148. part_completed: true
  149. // completed: true
  150. }).then(res => {
  151. setRecords((res as any).data)
  152. })
  153. }
  154. const startArc = (time: number) => {
  155. var date = new Date(time);
  156. var hour = date.getHours();
  157. var minute = date.getMinutes();
  158. var second = date.getSeconds();
  159. return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  160. }
  161. const durationArc = (start_time: number, end_time: number) => {
  162. var duration = (end_time - start_time) / 1000;
  163. return duration / (24 * 3600) * 2 * Math.PI;
  164. }
  165. function bigRing() {
  166. var currentRecord = (homeData as any).current_record
  167. var common = getCommon(null, true)
  168. common.radius = bigRingRadius;
  169. common.lineWidth = ringWidth;
  170. var bgRing = getBgRing()
  171. var realRing1 = getSchedule(currentRecord, true, true)
  172. realRing1.color = ColorType.fast + '66'
  173. var list: any = []
  174. if (multiData[0].checked) {
  175. const realRingBig: RealRing = {
  176. color: global.fastColor ? global.fastColor : ColorType.fast,
  177. startArc: startArc(currentRecord.fast.target_start_time),
  178. durationArc: durationArc(currentRecord.fast.target_start_time, currentRecord.sleep.target_start_time)
  179. }
  180. list.push(realRingBig)
  181. }
  182. if (multiData[1].checked) {
  183. const realRingBig: RealRing = {
  184. color: global.fastColor ? global.fastColor : ColorType.fast,
  185. startArc: startArc(currentRecord.sleep.target_start_time),
  186. durationArc: durationArc(currentRecord.sleep.target_start_time, currentRecord.sleep.target_end_time)
  187. }
  188. list.push(realRingBig)
  189. }
  190. if (multiData[2].checked) {
  191. const realRingBig: RealRing = {
  192. color: global.fastColor ? global.fastColor : ColorType.fast,
  193. startArc: startArc(currentRecord.sleep.target_end_time),
  194. durationArc: durationArc(currentRecord.sleep.target_end_time, currentRecord.fast.target_end_time)
  195. }
  196. list.push(realRingBig)
  197. }
  198. var points: any = []
  199. for (var i = 0; i < 12; i++) {
  200. var dot: CurrentDot = {
  201. color: 'red',
  202. lineWidth: 8,
  203. borderColor: 'black',
  204. timestamp: new Date().getTime() + i * 3600 * 1000 * 2
  205. }
  206. points.push(dot)
  207. }
  208. return <Rings common={common} bgRing={bgRing} realRing={realRing1} stageList={list} dotList={points} canvasId={'testA'} />
  209. }
  210. function smallRing() {
  211. var common = getCommon(null, true)
  212. common.radius = smallRingRadius;
  213. common.lineWidth = ringWidth;
  214. var bgRing = getBgRing()
  215. var realRing1 = getSchedule((homeData as any).current_record, false, false)
  216. return <Rings common={common} bgRing={bgRing} realRing={realRing1} canvasId={'testB'} />
  217. }
  218. function rings() {
  219. return <View style={{ display: 'flex', flexDirection: 'row', marginLeft: 100, marginBottom: 30 }}>
  220. <View style={{ position: 'relative', zIndex: 1 }}>
  221. {
  222. bigRing()
  223. }
  224. {
  225. <View style={{ display: 'flex', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }}>
  226. {
  227. smallRing()
  228. }
  229. </View>
  230. }
  231. </View>
  232. </View>
  233. }
  234. global.indexPageRefresh = () => {
  235. getCheckData()
  236. }
  237. global.showIndexModal = (isShow: boolean, detail: any, debugNode?: any) => {
  238. global.showModal = isShow
  239. setDebugInfo(debugNode)
  240. setShowModal(isShow)
  241. setModalDetail(detail)
  242. }
  243. global.showIndexModal2 = (isShow: boolean, detail: any) => {
  244. setDebugInfo(null)
  245. global.showModal = isShow
  246. setShowModal2(isShow)
  247. setModalDetail2(detail)
  248. }
  249. global.changeTargetDuration = (duration: number, isFast: boolean) => {
  250. var obj = JSON.parse(JSON.stringify(homeData))
  251. var record = obj.current_record
  252. if (isFast) {
  253. record.fast.target_end_time = record.fast.target_start_time + duration * 60 * 1000
  254. }
  255. else {
  256. record.sleep.target_end_time = record.sleep.target_start_time + duration * 60 * 1000
  257. }
  258. setHomeData(obj)
  259. }
  260. global.updateFastSleepData = (data: any) => {
  261. if (data.id == (homeData as any).current_record.id) {
  262. var obj = JSON.parse(JSON.stringify(homeData))
  263. obj.current_record = data
  264. setHomeData(obj)
  265. }
  266. }
  267. function modalContent() {
  268. if (showModal || showModal2) {
  269. if (process.env.TARO_ENV == 'weapp') {
  270. return <Modal
  271. testInfo={debugInfo}
  272. dismiss={() => {
  273. setDebugInfo(null)
  274. setShowModal(false);
  275. setShowModal2(false);
  276. }}
  277. confirm={() => { }}>
  278. {
  279. showModal ? modalDetail : modalDetail2
  280. }
  281. </Modal>
  282. }
  283. else if (process.env.TARO_ENV == 'rn') {
  284. return <PageContainer style={{ backgroundColor: '#1c1c1c' }}
  285. // overlayStyle='background-color:rgba(0,0,0,0.9)'
  286. // custom-style='background-color:#1c1c1c'
  287. overlayStyle={{ backgroundColor: 'rgba(0,0,0,0.9)' }}
  288. customStyle={{ backgroundColor: '#1c1c1c' }}
  289. closeOnSlideDown={false}
  290. onBeforeEnter={() => {
  291. Taro.hideTabBar();
  292. }}
  293. onBeforeLeave={() => {
  294. Taro.showTabBar();
  295. }}
  296. onClick={() => { alert('b') }}
  297. onClickOverlay={() => { alert('a') }}
  298. onAfterLeave={() => { setShowModal(false); setShowModal2(false) }}
  299. show={showModal || showModal2} round={true} overlay={true} position='bottom'
  300. >
  301. {
  302. showModal ? modalDetail : modalDetail2
  303. }
  304. </PageContainer>
  305. }
  306. }
  307. return <View />
  308. }
  309. async function getStorage(key: string) {
  310. try {
  311. const res = await Taro.getStorage({ key });
  312. return res.data;
  313. } catch {
  314. return '';
  315. }
  316. }
  317. if (!homeData) {
  318. return <View>
  319. <Tabbar index={0} />
  320. </View>
  321. }
  322. function more() {
  323. jumpPage('/pages/common/RecordsHistory?type=time&title=time', 'RecordsHistory', navigation, {
  324. type: 'time',
  325. title: 'time'
  326. })
  327. }
  328. function headerView() {
  329. return <ClockHeader homeData={homeData} />
  330. }
  331. var timestamp = new Date().getTime()
  332. function render() {
  333. return <Layout type={TemplateType.customHeader} header={headerView()} title={t('page.clock.title')} titleShowStyle={NaviBarTitleShowType.scrollToShow}>
  334. <View className="index_container">
  335. <Text className="count">{count}</Text>
  336. <Box>
  337. <View>
  338. <IndexItem type="FAST_SLEEP" data={homeData} time={timestamp} showStage={false} />
  339. {
  340. user.isLogin ? <IndexConsole record={homeData} /> : <StageSelector />
  341. }
  342. </View>
  343. </Box>
  344. {/* {
  345. user.isLogin && <DayLight />
  346. } */}
  347. <DayNightCard isNight={true} switchChanged={(e) => { }} />
  348. {
  349. user.isLogin && records.length > 0 && <View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', marginTop: rpxToPx(40), }}>
  350. {
  351. <Text className="discovery1" style={{ marginBottom: 0 }}>{t('feature.track_time_duration.record_fast_sleep.header.latest_record')}</Text>
  352. }
  353. {
  354. process.env.TARO_ENV == 'weapp' && <Text className="fast_sleep_more index_more" onClick={more}>{t('feature.track_time_duration.record_fast_sleep.header.btn_show_all')}</Text>
  355. }
  356. {
  357. process.env.TARO_ENV == 'rn' && <GradientText onClick={more} style={{ fontSize: rpxToPx(32), fontWeight: 'bold' }}>{t('feature.track_time_duration.record_fast_sleep.header.btn_show_all')}</GradientText>
  358. }
  359. </View>
  360. }
  361. {
  362. records.length > 0 && <View className="fast_sleep_item_bg">
  363. <RecordFastSleep data={records[0]} type='record' index={-20000} />
  364. </View>
  365. }
  366. {
  367. user.isLogin && <View style={{ marginTop: rpxToPx(60), display: 'flex', flexDirection: 'column' }}>
  368. <Text className="discovery1" >周统计</Text>
  369. <WeekCalendar />
  370. </View>
  371. }
  372. <View style={{ height: 100 }} />
  373. {
  374. modalContent()
  375. }
  376. {
  377. homeData && <SegmentPop data={homeData} />
  378. }
  379. {
  380. homeData && <DurationPicker record={(homeData as any).current_record} />
  381. }
  382. <Tabbar index={0} />
  383. </View>
  384. </Layout>
  385. }
  386. // if (process.env.TARO_ENV == 'rn') {
  387. // return <ScrollView>
  388. // {
  389. // render()
  390. // }
  391. // </ScrollView>
  392. // }
  393. return render()
  394. }