clock.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. const handlePageShow = () => {
  57. console.log('页面展示,相当于onResume');
  58. // 进行相应的处理
  59. // ...
  60. };
  61. Taro.eventCenter.on('onShow', handlePageShow);
  62. }, [])
  63. useEffect(() => {
  64. startTimer();
  65. return () => {
  66. // 在组件卸载时清除定时器
  67. if (timerId) {
  68. clearInterval(timerId);
  69. }
  70. };
  71. }, [timerId]);
  72. const startTimer = () => {
  73. // 避免重复启动定时器
  74. if (timerId) {
  75. return;
  76. }
  77. const id = setInterval(() => {
  78. setCounter((prevCounter) => prevCounter + 1);
  79. }, 1000);
  80. setTimerId(id as any);
  81. };
  82. useReady(async () => {
  83. const userData = await getStorage('userData');
  84. if (userData) {
  85. dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
  86. // return JSON.parse(userData as string) as UserState;
  87. setTimeout(() => {
  88. checkWXPubFollow()
  89. getCheckData()
  90. }, 200)
  91. }
  92. })
  93. useDidShow(() => {
  94. if (user.isLogin) {
  95. checkWXPubFollow()
  96. getCheckData();
  97. }
  98. })
  99. function getCheckData() {
  100. getClocks().then(res => {
  101. dispatch(setScenario((res as any).scenario));
  102. machine.context.checkData = res as any;
  103. global.scenario = (res as any).current_record.scenario;
  104. // trackTimeService.send({ type: 'RESET' });
  105. // trackTimeService.send({ type: (res as any).current_record.scenario });
  106. const currentState = trackTimeService.getSnapshot();
  107. let json = {};
  108. var key = (res as any).current_record.scenario
  109. var status = (res as any).current_record.status
  110. json[key] = status
  111. currentState.value = json;
  112. // debugger
  113. machine.context.currentStatus = `${key}.${status}`;//'mixed.ON_GOING2'
  114. setCheckData(res as any)
  115. })
  116. return
  117. getChecks().then(res => {
  118. if ((res as any).scenario) {
  119. dispatch(setScenario((res as any).scenario));
  120. global.scenario = (res as any).scenario;
  121. // global.checkData = res;
  122. // console.log((res as any).scenario.name + ' ' + (res as any).status);
  123. // trackTimeService.send({type:(res as any).scenario.name});
  124. // setSpecifiedStatus('FAST_SLEEP', 'ON_GOING');
  125. // 初始化状态机
  126. // const initialState = machine.initialState;
  127. // 设置指定状态为 C->3
  128. // const nextState = machine.transition(initialState, 'SET_SPECIFIED_STATE');
  129. // const specifiedState = setSpecifiedState(nextState, 'FAST_SLEEP');
  130. // setSpecifiedState();
  131. // console.log('name:'+(res as any).scenario.name)
  132. // machine.reset();
  133. trackTimeService.send({ type: 'RESET' });
  134. trackTimeService.send({ type: (res as any).scenario.name });
  135. const currentState = trackTimeService.getSnapshot();
  136. let json = {};
  137. var key = (res as any).scenario.name
  138. json[key] = (res as any).status
  139. currentState.value = json;
  140. // debugger
  141. machine.context.currentStatus = `${key}.${(res as any).status}`;//'mixed.ON_GOING2'
  142. machine.context.checkData = res as any;
  143. // debugger
  144. // trackTimeService.send('APPLE');
  145. // machine.transition(`${key}.${(res as any).status}`,'' as any);
  146. // trackTimeService.send({type:'START_FAST'});
  147. // trackTimeService.send({type:'START_SLEEP'});
  148. // trackTimeService.send({type:'END_SLEEP'});
  149. // trackTimeService.send({type:'END_SLEEP'});
  150. // trackTimeService.send({type:'START_FAST'});
  151. // trackTimeService.send({type:'END_FAST'});
  152. // trackTimeService.send({type:'START_SLEEP'});
  153. // console.log(currentState.value);
  154. // debugger
  155. // trackTimeService.send('setCurrentStatus', {status:(res as any).status});
  156. // setCurrentStatus((res as any).status);
  157. // trackTimeService.send({type:'START_FAST'});
  158. }
  159. })
  160. }
  161. function checkWXPubFollow() {
  162. wxPubFollow().then(res => {
  163. setIsFollowed((res as any).wx_pub_followed)
  164. // if ((res as any).wx_pub_followed == true) {
  165. // // setTimeout(() => {
  166. // // dispatch(setWXPubFollow(true));
  167. // // }, 1000)
  168. // // dispatch(setWXPubFollow(true));
  169. // setIsFollowed((res as any).wx_pub_followed)
  170. // }
  171. })
  172. }
  173. function followWxPub() {
  174. const resource = common.resources.filter((item: any) => {
  175. return item.code == 'follow_wx_pub'
  176. })
  177. Taro.showModal({
  178. title: '提示',
  179. content: '关注公众号后可接收提醒\n点击确定,前往关注',
  180. showCancel: true,
  181. success(result) {
  182. if (result.confirm) {
  183. Taro.navigateTo({
  184. url: '/pages/H5?title=fast16cc 关注服务号&url=' + resource[0].url,
  185. })
  186. }
  187. },
  188. })
  189. }
  190. async function getStorage(key: string) {
  191. try {
  192. const res = await Taro.getStorage({ key });
  193. return res.data;
  194. } catch {
  195. return '';
  196. }
  197. }
  198. function schedule() {
  199. if (!user.isLogin) {
  200. return <View />
  201. }
  202. // if ((checkData as any).current_record.scenario == 'FAST_SLEEP') {
  203. return <Schedule />
  204. // }
  205. // return <View />
  206. }
  207. return (
  208. <View style={{ flex: 1, flexDirection: 'column', display: 'flex', backgroundColor: '#000', color: '#fff' }}>
  209. <TitleBar />
  210. <Clocks />
  211. <View className='console_box'>
  212. <Console />
  213. </View>
  214. <More ref={global.moreRef}/>
  215. {
  216. !isFollowed && user.isLogin && <Text style={{ textAlign: 'center', width: '100%' }} onClick={() => followWxPub()}>去关注公众号</Text>
  217. }
  218. {
  219. user.isLogin && <Text onClick={()=>{
  220. Taro.navigateTo({
  221. url:'/pages/RecordsHistory'
  222. })
  223. }}>查看全部记录</Text>
  224. }
  225. {
  226. checkData && schedule()
  227. }
  228. {
  229. checkData && (checkData as any).latest_record && <Schedule type='latest' />
  230. }
  231. {/* <Picker mode="time" start="07:00" end="20:00">
  232. <Text style="color: #fff;">test picker</Text>
  233. </Picker> */}
  234. <View style={{ height: 100 }} />
  235. </View>
  236. )
  237. }