ClockMain.tsx 36 KB

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