Clock.weapp.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. import { Component, PropsWithChildren, useEffect, useRef, useState } from 'react'
  2. import { View, Text, Button, Input, Picker, Swiper, SwiperItem, Icon, PageContainer } from '@tarojs/components'
  3. import Tabbar from "@/components/navigation/TabBar";
  4. import '../index/index.scss'
  5. import './Clock.scss'
  6. // import './context/locales/index'
  7. import '@/context/locales/index'
  8. import Taro, { useDidShow, usePageScroll, useReady, useRouter, useShareAppMessage } from '@tarojs/taro'
  9. // import ComponentA from './component'
  10. import { useDispatch, useSelector } from 'react-redux';
  11. import { getInfoSuccess, logoutSuccess } from '@/store/user';
  12. import { wxPubFollow } from '@/services/permission';
  13. import { gobalConfigs, staticResources, uploadSessionKey } from '@/services/common';
  14. import Clocks from '@/features/trackTimeDuration/components/Clock';
  15. import Console from '@/features/trackTimeDuration/components/Console';
  16. import Schedule from '@/features/trackTimeDuration/components/Schedule';
  17. import { getChecks, getClocks } from '@/services/trackTimeDuration';
  18. import { setScenario } from '@/store/scenario';
  19. import { setSpecifiedStatus, setSpecifiedState, machine } from '@/store/trackTimeMachine';
  20. import trackTimeService from '@/store/trackTimeMachine';
  21. import { setWXFollow } from '@/store/permission';
  22. import Tooltip from '@/components/view/Tooltip'
  23. import RequestType, { thirdPartRequest } from '@/services/thirdPartRequest'
  24. import { setConfigs, setTabbarStatus } from '@/store/common'
  25. import RecordFastSleep from '@/features/trackTimeDuration/components/RecordFastSleep'
  26. import Box from '@/components/layout/Box'
  27. import Layout from '@/components/layout/layout'
  28. import { CheckBoxType, NaviBarTitleShowType, TemplateType } from '@/utils/types'
  29. import { updateScenario } from '@/store/time'
  30. import { showModal } from '@/store/modal'
  31. import { ConsoleType, changeConsoleStatus } from '@/store/console'
  32. import TitleView from '@/features/trackTimeDuration/components/TitleView'
  33. import StatusIndicator from '@/features/trackTimeDuration/components/StatusIndicator'
  34. import { useTranslation } from 'react-i18next'
  35. import TableCellHeader from '@/components/layout/TableCellHeader'
  36. import SectionHeader from '@/components/layout/SectionHeader'
  37. import Header from '@/components/layout/Header'
  38. import { TimeFormatter } from '@/utils/time_format'
  39. import NoData from '@/components/view/NoData'
  40. import { ColorType } from '@/context/themes/color'
  41. import { jumpPage } from '@/features/trackTimeDuration/hooks/Common'
  42. import { ChooseScenarioBtn } from '@/features/common/SpecBtns'
  43. import { rpxToPx } from '@/utils/tools'
  44. import Modal from '@/components/layout/Modal'
  45. // import TabBar from '../../components/Tabbar';
  46. // import Rings from '@components/view/Rings';
  47. let useNavigation;
  48. if (process.env.TARO_ENV == 'rn') {
  49. useNavigation = require("@react-navigation/native").useNavigation
  50. }
  51. export default function IndexPage() {
  52. const dispatch = useDispatch();
  53. const { t } = useTranslation()
  54. const [checkData, setCheckData] = useState(null)
  55. const user = useSelector((state: any) => state.user);
  56. const time = useSelector((state: any) => state.time);
  57. const common = useSelector((state: any) => state.common);
  58. const consoleData = useSelector((state: any) => state.console);
  59. const [counter, setCounter] = useState(0)
  60. const [timerId, setTimerId] = useState(null)
  61. const [needShowAddTip, setNeedShowAddTip] = useState(false)
  62. const [showErrorPage, setErrorPage] = useState(false)
  63. const [swiperIndex, setSwiperIndex] = useState(0)
  64. const [autoPlay, setAutoPlay] = useState(false)
  65. const [showModal, setShowModal] = useState(false)
  66. const [modalDetail, setModalDetail] = useState<any>(null)
  67. const [showModal2, setShowModal2] = useState(false)
  68. const [modalDetail2, setModalDetail2] = useState<any>(null)
  69. const [showSingleFastEnd, setShowSingleFastEnd] = useState(false)
  70. const [consoleStatus, setConsoleStatus] = useState(consoleData.status)
  71. const [showLogin, setShowLogin] = useState(false)
  72. const [showTip, setShowTip] = useState(false)
  73. const [isModal1, setIsModal1] = useState(false)
  74. const [debugInfo, setDebugInfo] = useState(null)
  75. let navigation;
  76. if (useNavigation) {
  77. navigation = useNavigation()
  78. }
  79. global.dispatch = dispatch;
  80. global.checkData = () => {
  81. getCheckData()
  82. }
  83. useEffect(() => {
  84. global.consoleType = 'idle'
  85. dispatch(staticResources() as any);
  86. // dispatch(gobalConfigs() as any);
  87. trackTimeService.onTransition(state => {
  88. if ((state.value as any).FAST_SLEEP == 'COMPLETED' ||
  89. (state.value as any).FAST == 'ONGOING' ||
  90. (state.value as any).SLEEP == 'ONGOING' ||
  91. (state.value as any).FAST_SLEEP == 'ONGOING1' ||
  92. (state.value as any).FAST_SLEEP == 'ONGOING2' ||
  93. (state.value as any).FAST_SLEEP == 'ONGOING3' ||
  94. (state.value as any).FAST == 'COMPLETED' ||
  95. (state.value as any).SLEEP == 'COMPLETED') {
  96. getCheckData()
  97. }
  98. })
  99. }, [])
  100. useEffect(() => {
  101. setConsoleStatus(consoleData.status)
  102. switch (consoleData.status) {
  103. // case ConsoleType.going:
  104. // setShowSingleFastEnd(true)
  105. // setTimeout(() => {
  106. // setShowSingleFastEnd(false)
  107. // setSwiperIndex(1)
  108. // setAutoPlay(true)
  109. // dispatch(changeConsoleStatus({ status: ConsoleType.end }))
  110. // }, 2000)
  111. // break
  112. case ConsoleType.end:
  113. setTimeout(() => {
  114. dispatch(changeConsoleStatus({ status: ConsoleType.idle }))
  115. }, 2000)
  116. break
  117. }
  118. }, [consoleData.status])
  119. useEffect(() => {
  120. if (user.isLogin) {
  121. //检查用户是否添加过小程序
  122. checkAddToMini();
  123. //检查session是否过期
  124. checkSession()
  125. getCheckData()
  126. }
  127. }, [user.isLogin])
  128. function checkSession() {
  129. if (process.env.TARO_ENV === 'weapp') {
  130. thirdPartRequest(RequestType.RequestTypeCheckSession).then(res => {
  131. }).catch(err => {
  132. console.log('session_key 已经失效,需要更新登录code')
  133. thirdPartRequest(RequestType.RequestTypeWXLogin).then(result => {
  134. uploadSessionKey({ type: 'WX_MP', code: (result as any).code });
  135. })
  136. })
  137. }
  138. }
  139. function checkAddToMini() {
  140. process.env.TARO_ENV == 'weapp' &&
  141. wx.checkIsAddedToMyMiniProgram({
  142. success: (res) => {
  143. if (!res.added) {
  144. setNeedShowAddTip(true)
  145. }
  146. },
  147. fail: (e) => {
  148. }
  149. });
  150. }
  151. useEffect(() => {
  152. startTimer();
  153. return () => {
  154. // 在组件卸载时清除定时器
  155. if (timerId) {
  156. clearInterval(timerId);
  157. }
  158. };
  159. }, []);
  160. const startTimer = () => {
  161. // 避免重复启动定时器
  162. if (timerId) {
  163. return;
  164. }
  165. const id = setInterval(() => {
  166. setShowLogin(true)
  167. if (global.showModal) {
  168. return
  169. }
  170. setCounter((prevCounter) => prevCounter + 1);
  171. //每天0点刷新一下打卡数据
  172. var now = new Date()
  173. if (now.getHours() == 0 && now.getMinutes() == 0) {
  174. getCheckData()
  175. }
  176. }, 1000);
  177. setTimerId(id as any);
  178. };
  179. useReady(async () => {
  180. const userData = await getStorage('userData');
  181. if (userData) {
  182. dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
  183. setTimeout(() => {
  184. checkWXPubFollow()
  185. getCheckData()
  186. }, 200)
  187. }
  188. })
  189. if (process.env.TARO_ENV == 'weapp') {
  190. useShareAppMessage((e) => {
  191. return {
  192. title: t('share.title'),
  193. path: 'pages/clock/Clock'
  194. }
  195. })
  196. }
  197. function clearTempScenarioCache() {
  198. global.schedule_fast = null
  199. global.schedule_sleep = null
  200. Taro.removeStorage({
  201. key: 'tempScenario',
  202. success: function (res) {
  203. }
  204. })
  205. }
  206. usePageScroll((e) => {
  207. })
  208. useDidShow(() => {
  209. // global.updateTab(0)
  210. if (user.isLogin) {
  211. checkWXPubFollow()
  212. // getCheckData();
  213. }
  214. clearTempScenarioCache()
  215. })
  216. global.refreshTime = () => {
  217. getCheckData()
  218. }
  219. global.showSingleFastEnd = () => {
  220. setShowSingleFastEnd(true)
  221. }
  222. function tapClock(e) {
  223. // if (global.showModal) {
  224. // return
  225. // }
  226. if (process.env.TARO_ENV == 'weapp') {
  227. e.stopPropagation()
  228. }
  229. if (!user.isLogin) {
  230. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  231. }
  232. else {
  233. Taro.vibrateShort({
  234. type: 'medium'
  235. })
  236. }
  237. }
  238. function tapLogin(e) {
  239. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  240. }
  241. function getCheckData() {
  242. getClocks().then(res => {
  243. setErrorPage(false)
  244. dispatch(updateScenario((res as any).current_record))
  245. dispatch(setConfigs((res as any).time_input_schema));
  246. dispatch(setScenario((res as any).scenario));
  247. if ((res as any).theme_color) {
  248. global.fastColor = (res as any).theme_color.fast
  249. global.sleepColor = (res as any).theme_color.sleep
  250. }
  251. machine.context.checkData = res as any;
  252. global.scenario = (res as any).current_record.scenario;
  253. const currentState = trackTimeService.getSnapshot();
  254. let json = {};
  255. var key = (res as any).current_record.scenario
  256. var status = (res as any).current_record.status
  257. json[key] = status
  258. currentState.value = json;
  259. machine.context.currentStatus = `${key}.${status}`;//'mixed.ON_GOING2'
  260. setCheckData(res as any)
  261. if ((res as any).current_record.status != 'ONGOING1') {
  262. setSwiperIndex(0)
  263. }
  264. else {
  265. if (global.consoleType == 'going') {
  266. setShowSingleFastEnd(true)
  267. setTimeout(() => {
  268. setShowSingleFastEnd(false)
  269. setSwiperIndex(1)
  270. setAutoPlay(true)
  271. global.consoleType = 'end'
  272. dispatch(changeConsoleStatus({ status: ConsoleType.end }))
  273. setTimeout(() => {
  274. global.consoleType = 'idle'
  275. }, 2000)
  276. }, 500)
  277. }
  278. else {
  279. }
  280. }
  281. }).catch(e => {
  282. if (!checkData) {
  283. setErrorPage(true)
  284. }
  285. else {
  286. }
  287. })
  288. }
  289. function checkWXPubFollow() {
  290. var params;
  291. if (global.forceRefreshWXPub) {
  292. params = {
  293. force_refresh: true
  294. }
  295. }
  296. global.forceRefreshWXPub = false
  297. wxPubFollow(params).then(res => {
  298. dispatch(setWXFollow((res as any).wx_pub_followed));
  299. console.log('当前的关注状态' + (res as any).wx_pub_followed);
  300. })
  301. }
  302. async function getStorage(key: string) {
  303. try {
  304. const res = await Taro.getStorage({ key });
  305. return res.data;
  306. } catch {
  307. return '';
  308. }
  309. }
  310. function schedule() {
  311. if (!user.isLogin) {
  312. return <View />
  313. }
  314. return <Schedule data={(checkData as any).current_record} />
  315. }
  316. function needSwiper() {
  317. var isNeed = time.scenario == 'FAST_SLEEP' &&
  318. (time.status == 'WAIT_FOR_START' ||
  319. time.status == 'ONGOING1' ||
  320. time.status == 'ONGOING2')
  321. if (showSingleFastEnd) {
  322. return false;
  323. }
  324. return isNeed
  325. }
  326. global.showClockModal = (isShow: boolean, detail: any, debugNode?: any) => {
  327. global.showModal = isShow
  328. setIsModal1(true)
  329. setDebugInfo(debugNode)
  330. setShowModal(isShow)
  331. setModalDetail(detail)
  332. }
  333. global.showClockModal2 = (isShow: boolean, detail: any) => {
  334. setDebugInfo(null)
  335. global.showModal = isShow
  336. setIsModal1(false)
  337. setShowModal2(isShow)
  338. setModalDetail2(detail)
  339. }
  340. function headerView() {
  341. return <TitleView title={t('page.clock.title')} showAddBtn={true}>
  342. <StatusIndicator />
  343. </TitleView>
  344. }
  345. function errorView() {
  346. if (showErrorPage) {
  347. return <NoData refresh={() => { getCheckData() }} />
  348. }
  349. return <View />
  350. }
  351. function consoleView() {
  352. if (!checkData) {
  353. return <View />
  354. }
  355. return <View className='console_bg'>
  356. {
  357. needSwiper() ? <Swiper className='swiper' indicatorColor='#333'
  358. indicatorActiveColor='#999'
  359. current={swiperIndex}
  360. autoplay={autoPlay}
  361. duration={300}
  362. interval={300}
  363. // indicator-margin={30}
  364. indicator-offset={[0, -30]}
  365. indicator-height={30}
  366. indicatorDots={global.consoleType == 'idle'}
  367. onChange={(e) => {
  368. setSwiperIndex(e.detail.current)
  369. if (e.detail.current == 0)
  370. setAutoPlay(false)
  371. }}
  372. >
  373. <SwiperItem className='swiperItem'>
  374. <Console />
  375. </SwiperItem>
  376. <SwiperItem className='swiperItem'>
  377. <Console isNextStep={true} />
  378. </SwiperItem>
  379. </Swiper> :
  380. <Console isNextStep={showSingleFastEnd} />
  381. }
  382. </View>
  383. }
  384. usePageScroll((e) => {
  385. if (e.scrollTop > 70) {
  386. setShowTip(true)
  387. }
  388. else {
  389. setShowTip(false)
  390. }
  391. })
  392. function detail() {
  393. return (
  394. <Layout type={TemplateType.customHeader} header={headerView()} title={t('page.clock.title')} titleShowStyle={NaviBarTitleShowType.scrollToShow}>
  395. <View style={{ flex: 1, flexDirection: 'column', display: 'flex', backgroundColor: '#000', color: '#fff' }}>
  396. {
  397. needShowAddTip && showTip && <Tooltip title="添加到我的小程序" closeTip={() => { setNeedShowAddTip(false) }} />
  398. }
  399. {/* <View style={{ width: '100%', height: 400, backgroundColor: 'gray' }}></View> */}
  400. <Box>
  401. <View className='clock_bg' onClick={tapClock}>
  402. <Clocks showCoverView={!global.showModal} />
  403. </View>
  404. </Box>
  405. {
  406. !user.isLogin && showLogin && <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', marginTop: rpxToPx(36) }}>
  407. <ChooseScenarioBtn title='登录体验' background={ColorType.fast} onClick={tapLogin} />
  408. </View>
  409. }
  410. {/* <Box>
  411. <View className='clock_bg' onClick={tapClock}>
  412. <Clocks />
  413. </View>
  414. </Box> */}
  415. {
  416. errorView()
  417. }
  418. {
  419. user.isLogin && consoleView()
  420. }
  421. {
  422. user.isLogin && checkData && schedule()
  423. }
  424. {
  425. user.isLogin && <View style={{ backgroundColor: global.isDebug ? 'pink' : 'transparent' }}>
  426. {
  427. checkData && (checkData as any).latest_record &&
  428. <SectionHeader top={48} bottom={24} title={t('feature.track_time_duration.record_fast_sleep.header.latest_record')}>
  429. <Header title='' action={() => {
  430. jumpPage('/pages/common/RecordsHistory?type=time&title=time')
  431. }} />
  432. </SectionHeader>
  433. }
  434. </View>
  435. }
  436. {
  437. user.isLogin && checkData && (checkData as any).latest_record &&
  438. <RecordFastSleep type='latest' data={(checkData as any).latest_record} delSuccess={getCheckData} />
  439. }
  440. <View style={{ height: 100 }} />
  441. </View>
  442. {/* <PageContainer style={{ backgroundColor: '#1c1c1c' }}
  443. overlayStyle='background-color:rgba(0,0,0,0.9)'
  444. custom-style='background-color:#1c1c1c'
  445. closeOnSlideDown={false}
  446. onBeforeEnter={() => {
  447. dispatch(setTabbarStatus(false));
  448. Taro.hideTabBar();
  449. }}
  450. onBeforeLeave={() => {
  451. dispatch(setTabbarStatus(true));
  452. Taro.showTabBar();
  453. }}
  454. onAfterLeave={() => { setShowModal(false); setShowModal2(false) }}
  455. show={showModal || showModal2} round={true} overlay={true} position='bottom'
  456. >
  457. {
  458. isModal1 ? modalDetail : modalDetail2
  459. }
  460. </PageContainer> */}
  461. {
  462. (showModal || showModal2) && <Modal
  463. testInfo={debugInfo}
  464. dismiss={() => {
  465. setDebugInfo(null)
  466. setShowModal(false); setShowModal2(false)
  467. }}
  468. confirm={() => { }}>
  469. {
  470. isModal1 ? modalDetail : modalDetail2
  471. }
  472. </Modal>
  473. }
  474. {/* {
  475. showModal && modalDetail
  476. }
  477. {
  478. showModal2 && modalDetail2
  479. } */}
  480. </Layout>
  481. )
  482. }
  483. if (process.env.TARO_ENV == 'rn')
  484. return <View />
  485. return <View>
  486. {detail()}
  487. <Tabbar index={0} />
  488. </View>
  489. }