Clock.tsx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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, 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. 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 JPush;
  66. let checkNotification, uploadPermissions, NativeAppEventEmitter;
  67. let messaging;
  68. if (process.env.TARO_ENV == 'rn') {
  69. messaging = require('@react-native-firebase/messaging').default
  70. JPush = require('jpush-react-native').default;
  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 permission = useSelector((state: any) => state.permission);
  104. const common = useSelector((state: any) => state.common);
  105. const scrollRef = useRef(null);
  106. const nightStore = useSelector((state: any) => state.night);
  107. if (process.env.TARO_ENV == 'weapp') {
  108. useShareAppMessage((e) => {
  109. return {
  110. title: t('feature.track_time_duration.common.share_title'),
  111. path: 'pages/clock/Clock'
  112. }
  113. })
  114. }
  115. useEffect(() => {
  116. console.log(new Date().toString());
  117. dispatch(staticResources() as any);
  118. timer = setInterval(() => {
  119. var now = new Date()
  120. if (now.getDay() == 0 && now.getHours() == 12 && now.getMinutes() == 0 && now.getSeconds() == 0) {
  121. if (global.refrehWeekly) {
  122. global.refrehWeekly()
  123. }
  124. }
  125. if (global.pauseIndexTimer || pauseTimer) {
  126. return
  127. }
  128. setCount((prevCounter) => prevCounter + 1)
  129. }, 1000)
  130. }, [])
  131. useEffect(() => {
  132. // Taro.request({
  133. // url:'https://api.fast.dev.liveplus.fun/api/static-resource-urls',
  134. // method:'GET',
  135. // data:{},
  136. // timeout:20000,
  137. // success(result) {
  138. // showAlert({
  139. // title:'success',
  140. // content:JSON.stringify(result)
  141. // })
  142. // },
  143. // fail(res) {
  144. // showAlert({
  145. // title:'fail',
  146. // content:JSON.stringify(res)
  147. // })
  148. // },
  149. // })
  150. if (process.env.TARO_ENV == 'weapp') {
  151. loadWXCache()
  152. }
  153. else {
  154. loadRNCache()
  155. }
  156. if (process.env.TARO_ENV == 'rn') {
  157. JPush.setBadge({
  158. badge: 0,
  159. appBadge: 0
  160. })
  161. JPush.isNotificationEnabled((res) => {
  162. if (res) {
  163. const test = require('@/utils/push').default
  164. test()
  165. }
  166. })
  167. AppState.addEventListener('change', handleAppStateChange);
  168. /*
  169. if (Taro.getSystemInfoSync().platform == 'ios') {
  170. setTimeout(() => {
  171. var Jto = require('react-native').NativeModules.NativeBridge;
  172. // Jto.authNotification()
  173. Jto.getNotificationAuthStatus()
  174. }, 5000)
  175. NativeAppEventEmitter.addListener('notificationResult', (data) => {
  176. console.log(data)
  177. showAlert({
  178. title:'notification status',
  179. content:data
  180. })
  181. })
  182. }*/
  183. }
  184. }, [])
  185. useEffect(() => {
  186. console.log(user.isLogin)
  187. getCheckData()
  188. // global.pauseIndexTimer = !user.isLogin
  189. if (user.isLogin) {
  190. uploadUserClient();
  191. checkAuthorized();
  192. //检查用户是否添加过小程序
  193. checkAddToMini();
  194. if (process.env.TARO_ENV == 'rn') {
  195. if (Taro.getSystemInfoSync().platform == 'ios') {
  196. var Jto = require('react-native').NativeModules.NativeBridge;
  197. Jto.getNotificationAuthStatus()
  198. NativeAppEventEmitter.addListener('notificationResult', (data) => {
  199. global.notification = data;
  200. uploadPermissions()
  201. })
  202. }
  203. // uploadPermissions()
  204. JPush.isNotificationEnabled((res) => {
  205. if (res) {
  206. const test = require('@/utils/push').default
  207. test()
  208. }
  209. })
  210. checkVersionUpdate()
  211. }
  212. }
  213. else {
  214. dispatch(clearNightStore());
  215. }
  216. }, [user.isLogin])
  217. useReady(async () => {
  218. const userData = await getStorage('userData');
  219. if (userData) {
  220. dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
  221. getHistory()
  222. }
  223. })
  224. async function checkVersionUpdate() {
  225. if (!user.isLogin) {
  226. return
  227. }
  228. if (process.env.TARO_ENV == 'rn') {
  229. systemVersion().then(res => {
  230. if ((res as any).result == 'UPDATE' && !showUpdate) {
  231. showUpdate = true
  232. showAlert({
  233. title: (res as any).title,
  234. content: (res as any).description,
  235. showCancel: true,
  236. confirmText: '更新',
  237. confirm: () => {
  238. Linking.openURL((res as any).download_url)
  239. }
  240. })
  241. }
  242. else if ((res as any).result == 'FORCE_UPDATE') {
  243. showAlert({
  244. title: (res as any).title,
  245. content: (res as any).description,
  246. showCancel: false,
  247. confirmText: '更新',
  248. confirm: () => {
  249. Linking.openURL((res as any).download_url)
  250. }
  251. })
  252. }
  253. })
  254. return;
  255. }
  256. const showAlert1 = await getStorage('120alert') || false;
  257. if (!showAlert1) {
  258. showAlert({
  259. title: '版本更新提示',
  260. content: '「连续纪录」新增归零倒计时提示⏳\n别让连续记录终止, 将它们一直保持下去!',
  261. showCancel: false,
  262. confirmText: '我知道了'
  263. })
  264. }
  265. Taro.setStorage({ key: '120alert', data: true })
  266. }
  267. function checkAddToMini() {
  268. if (process.env.TARO_ENV == 'weapp') {
  269. const version = Taro.getAppBaseInfo().SDKVersion
  270. if (compareVersion(version, '2.30.3') >= 0) {
  271. wx.checkIsAddedToMyMiniProgram({
  272. success: (res) => {
  273. if (!res.added) {
  274. setNeedShowAddTip(true)
  275. }
  276. },
  277. fail: (e) => {
  278. }
  279. });
  280. }
  281. }
  282. }
  283. useDidShow(() => {
  284. checkTimeZone()
  285. setCount(pre => pre + 1)
  286. //resume timer
  287. pauseTimer = false
  288. if (user.isLogin) {
  289. if (global.refreshRecent) {
  290. global.refreshRecent()
  291. }
  292. }
  293. setTimeout(() => {
  294. checkVersionUpdate();
  295. updateNotificationStatus();
  296. }, 1000)
  297. })
  298. usePageScroll((e) => {
  299. if (e.scrollTop > 70) {
  300. setShowTip(true)
  301. }
  302. else {
  303. setShowTip(false)
  304. }
  305. })
  306. useDidHide(() => {
  307. //pause timer
  308. pauseTimer = true
  309. })
  310. global.refreshIndex = () => {
  311. setCount((prevCounter) => prevCounter + 1)
  312. }
  313. const handleAppStateChange = (nextAppState) => {
  314. console.log(nextAppState)
  315. if (nextAppState != 'active') {
  316. return
  317. }
  318. checkTimeZone()
  319. updateNotificationStatus()
  320. };
  321. function updateNotificationStatus() {
  322. if (process.env.TARO_ENV == 'rn') {
  323. if (user.isLogin) {
  324. if (Taro.getSystemInfoSync().platform == 'ios') {
  325. var Jto = require('react-native').NativeModules.NativeBridge;
  326. Jto.getNotificationAuthStatus()
  327. }
  328. }
  329. JPush.setBadge({
  330. badge: 0,
  331. appBadge: 0
  332. })
  333. }
  334. }
  335. function checkTimeZone() {
  336. setCount((prevCounter) => prevCounter + 1)
  337. var timeZoneFormatted = getTimezone()
  338. Taro.getStorage({
  339. key: 'last_tz',
  340. success: function (res) {
  341. if (res.data && res.data != timeZoneFormatted) {
  342. if (global.refrehWeekly) {
  343. global.refrehWeekly()
  344. }
  345. // if (global.refreshNight){
  346. // global.refreshNight()
  347. // }
  348. // if (global.refreshDay){
  349. // global.refreshDay()
  350. // }
  351. if (global.currentStatus != 'WAIT_FOR_START') {
  352. showAlert({
  353. title: t('feature.track_time_duration.change_tz_alert.title'),
  354. content: t('feature.track_time_duration.change_tz_alert.content', { tz: timeZoneFormatted }),
  355. showCancel: false,
  356. confirmText: t('feature.track_time_duration.change_tz_alert.confirm'),
  357. })
  358. }
  359. }
  360. },
  361. complete: function () {
  362. Taro.setStorage({ key: 'last_tz', data: timeZoneFormatted })
  363. }
  364. })
  365. }
  366. function uploadUserClient() {
  367. var systemInfo = Taro.getSystemInfoSync();
  368. console.log(systemInfo)
  369. var language: any = systemInfo.language
  370. if (process.env.TARO_ENV == 'rn') {
  371. var NativeModules = require('react-native').NativeModules;
  372. if (Taro.getSystemInfoSync().platform == 'ios') {
  373. // language = NativeModules.SettingsManager.settings.AppleLocale
  374. // languageList = NativeModules.SettingsManager.settings.AppleLanguages
  375. language = {
  376. AppleLocale: NativeModules.SettingsManager.settings.AppleLocale,
  377. AppleLanguages: NativeModules.SettingsManager.settings.AppleLanguages,
  378. NSLanguages: NativeModules.SettingsManager.settings.NSLanguages
  379. }
  380. }
  381. else {
  382. language = NativeModules.I18nManager.localeIdentifier
  383. }
  384. // iOS:
  385. // var locale = NativeModules.SettingsManager.settings.AppleLocale ||
  386. // NativeModules.SettingsManager.settings.AppleLanguages[0] // "fr_FR"
  387. // showAlert({
  388. // title: 'locale',
  389. // content: JSON.stringify(NativeModules.SettingsManager.settings)
  390. // })
  391. // Android:
  392. // locale = NativeModules.I18nManager.localeIdentifier // "fr_FR"
  393. }
  394. var timeZoneFormatted = getTimezone()
  395. var timeZoneId = ''
  396. var timeZoneName = ''
  397. if (Taro.getSystemInfoSync().platform == 'ios') {
  398. timeZoneId = getTimezoneId()
  399. timeZoneName = getTimezoneName()
  400. }
  401. clientInfo({
  402. client: {
  403. client_type: process.env.TARO_ENV == 'weapp' ? 'MP' : 'APP',
  404. client_version: process.env.TARO_ENV == 'weapp' ? WX_VERSION : APP_VERSION,//Taro.getAccountInfoSync().miniProgram.version : '1.0',//'1.0'
  405. wx_version: process.env.TARO_ENV == 'weapp' ? systemInfo.version : '_'
  406. },
  407. meta: {
  408. language: language,
  409. timezone: {
  410. gmt: timeZoneFormatted,
  411. id: timeZoneId,
  412. name: timeZoneName
  413. },
  414. },
  415. device: {
  416. brand: systemInfo.brand,
  417. model: systemInfo.model,
  418. platform: systemInfo.platform,
  419. system: systemInfo.system
  420. },
  421. perm: {
  422. wifi_enabled: systemInfo.wifiEnabled,
  423. location_authorized: systemInfo.locationAuthorized,
  424. location_enabled: systemInfo.locationEnabled
  425. }
  426. })
  427. }
  428. function loadWXCache() {
  429. var showDayRing = Taro.getStorageSync('showDayRing') || false;
  430. var showNightRing = Taro.getStorageSync('showNightRing') || false;
  431. dispatch(showDay(showDayRing))
  432. dispatch(showNight(showNightRing))
  433. var gps = Taro.getStorageSync('gps')
  434. if (gps) {
  435. global.locationDetail = JSON.parse(gps)
  436. }
  437. var userData = Taro.getStorageSync('userData')
  438. if (userData) {
  439. dispatch(getInfoSuccess(JSON.parse(userData)));
  440. }
  441. }
  442. async function loadRNCache() {
  443. var showDayRing = await getStorage('showDayRing') || false;
  444. var showNightRing = await getStorage('showNightRing') || false;
  445. dispatch(showDay(showDayRing))
  446. dispatch(showNight(showNightRing))
  447. var gps = await getStorage('gps')
  448. if (gps) {
  449. global.locationDetail = JSON.parse(gps)
  450. }
  451. var userData = await getStorage('userData')
  452. console.log(userData)
  453. if (userData) {
  454. dispatch(getInfoSuccess(JSON.parse(userData)));
  455. }
  456. // //测试直接登录
  457. // else {
  458. // userData = '{"metrics_times":0,"ongoing":false,"id":"fe6641e66a0e411937a52511e19851cd","mobile_bind":false,"mobile":"","avatar":"https://api.fast.dev.liveplus.fun/api/avatars/fast_441b5a21.svg","nickname":"fast_441b5a21","type":"GENERAL","lv":1,"invite_code":"LVQ5","input_invite_code":true,"reg_time":1712838856000,"login_bind":{"wx":false,"wx_mp":false,"wx_pub":false,"qq":false,"weibo":false},"test_user":true,"manager_user":false,"web_user":false,"rjv_balance":0,"day_level":1,"scenario_select_count":77,"push_enabled":false,"is_new_user":true,"lang":"en","timezone":{},"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJhY2NvdW50Iiwic3ViIjoiZmU2NjQxZTY2YTBlNDExOTM3YTUyNTExZTE5ODUxY2QiLCJpc3MiOiJmYXN0Iiwibmlja25hbWUiOiJmYXN0XzQ0MWI1YTIxIiwidHlwIjoiQmVhcmVyIiwic2Vzc2lvbl9zdGF0ZSI6IjA0NTcxOGYzN2Y5NTYzYjcxMTNkMWZhODI5NmE2MjQ2IiwiZXhwIjoxNzQ2Njg4NTYxLCJpYXQiOjE3MTUxNTI1NjF9.x1CguOILCTjT8lKcEdSEeuS9ACc5QK2G4Rxmcz9OMLQ","need_improve":false}'
  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:any = null
  499. array.map(item => {
  500. if (item.name == 'FAST_SLEEP') {
  501. schedule = item.schedule;
  502. schedule.sleep.prev_input = item.prev_input;
  503. }
  504. })
  505. if (user.isLogin) {
  506. dispatch(updateScenario(list[0].current_record))
  507. dispatch(setConfigs(list[0].time_input_schema));
  508. dispatch(setScenario(list[0].scenario));
  509. global.scenario = list[0].scenario.name;
  510. }
  511. dispatch(setCurrentRecord(list[0].current_record))
  512. dispatch(setSchedule(schedule))
  513. // setLoaded(true)
  514. }
  515. function getHistory() {
  516. if (user.isLogin)
  517. getClockRecords({
  518. page: 1,
  519. limit: 1,
  520. // part_completed: true
  521. completed: true
  522. }).then(res => {
  523. setRecords((res as any).data)
  524. })
  525. }
  526. global.indexPageRefresh = () => {
  527. getCheckData()
  528. if (global.swiperDayNightRefresh) {
  529. global.swiperDayNightRefresh()
  530. }
  531. }
  532. global.showIndexModal = (isShow: boolean, detail: any, debugNode?: any) => {
  533. global.showModal = isShow
  534. setDebugInfo(debugNode)
  535. setShowModal(isShow)
  536. setModalDetail(detail)
  537. }
  538. global.showIndexModal2 = (isShow: boolean, detail: any) => {
  539. setDebugInfo(null)
  540. global.showModal = isShow
  541. setShowModal2(isShow)
  542. setModalDetail2(detail)
  543. }
  544. global.changeTargetDuration = (duration: number, isFast: boolean) => {
  545. var obj = JSON.parse(JSON.stringify(homeData))
  546. var record = obj.current_record
  547. if (isFast) {
  548. record.fast.target_end_time = record.fast.target_start_time + duration * 60 * 1000
  549. }
  550. else {
  551. record.sleep.target_end_time = record.sleep.target_start_time + duration * 60 * 1000
  552. }
  553. setHomeData(obj)
  554. }
  555. global.updateFastSleepData = (data: any, schedule?: any) => {
  556. if (data.id == (homeData as any).current_record.id) {
  557. var obj = JSON.parse(JSON.stringify(homeData))
  558. obj.current_record = data
  559. if (schedule) {
  560. obj.scenario.schedule = schedule
  561. }
  562. setHomeData(obj)
  563. }
  564. switch (data.scenario) {
  565. case 'FAST':
  566. if (data.status == 'WAIT_FOR_START') {
  567. dispatch(changeFastDuration(data.fast.target_end_time - data.fast.target_start_time))
  568. }
  569. break;
  570. case 'SLEEP':
  571. if (data.status == 'WAIT_FOR_START') {
  572. dispatch(changeSleepDuration(data.sleep.target_end_time - data.sleep.target_start_time))
  573. }
  574. break;
  575. case 'FAST_SLEEP':
  576. if (data.status == 'WAIT_FOR_START') {
  577. dispatch(changeFastDuration(data.fast.target_end_time - data.fast.target_start_time))
  578. }
  579. else if (data.status == 'ONGOING1') {
  580. dispatch(changeSleepDuration(data.sleep.target_end_time - data.sleep.target_start_time))
  581. }
  582. break;
  583. }
  584. }
  585. global.popScheduleAlert = (scenario, startTime) => {
  586. if (process.env.TARO_ENV == 'weapp') {
  587. if (permission.wxPubFollow) {
  588. showAlert({
  589. title: t('feature.track_time_duration.reminders.schedule_title'),
  590. content: scenario.name == 'FAST' ?
  591. t('feature.track_time_duration.reminders.enable_schedule_fast_content', { time: startTime }) :
  592. t('feature.track_time_duration.reminders.enable_schedule_sleep_content', { time: startTime }),
  593. showCancel: false,
  594. confirmText: t('feature.track_time_duration.reminders.ok')
  595. })
  596. }
  597. else {
  598. showAlert({
  599. title: t('feature.track_time_duration.reminders.schedule_title'),
  600. content: scenario.name == 'FAST' ?
  601. t('feature.track_time_duration.reminders.schedule_fast_content', { time: startTime }) :
  602. t('feature.track_time_duration.reminders.schedule_sleep_content', { time: startTime }),
  603. cancelText: t('feature.track_time_duration.reminders.later'),
  604. confirmText: t('feature.track_time_duration.reminders.open'),
  605. showCancel: true,
  606. confirm: () => {
  607. followWxPub()
  608. }
  609. })
  610. }
  611. }
  612. else {
  613. JPush.isNotificationEnabled((res) => {
  614. if (res) {
  615. showAlert({
  616. title: t('feature.track_time_duration.reminders.schedule_title'),
  617. content: scenario.name == 'FAST' ?
  618. t('feature.track_time_duration.reminders.enable_schedule_fast_content', { time: startTime }) :
  619. t('feature.track_time_duration.reminders.enable_schedule_sleep_content', { time: startTime }),
  620. showCancel: false,
  621. confirmText: t('feature.track_time_duration.reminders.ok')
  622. })
  623. }
  624. else {
  625. showAlert({
  626. title: t('feature.track_time_duration.reminders.schedule_title'),
  627. content: scenario.name == 'FAST' ?
  628. t('feature.track_time_duration.reminders.schedule_fast_content', { time: startTime }) :
  629. t('feature.track_time_duration.reminders.schedule_sleep_content', { time: startTime }),
  630. cancelText: t('feature.track_time_duration.reminders.later'),
  631. confirmText: t('feature.track_time_duration.reminders.open'),
  632. showCancel: true,
  633. confirm: () => {
  634. checkNotification()
  635. // Linking.openURL('app-settings:notifications')
  636. }
  637. })
  638. }
  639. })
  640. }
  641. }
  642. global.popMixScheduleAlert = (time1, time2) => {
  643. if (process.env.TARO_ENV == 'weapp') {
  644. if (permission.wxPubFollow) {
  645. showAlert({
  646. title: t('feature.track_time_duration.reminders.schedule_title'),
  647. content: t('feature.track_time_duration.reminders.enable_schedule_mix_content', { time1: time1, time2: time2 }),
  648. showCancel: false,
  649. confirmText: t('feature.track_time_duration.reminders.ok')
  650. })
  651. }
  652. else {
  653. showAlert({
  654. title: t('feature.track_time_duration.reminders.schedule_title'),
  655. content:
  656. t('feature.track_time_duration.reminders.schedule_mix_content'),
  657. cancelText: t('feature.track_time_duration.reminders.later'),
  658. confirmText: t('feature.track_time_duration.reminders.open'),
  659. showCancel: true,
  660. confirm: () => {
  661. followWxPub()
  662. }
  663. })
  664. }
  665. }
  666. else {
  667. JPush.isNotificationEnabled((res) => {
  668. if (res) {
  669. showAlert({
  670. title: t('feature.track_time_duration.reminders.schedule_title'),
  671. content: t('feature.track_time_duration.reminders.enable_schedule_mix_content', { time1: time1, time2: time2 }),
  672. showCancel: false,
  673. confirmText: t('feature.track_time_duration.reminders.ok')
  674. })
  675. }
  676. else {
  677. showAlert({
  678. title: t('feature.track_time_duration.reminders.schedule_title'),
  679. content:
  680. t('feature.track_time_duration.reminders.schedule_mix_content'),
  681. cancelText: t('feature.track_time_duration.reminders.later'),
  682. confirmText: t('feature.track_time_duration.reminders.open'),
  683. showCancel: true,
  684. confirm: () => {
  685. checkNotification()
  686. // Linking.openURL('app-settings:notifications')
  687. }
  688. })
  689. }
  690. })
  691. }
  692. }
  693. function followWxPub() {
  694. const resource = common.resources.filter((item: any) => {
  695. return item.code == 'follow_wx_pub'
  696. })
  697. jumpPage('/pages/common/H5?title=fast16cc 关注服务号&url=' + resource[0].url)
  698. }
  699. function modalContent() {
  700. if (showModal || showModal2) {
  701. return <Modal
  702. testInfo={debugInfo}
  703. dismiss={() => {
  704. setDebugInfo(null)
  705. setShowModal(false);
  706. setShowModal2(false);
  707. global.pauseIndexTimer = false
  708. }}
  709. confirm={() => { }}>
  710. {
  711. showModal ? modalDetail : modalDetail2
  712. }
  713. </Modal>
  714. }
  715. return <View />
  716. }
  717. async function getStorage(key: string) {
  718. try {
  719. const res = await Taro.getStorage({ key });
  720. return res.data;
  721. } catch {
  722. return '';
  723. }
  724. }
  725. function more() {
  726. jumpPage('/pages/common/RecordsHistory?type=time&title=time', 'RecordsHistory', navigation, {
  727. type: 'time',
  728. title: 'time'
  729. })
  730. }
  731. function tapLogin() {
  732. if (!user.isLogin) {
  733. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  734. }
  735. }
  736. function headerView() {
  737. return <ClockHeader homeData={homeData} />
  738. }
  739. global.scrollToLatest = () => {
  740. needScroll = true;
  741. }
  742. var timestamp = new Date().getTime()
  743. function render() {
  744. if (!loaded) {
  745. return <Layout type={TemplateType.customHeader} header={headerView()} title={t('page.clock.title')} titleShowStyle={NaviBarTitleShowType.scrollToShow}>
  746. <View style={{ width: rpxToPx(750), height: rpxToPx(900), display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
  747. <AtActivityIndicator size={40} color="#fff" />
  748. {
  749. process.env.TARO_ENV == 'weapp' && <Tabbar index={0} />
  750. }
  751. </View>
  752. </Layout>
  753. }
  754. return <Layout type={TemplateType.customHeader} header={headerView()} title={t('page.clock.title')} titleShowStyle={NaviBarTitleShowType.scrollToShow}>
  755. <View className="index_container">
  756. {
  757. needShowAddTip && showTip && <Tooltip title="添加到我的小程序" closeTip={() => { setNeedShowAddTip(false) }} />
  758. }
  759. <Text className="count">{count}</Text>
  760. <Box>
  761. <View>
  762. <IndexItem type="FAST_SLEEP" data={homeData} time={timestamp} showStage={false} />
  763. {
  764. user.isLogin ? <IndexConsole record={homeData} count={count}/> : <StageSelector />
  765. }
  766. </View>
  767. </Box>
  768. {
  769. process.env.TARO_ENV == 'weapp' && <DayNightCard isNight={true} count={count} />
  770. }
  771. {
  772. process.env.TARO_ENV == 'rn' && <DayNightSwiper count={count} />
  773. }
  774. {
  775. process.env.TARO_ENV == 'rn' && <View style={{ height: rpxToPx(20) }} />
  776. }
  777. {user.isLogin && <AllRings data={homeData} time={timestamp} />}
  778. {
  779. user.isLogin && records.length > 0 && <View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', marginTop: rpxToPx(40), }}>
  780. {
  781. <Text className="discovery1" style={{ marginBottom: 0 }}>{t('feature.track_time_duration.record_fast_sleep.header.latest_record')}</Text>
  782. }
  783. {
  784. 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>
  785. }
  786. {
  787. process.env.TARO_ENV == 'rn' && <View onClick={more}>
  788. <GradientText style={{ fontSize: rpxToPx(32), fontWeight: 'bold', marginRight: rpxToPx(46) }}>{t('feature.track_time_duration.record_fast_sleep.header.btn_show_all')}</GradientText>
  789. </View>
  790. }
  791. </View>
  792. }
  793. {
  794. user.isLogin && records.length > 0 && <View id="latest" className="fast_sleep_item_bg">
  795. <RecordFastSleep data={records[0]} type='record' index={-20000} />
  796. </View>
  797. }
  798. {
  799. loaded && !user.isLogin && <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', marginTop: rpxToPx(36) }}>
  800. <ChooseScenarioBtn title={t('feature.auth.login.login_now')} background={ColorType.fast} onClick={tapLogin} />
  801. </View>
  802. }
  803. {
  804. user.isLogin && !showErrorPage && <View style={{ marginTop: rpxToPx(60), display: 'flex', flexDirection: 'column' }}>
  805. <Text className="discovery1" >{t('feature.track_time_duration.weekly.title')}</Text>
  806. <WeekCalendar />
  807. </View>
  808. }
  809. {
  810. user.isLogin && <Streaks count={count} />
  811. }
  812. {
  813. process.env.TARO_ENV == 'weapp' && <View style={{ height: 100 }} />
  814. }
  815. {
  816. modalContent()
  817. }
  818. {
  819. homeData && <SegmentPop data={homeData} />
  820. }
  821. {
  822. homeData && <DurationPicker record={(homeData as any).current_record} />
  823. }
  824. <Tabbar index={0} />
  825. </View>
  826. </Layout>
  827. }
  828. // if (process.env.TARO_ENV == 'rn') {
  829. // return <ScrollView>
  830. // {
  831. // render()
  832. // }
  833. // </ScrollView>
  834. // }
  835. return render()
  836. }