clock.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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, logoutSuccess } from '@/store/user';
  10. import { wxPubFollow } from '@/services/permission';
  11. import { gobalConfigs, staticResources, uploadSessionKey } 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 { setWXFollow } from '@/store/permission';
  23. import Tooltip from '@/components/Tooltip'
  24. import RequestType, { thirdPartRequest } from '@/services/thirdPartRequest'
  25. import { setConfigs } from '@/store/common'
  26. // import TabBar from '../../components/Tabbar';
  27. export default function IndexPage() {
  28. const dispatch = useDispatch();
  29. const array: any[] = []
  30. const [checkData, setCheckData] = useState(null)
  31. const user = useSelector((state: any) => state.user);
  32. const permission = useSelector((state: any) => state.permission);
  33. const common = useSelector((state: any) => state.common);
  34. const [counter, setCounter] = useState(0)
  35. const [timerId, setTimerId] = useState(null)
  36. const [needShowAddTip, setNeedShowAddTip] = useState(false)
  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. trackTimeService.onTransition(state => {
  44. if ((state.value as any).FAST_SLEEP == 'COMPLETED' ||
  45. (state.value as any).FAST == 'ONGOING' ||
  46. (state.value as any).SLEEP == 'ONGOING' ||
  47. (state.value as any).FAST_SLEEP == 'ONGOING1' ||
  48. (state.value as any).FAST_SLEEP == 'ONGOING2' ||
  49. (state.value as any).FAST_SLEEP == 'ONGOING3' ||
  50. (state.value as any).FAST == 'COMPLETED' ||
  51. (state.value as any).SLEEP == 'COMPLETED') {
  52. getCheckData()
  53. }
  54. })
  55. }, [])
  56. useEffect(() => {
  57. if (user.isLogin) {
  58. //检查用户是否添加过小程序
  59. checkAddToMini();
  60. //检查session是否过期
  61. checkSession()
  62. }
  63. }, [user.isLogin])
  64. function checkSession() {
  65. // Taro.login().then(res => {
  66. // // code = res.code;
  67. // uploadSessionKey({ type: 'WX_MP', code: res.code });
  68. // })
  69. // return
  70. if (process.env.TARO_ENV === 'weapp') {
  71. console.log('session request')
  72. thirdPartRequest(RequestType.RequestTypeCheckSession).then(res => {
  73. console.log('session_key 未过期,并且在本生命周期一直有效')
  74. }).catch(err => {
  75. console.log('session_key 已经失效,需要更新登录code')
  76. thirdPartRequest(RequestType.RequestTypeWXLogin).then(result => {
  77. uploadSessionKey({ type: 'WX_MP', code: (result as any).code });
  78. })
  79. })
  80. }
  81. }
  82. function checkAddToMini() {
  83. process.env.TARO_ENV == 'weapp' &&
  84. wx.checkIsAddedToMyMiniProgram({
  85. success: (res) => {
  86. if (!res.added) {
  87. setNeedShowAddTip(true)
  88. }
  89. },
  90. fail: (e) => {
  91. console.log(e)
  92. }
  93. });
  94. }
  95. useEffect(() => {
  96. startTimer();
  97. return () => {
  98. // 在组件卸载时清除定时器
  99. if (timerId) {
  100. clearInterval(timerId);
  101. }
  102. };
  103. }, [timerId]);
  104. const startTimer = () => {
  105. // 避免重复启动定时器
  106. if (timerId) {
  107. return;
  108. }
  109. const id = setInterval(() => {
  110. setCounter((prevCounter) => prevCounter + 1);
  111. }, 1000);
  112. setTimerId(id as any);
  113. };
  114. useReady(async () => {
  115. // console.log('wwwwwwww')
  116. const userData = await getStorage('userData');
  117. if (userData) {
  118. dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
  119. // return JSON.parse(userData as string) as UserState;
  120. setTimeout(() => {
  121. checkWXPubFollow()
  122. getCheckData()
  123. }, 200)
  124. }
  125. })
  126. function clearTempScenarioCache() {
  127. global.schedule_fast = null
  128. global.schedule_sleep = null
  129. Taro.removeStorage({
  130. key: 'tempScenario',
  131. success: function (res) {
  132. }
  133. })
  134. }
  135. useDidShow(() => {
  136. if (user.isLogin) {
  137. checkWXPubFollow()
  138. getCheckData();
  139. }
  140. clearTempScenarioCache()
  141. })
  142. function getCheckData() {
  143. getClocks().then(res => {
  144. dispatch(setConfigs((res as any).time_input_schema));
  145. dispatch(setScenario((res as any).scenario));
  146. machine.context.checkData = res as any;
  147. global.scenario = (res as any).current_record.scenario;
  148. // trackTimeService.send({ type: 'RESET' });
  149. // trackTimeService.send({ type: (res as any).current_record.scenario });
  150. const currentState = trackTimeService.getSnapshot();
  151. let json = {};
  152. var key = (res as any).current_record.scenario
  153. var status = (res as any).current_record.status
  154. json[key] = status
  155. currentState.value = json;
  156. // debugger
  157. machine.context.currentStatus = `${key}.${status}`;//'mixed.ON_GOING2'
  158. setCheckData(res as any)
  159. })
  160. return
  161. getChecks().then(res => {
  162. if ((res as any).scenario) {
  163. dispatch(setScenario((res as any).scenario));
  164. global.scenario = (res as any).scenario;
  165. // global.checkData = res;
  166. // console.log((res as any).scenario.name + ' ' + (res as any).status);
  167. // trackTimeService.send({type:(res as any).scenario.name});
  168. // setSpecifiedStatus('FAST_SLEEP', 'ON_GOING');
  169. // 初始化状态机
  170. // const initialState = machine.initialState;
  171. // 设置指定状态为 C->3
  172. // const nextState = machine.transition(initialState, 'SET_SPECIFIED_STATE');
  173. // const specifiedState = setSpecifiedState(nextState, 'FAST_SLEEP');
  174. // setSpecifiedState();
  175. // console.log('name:'+(res as any).scenario.name)
  176. // machine.reset();
  177. trackTimeService.send({ type: 'RESET' });
  178. trackTimeService.send({ type: (res as any).scenario.name });
  179. const currentState = trackTimeService.getSnapshot();
  180. let json = {};
  181. var key = (res as any).scenario.name
  182. json[key] = (res as any).status
  183. currentState.value = json;
  184. // debugger
  185. machine.context.currentStatus = `${key}.${(res as any).status}`;//'mixed.ON_GOING2'
  186. machine.context.checkData = res as any;
  187. // debugger
  188. // trackTimeService.send('APPLE');
  189. // machine.transition(`${key}.${(res as any).status}`,'' as any);
  190. // trackTimeService.send({type:'START_FAST'});
  191. // trackTimeService.send({type:'START_SLEEP'});
  192. // trackTimeService.send({type:'END_SLEEP'});
  193. // trackTimeService.send({type:'END_SLEEP'});
  194. // trackTimeService.send({type:'START_FAST'});
  195. // trackTimeService.send({type:'END_FAST'});
  196. // trackTimeService.send({type:'START_SLEEP'});
  197. // console.log(currentState.value);
  198. // debugger
  199. // trackTimeService.send('setCurrentStatus', {status:(res as any).status});
  200. // setCurrentStatus((res as any).status);
  201. // trackTimeService.send({type:'START_FAST'});
  202. }
  203. })
  204. }
  205. function checkWXPubFollow() {
  206. wxPubFollow().then(res => {
  207. dispatch(setWXFollow((res as any).wx_pub_followed));
  208. })
  209. }
  210. function followWxPub() {
  211. const resource = common.resources.filter((item: any) => {
  212. return item.code == 'follow_wx_pub'
  213. })
  214. Taro.showModal({
  215. title: '提示',
  216. content: '关注公众号后可接收提醒\n点击确定,前往关注',
  217. showCancel: true,
  218. success(result) {
  219. if (result.confirm) {
  220. Taro.navigateTo({
  221. url: '/pages/H5?title=fast16cc 关注服务号&url=' + resource[0].url,
  222. })
  223. }
  224. },
  225. })
  226. }
  227. async function getStorage(key: string) {
  228. try {
  229. const res = await Taro.getStorage({ key });
  230. return res.data;
  231. } catch {
  232. return '';
  233. }
  234. }
  235. function schedule() {
  236. if (!user.isLogin) {
  237. return <View />
  238. }
  239. // if ((checkData as any).current_record.scenario == 'FAST_SLEEP') {
  240. return <Schedule />
  241. // }
  242. // return <View />
  243. }
  244. return (
  245. <View style={{ flex: 1, flexDirection: 'column', display: 'flex', backgroundColor: '#000', color: '#fff' }}>
  246. {
  247. needShowAddTip && <Tooltip title="添加到我的小程序" closeTip={() => { setNeedShowAddTip(false) }} />
  248. }
  249. <TitleBar />
  250. <Clocks />
  251. <View className='console_box'>
  252. <Console />
  253. </View>
  254. <More ref={global.moreRef} />
  255. {
  256. !permission.wxPubFollow && user.isLogin && <Text style={{ textAlign: 'center', width: '100%' }} onClick={() => followWxPub()}>去关注公众号</Text>
  257. }
  258. {
  259. user.isLogin && <Text onClick={() => {
  260. Taro.navigateTo({
  261. url: '/pages/RecordsHistory?type=time'
  262. })
  263. }}>查看全部记录</Text>
  264. }
  265. {
  266. checkData && schedule()
  267. }
  268. {
  269. checkData && (checkData as any).latest_record && <Schedule type='latest' />
  270. }
  271. {/* <Picker mode="time" start="07:00" end="20:00">
  272. <Text style="color: #fff;">test picker</Text>
  273. </Picker> */}
  274. <View style={{ height: 100 }} />
  275. {/* <View className='popDemo' catchMove/> */}
  276. </View>
  277. )
  278. }