import { View, Text, Image } from "@tarojs/components"; import './index_time.scss'; import { jumpPage } from "@/features/trackTimeDuration/hooks/Common"; import { getCommon, getDot } from "@/features/trackTimeDuration/hooks/RingData"; import Rings, { RingCommon, BgRing, TargetRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings"; import { useEffect, useState } from "react"; import DeviceInfo from 'react-native-device-info'; import dayjs from "dayjs"; import moment from 'moment-timezone' import Modal from "@/components/layout/Modal.weapp"; import { getResources, loginAnonymous, place, systemVersion } from "./services/net"; import Taro, { useDidShow } from "@tarojs/taro"; import { useTranslation } from "react-i18next"; import 'dayjs/locale/zh-cn'; import 'dayjs/locale/en'; import momentT from 'moment'; import { getTimezone, getTimezoneName, kIsAndroid } from "@/utils/tools"; import { AppState, Linking, SafeAreaView } from "react-native"; import { method } from "lodash"; import { AtActivityIndicator } from "taro-ui"; import showAlert from "@/components/basic/Alert"; // import 'moment/locale/en'; let showUpdate = false; let LinearGradient, useActionSheet, useNavigation if (process.env.TARO_ENV == 'rn') { LinearGradient = require('react-native-linear-gradient').default useActionSheet = require('@expo/react-native-action-sheet').useActionSheet useNavigation = require("@react-navigation/native").useNavigation } export default function IndexTimePage() { let navigation; if (useNavigation) { navigation = useNavigation() } const [count, setCount] = useState(0) const [showLanguage, setShowLanguage] = useState(false) const [isEn, setIsEn] = useState(true) const [showDst, setShowDst] = useState(false) const [current, setCurrent] = useState(null) const [loading, setLoading] = useState(false) const [resources, setResources] = useState([]) const { t, i18n } = useTranslation() useEffect(() => { getLanguage(); getResources().then(res => { setResources(res) }) AppState.addEventListener('change', handleAppStateChange); login() setInterval(() => { setCount(t => t + 1) const now = new Date() const seconds = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds() if (current && current.time && current.time.timezone.use_dst) { if (current.time_changes && current.time_changes.length > 1) { const date = new Date(current.time_changes[1].timestamp) const seconds2 = date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds() if (seconds == seconds2) { refresh() } } } }, 1000) }, []) function login() { loginAnonymous(kIsAndroid ? DeviceInfo.getAndroidIdSync() : DeviceInfo.getDeviceId()).then(res => { global.token = (res as any).token Taro.setStorage({ key: 'token', data: (res as any).token }) checkVersionUpdate() }).catch(e => { console.log('login error', e) }) } useDidShow(() => { checkVersionUpdate() }) const handleAppStateChange = (nextAppState) => { console.log(nextAppState) if (nextAppState != 'active') { return } if (nextAppState == 'active') { console.log('active') refresh() } }; async function checkVersionUpdate() { systemVersion('time').then(res => { if ((res as any).result == 'UPDATE' && !showUpdate) { showUpdate = true showAlert({ title: (res as any).title, content: (res as any).description, showCancel: true, confirmText: 'Update', confirm: () => { Linking.openURL((res as any).download_url) } }) } else if ((res as any).result == 'FORCE_UPDATE') { showAlert({ title: (res as any).title, content: (res as any).description, showCancel: false, confirmText: 'Update', confirm: () => { Linking.openURL((res as any).download_url) } }) } }) } function refresh() { if (current) { chooseLocation({ lat: current.geo.lat, lng: current.geo.lng }, 'QUERY') } } async function getLanguage() { var strLocation = await getStorage('lastLocation') if (strLocation) { setCurrent(JSON.parse(strLocation)) } var language = await getStorage('language') || 'en' global.language = language; i18n.changeLanguage(language) setIsEn(language == 'en') dayjs.locale(language == 'en' ? 'en' : 'zh-cn'); require('moment/locale/en-gb') require('moment/locale/zh-cn') momentT.locale(language == 'en' ? 'en-gb' : 'zh-cn') moment.defineLocale(language == 'en' ? 'en-gb' : 'zh-cn', (momentT.localeData() as any)._config) moment.locale(language == 'en' ? 'en-gb' : 'zh-cn') var token = await getStorage('token') || '' global.token = token refresh() // moment.locale(language == 'en' ? 'en' : 'zh-cn'); } async function getStorage(key: string) { try { const res = await Taro.getStorage({ key }); return res.data; } catch { return ''; } } function changeLanguage(isEnglish: boolean) { global.language = isEnglish ? 'en' : 'zh'; i18n.changeLanguage(isEnglish ? 'en' : 'zh') setIsEn(isEnglish) Taro.setStorage({ key: 'language', data: isEnglish ? 'en' : 'zh' }) setShowLanguage(false) dayjs.locale(isEnglish ? 'en' : 'zh-cn'); require('moment/locale/en-gb') require('moment/locale/zh-cn') momentT.locale(isEnglish ? 'en-gb' : 'zh-cn') moment.defineLocale(isEnglish ? 'en-gb' : 'zh-cn', (momentT.localeData() as any)._config) moment.locale(isEnglish ? 'en-gb' : 'zh-cn') // moment.locale(isEnglish ? 'en' : 'zh-cn'); if (current) { chooseLocation({ lat: current.geo.lat, lng: current.geo.lng }, 'QUERY') } } async function goMap() { if (loading) { return; } const zoom = await getStorage('zoom') || 8 jumpPage('', 'map', navigation, { source: 'time_of_day', zoom: zoom, lat: current ? current.geo.lat : null, lng: current ? current.geo.lng : null, chooseLocation: chooseLocation }) } function goRecords() { if (loading) { return; } if (!current) { return } jumpPage('', 'MyPlaces', navigation, { clearLocation: clearLocation, changeLocation: changeLocation }) } function showPrivacy() { const resource = resources.filter((item: any) => { const code = isEn ? "time_privacy_en" : "time_privacy_cn" return item.code == code }) if (!resource || resource.length == 0) { Taro.showToast({ title: 'Not found.', icon: 'none' }) return } jumpPage('/pages/common/H5?title=User Agreement&url=' + resource[0].url, 'H5', navigation, { title: resource[0].name, url: resource[0].url }) } function chooseLocation(res: any, method: string) { if (res.lng < -180) { res.lng += 360 } if (res.lng > 180) { res.lng -= 360 } if (res.lat > 90) { res.lat -= 90 } if (res.lat < -90) { res.lat += 90 } setLoading(true) place(res.lat, res.lng, method).then(res => { console.log(res) setCurrent((res as any).location) saveLocation((res as any).location) setLoading(false) }).catch(e => { console.log(e) setLoading(false) }) } async function saveLocation(location: any) { Taro.setStorage({ key: 'lastLocation', data: JSON.stringify(location) }) var locations = await getStorage('locations') var list: any = [] var isFind = false; if (locations) { list = JSON.parse(locations) } list.map(item => { if (item.geo.lat == location.geo.lat && item.geo.lng == location.geo.lng) { isFind = true } }) if (!isFind) { list.push(location) } Taro.setStorage({ key: 'locations', data: JSON.stringify(list) }) } function clearLocation() { Taro.clearStorage() setCurrent(null) } function changeLocation(location) { setCurrent(location) Taro.setStorage({ key: 'lastLocation', data: JSON.stringify(location) }) chooseLocation({ lat: location.geo.lat, lng: location.geo.lng }, 'SWITCH') } function getLocation() { if (current.geo) { if (current.geo.city) { return current.geo.city } if (current.geo.country) { return current.geo.country } return `${Math.abs(parseInt(current.geo.lat))}°${parseInt(current.geo.lat) < 0 ? 'S' : 'N'} ${Math.abs(parseInt(current.geo.lng))}°${parseInt(current.geo.lng) < 0 ? 'W' : 'E'}` } return t('time_of_day.index.unknown') } function getTimezone1() { if (current.time && current.time.timezone) { return current.time.timezone.offset_id + ' · ' + current.time.timezone.name } return '' } const common: RingCommon = { useCase: 'ChooseScenario', radius: 115, lineWidth: 26, isFast: true, status: 'WAIT_FOR_START' } const bgRing: BgRing = { color: '#EAE9E9' } function getArc() { var hour = new Date().getHours() var minute = new Date().getMinutes() var second = new Date().getSeconds() if (current) { var strHour = moment().tz(current.time.timezone.id).format('H') hour = parseInt(strHour) var strMin = moment().tz(current.time.timezone.id).format('m') minute = parseInt(strMin) var strSecond = moment().tz(current.time.timezone.id).format('s') second = parseInt(strSecond) } return (hour * 3600 + minute * 60 + second) / (24 * 3600) * 2 * Math.PI } function ring() { var offset = 0 var hour = new Date().getHours() var minute = new Date().getMinutes() if (current) { var strHour = moment().tz(current.time.timezone.id).format('H') hour = parseInt(strHour) var strMin = moment().tz(current.time.timezone.id).format('m') minute = parseInt(strMin) } if (hour != new Date().getHours() || minute != new Date().getMinutes()) { offset = hour * 60 + minute - new Date().getHours() * 60 - new Date().getMinutes() } const currentDot: CurrentDot = { color: '#CACACA', lineWidth: 10, borderColor: '#fff', offset: offset, whiteIcon: true } const targetRing: TargetRing = { color: '#CACACA', startArc: -Math.PI / 2, durationArc: getArc() } return } function languageModalContent() { return changeLanguage(isEn ? true : false)}> {isEn ? 'English' : '中文'} changeLanguage(isEn ? false : true)}> {!isEn ? 'English' : '中文'} } function formatTime(format: string, timestamp?: number) { // var moment = require('moment-timezone'); if (current) { if (timestamp) { return moment(timestamp).tz(current.time.timezone.id).format(format) } return moment().tz(current.time.timezone.id).format(format) } return dayjs().format(format) } function dstTitle(index: number) { if (index == 0) { return t('time_of_day.index.last_change') } return t('time_of_day.index.next_change') } function dstDesc(item: any, index: number) { if (index == 0) { if (item.new_dst > 0) { return t('time_of_day.index.summer_started') } return t('time_of_day.index.winter_started') } if (item.new_dst > 0) { return t('time_of_day.index.summer_starts') } return t('time_of_day.index.winter_starts') } function isNight() { var hour = new Date().getHours() if (current) { var strHour = moment().tz(current.time.timezone.id).format('H') hour = parseInt(strHour) } if (hour >= 18 || hour < 6) { return true } return false } function dstModalContent() { return { current.time_changes.map((item, index) => { return {formatTime('MMMM', item.timestamp)} {formatTime('D', item.timestamp)} {formatTime('dddd', item.timestamp)} {formatTime('YYYY', item.timestamp)} {item.title} {item.subtitle} { item.descriptions.map((desc, j) => { return · {desc} }) } }) } } function detail() { return { ring() } {formatTime('HH:mm:ss')} {global.language == 'en' ? formatTime('dddd, MMM D') : formatTime('MMMD日 dddd')} { loading && } { loading && {t('time_of_day.index.updating')} } { !loading && } { !loading && {current ? getLocation() : t('time_of_day.index.unknown')} } { if (current && current.time && current.time.timezone.use_dst) { setShowDst(true) } }}> {current ? getTimezone1() : getTimezone()} { current && current.time && current.time.timezone.use_dst && } {!current ? t('time_of_day.index.pick_location') : t('time_of_day.index.change_location')} { current && } {/* {!current ? t('time_of_day.index.pick_location') : t('time_of_day.index.change_location')} { current && } */} {t('time_of_day.index.version')} {t('time_of_day.index.privacy')} setShowLanguage(true)}>{isEn ? 'English' : '中文'} setShowLanguage(true)} /> { showLanguage && { setShowLanguage(false) }} confirm={() => { }}> { languageModalContent() } } { showDst && { setShowDst(false) }} confirm={() => { }}> { dstModalContent() } } } return detail() }