clock.tsx 10 KB

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