clock.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { Component, PropsWithChildren, useEffect, useRef, useState } from 'react'
  2. import { View, Text, Button, Input, Picker } from '@tarojs/components'
  3. import './index/index.scss'
  4. import './Clock.scss'
  5. import Taro, { useDidShow, useReady, useRouter } from '@tarojs/taro'
  6. // import ComponentA from './component'
  7. import Rings from '@components/Rings';
  8. import { useDispatch, useSelector } from 'react-redux';
  9. import { getInfoSuccess } from '@/store/user';
  10. import { wxPubFollow } from '@/services/permission';
  11. import { gobalConfigs, staticResources } from '@/services/common';
  12. import { use } from 'i18next';
  13. import Clocks from '@/features/trackTimeDuration/components/Clock';
  14. import Console from '@/features/trackTimeDuration/components/Console';
  15. import More from '@/features/trackTimeDuration/components/More';
  16. import Schedule from '@/features/trackTimeDuration/components/Schedule';
  17. import TitleBar from '@/features/trackTimeDuration/components/TitleBar';
  18. import { getChecks, getClocks } from '@/services/trackTimeDuration';
  19. import { setScenario } from '@/store/scenario';
  20. import { setSpecifiedStatus, setSpecifiedState, machine } from '@/store/trackTimeMachine';
  21. import trackTimeService from '@/store/trackTimeMachine';
  22. import { setWXPubFollow } from '@/store/permission';
  23. // import TabBar from '../../components/Tabbar';
  24. export default function IndexPage() {
  25. const dispatch = useDispatch();
  26. const array: any[] = []
  27. const [isFollowed, setIsFollowed] = useState(false)
  28. const [checkData, setCheckData] = useState(null)
  29. const user = useSelector((state: any) => state.user);
  30. const permission = useSelector((state: any) => state.permission);
  31. const common = useSelector((state: any) => state.common);
  32. const [counter, setCounter] = useState(0)
  33. const [timerId, setTimerId] = useState(null)
  34. // const moreRef = useRef<any>(null);
  35. // global.moreRef = Taro.createRef()
  36. array.push('ffff');
  37. array.push(<Text>ffff</Text>)
  38. // array.push(<Rings radius={50}/>)
  39. global.dispatch = dispatch;
  40. useEffect(() => {
  41. dispatch(staticResources() as any);
  42. dispatch(gobalConfigs() as any);
  43. // dispatch(gobalConfigs() as any);
  44. trackTimeService.onTransition(state => {
  45. if ((state.value as any).FAST_SLEEP == 'COMPLETED' ||
  46. (state.value as any).FAST == 'ONGOING' ||
  47. (state.value as any).SLEEP == 'ONGOING' ||
  48. (state.value as any).FAST_SLEEP == 'ONGOING1' ||
  49. (state.value as any).FAST_SLEEP == 'ONGOING2' ||
  50. (state.value as any).FAST_SLEEP == 'ONGOING3' ||
  51. (state.value as any).FAST == 'COMPLETED' ||
  52. (state.value as any).SLEEP == 'COMPLETED') {
  53. getCheckData()
  54. }
  55. })
  56. }, [])
  57. useEffect(() => {
  58. startTimer();
  59. return () => {
  60. // 在组件卸载时清除定时器
  61. if (timerId) {
  62. clearInterval(timerId);
  63. }
  64. };
  65. }, [timerId]);
  66. const startTimer = () => {
  67. // 避免重复启动定时器
  68. if (timerId) {
  69. return;
  70. }
  71. const id = setInterval(() => {
  72. setCounter((prevCounter) => prevCounter + 1);
  73. }, 1000);
  74. setTimerId(id as any);
  75. };
  76. useReady(async () => {
  77. const userData = await getStorage('userData');
  78. if (userData) {
  79. dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
  80. // return JSON.parse(userData as string) as UserState;
  81. setTimeout(() => {
  82. checkWXPubFollow()
  83. getCheckData()
  84. }, 200)
  85. }
  86. })
  87. useDidShow(() => {
  88. if (user.isLogin) {
  89. checkWXPubFollow()
  90. getCheckData();
  91. }
  92. })
  93. function getCheckData() {
  94. getClocks().then(res => {
  95. dispatch(setScenario((res as any).scenario));
  96. machine.context.checkData = res as any;
  97. global.scenario = (res as any).current_record.scenario;
  98. // trackTimeService.send({ type: 'RESET' });
  99. // trackTimeService.send({ type: (res as any).current_record.scenario });
  100. const currentState = trackTimeService.getSnapshot();
  101. let json = {};
  102. var key = (res as any).current_record.scenario
  103. var status = (res as any).current_record.status
  104. json[key] = status
  105. currentState.value = json;
  106. // debugger
  107. machine.context.currentStatus = `${key}.${status}`;//'mixed.ON_GOING2'
  108. setCheckData(res as any)
  109. })
  110. return
  111. getChecks().then(res => {
  112. if ((res as any).scenario) {
  113. dispatch(setScenario((res as any).scenario));
  114. global.scenario = (res as any).scenario;
  115. // global.checkData = res;
  116. // console.log((res as any).scenario.name + ' ' + (res as any).status);
  117. // trackTimeService.send({type:(res as any).scenario.name});
  118. // setSpecifiedStatus('FAST_SLEEP', 'ON_GOING');
  119. // 初始化状态机
  120. // const initialState = machine.initialState;
  121. // 设置指定状态为 C->3
  122. // const nextState = machine.transition(initialState, 'SET_SPECIFIED_STATE');
  123. // const specifiedState = setSpecifiedState(nextState, 'FAST_SLEEP');
  124. // setSpecifiedState();
  125. // console.log('name:'+(res as any).scenario.name)
  126. // machine.reset();
  127. trackTimeService.send({ type: 'RESET' });
  128. trackTimeService.send({ type: (res as any).scenario.name });
  129. const currentState = trackTimeService.getSnapshot();
  130. let json = {};
  131. var key = (res as any).scenario.name
  132. json[key] = (res as any).status
  133. currentState.value = json;
  134. // debugger
  135. machine.context.currentStatus = `${key}.${(res as any).status}`;//'mixed.ON_GOING2'
  136. machine.context.checkData = res as any;
  137. // debugger
  138. // trackTimeService.send('APPLE');
  139. // machine.transition(`${key}.${(res as any).status}`,'' as any);
  140. // trackTimeService.send({type:'START_FAST'});
  141. // trackTimeService.send({type:'START_SLEEP'});
  142. // trackTimeService.send({type:'END_SLEEP'});
  143. // trackTimeService.send({type:'END_SLEEP'});
  144. // trackTimeService.send({type:'START_FAST'});
  145. // trackTimeService.send({type:'END_FAST'});
  146. // trackTimeService.send({type:'START_SLEEP'});
  147. // console.log(currentState.value);
  148. // debugger
  149. // trackTimeService.send('setCurrentStatus', {status:(res as any).status});
  150. // setCurrentStatus((res as any).status);
  151. // trackTimeService.send({type:'START_FAST'});
  152. }
  153. })
  154. }
  155. function checkWXPubFollow() {
  156. wxPubFollow().then(res => {
  157. setIsFollowed((res as any).wx_pub_followed)
  158. // if ((res as any).wx_pub_followed == true) {
  159. // // setTimeout(() => {
  160. // // dispatch(setWXPubFollow(true));
  161. // // }, 1000)
  162. // // dispatch(setWXPubFollow(true));
  163. // setIsFollowed((res as any).wx_pub_followed)
  164. // }
  165. })
  166. }
  167. function followWxPub() {
  168. const resource = common.resources.filter((item: any) => {
  169. return item.code == 'follow_wx_pub'
  170. })
  171. Taro.showModal({
  172. title: '提示',
  173. content: '关注公众号后可接收提醒\n点击确定,前往关注',
  174. showCancel: true,
  175. success(result) {
  176. if (result.confirm) {
  177. Taro.navigateTo({
  178. url: '/pages/H5?title=fast16cc 关注服务号&url=' + resource[0].url,
  179. })
  180. }
  181. },
  182. })
  183. }
  184. async function getStorage(key: string) {
  185. try {
  186. const res = await Taro.getStorage({ key });
  187. return res.data;
  188. } catch {
  189. return '';
  190. }
  191. }
  192. function schedule() {
  193. if (!user.isLogin) {
  194. return <View />
  195. }
  196. // if ((checkData as any).current_record.scenario == 'FAST_SLEEP') {
  197. return <Schedule />
  198. // }
  199. // return <View />
  200. }
  201. return (
  202. <View style={{ flex: 1, flexDirection: 'column', display: 'flex', backgroundColor: '#000', color: '#fff' }}>
  203. <TitleBar />
  204. <Clocks />
  205. <View className='console_box'>
  206. {/* <Console /> */}
  207. </View>
  208. <More ref={global.moreRef}/>
  209. {
  210. !isFollowed && user.isLogin && <Text style={{ textAlign: 'center', width: '100%' }} onClick={() => followWxPub()}>去关注公众号</Text>
  211. }
  212. {
  213. user.isLogin && <Text onClick={()=>{
  214. Taro.navigateTo({
  215. url:'/pages/RecordsHistory'
  216. })
  217. }}>查看全部记录</Text>
  218. }
  219. {
  220. checkData && schedule()
  221. }
  222. {
  223. checkData && (checkData as any).latest_record && <Schedule type='latest' />
  224. }
  225. {/* <Picker mode="time" start="07:00" end="20:00">
  226. <Text style="color: #fff;">test picker</Text>
  227. </Picker> */}
  228. <View style={{ height: 100 }} />
  229. </View>
  230. )
  231. }