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 { loginAnonymous, place } from "./services/net"; import Taro from "@tarojs/taro"; import { useTranslation } from "react-i18next"; import 'dayjs/locale/zh-cn'; import 'dayjs/locale/en'; // import 'moment/locale/en'; 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 { t, i18n } = useTranslation() useEffect(() => { getLanguage(); login() setInterval(() => { setCount(t => t + 1) }, 1000) }, []) 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'); // 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'); // moment.locale(isEnglish ? 'en' : 'zh-cn'); if (current) { chooseLocation({ lat: current.geo.lat, lng: current.geo.lng }) } } function login() { loginAnonymous(DeviceInfo.getDeviceSync()).then(res => { console.log('login success', res) }).catch(e => { console.log('login error', e) }) } function goMap() { jumpPage('', 'map', navigation, { source: 'time_of_day', lat: current ? current.geo.lat : null, lng: current ? current.geo.lng : null, chooseLocation: chooseLocation }) } function goRecords() { if (!current) { return } jumpPage('','MyPlaces',navigation) } function chooseLocation(res: any) { place(res.lat, res.lng).then(res => { console.log(res) setCurrent((res as any).location) saveLocation((res as any).location) }).catch(e => { console.log(e) }) } function saveLocation(location: any) { Taro.setStorage({ key: 'lastLocation', data: JSON.stringify(location) }) } function getLocation() { if (current.geo) { if (current.geo.city) { return current.geo.city } if (current.geo.country) { return current.geo.country } } return t('time_of_day.index.unknown') } function getTimezone() { if (current.time && current.time.timezone) { return current.time.timezone.abbrev + ' · ' + 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: '#ffffff', offset: offset } const targetRing: TargetRing = { color: '#CACACA', startArc: -Math.PI / 2, durationArc: getArc() } return } function languageModalContent() { return changeLanguage(true)}> English { isEn && } changeLanguage(false)}> 中文 { !isEn && } } 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)} {dstTitle(index)} {dstDesc(item, index)} { item.descriptions.map((desc, j) => { return · {desc} }) } }) } } return { ring() } {formatTime('HH:mm:ss')} {global.language == 'en' ? formatTime('dddd, MMM DD') : formatTime('MMMD dddd')} {current ? getLocation() : t('time_of_day.index.unknown')} { if (current && current.time && current.time.timezone.use_dst) { setShowDst(true) } }}> {current ? getTimezone() : 'UTC+8'} { current && current.time && current.time.timezone.use_dst && } {t('time_of_day.index.pick_location')} { current && } setShowLanguage(true)}> {isEn ? 'English' : '中文'} { showLanguage && { setShowLanguage(false) }} confirm={() => { }}> { languageModalContent() } } { showDst && { setShowDst(false) }} confirm={() => { }}> { dstModalContent() } } }