|
@@ -1,4 +1,4 @@
|
|
|
-import { View, Text } from "@tarojs/components";
|
|
|
|
|
|
|
+import { View, Text, Image } from "@tarojs/components";
|
|
|
import './calendar.scss'
|
|
import './calendar.scss'
|
|
|
import dayjs from "dayjs";
|
|
import dayjs from "dayjs";
|
|
|
import { useEffect, useState } from "react";
|
|
import { useEffect, useState } from "react";
|
|
@@ -6,6 +6,8 @@ import { rpxToPx } from "@/utils/tools";
|
|
|
import { streaks } from "@/services/health";
|
|
import { streaks } from "@/services/health";
|
|
|
import { useSelector } from "react-redux";
|
|
import { useSelector } from "react-redux";
|
|
|
import { getThemeColor } from "./hooks/health_hooks";
|
|
import { getThemeColor } from "./hooks/health_hooks";
|
|
|
|
|
+import NewButton, { NewButtonType } from "@/_health/base/new_button";
|
|
|
|
|
+import { MainColorType } from "@/context/themes/color";
|
|
|
|
|
|
|
|
export default function Calendar(props: { year: number, month: number }) {
|
|
export default function Calendar(props: { year: number, month: number }) {
|
|
|
const weeks = ['日', '一', '二', '三', '四', '五', '六']
|
|
const weeks = ['日', '一', '二', '三', '四', '五', '六']
|
|
@@ -13,24 +15,28 @@ export default function Calendar(props: { year: number, month: number }) {
|
|
|
const [spaces, setSpaces] = useState<any>([])
|
|
const [spaces, setSpaces] = useState<any>([])
|
|
|
const [days, setDays] = useState<any>([])
|
|
const [days, setDays] = useState<any>([])
|
|
|
const health = useSelector((state: any) => state.health);
|
|
const health = useSelector((state: any) => state.health);
|
|
|
|
|
+ const [year, setYear] = useState(props.year)
|
|
|
|
|
+ const [month, setMonth] = useState(props.month)
|
|
|
|
|
+ const [current, setCurrent] = useState<any>(null)
|
|
|
|
|
+ const [loaded,setLoaded] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
loadData()
|
|
loadData()
|
|
|
- }, [])
|
|
|
|
|
|
|
+ }, [month])
|
|
|
|
|
|
|
|
function loadData() {
|
|
function loadData() {
|
|
|
|
|
|
|
|
- const firstDay = new Date(props.year, props.month - 1, 1);
|
|
|
|
|
|
|
+ const firstDay = new Date(year, month - 1, 1);
|
|
|
const firstDayOfWeek = firstDay.getDay();
|
|
const firstDayOfWeek = firstDay.getDay();
|
|
|
const spaceCount = firstDayOfWeek > indexBeginWeek ? firstDayOfWeek - indexBeginWeek : firstDayOfWeek - indexBeginWeek + 7
|
|
const spaceCount = firstDayOfWeek > indexBeginWeek ? firstDayOfWeek - indexBeginWeek : firstDayOfWeek - indexBeginWeek + 7
|
|
|
setSpaces(new Array(spaceCount).fill(''))
|
|
setSpaces(new Array(spaceCount).fill(''))
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个 Date 对象来获取该月的最后一天
|
|
// 创建一个 Date 对象来获取该月的最后一天
|
|
|
- const lastDay = new Date(props.year, props.month, 0); // 0 表示上一个月的最后一天
|
|
|
|
|
|
|
+ const lastDay = new Date(year, month, 0); // 0 表示上一个月的最后一天
|
|
|
const totalDays = lastDay.getDate();
|
|
const totalDays = lastDay.getDate();
|
|
|
var list: any = []
|
|
var list: any = []
|
|
|
for (var i = 1; i <= totalDays; i++) {
|
|
for (var i = 1; i <= totalDays; i++) {
|
|
@@ -54,52 +60,83 @@ export default function Calendar(props: { year: number, month: number }) {
|
|
|
|
|
|
|
|
|
|
|
|
|
streaks({
|
|
streaks({
|
|
|
- window:health.mode,
|
|
|
|
|
- month:202409
|
|
|
|
|
- }).then(res=>{
|
|
|
|
|
|
|
+ window: health.mode,
|
|
|
|
|
+ month: year + (month + '').padStart(2, '0')
|
|
|
|
|
+ }).then(res => {
|
|
|
const array = (res as any).streaks
|
|
const array = (res as any).streaks
|
|
|
- for (var i=0;i<list.length;i++){
|
|
|
|
|
|
|
+ for (var i = 0; i < list.length; i++) {
|
|
|
var obj = list[i]
|
|
var obj = list[i]
|
|
|
- var strMonth = props.month<10?'0'+props.month:props.month+''
|
|
|
|
|
- var strDay = obj.day<10?'0'+obj.day:obj.day
|
|
|
|
|
|
|
+ var strMonth = month < 10 ? '0' + month : month + ''
|
|
|
|
|
+ var strDay = obj.day < 10 ? '0' + obj.day : obj.day
|
|
|
|
|
|
|
|
- const d = parseInt(props.year+''+strMonth+strDay)
|
|
|
|
|
- for (var j=0;j<array.length;j++){
|
|
|
|
|
|
|
+ const d = parseInt(year + '' + strMonth + strDay)
|
|
|
|
|
+ for (var j = 0; j < array.length; j++) {
|
|
|
const streak = array[j]
|
|
const streak = array[j]
|
|
|
- if (d==streak.start_date && streak.start_date==streak.end_date){
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (d == streak.start_date && streak.start_date == streak.end_date) {
|
|
|
obj.full = true
|
|
obj.full = true
|
|
|
|
|
+ obj.current = streak.is_current
|
|
|
|
|
+ obj.showStreak = true;
|
|
|
}
|
|
}
|
|
|
- else if (d == streak.start_date && streak.start_date<streak.end_date){
|
|
|
|
|
|
|
+ else if (d == streak.start_date && streak.start_date < streak.end_date) {
|
|
|
obj.begin = true
|
|
obj.begin = true
|
|
|
|
|
+ obj.current = streak.is_current
|
|
|
|
|
+ obj.showStreak = true;
|
|
|
}
|
|
}
|
|
|
- else if (d == streak.end_date && streak.start_date<streak.end_date){
|
|
|
|
|
|
|
+ else if (d == streak.end_date && streak.start_date < streak.end_date) {
|
|
|
obj.right = true
|
|
obj.right = true
|
|
|
|
|
+ obj.current = streak.is_current
|
|
|
|
|
+ obj.showStreak = true;
|
|
|
}
|
|
}
|
|
|
- else if (streak.start_date<d && d<streak.end_date){
|
|
|
|
|
|
|
+ else if (streak.start_date < d && d < streak.end_date) {
|
|
|
obj.center = true
|
|
obj.center = true
|
|
|
|
|
+ obj.current = streak.is_current
|
|
|
|
|
+ obj.showStreak = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ obj.isToday = false
|
|
|
|
|
+ if (year == new Date().getFullYear() && month == new Date().getMonth() + 1 && obj.day == new Date().getDate()) {
|
|
|
|
|
+ obj.isToday = true
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
setDays([...list])
|
|
setDays([...list])
|
|
|
|
|
+
|
|
|
|
|
+ setLoaded(true)
|
|
|
|
|
+ setCurrent((res as any).current)
|
|
|
// console.log(list)
|
|
// console.log(list)
|
|
|
- }).catch(e=>{
|
|
|
|
|
|
|
+ }).catch(e => {
|
|
|
|
|
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function itemClass(item) {
|
|
function itemClass(item) {
|
|
|
if (item.begin) {
|
|
if (item.begin) {
|
|
|
|
|
+ if (item.isToday) {
|
|
|
|
|
+ return 'left_calendar_item today_content'
|
|
|
|
|
+ }
|
|
|
return 'left_calendar_item'
|
|
return 'left_calendar_item'
|
|
|
}
|
|
}
|
|
|
if (item.center) {
|
|
if (item.center) {
|
|
|
|
|
+ if (item.isToday) {
|
|
|
|
|
+ return 'center_calendar_item today_content'
|
|
|
|
|
+ }
|
|
|
return 'center_calendar_item'
|
|
return 'center_calendar_item'
|
|
|
}
|
|
}
|
|
|
if (item.right) {
|
|
if (item.right) {
|
|
|
|
|
+ if (item.isToday) {
|
|
|
|
|
+ return 'right_calendar_item today_content'
|
|
|
|
|
+ }
|
|
|
return 'right_calendar_item'
|
|
return 'right_calendar_item'
|
|
|
}
|
|
}
|
|
|
if (item.full) {
|
|
if (item.full) {
|
|
|
|
|
+ if (item.isToday) {
|
|
|
|
|
+ return 'full_calendar_item today_content'
|
|
|
|
|
+ }
|
|
|
return 'full_calendar_item'
|
|
return 'full_calendar_item'
|
|
|
}
|
|
}
|
|
|
|
|
+ if (item.isToday) {
|
|
|
|
|
+ return 'today1'
|
|
|
|
|
+ }
|
|
|
return 'calendar_item'
|
|
return 'calendar_item'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -119,47 +156,124 @@ export default function Calendar(props: { year: number, month: number }) {
|
|
|
return 'normal_day'
|
|
return 'normal_day'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function bgColor(item){
|
|
|
|
|
- if (itemClass(item)=='calendar_item'){
|
|
|
|
|
|
|
+ function bgColor(item) {
|
|
|
|
|
+ if (item.showStreak && !item.current) {
|
|
|
|
|
+ return '#B2B2B21A'
|
|
|
|
|
+ }
|
|
|
|
|
+ if (itemClass(item) == 'calendar_item') {
|
|
|
return 'transparent'
|
|
return 'transparent'
|
|
|
}
|
|
}
|
|
|
- return getThemeColor(health.mode)+'33'
|
|
|
|
|
|
|
+ if (itemClass(item) == 'today1') {
|
|
|
|
|
+
|
|
|
|
|
+ return '#000'
|
|
|
|
|
+ }
|
|
|
|
|
+ return getThemeColor(health.mode) + '33'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function textColor(item){
|
|
|
|
|
- if (itemTextClass(item)=='normal_day'){
|
|
|
|
|
- return '#808080'
|
|
|
|
|
|
|
+ function textColor(item) {
|
|
|
|
|
+ // if (itemTextClass(item) == 'normal_day') {
|
|
|
|
|
+ // return '#000000'
|
|
|
|
|
+ // }
|
|
|
|
|
+ if (item.isToday) {
|
|
|
|
|
+ if (item.begin || item.full || item.right || item.left) {
|
|
|
|
|
+ return '#000'
|
|
|
|
|
+ }
|
|
|
|
|
+ return '#ffffff'
|
|
|
}
|
|
}
|
|
|
- return getThemeColor(health.mode)
|
|
|
|
|
|
|
+ return '#000000'
|
|
|
|
|
+ // return getThemeColor(health.mode)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return <View className="calendar_main">
|
|
|
|
|
- <View className="calendar_header">{dayjs().format('YYYY年M月')}</View>
|
|
|
|
|
- <View className="calendar_body">
|
|
|
|
|
- <View className="calendar_weekly">
|
|
|
|
|
- {
|
|
|
|
|
- weeks.map((item, index) => {
|
|
|
|
|
- return <View className="week_item" key={index}>{item}</View>
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ function leftSpace(days){
|
|
|
|
|
+ if (days<10){
|
|
|
|
|
+ return rpxToPx(98)
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (days<100){
|
|
|
|
|
+ return rpxToPx(71)
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (days<1000){
|
|
|
|
|
+ return rpxToPx(54)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function content() {
|
|
|
|
|
+ return <View className="calendar_main2">
|
|
|
|
|
+ <View className="calendar_header">
|
|
|
|
|
+
|
|
|
|
|
+ <NewButton type={NewButtonType.img} onClick={() => {
|
|
|
|
|
+ const date = new Date(year, month - 1); // month - 1 因为月份是从 0 开始
|
|
|
|
|
+ date.setMonth(date.getMonth() - 1);
|
|
|
|
|
+ setYear(date.getFullYear())
|
|
|
|
|
+ setMonth(date.getMonth() + 1)
|
|
|
|
|
+ }}>
|
|
|
|
|
+ <Image src={require('@assets/_health/pre.png')} style={{ width: rpxToPx(36), height: rpxToPx(36) }} />
|
|
|
|
|
+ </NewButton>
|
|
|
|
|
+ <Text style={{ width: rpxToPx(236), textAlign: 'center' }}>{`${year}年${month}月`}</Text>
|
|
|
|
|
+
|
|
|
|
|
+ <NewButton type={NewButtonType.img} onClick={() => {
|
|
|
|
|
+ const date = new Date(year, month + 1); // month - 1 因为月份是从 0 开始
|
|
|
|
|
+ date.setMonth(date.getMonth() - 1);
|
|
|
|
|
+ setYear(date.getFullYear())
|
|
|
|
|
+ setMonth(date.getMonth() + 1)
|
|
|
|
|
+ }}>
|
|
|
|
|
+ <Image src={require('@assets/_health/next.png')} style={{ width: rpxToPx(36), height: rpxToPx(36) }} />
|
|
|
|
|
+ </NewButton>
|
|
|
</View>
|
|
</View>
|
|
|
- <View className="calendar_main">
|
|
|
|
|
- {
|
|
|
|
|
- spaces.map((item, i) => {
|
|
|
|
|
- return <View className="calendar_item" style={{ width: rpxToPx(750) / 7 }} key={i * 10} />
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
- {
|
|
|
|
|
- days.map((item, index) => {
|
|
|
|
|
- return <View className={itemClass(item)} style={{ width: rpxToPx(750) / 7,backgroundColor:bgColor(item) }} key={index}>
|
|
|
|
|
- <Text className={itemTextClass(item)} style={{color:textColor(item)}}>{item.day}</Text>
|
|
|
|
|
- </View>
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ <View className="calendar_body">
|
|
|
|
|
+ <View className="calendar_weekly">
|
|
|
|
|
+ {
|
|
|
|
|
+ weeks.map((item, index) => {
|
|
|
|
|
+ return <View className="week_item" key={index}>{item}</View>
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ </View>
|
|
|
|
|
+ <View className="calendar_main3">
|
|
|
|
|
+ {
|
|
|
|
|
+ spaces.map((item, i) => {
|
|
|
|
|
+ return <View className="calendar_item" style={{ width: rpxToPx(90) }} key={i * 10} />
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ {
|
|
|
|
|
+ days.map((item, index) => {
|
|
|
|
|
+ return <View className={itemClass(item)} style={{ width: rpxToPx(90), backgroundColor: bgColor(item) }} key={index}>
|
|
|
|
|
+ <Text className={itemTextClass(item)} style={{ color: textColor(item) }}>{item.day}</Text>
|
|
|
|
|
+ </View>
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ </View>
|
|
|
|
|
+ </View>
|
|
|
|
|
+ <View className="calendar_footer">
|
|
|
|
|
+
|
|
|
</View>
|
|
</View>
|
|
|
</View>
|
|
</View>
|
|
|
- <View className="calendar_footer">
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ function currentContent(){
|
|
|
|
|
+ return <View style={{display:'flex',flexDirection:'row',alignItems:'center',height:rpxToPx(208)}}>
|
|
|
|
|
+ {
|
|
|
|
|
+ loaded && <View style={{display:'flex',flexDirection:'row',alignItems:'center',height:rpxToPx(208)}}>
|
|
|
|
|
+ <View className="streak_days" style={{color:getThemeColor(health.mode),
|
|
|
|
|
+ marginLeft:leftSpace(current?current.days:0),
|
|
|
|
|
+ marginRight:rpxToPx(36)
|
|
|
|
|
+ }}>{current?current.days:'0'}</View>
|
|
|
|
|
+ <View style={{display:'flex',flexDirection:'column',}}>
|
|
|
|
|
+ <View className="h44 bold" style={{color:getThemeColor(health.mode)}}>{current.days==1?'Day':'Days'}</View>
|
|
|
|
|
+ <View className="h24">Log your xxx daily to build a streak</View>
|
|
|
|
|
+ <View className="h20" style={{color:MainColorType.g02,marginTop:rpxToPx(12)}}>Log your xxx daily to build a streak</View>
|
|
|
|
|
+ </View>
|
|
|
|
|
+ </View>
|
|
|
|
|
+ }
|
|
|
</View>
|
|
</View>
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return <View className="calendar_main">
|
|
|
|
|
+ {
|
|
|
|
|
+ currentContent()
|
|
|
|
|
+ }
|
|
|
|
|
+ {
|
|
|
|
|
+ content()
|
|
|
|
|
+ }
|
|
|
</View>
|
|
</View>
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|