import { View, Text, ScrollView, PageContainer } from "@tarojs/components"; import './Metric.scss' import { useDispatch, useSelector } from "react-redux"; import { useEffect, useRef, useState } from "react"; import Taro from "@tarojs/taro"; import { metricCards, metricFollows, metricGroups, uploadMetric, uploadSteps } from "@/services/trackSomething"; import { TimeFormatter } from "@/utils/time_format"; import MetricItem from "./MetricItem"; import Modal from "@/components/layout/Modal"; import LimitPickers from "@/components/input/LimitPickers"; import SlidngScale from "@/components/input/SlidngScale"; import NoData from "@/components/view/NoData"; import { alphaToHex } from "@/utils/tools"; import Layout from "@/components/layout/layout"; import { MetricModalType, ModalType, NaviBarTitleShowType, TemplateType } from "@/utils/types"; import { useTranslation } from "react-i18next"; import { jumpPage } from "@/features/trackTimeDuration/hooks/Common"; import TitleView from "@/features/trackTimeDuration/components/TitleView"; import { setTabbarStatus } from "@/store/common"; import MetricModalChoose from "./MetricModalChoose"; import MetricModalOrder from './MetricModalOrder'; let useNavigation; if (process.env.TARO_ENV == 'rn') { useNavigation = require("@react-navigation/native").useNavigation } export default function Component(props: any) { const { t } = useTranslation() const user = useSelector((state: any) => state.user); const [list, setList] = useState([]) const [isModalOpen, setIsModalOpen] = useState(false); const [isTimePickerOpen, setIsTimePickerOpen] = useState(false); const [pickerValue, setPickerValue] = useState([]); const [pickerItems, setPickerItems] = useState([]); const [isPoint, setIsPoint] = useState(false) const [metricItem, setMetricItem] = useState({}) const [strTime, setStrTime] = useState('') const [time, setTime] = useState(0) const [showErrorPage, setErrorPage] = useState(false) const [setupTime, setSetupTime] = useState(0) const [triggered, setTriggered] = useState(true) const limitPickerRef = useRef(null); const [count, setCount] = useState(0) const [showPageContainer, setShowPageContainer] = useState(false) const [modalType, setModalType] = useState(MetricModalType.none) const [themeColor, setThemeColor] = useState('#00ffff') const [groups, setGroups] = useState([]) const [limits, setLimits] = useState(null) const [orders, setOrders] = useState([]) const dispatch = useDispatch(); let navigation; if (useNavigation) { navigation = useNavigation() } //未登录<->已登录 状态切换时,执行一次授权检查 useEffect(() => { getCards(); getGroups(); const now = new Date(); const nextMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 0, 0); const timeUntilMidnight = nextMidnight.getTime() - now.getTime(); setTimeout(() => { setCount(count + 1) }, timeUntilMidnight); }, [user.isLogin]) const openModal = () => { setSetupTime(new Date().getTime()) setIsModalOpen(true); }; const closeModal = () => { setIsModalOpen(false); }; global.refreshMetric = () => { getCards() } function getCards() { setTriggered(true) metricCards().then(res => { Taro.stopPullDownRefresh() setErrorPage(false) setList((res as any).cards) setTriggered(false) if ((res as any).cards.length > 0) { var obj = (res as any).cards[0] setThemeColor(obj.theme_color) } var list: any = []; (res as any).cards.map(item => { list.push({ name: item.name, code: item.code }) }) setOrders(list) }).catch(e => { Taro.stopPullDownRefresh() if (list.length == 0) { setErrorPage(true) } setTriggered(false) }) } function getGroups() { metricGroups().then(res => { setGroups((res as any).groups) setLimits((res as any).num_options_limit) }) } //ts 把数组items: [{code: "_walk", value: 2180},{code: "_walk", value: 4444}]中的value取出来,/分割,组成字符串,如2180/4444 function getValues(items) { var values = '' items.map((item, index) => { if (index == 0) { values = item.value } else { values = values + '/' + item.value } }) return values } function goDetail(item) { if (user.isLogin) { if (!item.latest_record) { return } jumpPage('/pages/common/RecordsHistory?type=metric&refreshList=getCards&code=' + item.code + `&title=${item.name}` + '&themeColor=' + item.theme_color) } else { jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation) } } function record(item: any) { if (user.isLogin) { var now = new Date(); var t = (now.getHours() < 10 ? '0' + now.getHours() : now.getHours()) + ":" + (now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes()); // setStrTime(t) setStrTime(TimeFormatter.dateTimeFormate(now.getTime())) setTime(now.getTime()) setMetricItem(item) openModal() } else { jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation) } } function showTimePicker() { setIsTimePickerOpen(true) } function chooseTime(e) { setTime(e); setStrTime(TimeFormatter.dateTimeFormate(e)) setIsTimePickerOpen(false) } function cancelModal() { (metricItem as any).schemas.map((item, index) => { item.tempValue = '' }) closeModal() } function confirmModal() { closeModal() var date = new Date(time) var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); var array: any[] = []; (metricItem as any).schemas.map((item, index) => { array.push({ code: item.code, value: item.tempValue && item.tempValue > 0 ? item.tempValue : item.default_value }) }) var params = { code: (metricItem as any).code, timestamp: time, date: strDate, items: array, extra: { set_time: setupTime, confirm_time: new Date().getTime() } } global.postBtnUpdateStatus('normal'); uploadMetric(params).then(res => { // getCards(); (metricItem as any).schemas.map((item, index) => { item.tempValue = '' }) list.map((item, index) => { if ((item as any).code == (res as any).code) { (item as any).latest_record = (res as any).latest_record; (item as any).schemas = (res as any).schemas; } }) setList(JSON.parse(JSON.stringify(list))) global.postBtnUpdateStatus('idle'); setTimeout(() => { setCount(count + 1) }, 31000) }) } const limitDay = 500 const limit = new Date().getTime() - limitDay * 3600 * 1000 * 24; function detail() { return { list.map((item: any, index: number) => { var unit = '' var value = t('feature.track_something.metric.no_record') var desc = t('feature.track_something.metric.check_unlock_data') var showDetail = false; if (item.latest_record) { unit = item.schemas[0].default_unit value = getValues(item.latest_record.items) desc = TimeFormatter.dateDescription(item.latest_record.timestamp, true) showDetail = true } if (!user.isLogin) { value = t('feature.track_something.metric.un_login') desc = t('feature.track_something.metric.login_can_check') } return { goDetail(item) }} onClick={() => { record(item) }} /> }) } } function detailContent() { // if (process.env.TARO_ENV=='rn'){ // return // { // detail() // } // // // } return detail() } function addBtnClick() { Taro.showActionSheet({ itemList: [t('feature.track_something.metric.choose_metric'), t('feature.track_something.metric.order')] }) .then(res => { switch (res.tapIndex) { case 0: global.metricAdd = true setModalType(MetricModalType.choose) dispatch(setTabbarStatus(false)) setShowPageContainer(true) break; case 1: global.metricAdd = false setModalType(MetricModalType.order) dispatch(setTabbarStatus(false)) setShowPageContainer(true) break; } }) .catch(err => { console.log(err.errMsg) }) } function headerView() { return } function metricModalContent() { switch (modalType) { case MetricModalType.choose: return case MetricModalType.order: return } return } function modalCancel() { setShowPageContainer(false) } function modalConfirm(datas) { var array: any = [] datas.map(item => { array.push(item.code) }) metricFollows({ codes: array }).then(res => { getCards() getGroups() }) setShowPageContainer(false) setTimeout(() => { dispatch(setTabbarStatus(false)) setModalType(MetricModalType.order) setShowPageContainer(true) }, 500) } function modalOrderCancel() { global.metricAdd ? modalCancel() : modalCancel() if (global.metricAdd) { setShowPageContainer(false) setTimeout(() => { dispatch(setTabbarStatus(false)) setModalType(MetricModalType.choose) setShowPageContainer(true) }, 500) } else { setShowPageContainer(false) } } function modalOrderConfirm() { setShowPageContainer(false) } return { getCards() }} /> : detailContent()} title={t('page.metric.title')} type={TemplateType.customHeader} header={headerView()} refresh={() => { getCards() }} triggered={triggered} titleShowStyle={NaviBarTitleShowType.scrollToShow} /> { dispatch(setTabbarStatus(true)) }} onAfterLeave={() => { setShowPageContainer(false); }}> { metricModalContent() } { isModalOpen && {(metricItem as any).name ? (metricItem as any).name : ''} { (metricItem as any).schemas.map((item, index) => { return { (metricItem as any).schemas.length > 1 && {item.name} } { item.tempValue = e }} /> }) } {strTime} { t('feature.common.picker_cancel_btn') } { t('feature.common.picker_confirm_btn') } {/* 取消 确认 */} } { isTimePickerOpen && setIsTimePickerOpen(false)} confirm={() => { var picker = limitPickerRef.current; chooseTime((picker as any).getConfirmData()); setIsTimePickerOpen(false); }}> { setIsTimePickerOpen(false) }} onChange={(e) => { chooseTime(e) // pickerConfirm(e) // hidePicker() }} /> } }