|
|
@@ -3,8 +3,26 @@ import { View, Text } from "@tarojs/components";
|
|
|
import './CheckAccess.scss'
|
|
|
|
|
|
import { useContext, useEffect, useState } from "react";
|
|
|
+import { TimeFormatter } from "@/utils/time_format";
|
|
|
+import { useDispatch, useSelector } from "react-redux";
|
|
|
+import { getPlans, setPlan } from "@/services/trackTimeDuration";
|
|
|
+import { chooseMode, initTarget, updateStep } from "@/store/set_target";
|
|
|
+import { jumpPage } from "../hooks/Common";
|
|
|
+import { useDidShow } from "@tarojs/taro";
|
|
|
+import Taro from "@tarojs/taro";
|
|
|
|
|
|
-export default function CheckAccess() {
|
|
|
+let confirmAction: any = null;
|
|
|
+let cancelAction: any = null;
|
|
|
+let timer;
|
|
|
+let access;
|
|
|
+let useNavigation, AppState;
|
|
|
+if (process.env.TARO_ENV == 'rn') {
|
|
|
+ AppState = require("react-native").AppState
|
|
|
+ useNavigation = require("@react-navigation/native").useNavigation
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+export default function CheckAccess(props: { record: any, count: number }) {
|
|
|
const [showFastAlert, setShowFastAlert] = useState(false)
|
|
|
const [alertTitle, setAlertTitle] = useState('')
|
|
|
const [alertContent, setAlertContent] = useState('')
|
|
|
@@ -12,11 +30,81 @@ export default function CheckAccess() {
|
|
|
const [cancelText, setCancelText] = useState('')
|
|
|
const [confirmText, setConfirmText] = useState('')
|
|
|
|
|
|
+ //判断选择混合场景,如果成功了,就弹窗
|
|
|
+ const [checkChooseMixed, setCheckChooseMixed] = useState(false)
|
|
|
+ const [chooseMixedSuccess, setChooseMixedSuccess] = useState(false)
|
|
|
+
|
|
|
+ const ring = useSelector((state: any) => state.ring);
|
|
|
+ const dispatch = useDispatch();
|
|
|
+
|
|
|
+ let navigation;
|
|
|
+ if (useNavigation) {
|
|
|
+ navigation = useNavigation()
|
|
|
+ }
|
|
|
+
|
|
|
+ // const [confirmAction, setConfirmAction] = useState<any>(null)
|
|
|
+ // const [access, setAccess] = useState<any>(null)
|
|
|
|
|
|
useEffect(() => {
|
|
|
+ var obj = props.record
|
|
|
+ if (obj.access) {
|
|
|
+ const currentStatus = obj.access.current.qualification.status
|
|
|
+ const preStatus = obj.access.previous.qualification.status
|
|
|
+ if (preStatus == 'PROVISIONAL_QUALIFIED' && currentStatus == 'NOT_QUALIFIED' && obj.current_record.status == 'WAIT_FOR_START') {
|
|
|
+ lose()
|
|
|
+ }
|
|
|
+ else if (currentStatus == 'PROVISIONAL_QUALIFIED' && new Date().getTime() > obj.access.current.qualification.expire) {
|
|
|
+ if (global.indexPageRefresh) {
|
|
|
+ global.indexPageRefresh()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, [props.count])
|
|
|
+
|
|
|
+ // const handleAppStateChange = (nextAppState) => {
|
|
|
+ // console.log(nextAppState)
|
|
|
+ // if (nextAppState != 'active') {
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // };
|
|
|
+
|
|
|
+ function viewWillAppear() {
|
|
|
+ if (checkChooseMixed && chooseMixedSuccess) {
|
|
|
+ upgradeSuccess()
|
|
|
+ }
|
|
|
+ setCheckChooseMixed(false)
|
|
|
+ setChooseMixedSuccess(false)
|
|
|
+ }
|
|
|
+
|
|
|
+ useDidShow(() => {
|
|
|
+ console.log('user did show');
|
|
|
+ viewWillAppear()
|
|
|
+ })
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (process.env.TARO_ENV == 'rn') {
|
|
|
+ // AppState.addEventListener('change', handleAppStateChange);
|
|
|
+ navigation.addListener('focus', () => {
|
|
|
+ viewWillAppear()
|
|
|
+ debugger
|
|
|
+ });
|
|
|
+
|
|
|
+ // 当页面即将消失时执行
|
|
|
+ navigation.addListener('blur', () => {
|
|
|
+ debugger
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ global.chooseMixed = () => {
|
|
|
+ setChooseMixedSuccess(true)
|
|
|
+ }
|
|
|
global.checkAccess = (acccessData: any) => {
|
|
|
const currentStatus = acccessData.current.qualification.status
|
|
|
const preStatus = acccessData.previous.qualification.status
|
|
|
+ access = acccessData
|
|
|
+ // setAccess(acccessData)
|
|
|
|
|
|
if (preStatus == 'NOT_QUALIFIED' && currentStatus == 'NOT_QUALIFIED') {
|
|
|
const { streak_fast_current } = acccessData.current.qualification.condition
|
|
|
@@ -27,96 +115,224 @@ export default function CheckAccess() {
|
|
|
Two more days until unlocking 'Fasting with sleep.'`)
|
|
|
setShowCancel(false)
|
|
|
setConfirmText('Got it!')
|
|
|
-
|
|
|
- // showAlert({
|
|
|
- // title: 'Well Done',
|
|
|
- // content: `You're on a ${streak_fast_current}-day streak.
|
|
|
- // Two more days until unlocking 'Fasting with sleep.'`,
|
|
|
- // showCancel: false,
|
|
|
- // confirmText: 'Got it!',
|
|
|
- // })
|
|
|
- return;
|
|
|
}
|
|
|
else if (preStatus == 'NOT_QUALIFIED' && currentStatus == 'PROVISIONAL_QUALIFIED') {
|
|
|
+ const { streak_fast_current } = acccessData.current.qualification.condition
|
|
|
|
|
|
+ Taro.setStorage({ key: 'showLoseGain', data: false })
|
|
|
+ const { expire } = acccessData.current.qualification
|
|
|
setShowFastAlert(true)
|
|
|
setAlertTitle('Premium Access Unlocked')
|
|
|
- setAlertContent(`You've unlocked 'Fasting with Sleep' since you're on a x-day streak.`)
|
|
|
+ setAlertContent(`You've unlocked 'Fasting with Sleep' since you're on a ${streak_fast_current}-day streak.`)
|
|
|
setShowCancel(true)
|
|
|
setCancelText('Stay with Fasting Only')
|
|
|
- setConfirmText('Upgrade to Fasting with Sleep\n(Offer Expiring in 21:59:59)')
|
|
|
-
|
|
|
- // showAlert({
|
|
|
- // title: 'Premium Access Unlocked',
|
|
|
- // content: `You've unlocked 'Fasting with Sleep' since you're on a x-day streak.
|
|
|
- // Are you ready to up the game?`,
|
|
|
- // showCancel: true,
|
|
|
- // cancelText: 'Stay',
|
|
|
- // confirmText: 'Upgrade',
|
|
|
- // confirm: upgrade
|
|
|
- // })
|
|
|
- return;
|
|
|
+ setConfirmText(`Upgrade to Fasting with Sleep\n(Offer Expiring in ${TimeFormatter.countdown(expire)})`)
|
|
|
+ confirmAction = upgrade
|
|
|
+ timer = setInterval(() => {
|
|
|
+ if (new Date().getTime() > expire) {
|
|
|
+ cancel()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ setConfirmText(`Upgrade to Fasting with Sleep\n(Offer Expiring in ${TimeFormatter.countdown(expire)})`)
|
|
|
+ }, 1000)
|
|
|
}
|
|
|
else if (preStatus == 'PROVISIONAL_QUALIFIED' && currentStatus == 'PROVISIONAL_QUALIFIED') {
|
|
|
const { streak_fast_current, streak_sleep_current } = acccessData.current.qualification.condition
|
|
|
|
|
|
+ if (ring.current_record.scenario == 'FAST') {
|
|
|
+ const { expire } = acccessData.current.qualification
|
|
|
+ setShowFastAlert(true)
|
|
|
+ setAlertTitle('Premium Access Unlocked')
|
|
|
+ setAlertContent(`You've unlocked 'Fasting with Sleep' since you're on a ${streak_fast_current}-day streak.
|
|
|
+ \nAre you ready to up the game?`)
|
|
|
+ setShowCancel(true)
|
|
|
+ setConfirmText(`Upgrade to Fasting with Sleep
|
|
|
+ (Offer Expiring in ${TimeFormatter.countdown(expire)})`)
|
|
|
+ setCancelText('Stay with Fasting Only')
|
|
|
+ confirmAction = upgrade
|
|
|
+ timer = setInterval(() => {
|
|
|
+ if (new Date().getTime() > expire) {
|
|
|
+ cancel()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ setConfirmText(`Upgrade to Fasting with Sleep\n(Offer Expiring in ${TimeFormatter.countdown(expire)})`)
|
|
|
+ }, 1000)
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
setShowFastAlert(true)
|
|
|
setAlertTitle('Well Done')
|
|
|
setAlertContent(`You're on a ${streak_fast_current}-day fasting streak${streak_sleep_current > 0 ? ' and a ' + streak_sleep_current + '-day sleep streak' : ''}.\nKeep 'em going to stay in the game.'`)
|
|
|
setShowCancel(false)
|
|
|
setConfirmText('Got it!')
|
|
|
- // showAlert({
|
|
|
- // title: 'Well Done',
|
|
|
- // content: `You're on a ${streak_fast_current}-day fasting streak${streak_sleep_current > 0 ? ' and a ' + streak_sleep_current + '-day sleep streak' : ''}.
|
|
|
- // Keep 'em going to stay in the game.'`,
|
|
|
- // showCancel: false,
|
|
|
- // confirmText: 'Got it!',
|
|
|
- // })
|
|
|
- return;
|
|
|
}
|
|
|
else if (preStatus == 'PROVISIONAL_QUALIFIED' && currentStatus == 'NOT_QUALIFIED') {
|
|
|
- setShowFastAlert(true)
|
|
|
- setAlertTitle('Premium Access Lost')
|
|
|
- setAlertContent(`You've lost premium access to the 'Fasting with Sleep' feature
|
|
|
- since you lost your fasting streak. To regain access, become a PRO or achieve a new 3-day fasting streak.`)
|
|
|
- setShowCancel(true)
|
|
|
- setCancelText('Achieve')
|
|
|
- setConfirmText('Become')
|
|
|
- // showAlert({
|
|
|
- // title: 'Premium Access Lost',
|
|
|
- // content: `You've lost premium access to the 'Fasting with Sleep' feature
|
|
|
- // since you lost your fasting streak. To regain access, become a PRO or achieve a new 3-day fasting streak.`,
|
|
|
- // showCancel: true,
|
|
|
- // cancelText:'Achieve',
|
|
|
- // confirmText: 'Become',
|
|
|
- // })
|
|
|
- return;
|
|
|
+ lose();
|
|
|
+ // setShowFastAlert(true)
|
|
|
+ // setAlertTitle('Premium Access Lost')
|
|
|
+ // setAlertContent(`You've lost premium access to the 'Fasting with Sleep' feature
|
|
|
+ // since you lost your fasting streak. To regain access, become a PRO or achieve a new 3-day fasting streak.`)
|
|
|
+ // setShowCancel(true)
|
|
|
+ // setCancelText('Achieve New Fasting Streak')
|
|
|
+ // setConfirmText('Become a PRO member')
|
|
|
}
|
|
|
}
|
|
|
}, [])
|
|
|
|
|
|
+ function upgradeSuccess() {
|
|
|
+ const { expire } = access.current.qualification
|
|
|
+ setShowFastAlert(true)
|
|
|
+ setAlertTitle('Congrats on Your PRO Status!')
|
|
|
+ setAlertContent(`To preserve this status, continue your fasting streak in ${TimeFormatter.countdown(expire)} and begin your sleep streak. Remember to keep both of them going.`)
|
|
|
+ setShowCancel(false)
|
|
|
+ setConfirmText(`I got this!`)
|
|
|
+ timer = setInterval(() => {
|
|
|
+
|
|
|
+ if (new Date().getTime() > expire) {
|
|
|
+ debugger
|
|
|
+ cancel()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ setAlertContent(`To preserve this status, continue your fasting streak in ${TimeFormatter.countdown(expire)} and begin your sleep streak. Remember to keep both of them going.`)
|
|
|
+ }, 1000)
|
|
|
+ }
|
|
|
+
|
|
|
+ async function lose() {
|
|
|
+ var showDayRing = await getStorage('showLoseGain') || false;
|
|
|
+ if (showDayRing) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ setShowFastAlert(true)
|
|
|
+ setAlertTitle('Premium Access Lost')
|
|
|
+ setAlertContent(`You've lost premium access to the 'Fasting with Sleep' feature
|
|
|
+ since you lost your fasting streak. To regain access, become a PRO or achieve a new 3-day fasting streak.`)
|
|
|
+ setShowCancel(true)
|
|
|
+ setCancelText('Achieve New Fasting Streak')
|
|
|
+ setConfirmText('Become a PRO member')
|
|
|
+ Taro.setStorage({ key: 'showLoseGain', data: true })
|
|
|
+
|
|
|
+ changeFastScenaria()
|
|
|
+ }
|
|
|
+
|
|
|
+ function changeFastScenaria() {
|
|
|
+ const { start_time, end_time } = ring.schedule.fast
|
|
|
+ var params: any = {
|
|
|
+ scenario: 'FAST',
|
|
|
+ schedule: {
|
|
|
+ fast: {
|
|
|
+ start_time: start_time,
|
|
|
+ end_time: end_time,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+ setPlan(params).then(res => {
|
|
|
+ if (global.indexPageRefresh) {
|
|
|
+ global.indexPageRefresh()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ async function getStorage(key: string) {
|
|
|
+ try {
|
|
|
+ const res = await Taro.getStorage({ key });
|
|
|
+ return res.data;
|
|
|
+ } catch {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
function upgrade() {
|
|
|
+ const { prev_input, start_time, end_time } = ring.schedule.sleep
|
|
|
+ if (prev_input) {
|
|
|
+ setShowFastAlert(true)
|
|
|
+ setAlertTitle('Update Sleep Schedule')
|
|
|
+ setAlertContent(`Your sleep schedule was ${start_time} - ${end_time}.
|
|
|
+ Do you want to update to a new schedule?`)
|
|
|
+ setShowCancel(true)
|
|
|
+ setCancelText('Keep old schedule')
|
|
|
+ setConfirmText('Update new shedule')
|
|
|
+ setTimeout(() => {
|
|
|
+ confirmAction = goSetting
|
|
|
+ }, 100)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ goSetting()
|
|
|
+ }
|
|
|
+ // const { expire } = access.current.qualification
|
|
|
+ // setShowFastAlert(true)
|
|
|
+ // setAlertTitle('Congrats on Your PRO Status!')
|
|
|
+ // setAlertContent(`To preserve this status, continue your fasting streak in ${TimeFormatter.countdown(expire)} and begin your sleep streak. Remember to keep both of them going.`)
|
|
|
+ // setShowCancel(false)
|
|
|
+ // setConfirmText(`I got this!`)
|
|
|
+ // timer = setInterval(() => {
|
|
|
+ // debugger
|
|
|
+ // if (new Date().getTime() > expire) {
|
|
|
+ // cancel()
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // setAlertContent(`To preserve this status, continue your fasting streak in ${TimeFormatter.countdown(expire)} and begin your sleep streak. Remember to keep both of them going.`)
|
|
|
+ // }, 1000)
|
|
|
+ }
|
|
|
+
|
|
|
+ function goSetting() {
|
|
|
+ getPlans().then(res => {
|
|
|
+ const data = res as { scenarios: any[] };
|
|
|
+ console.log(data)
|
|
|
+ var targets: any = {}
|
|
|
+ data.scenarios.forEach((item) => {
|
|
|
+ if (item.name == 'FAST') {
|
|
|
+
|
|
|
+ if (item.schedule.fast.duration.prev_input) {
|
|
|
+ item.schedule.fast.duration.last_value = item.schedule.fast.duration.init_value
|
|
|
+ }
|
|
|
+ targets.fast = item
|
|
|
+ }
|
|
|
+ else if (item.name == 'SLEEP') {
|
|
|
+ targets.sleep = item
|
|
|
+ }
|
|
|
+ })
|
|
|
+ dispatch(initTarget(targets))
|
|
|
+ dispatch(updateStep({ step: 2 }))
|
|
|
+ dispatch(chooseMode({ isMixed: true }))
|
|
|
+ setCheckChooseMixed(true)
|
|
|
+ jumpPage(`/pages/clock/Suggest`, 'Suggest', navigation, {})
|
|
|
+ })
|
|
|
|
|
|
}
|
|
|
|
|
|
function confirm() {
|
|
|
+ debugger
|
|
|
setShowFastAlert(false)
|
|
|
+ if (timer) {
|
|
|
+ clearInterval(timer)
|
|
|
+ timer = null
|
|
|
+ }
|
|
|
+ if (confirmAction) confirmAction()
|
|
|
+ confirmAction = null;
|
|
|
}
|
|
|
|
|
|
function cancel() {
|
|
|
+ debugger
|
|
|
setShowFastAlert(false)
|
|
|
+ confirmAction = null;
|
|
|
+ if (timer) {
|
|
|
+ clearInterval(timer)
|
|
|
+ timer = null
|
|
|
+ }
|
|
|
+ if (cancelAction) cancelAction()
|
|
|
+ cancelAction = null;
|
|
|
}
|
|
|
|
|
|
function alertPop() {
|
|
|
return <View className="fast_alert_container">
|
|
|
- <View className="fast_alert_content">
|
|
|
+ <View className="fast_alert_content" catchMove>
|
|
|
<View className="fast_alert_title">{alertTitle}</View>
|
|
|
<View className="fast_alert_detail">{alertContent}</View>
|
|
|
<View className="fast_alert_confirm" onClick={confirm}>{confirmText}</View>
|
|
|
{
|
|
|
showCancel && <View className="fast_alert_cancel" onClick={cancel}>{cancelText}</View>
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
</View>
|
|
|
</View>
|
|
|
}
|