Clock.tsx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. import { View, Text, Image, ScrollView, PageContainer, Swiper, SwiperItem, Switch } from "@tarojs/components";
  2. import Tabbar from "@/components/navigation/TabBar";
  3. import IndexItem from '@/features/trackTimeDuration/components/IndexItem';
  4. import Rings from "@/features/trackTimeDuration/components/Rings";
  5. import './Clock.scss'
  6. import { useDispatch, useSelector } from "react-redux";
  7. import { useDidHide, useDidShow, usePageScroll, useReady, useShareAppMessage } from "@tarojs/taro";
  8. import Taro from "@tarojs/taro";
  9. import { getInfoSuccess } from "@/store/user";
  10. import { clockHome, clockSummaryRecords, clockSummaryStats, getClockRecords, getClocks, getPlans } from "@/services/trackTimeDuration";
  11. import { updateScenario } from "@/store/time";
  12. import { setConfigs } from "@/store/common";
  13. import { setScenario, setStep } from "@/store/scenario";
  14. import { useEffect, useRef, useState } from "react";
  15. import { IconPlus, IconRadioCheck, IconRadioCross } from "@/components/basic/Icons";
  16. import { ColorType } from "@/context/themes/color";
  17. import { bigRingRadius, getBgRing, getCommon, getDot, getSchedule, ringWidth, smallRingRadius } from "@/features/trackTimeDuration/hooks/RingData";
  18. import { RealRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  19. import IndexConsole from "@/features/trackTimeDuration/components/IndexConsole";
  20. import Modal from '@/components/layout/Modal'
  21. import { compareVersion, getTimezone, getTimezoneName, rpxToPx } from "@/utils/tools";
  22. import RecordFastSleep from "@/features/trackTimeDuration/components/RecordFastSleep";
  23. import DayLight from "@/features/trackTimeDuration/components/DayLight";
  24. import { getInfo, latestLocation } from "@/services/user";
  25. import { TimeFormatter } from "@/utils/time_format";
  26. import WeekCalendar from "@/features/trackTimeDuration/components/WeekCalendar";
  27. import { useTranslation } from "react-i18next";
  28. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  29. import Layout from "@/components/layout/layout";
  30. import { ModalType, NaviBarTitleShowType, TemplateType } from "@/utils/types";
  31. import TitleView from "@/features/trackTimeDuration/components/TitleView";
  32. import ClockHeader from "@/features/trackTimeDuration/components/ClockHeader";
  33. import DurationPicker from "@/features/trackTimeDuration/components/DurationPicker";
  34. import SegmentPop from "@/features/trackTimeDuration/components/SegmentPop";
  35. import Box from "@/components/layout/Box";
  36. import DayNightCard from "@/features/daynight/DayNightCard";
  37. import StageSelector from "@/features/trackTimeDuration/components/StageSelector";
  38. import { ChooseScenarioBtn } from "@/features/common/SpecBtns";
  39. import { clearNightStore, showNight } from "@/store/night";
  40. import { showDay } from "@/store/day";
  41. import { clientInfo, staticResources, systemVersion } from "@/services/common";
  42. import { changeFastDuration, changeSleepDuration, setCurrentRecord, setSchedule } from "@/store/ring";
  43. import { checkAuthorized } from "@/utils/check_authorized";
  44. import NoData from "@/components/view/NoData";
  45. import { AtActivityIndicator } from "taro-ui";
  46. import Tooltip from "@/components/view/Tooltip";
  47. import AllRings from "@/features/daynight/AllRings";
  48. import Streaks from "@/features/trackTimeDuration/components/Streaks";
  49. import dayjs from 'dayjs'
  50. import DayNightSwiper from "@/features/daynight/DayNightSwiper";
  51. import showAlert from "@/components/basic/Alert";
  52. import { APP_VERSION, WX_VERSION } from "@/services/http/api";
  53. import { title } from "process";
  54. const utc = require('dayjs/plugin/utc')
  55. const timezone = require('dayjs/plugin/timezone')
  56. dayjs.extend(utc)
  57. dayjs.extend(timezone)
  58. let GradientText
  59. let useNavigation;
  60. let timer
  61. let pauseTimer = false;
  62. let AppState;
  63. let needScroll = false;
  64. let showUpdate = false;
  65. let Linking;
  66. let JPush;
  67. let checkNotification, uploadPermissions, NativeAppEventEmitter;
  68. if (process.env.TARO_ENV == 'rn') {
  69. JPush = require('jpush-react-native').default;
  70. Linking = require('react-native').Linking;
  71. AppState = require("react-native").AppState
  72. NativeAppEventEmitter = require("react-native").NativeAppEventEmitter
  73. GradientText = require('@/components/basic/GradientText').default
  74. useNavigation = require("@react-navigation/native").useNavigation
  75. checkNotification = require('@/utils/native_permission_check').checkNotification;
  76. uploadPermissions = require('@/utils/native_permission_check').uploadPermissions;
  77. }
  78. const defaultValue = `[{"scenario":{"name":"FAST_SLEEP","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"},"sleep":{"start_time":"22:00","end_time":"07:00"}}},"current_record":{"scenario":"FAST_SLEEP","status":"WAIT_FOR_START","fast":{"status":"WAIT_FOR_START","target_start_time":1710514800000,"target_end_time":1710576000000},"sleep":{"status":"WAIT_FOR_START","target_start_time":1710540000000,"target_end_time":1710572400000}},"wx_pub_followed":false,"time_input_schema":{"min":60,"max":1380,"step":5},"theme_color":{"fast":"#00FFFF","sleep":"#8183FF"}},{"scenarios":[{"name":"FAST","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"}}},{"name":"SLEEP","count":0,"schedule":{"sleep":{"start_time":"22:00","end_time":"07:00"}}},{"name":"FAST_SLEEP","count":0,"schedule":{"fast":{"start_time":"15:00","end_time":"08:00"},"sleep":{"start_time":"22:00","end_time":"07:00"}}}],"select_count":0,"theme_color":{"fast":"#00FFFF","sleep":"#8183FF"}}]`
  79. export default function Page() {
  80. const dispatch = useDispatch();
  81. global.dispatch = dispatch;
  82. let navigation;
  83. if (useNavigation) {
  84. navigation = useNavigation()
  85. }
  86. const { t } = useTranslation()
  87. const user = useSelector((state: any) => state.user);
  88. const time = useSelector((state: any) => state.time);
  89. const [count, setCount] = useState(0)
  90. const [homeData, setHomeData] = useState(null)
  91. const [loaded, setLoaded] = useState(false)
  92. const [showErrorPage, setErrorPage] = useState(false)
  93. const [showModal, setShowModal] = useState(false)
  94. const [modalDetail, setModalDetail] = useState<any>(null)
  95. const [showModal2, setShowModal2] = useState(false)
  96. const [modalDetail2, setModalDetail2] = useState<any>(null)
  97. const [debugInfo, setDebugInfo] = useState(null)
  98. const [needShowAddTip, setNeedShowAddTip] = useState(false)
  99. const [showTip, setShowTip] = useState(false)
  100. const [records, setRecords] = useState([])
  101. const permission = useSelector((state: any) => state.permission);
  102. const common = useSelector((state: any) => state.common);
  103. const scrollRef = useRef(null);
  104. const nightStore = useSelector((state: any) => state.night);
  105. if (process.env.TARO_ENV == 'weapp') {
  106. useShareAppMessage((e) => {
  107. return {
  108. title: t('feature.track_time_duration.common.share_title'),
  109. path: 'pages/clock/Clock'
  110. }
  111. })
  112. }
  113. useEffect(() => {
  114. // var moment = require('moment-timezone');
  115. // var aa = moment(new Date().getTime()).tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss')
  116. // showAlert({
  117. // title:'sss',
  118. // content:aa
  119. // })
  120. dispatch(staticResources() as any);
  121. timer = setInterval(() => {
  122. var now = new Date()
  123. if (now.getDay() == 0 && now.getHours() == 12 && now.getMinutes() == 0 && now.getSeconds() == 0) {
  124. if (global.refrehWeekly) {
  125. global.refrehWeekly()
  126. }
  127. }
  128. if (global.pauseIndexTimer || pauseTimer) {
  129. return
  130. }
  131. setCount((prevCounter) => prevCounter + 1)
  132. }, 1000)
  133. }, [])
  134. useEffect(() => {
  135. // Taro.request({
  136. // url:'https://api.fast.dev.liveplus.fun/api/static-resource-urls',
  137. // method:'GET',
  138. // data:{},
  139. // timeout:20000,
  140. // success(result) {
  141. // showAlert({
  142. // title:'success',
  143. // content:JSON.stringify(result)
  144. // })
  145. // },
  146. // fail(res) {
  147. // showAlert({
  148. // title:'fail',
  149. // content:JSON.stringify(res)
  150. // })
  151. // },
  152. // })
  153. if (process.env.TARO_ENV == 'weapp') {
  154. loadWXCache()
  155. }
  156. else {
  157. loadRNCache()
  158. if (Taro.getSystemInfoSync().platform != 'ios') {
  159. const test = require('@/utils/push').default
  160. test()
  161. }
  162. }
  163. if (process.env.TARO_ENV == 'rn') {
  164. JPush.setBadge({
  165. badge: 0,
  166. appBadge: 0
  167. })
  168. JPush.isNotificationEnabled((res) => {
  169. if (res) {
  170. const test = require('@/utils/push').default
  171. test()
  172. }
  173. })
  174. AppState.addEventListener('change', handleAppStateChange);
  175. /*
  176. if (Taro.getSystemInfoSync().platform == 'ios') {
  177. setTimeout(() => {
  178. var Jto = require('react-native').NativeModules.NativeBridge;
  179. // Jto.authNotification()
  180. Jto.getNotificationAuthStatus()
  181. }, 5000)
  182. NativeAppEventEmitter.addListener('notificationResult', (data) => {
  183. console.log(data)
  184. showAlert({
  185. title:'notification status',
  186. content:data
  187. })
  188. })
  189. }*/
  190. }
  191. }, [])
  192. useEffect(() => {
  193. getCheckData()
  194. // global.pauseIndexTimer = !user.isLogin
  195. if (user.isLogin) {
  196. uploadUserClient();
  197. checkAuthorized();
  198. //检查用户是否添加过小程序
  199. checkAddToMini();
  200. if (process.env.TARO_ENV == 'rn') {
  201. if (Taro.getSystemInfoSync().platform == 'ios') {
  202. var Jto = require('react-native').NativeModules.NativeBridge;
  203. Jto.getNotificationAuthStatus()
  204. NativeAppEventEmitter.addListener('notificationResult', (data) => {
  205. global.notification = data;
  206. uploadPermissions()
  207. })
  208. }
  209. // uploadPermissions()
  210. JPush.isNotificationEnabled((res) => {
  211. if (res) {
  212. const test = require('@/utils/push').default
  213. test()
  214. }
  215. })
  216. checkVersionUpdate()
  217. }
  218. }
  219. else {
  220. dispatch(clearNightStore());
  221. }
  222. }, [user.isLogin])
  223. useReady(async () => {
  224. const userData = await getStorage('userData');
  225. if (userData) {
  226. dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
  227. getHistory()
  228. }
  229. })
  230. async function checkVersionUpdate() {
  231. if (!user.isLogin) {
  232. return
  233. }
  234. if (process.env.TARO_ENV == 'rn') {
  235. systemVersion().then(res => {
  236. if ((res as any).result == 'UPDATE' && !showUpdate) {
  237. showUpdate = true
  238. showAlert({
  239. title: (res as any).title,
  240. content: (res as any).description,
  241. showCancel: true,
  242. confirmText: '更新',
  243. confirm: () => {
  244. Linking.openURL((res as any).download_url)
  245. }
  246. })
  247. }
  248. else if ((res as any).result == 'FORCE_UPDATE') {
  249. showAlert({
  250. title: (res as any).title,
  251. content: (res as any).description,
  252. showCancel: false,
  253. confirmText: '更新',
  254. confirm: () => {
  255. Linking.openURL((res as any).download_url)
  256. }
  257. })
  258. }
  259. })
  260. return;
  261. }
  262. const showAlert1 = await getStorage('120alert') || false;
  263. if (!showAlert1) {
  264. showAlert({
  265. title: '版本更新提示',
  266. content: '「连续纪录」新增归零倒计时提示⏳\n别让连续记录终止, 将它们一直保持下去!',
  267. showCancel: false,
  268. confirmText: '我知道了'
  269. })
  270. }
  271. Taro.setStorage({ key: '120alert', data: true })
  272. }
  273. function checkAddToMini() {
  274. if (process.env.TARO_ENV == 'weapp') {
  275. const version = Taro.getAppBaseInfo().SDKVersion
  276. if (compareVersion(version, '2.30.3') >= 0) {
  277. wx.checkIsAddedToMyMiniProgram({
  278. success: (res) => {
  279. if (!res.added) {
  280. setNeedShowAddTip(true)
  281. }
  282. },
  283. fail: (e) => {
  284. }
  285. });
  286. }
  287. }
  288. }
  289. useDidShow(() => {
  290. checkTimeZone()
  291. setCount(pre => pre + 1)
  292. //resume timer
  293. pauseTimer = false
  294. if (user.isLogin) {
  295. if (global.refreshRecent) {
  296. global.refreshRecent()
  297. }
  298. }
  299. setTimeout(() => {
  300. checkVersionUpdate();
  301. updateNotificationStatus();
  302. }, 1000)
  303. })
  304. usePageScroll((e) => {
  305. if (e.scrollTop > 70) {
  306. setShowTip(true)
  307. }
  308. else {
  309. setShowTip(false)
  310. }
  311. })
  312. useDidHide(() => {
  313. //pause timer
  314. pauseTimer = true
  315. })
  316. global.refreshIndex = () => {
  317. setCount((prevCounter) => prevCounter + 1)
  318. }
  319. const handleAppStateChange = (nextAppState) => {
  320. console.log(nextAppState)
  321. if (nextAppState != 'active') {
  322. return
  323. }
  324. checkTimeZone()
  325. updateNotificationStatus()
  326. };
  327. function updateNotificationStatus() {
  328. if (process.env.TARO_ENV == 'rn') {
  329. if (user.isLogin) {
  330. if (Taro.getSystemInfoSync().platform == 'ios') {
  331. var Jto = require('react-native').NativeModules.NativeBridge;
  332. Jto.getNotificationAuthStatus()
  333. }
  334. }
  335. JPush.setBadge({
  336. badge: 0,
  337. appBadge: 0
  338. })
  339. }
  340. }
  341. function checkTimeZone() {
  342. setCount((prevCounter) => prevCounter + 1)
  343. var timeZoneFormatted = getTimezone()
  344. Taro.getStorage({
  345. key: 'last_tz',
  346. success: function (res) {
  347. if (res.data && res.data != timeZoneFormatted) {
  348. if (global.refrehWeekly) {
  349. global.refrehWeekly()
  350. }
  351. // if (global.refreshNight){
  352. // global.refreshNight()
  353. // }
  354. // if (global.refreshDay){
  355. // global.refreshDay()
  356. // }
  357. if (global.currentStatus != 'WAIT_FOR_START') {
  358. showAlert({
  359. title: t('feature.track_time_duration.change_tz_alert.title'),
  360. content: t('feature.track_time_duration.change_tz_alert.content', { tz: timeZoneFormatted }),
  361. showCancel: false,
  362. confirmText: t('feature.track_time_duration.change_tz_alert.confirm'),
  363. })
  364. }
  365. }
  366. },
  367. complete: function () {
  368. Taro.setStorage({ key: 'last_tz', data: timeZoneFormatted })
  369. }
  370. })
  371. }
  372. function uploadUserClient() {
  373. var systemInfo = Taro.getSystemInfoSync();
  374. console.log(systemInfo)
  375. var language: any = systemInfo.language
  376. if (process.env.TARO_ENV == 'rn') {
  377. var NativeModules = require('react-native').NativeModules;
  378. if (Taro.getSystemInfoSync().platform == 'ios') {
  379. // language = NativeModules.SettingsManager.settings.AppleLocale
  380. // languageList = NativeModules.SettingsManager.settings.AppleLanguages
  381. language = {
  382. AppleLocale: NativeModules.SettingsManager.settings.AppleLocale,
  383. AppleLanguages: NativeModules.SettingsManager.settings.AppleLanguages,
  384. NSLanguages: NativeModules.SettingsManager.settings.NSLanguages
  385. }
  386. }
  387. else {
  388. language = NativeModules.I18nManager.localeIdentifier
  389. }
  390. // iOS:
  391. // var locale = NativeModules.SettingsManager.settings.AppleLocale ||
  392. // NativeModules.SettingsManager.settings.AppleLanguages[0] // "fr_FR"
  393. // showAlert({
  394. // title: 'locale',
  395. // content: JSON.stringify(NativeModules.SettingsManager.settings)
  396. // })
  397. // Android:
  398. // locale = NativeModules.I18nManager.localeIdentifier // "fr_FR"
  399. }
  400. var timeZoneFormatted = getTimezone()
  401. var timeZoneId = ''
  402. var timeZoneName = ''
  403. if (Taro.getSystemInfoSync().platform == 'ios') {
  404. timeZoneId = dayjs.tz.guess()
  405. timeZoneName = getTimezoneName()
  406. }
  407. clientInfo({
  408. client: {
  409. client_type: process.env.TARO_ENV == 'weapp' ? 'MP' : 'APP',
  410. client_version: process.env.TARO_ENV == 'weapp' ? WX_VERSION : APP_VERSION,//Taro.getAccountInfoSync().miniProgram.version : '1.0',//'1.0'
  411. wx_version: process.env.TARO_ENV == 'weapp' ? systemInfo.version : '_'
  412. },
  413. meta: {
  414. language: language,
  415. timezone: {
  416. gmt: timeZoneFormatted,
  417. id: timeZoneId,
  418. name:timeZoneName
  419. },
  420. },
  421. device: {
  422. brand: systemInfo.brand,
  423. model: systemInfo.model,
  424. platform: systemInfo.platform,
  425. system: systemInfo.system
  426. },
  427. perm: {
  428. wifi_enabled: systemInfo.wifiEnabled,
  429. location_authorized: systemInfo.locationAuthorized,
  430. location_enabled: systemInfo.locationEnabled
  431. }
  432. })
  433. }
  434. function loadWXCache() {
  435. var showDayRing = Taro.getStorageSync('showDayRing') || false;
  436. var showNightRing = Taro.getStorageSync('showNightRing') || false;
  437. dispatch(showDay(showDayRing))
  438. dispatch(showNight(showNightRing))
  439. var gps = Taro.getStorageSync('gps')
  440. if (gps) {
  441. global.locationDetail = JSON.parse(gps)
  442. }
  443. var userData = Taro.getStorageSync('userData')
  444. if (userData) {
  445. dispatch(getInfoSuccess(JSON.parse(userData)));
  446. }
  447. }
  448. async function loadRNCache() {
  449. var showDayRing = await getStorage('showDayRing') || false;
  450. var showNightRing = await getStorage('showNightRing') || false;
  451. dispatch(showDay(showDayRing))
  452. dispatch(showNight(showNightRing))
  453. var gps = await getStorage('gps')
  454. if (gps) {
  455. global.locationDetail = JSON.parse(gps)
  456. }
  457. var userData = await getStorage('userData')
  458. if (userData) {
  459. dispatch(getInfoSuccess(JSON.parse(userData)));
  460. }
  461. }
  462. function getCheckData() {
  463. Promise.all([getClocks(), getPlans()]).then((list) => {
  464. setErrorPage(false)
  465. setCheckData(list)
  466. if (needScroll) {
  467. needScroll = false
  468. if (process.env.TARO_ENV == 'weapp') {
  469. setTimeout(() => {
  470. Taro.createSelectorQuery().select('#latest').boundingClientRect((rect) => {
  471. Taro.pageScrollTo({
  472. scrollTop: (rect as any).top,
  473. duration: 150
  474. })
  475. }).exec()
  476. }, 100)
  477. }
  478. else {
  479. // debugger
  480. // (scrollRef.current as any).scrollTo({ y: rpxToPx(400), animated: true })
  481. }
  482. }
  483. setLoaded(true)
  484. }).catch((e) => {
  485. setErrorPage(true)
  486. var list = JSON.parse(defaultValue)
  487. setCheckData(list)
  488. needScroll = false
  489. setLoaded(true)
  490. })
  491. getHistory()
  492. }
  493. function setCheckData(list: any) {
  494. setHomeData(list[0])
  495. global.homeData = list[0]
  496. global.currentStatus = list[0].current_record.status;
  497. var array = list[1].scenarios
  498. var schedule = null
  499. array.map(item => {
  500. if (item.name == 'FAST_SLEEP') {
  501. schedule = item.schedule;
  502. }
  503. })
  504. if (user.isLogin) {
  505. dispatch(updateScenario(list[0].current_record))
  506. dispatch(setConfigs(list[0].time_input_schema));
  507. dispatch(setScenario(list[0].scenario));
  508. global.scenario = list[0].scenario.name;
  509. }
  510. dispatch(setCurrentRecord(list[0].current_record))
  511. dispatch(setSchedule(schedule))
  512. // setLoaded(true)
  513. }
  514. function getHistory() {
  515. if (user.isLogin)
  516. getClockRecords({
  517. page: 1,
  518. limit: 1,
  519. // part_completed: true
  520. completed: true
  521. }).then(res => {
  522. setRecords((res as any).data)
  523. })
  524. }
  525. global.indexPageRefresh = () => {
  526. getCheckData()
  527. if (global.swiperDayNightRefresh) {
  528. global.swiperDayNightRefresh()
  529. }
  530. }
  531. global.showIndexModal = (isShow: boolean, detail: any, debugNode?: any) => {
  532. global.showModal = isShow
  533. setDebugInfo(debugNode)
  534. setShowModal(isShow)
  535. setModalDetail(detail)
  536. }
  537. global.showIndexModal2 = (isShow: boolean, detail: any) => {
  538. setDebugInfo(null)
  539. global.showModal = isShow
  540. setShowModal2(isShow)
  541. setModalDetail2(detail)
  542. }
  543. global.changeTargetDuration = (duration: number, isFast: boolean) => {
  544. var obj = JSON.parse(JSON.stringify(homeData))
  545. var record = obj.current_record
  546. if (isFast) {
  547. record.fast.target_end_time = record.fast.target_start_time + duration * 60 * 1000
  548. }
  549. else {
  550. record.sleep.target_end_time = record.sleep.target_start_time + duration * 60 * 1000
  551. }
  552. setHomeData(obj)
  553. }
  554. global.updateFastSleepData = (data: any, schedule?: any) => {
  555. if (data.id == (homeData as any).current_record.id) {
  556. var obj = JSON.parse(JSON.stringify(homeData))
  557. obj.current_record = data
  558. if (schedule) {
  559. obj.scenario.schedule = schedule
  560. }
  561. setHomeData(obj)
  562. }
  563. switch (data.scenario) {
  564. case 'FAST':
  565. if (data.status == 'WAIT_FOR_START') {
  566. dispatch(changeFastDuration(data.fast.target_end_time - data.fast.target_start_time))
  567. }
  568. break;
  569. case 'SLEEP':
  570. if (data.status == 'WAIT_FOR_START') {
  571. dispatch(changeSleepDuration(data.sleep.target_end_time - data.sleep.target_start_time))
  572. }
  573. break;
  574. case 'FAST_SLEEP':
  575. if (data.status == 'WAIT_FOR_START') {
  576. dispatch(changeFastDuration(data.fast.target_end_time - data.fast.target_start_time))
  577. }
  578. else if (data.status == 'ONGOING1') {
  579. dispatch(changeSleepDuration(data.sleep.target_end_time - data.sleep.target_start_time))
  580. }
  581. break;
  582. }
  583. }
  584. global.popScheduleAlert = (scenario, startTime) => {
  585. if (process.env.TARO_ENV == 'weapp') {
  586. if (permission.wxPubFollow) {
  587. showAlert({
  588. title: t('feature.track_time_duration.reminders.schedule_title'),
  589. content: scenario.name == 'FAST' ?
  590. t('feature.track_time_duration.reminders.enable_schedule_fast_content', { time: startTime }) :
  591. t('feature.track_time_duration.reminders.enable_schedule_sleep_content', { time: startTime }),
  592. showCancel: false,
  593. confirmText: t('feature.track_time_duration.reminders.ok')
  594. })
  595. }
  596. else {
  597. showAlert({
  598. title: t('feature.track_time_duration.reminders.schedule_title'),
  599. content: scenario.name == 'FAST' ?
  600. t('feature.track_time_duration.reminders.schedule_fast_content', { time: startTime }) :
  601. t('feature.track_time_duration.reminders.schedule_sleep_content', { time: startTime }),
  602. cancelText: t('feature.track_time_duration.reminders.later'),
  603. confirmText: t('feature.track_time_duration.reminders.open'),
  604. showCancel: true,
  605. confirm: () => {
  606. followWxPub()
  607. }
  608. })
  609. }
  610. }
  611. else {
  612. JPush.isNotificationEnabled((res) => {
  613. if (res) {
  614. showAlert({
  615. title: t('feature.track_time_duration.reminders.schedule_title'),
  616. content: scenario.name == 'FAST' ?
  617. t('feature.track_time_duration.reminders.enable_schedule_fast_content', { time: startTime }) :
  618. t('feature.track_time_duration.reminders.enable_schedule_sleep_content', { time: startTime }),
  619. showCancel: false,
  620. confirmText: t('feature.track_time_duration.reminders.ok')
  621. })
  622. }
  623. else {
  624. showAlert({
  625. title: t('feature.track_time_duration.reminders.schedule_title'),
  626. content: scenario.name == 'FAST' ?
  627. t('feature.track_time_duration.reminders.schedule_fast_content', { time: startTime }) :
  628. t('feature.track_time_duration.reminders.schedule_sleep_content', { time: startTime }),
  629. cancelText: t('feature.track_time_duration.reminders.later'),
  630. confirmText: t('feature.track_time_duration.reminders.open'),
  631. showCancel: true,
  632. confirm: () => {
  633. checkNotification()
  634. // Linking.openURL('app-settings:notifications')
  635. }
  636. })
  637. }
  638. })
  639. }
  640. }
  641. global.popMixScheduleAlert = (time1, time2) => {
  642. if (process.env.TARO_ENV == 'weapp') {
  643. if (permission.wxPubFollow) {
  644. showAlert({
  645. title: t('feature.track_time_duration.reminders.schedule_title'),
  646. content: t('feature.track_time_duration.reminders.enable_schedule_mix_content', { time1: time1, time2: time2 }),
  647. showCancel: false,
  648. confirmText: t('feature.track_time_duration.reminders.ok')
  649. })
  650. }
  651. else {
  652. showAlert({
  653. title: t('feature.track_time_duration.reminders.schedule_title'),
  654. content:
  655. t('feature.track_time_duration.reminders.schedule_mix_content'),
  656. cancelText: t('feature.track_time_duration.reminders.later'),
  657. confirmText: t('feature.track_time_duration.reminders.open'),
  658. showCancel: true,
  659. confirm: () => {
  660. followWxPub()
  661. }
  662. })
  663. }
  664. }
  665. else {
  666. JPush.isNotificationEnabled((res) => {
  667. if (res) {
  668. showAlert({
  669. title: t('feature.track_time_duration.reminders.schedule_title'),
  670. content: t('feature.track_time_duration.reminders.enable_schedule_mix_content', { time1: time1, time2: time2 }),
  671. showCancel: false,
  672. confirmText: t('feature.track_time_duration.reminders.ok')
  673. })
  674. }
  675. else {
  676. showAlert({
  677. title: t('feature.track_time_duration.reminders.schedule_title'),
  678. content:
  679. t('feature.track_time_duration.reminders.schedule_mix_content'),
  680. cancelText: t('feature.track_time_duration.reminders.later'),
  681. confirmText: t('feature.track_time_duration.reminders.open'),
  682. showCancel: true,
  683. confirm: () => {
  684. checkNotification()
  685. // Linking.openURL('app-settings:notifications')
  686. }
  687. })
  688. }
  689. })
  690. }
  691. }
  692. function followWxPub() {
  693. const resource = common.resources.filter((item: any) => {
  694. return item.code == 'follow_wx_pub'
  695. })
  696. jumpPage('/pages/common/H5?title=fast16cc 关注服务号&url=' + resource[0].url)
  697. }
  698. function modalContent() {
  699. if (showModal || showModal2) {
  700. return <Modal
  701. testInfo={debugInfo}
  702. dismiss={() => {
  703. setDebugInfo(null)
  704. setShowModal(false);
  705. setShowModal2(false);
  706. global.pauseIndexTimer = false
  707. }}
  708. confirm={() => { }}>
  709. {
  710. showModal ? modalDetail : modalDetail2
  711. }
  712. </Modal>
  713. }
  714. return <View />
  715. }
  716. async function getStorage(key: string) {
  717. try {
  718. const res = await Taro.getStorage({ key });
  719. return res.data;
  720. } catch {
  721. return '';
  722. }
  723. }
  724. function more() {
  725. jumpPage('/pages/common/RecordsHistory?type=time&title=time', 'RecordsHistory', navigation, {
  726. type: 'time',
  727. title: 'time'
  728. })
  729. }
  730. function tapLogin() {
  731. // if (process.env.TARO_ENV=='rn'){
  732. // Taro.getLocation({
  733. // success(result) {
  734. // console.log(result)
  735. // },
  736. // fail(res) {
  737. // console.log(res)
  738. // },
  739. // })
  740. // return
  741. // }
  742. if (!user.isLogin) {
  743. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  744. return
  745. }
  746. }
  747. function headerView() {
  748. return <ClockHeader homeData={homeData} />
  749. }
  750. global.scrollToLatest = () => {
  751. needScroll = true;
  752. }
  753. var timestamp = new Date().getTime()
  754. function render() {
  755. if (!loaded) {
  756. return <Layout type={TemplateType.customHeader} header={headerView()} title={t('page.clock.title')} titleShowStyle={NaviBarTitleShowType.scrollToShow}>
  757. <View style={{ width: rpxToPx(750), height: rpxToPx(900), display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
  758. <AtActivityIndicator size={40} color="#fff" />
  759. {
  760. process.env.TARO_ENV == 'weapp' && <Tabbar index={0} />
  761. }
  762. </View>
  763. </Layout>
  764. }
  765. return <Layout type={TemplateType.customHeader} header={headerView()} title={t('page.clock.title')} titleShowStyle={NaviBarTitleShowType.scrollToShow}>
  766. <View className="index_container">
  767. {
  768. needShowAddTip && showTip && <Tooltip title="添加到我的小程序" closeTip={() => { setNeedShowAddTip(false) }} />
  769. }
  770. <Text className="count">{count}</Text>
  771. <Box>
  772. <View>
  773. <IndexItem type="FAST_SLEEP" data={homeData} time={timestamp} showStage={false} />
  774. {
  775. user.isLogin ? <IndexConsole record={homeData} /> : <StageSelector />
  776. }
  777. </View>
  778. </Box>
  779. {
  780. process.env.TARO_ENV == 'weapp' && <DayNightCard isNight={true} count={count} />
  781. }
  782. {
  783. process.env.TARO_ENV == 'rn' && <DayNightSwiper count={count} />
  784. // <Swiper
  785. // className="rn_swiper"
  786. // indicatorDots indicatorColor='#333'
  787. // indicatorActiveColor='#999'>
  788. // <SwiperItem>
  789. // <DayNightCard isNight={true} count={count} />
  790. // </SwiperItem>
  791. // <SwiperItem>
  792. // <DayNightCard isNight={false} count={count} />
  793. // </SwiperItem>
  794. // </Swiper>
  795. }
  796. {
  797. process.env.TARO_ENV == 'rn' && <View style={{ height: rpxToPx(20) }} />
  798. }
  799. {user.isLogin && <AllRings data={homeData} time={timestamp} />}
  800. {
  801. user.isLogin && records.length > 0 && <View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', marginTop: rpxToPx(40), }}>
  802. {
  803. <Text className="discovery1" style={{ marginBottom: 0 }}>{t('feature.track_time_duration.record_fast_sleep.header.latest_record')}</Text>
  804. }
  805. {
  806. process.env.TARO_ENV == 'weapp' && <Text className="fast_sleep_more index_more" onClick={more}>{t('feature.track_time_duration.record_fast_sleep.header.btn_show_all')}</Text>
  807. }
  808. {
  809. process.env.TARO_ENV == 'rn' && <View onClick={more}>
  810. <GradientText style={{ fontSize: rpxToPx(32), fontWeight: 'bold', marginRight: rpxToPx(46) }}>{t('feature.track_time_duration.record_fast_sleep.header.btn_show_all')}</GradientText>
  811. </View>
  812. }
  813. </View>
  814. }
  815. {
  816. user.isLogin && records.length > 0 && <View id="latest" className="fast_sleep_item_bg">
  817. <RecordFastSleep data={records[0]} type='record' index={-20000} />
  818. </View>
  819. }
  820. {
  821. loaded && !user.isLogin && <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', marginTop: rpxToPx(36) }}>
  822. <ChooseScenarioBtn title={t('feature.auth.login.login_now')} background={ColorType.fast} onClick={tapLogin} />
  823. </View>
  824. }
  825. {/* {
  826. showErrorPage && <NoData refresh={() => { getCheckData() }} />
  827. } */}
  828. {
  829. user.isLogin && !showErrorPage && <View style={{ marginTop: rpxToPx(60), display: 'flex', flexDirection: 'column' }}>
  830. <Text className="discovery1" >{t('feature.track_time_duration.weekly.title')}</Text>
  831. {/* <WeekCalendar /> */}
  832. </View>
  833. }
  834. {
  835. user.isLogin && <Streaks count={count} />
  836. }
  837. {
  838. process.env.TARO_ENV == 'weapp' && <View style={{ height: 100 }} />
  839. }
  840. {
  841. modalContent()
  842. }
  843. {
  844. homeData && <SegmentPop data={homeData} />
  845. }
  846. {
  847. homeData && <DurationPicker record={(homeData as any).current_record} />
  848. }
  849. <Tabbar index={0} />
  850. </View>
  851. </Layout>
  852. }
  853. // if (process.env.TARO_ENV == 'rn') {
  854. // return <ScrollView>
  855. // {
  856. // render()
  857. // }
  858. // </ScrollView>
  859. // }
  860. return render()
  861. }