|
|
@@ -0,0 +1,280 @@
|
|
|
+import { View } from "@tarojs/components";
|
|
|
+import './day_night.scss'
|
|
|
+import { useSelector } from "react-redux";
|
|
|
+import Taro from "@tarojs/taro";
|
|
|
+import { rpxToPx } from "@/utils/tools";
|
|
|
+import { IconClose } from "@/components/basic/Icons";
|
|
|
+import SunsetCanvas from "./sunset-canvas";
|
|
|
+import { useEffect, useState } from "react";
|
|
|
+import NewButton, { NewButtonType } from "@/_health/base/new_button";
|
|
|
+import { MainColorType } from "@/context/themes/color";
|
|
|
+import { TimeFormatter } from "@/utils/time_format";
|
|
|
+import { systemLocation } from "@/services/common";
|
|
|
+import { useTranslation } from "react-i18next";
|
|
|
+import dayjs from "dayjs";
|
|
|
+import { clearLocation } from "@/services/user";
|
|
|
+
|
|
|
+export default function DayNight(props: { onClose: any, data: any }) {
|
|
|
+ const record = useSelector((state: any) => state.record);
|
|
|
+ const user = useSelector((state: any) => state.user);
|
|
|
+ const systemInfo: any = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync();
|
|
|
+ const navigationBarHeight = systemInfo.statusBarHeight + 44;
|
|
|
+ const screenHeight = systemInfo.screenHeight
|
|
|
+ const safeAreaInsets = systemInfo.safeArea; // 获取安全区域信息
|
|
|
+
|
|
|
+ const [info, setInfo] = useState(props.data)
|
|
|
+ const [events, setEvents] = useState<any>(null)
|
|
|
+ const { t } = useTranslation()
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ setInfo(props.data)
|
|
|
+ if (props.data.events) {
|
|
|
+ var array = props.data.events.filter((item) => {
|
|
|
+ return item.sub_type != 'SUN_SOLAR_MIDNIGHT' && item.sub_type != 'SUN_SOLAR_NOON'
|
|
|
+ })
|
|
|
+ setEvents(array)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ setEvents([])
|
|
|
+ }
|
|
|
+
|
|
|
+ }, [props.data])
|
|
|
+
|
|
|
+ function getBackground() {
|
|
|
+ var time = record.time
|
|
|
+ if (!time) return '#fff'
|
|
|
+ const { background_colors } = time
|
|
|
+ if (!background_colors) {
|
|
|
+ return '#fff'
|
|
|
+ }
|
|
|
+ else if (background_colors.length == 1) {
|
|
|
+ return background_colors[0]
|
|
|
+ }
|
|
|
+ return `linear-gradient(to bottom, ${background_colors[0]}, ${background_colors[1]})`
|
|
|
+ }
|
|
|
+
|
|
|
+ function chooseLocation() {
|
|
|
+ Taro.chooseLocation({
|
|
|
+ latitude: info.choose_location ? parseFloat(info.location.lat + '') : undefined,
|
|
|
+ longitude: info.choose_location ? parseFloat(info.location.lng + '') : undefined,
|
|
|
+ success: function (res) {
|
|
|
+ console.log('choose location,', res)
|
|
|
+ uploadLocation(res)
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ Taro.showToast({
|
|
|
+ title: '位置修改失败!\n请重新选择就近位置',
|
|
|
+ icon: 'none'
|
|
|
+ })
|
|
|
+ },
|
|
|
+ complete(res) {
|
|
|
+
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ function uploadLocation(res) {
|
|
|
+ var today = new Date()
|
|
|
+ var yesterday = new Date(today.getTime() - 24 * 3600 * 1000)
|
|
|
+ var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000 * 5)
|
|
|
+ var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}`
|
|
|
+ var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
|
|
|
+ Taro.showLoading({
|
|
|
+ title: global.language == 'en' ? 'Loading' : '加载中'
|
|
|
+ })
|
|
|
+ systemLocation({
|
|
|
+ lat: res.latitude,
|
|
|
+ lng: res.longitude,
|
|
|
+ name: res.name,
|
|
|
+ address: res.address,
|
|
|
+ date_start: strYesterday,
|
|
|
+ date_end: strTomorrow,
|
|
|
+ coordinate_system_standard: process.env.TARO_ENV == 'weapp' ? 'GCJ-02' : 'WGS-84'
|
|
|
+ }).then(data => {
|
|
|
+ // global.refreshWindow()
|
|
|
+ Taro.eventCenter.trigger('refreshClockIndex')
|
|
|
+ Taro.hideLoading()
|
|
|
+ }).catch((e) => {
|
|
|
+ Taro.hideLoading()
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ function tapClearLocation() {
|
|
|
+ Taro.showModal({
|
|
|
+ title: t('feature.location.clear_alert_title'),
|
|
|
+ content: t('feature.location.clear_alert_content'),
|
|
|
+ success: function (res) {
|
|
|
+ if (res.confirm) {
|
|
|
+ clearLocation().then(res => {
|
|
|
+ Taro.eventCenter.trigger('refreshClockIndex')
|
|
|
+ })
|
|
|
+ } else if (res.cancel) {
|
|
|
+ console.log('用户点击取消')
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ function currentStatus() {
|
|
|
+ var now = new Date().getTime()
|
|
|
+ for (var i = 0; i < events.length - 1; i++) {
|
|
|
+ if (events[i].time.start_timestamp <= now && events[i + 1].time.start_timestamp > now) {
|
|
|
+ if (events[i].sub_type == 'SUN_SET') {
|
|
|
+ return '夜晚'
|
|
|
+ }
|
|
|
+ return '白天'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ''
|
|
|
+ }
|
|
|
+
|
|
|
+ function durationView() {
|
|
|
+ var start: any;
|
|
|
+ var end: any;
|
|
|
+ var now = new Date().getTime()
|
|
|
+ for (var i = 0; i < events.length - 1; i++) {
|
|
|
+ if (events[i].time.start_timestamp <= now && events[i + 1].time.start_timestamp > now) {
|
|
|
+ if (events[i].sub_type == 'SUN_SET') {
|
|
|
+ start = events[i]
|
|
|
+ }
|
|
|
+ end = events[i + 1]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var str1 = dayjs(start.time.start_timestamp).format('MM-DD HH:mm')
|
|
|
+ var str2 = dayjs(end.time.start_timestamp).format('MM-DD HH:mm')
|
|
|
+ if (TimeFormatter.isToday(start.time.start_timestamp)) {
|
|
|
+ str1 = dayjs(start.time.start_timestamp).format('HH:mm')
|
|
|
+ }
|
|
|
+ if (TimeFormatter.isToday(end.time.start_timestamp)) {
|
|
|
+ str2 = dayjs(end.time.start_timestamp).format('HH:mm')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TimeFormatter.isYesterday(start.time.start_timestamp)) {
|
|
|
+ str1 = (global.language == 'en' ? 'Yesterday ' : '昨天 ') + dayjs(start.time.start_timestamp).format('HH:mm')
|
|
|
+ }
|
|
|
+ if (TimeFormatter.isYesterday(end.time.start_timestamp)) {
|
|
|
+ str2 = (global.language == 'en' ? 'Yesterday ' : '昨天 ') + dayjs(end.time.start_timestamp).format('HH:mm')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TimeFormatter.isTomorrowday(start.time.start_timestamp)) {
|
|
|
+ str1 = (global.language == 'en' ? 'Tomorrow ' : '明天 ') + dayjs(start.time.start_timestamp).format('HH:mm')
|
|
|
+ }
|
|
|
+ if (TimeFormatter.isTomorrowday(end.time.start_timestamp)) {
|
|
|
+ str2 = (global.language == 'en' ? 'Tomorrow ' : '明天 ') + dayjs(end.time.start_timestamp).format('HH:mm')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TimeFormatter.isToday(start.time.start_timestamp)) {
|
|
|
+ str1 = dayjs(start.time.start_timestamp).format('HH:mm')
|
|
|
+ }
|
|
|
+ if (TimeFormatter.isToday(end.time.start_timestamp)) {
|
|
|
+ str2 = dayjs(end.time.start_timestamp).format('HH:mm')
|
|
|
+ }
|
|
|
+
|
|
|
+ return <View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', marginTop: rpxToPx(12), marginBottom: rpxToPx(52) }}>
|
|
|
+ {
|
|
|
+ info.is_polar_night && <View className="polar_tag">极夜</View>
|
|
|
+ }
|
|
|
+ {
|
|
|
+ info.is_polar_day && <View className="polar_tag">极昼</View>
|
|
|
+ }
|
|
|
+ <View className="h36 g01">{str1} - {str2}</View>
|
|
|
+ </View>
|
|
|
+ }
|
|
|
+
|
|
|
+ function pointCard() {
|
|
|
+ var start: any;
|
|
|
+ var end: any;
|
|
|
+ var now = new Date().getTime()
|
|
|
+ for (var i = 0; i < events.length - 1; i++) {
|
|
|
+ if (events[i].time.start_timestamp <= now && events[i + 1].time.start_timestamp > now) {
|
|
|
+ if (events[i].sub_type == 'SUN_SET') {
|
|
|
+ start = events[i]
|
|
|
+ }
|
|
|
+ end = events[i + 1]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return <View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
|
|
|
+ <View className="location_card" style={{ marginRight: rpxToPx(30) }}>
|
|
|
+ <View className="h36 bold">{start.title}</View>
|
|
|
+ <View className="h36 g01" style={{ marginTop: rpxToPx(8) }}>{dayjs(start.time.start_timestamp).format('HH:mm')}</View>
|
|
|
+ </View>
|
|
|
+ <View className="location_card">
|
|
|
+ <View className="h36 bold">{end.title}</View>
|
|
|
+ <View className="h36 g01" style={{ marginTop: rpxToPx(8) }}>{dayjs(end.time.start_timestamp).format('HH:mm')}</View>
|
|
|
+ </View>
|
|
|
+ </View>
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!events) return <View />
|
|
|
+
|
|
|
+
|
|
|
+ return <View className="day_night_bg">
|
|
|
+ <View className="day_night_content" style={{ background: getBackground() }}>
|
|
|
+ <View style={{ height: navigationBarHeight }}>
|
|
|
+ <View style={{
|
|
|
+ position: 'absolute',
|
|
|
+ left: 0,
|
|
|
+ right: 0,
|
|
|
+ bottom: 0,
|
|
|
+ height: 44,
|
|
|
+ display: 'flex',
|
|
|
+ alignItems: 'center',
|
|
|
+ justifyContent: 'center',
|
|
|
+ zIndex: 200
|
|
|
+ }}>
|
|
|
+ <View style={{
|
|
|
+ position: 'absolute',
|
|
|
+ width: rpxToPx(92),
|
|
|
+ height: rpxToPx(64),
|
|
|
+ left: 22,
|
|
|
+ top: 22 - rpxToPx(32)
|
|
|
+ }}
|
|
|
+ onClick={() => {
|
|
|
+ props.onClose()
|
|
|
+ }}>
|
|
|
+ <IconClose color={"#000"} width={rpxToPx(44)} height={rpxToPx(44)} />
|
|
|
+ </View>
|
|
|
+ <View className="h34 bold">{global.language == 'en' ? dayjs().format('ddd MMM D, YYYY') : dayjs().format('YYYY年MMMD日 ddd')}</View>
|
|
|
+ </View>
|
|
|
+ </View>
|
|
|
+ <View style={{ width: rpxToPx(750), display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
|
|
|
+ {
|
|
|
+ !info.choose_location && <View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
|
|
|
+ <View className="h50 bold" style={{ marginTop: rpxToPx(52), marginBottom: rpxToPx(12) }}>{new Date().getHours() >= 6 && new Date().getHours() < 18 ? t('health.sunrise_to_sunset') : t('health.sunset_to_sunrise')}</View>
|
|
|
+ <View className="h26" style={{ marginBottom: rpxToPx(52) }}>{new Date().getHours() >= 6 && new Date().getHours() < 18 ? t('health.rise_to_set_desc') : t('health.set_to_rise_desc')}</View>
|
|
|
+ <NewButton
|
|
|
+ type={NewButtonType.fill}
|
|
|
+ width={rpxToPx(374)}
|
|
|
+ height={rpxToPx(96)}
|
|
|
+ color={MainColorType.orange}
|
|
|
+ title="选择位置"
|
|
|
+ onClick={() => {
|
|
|
+ chooseLocation()
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ </View>
|
|
|
+ }
|
|
|
+ {
|
|
|
+ info.choose_location && <View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
|
|
|
+ <View className="h50 bold" style={{ marginTop: rpxToPx(52) }}>{currentStatus()}</View>
|
|
|
+ {
|
|
|
+ durationView()
|
|
|
+ }
|
|
|
+ {
|
|
|
+ pointCard()
|
|
|
+ }
|
|
|
+ <View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'center', marginTop: rpxToPx(26), position: 'relative', width: rpxToPx(750) }}>
|
|
|
+ <View className="h26">{info.location.name}</View>
|
|
|
+ <View className="h26" style={{ color: MainColorType.link, marginLeft: rpxToPx(12) }} onClick={chooseLocation}>更改位置</View>
|
|
|
+ {
|
|
|
+ user.test_user && <View style={{ position: 'absolute', right: 50 }} onClick={tapClearLocation}>清除位置</View>
|
|
|
+ }
|
|
|
+ </View>
|
|
|
+ </View>
|
|
|
+ }
|
|
|
+ </View>
|
|
|
+ {/* <SunsetCanvas sunset="16:20" sunrise="07:41" width={600} height={300} /> */}
|
|
|
+ </View>
|
|
|
+
|
|
|
+ </View>
|
|
|
+}
|