Clock.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { View } from "@tarojs/components";
  2. import './Clock.scss'
  3. import ClockNew from "./ClockNew";
  4. import { useEffect, useState } from "react";
  5. import Taro, { useShareAppMessage } from "@tarojs/taro";
  6. import { useDispatch } from "react-redux";
  7. import { getInfoSuccess } from "@/store/user";
  8. import { useTranslation } from "react-i18next";
  9. let useNavigation;
  10. if (process.env.TARO_ENV == 'rn') {
  11. useNavigation = require("@react-navigation/native").useNavigation
  12. }
  13. export default function Clock() {
  14. const dispatch = useDispatch();
  15. const [loaded,setLoaded] = useState(false)
  16. const { t } = useTranslation()
  17. let navigation;
  18. if (useNavigation) {
  19. navigation = useNavigation()
  20. }
  21. if (process.env.TARO_ENV == 'weapp') {
  22. useShareAppMessage((e) => {
  23. return {
  24. title: t('feature.track_time_duration.common.share_title'),
  25. path: 'pages/clock/Clock'
  26. }
  27. })
  28. }
  29. useEffect(() => {
  30. if (navigation) {
  31. navigation.setOptions({
  32. headerTitle: '',
  33. });
  34. }
  35. global.memberAlert = false;
  36. if (process.env.TARO_ENV == 'weapp') {
  37. loadWXCache()
  38. }
  39. else {
  40. loadRNCache()
  41. }
  42. }, [])
  43. function loadWXCache() {
  44. var gps = Taro.getStorageSync('gps')
  45. if (gps) {
  46. global.locationDetail = JSON.parse(gps)
  47. }
  48. global.memberAlert = Taro.getStorageSync('memberAlert') || false
  49. var userData = Taro.getStorageSync('userData')
  50. if (userData) {
  51. dispatch(getInfoSuccess(JSON.parse(userData)));
  52. }
  53. setLoaded(true)
  54. }
  55. async function loadRNCache() {
  56. var showDayRing = await getStorage('showDayRing') || false;
  57. var showNightRing = await getStorage('showNightRing') || false;
  58. global.memberAlert = await getStorage('memberAlert') || false
  59. var gps = await getStorage('gps')
  60. if (gps) {
  61. global.locationDetail = JSON.parse(gps)
  62. }
  63. var userData = await getStorage('userData')
  64. console.log(userData)
  65. if (userData) {
  66. dispatch(getInfoSuccess(JSON.parse(userData)));
  67. }
  68. setLoaded(true)
  69. }
  70. async function getStorage(key: string) {
  71. try {
  72. const res = await Taro.getStorage({ key });
  73. return res.data;
  74. } catch {
  75. return '';
  76. }
  77. }
  78. if (!loaded)
  79. return <View />
  80. return <View style={{flex:1}}>
  81. <ClockNew />
  82. </View>
  83. }