|
|
@@ -1,14 +1,14 @@
|
|
|
import { View, Text } from "@tarojs/components";
|
|
|
import './Metric.scss'
|
|
|
import { setAuth } from "../hooks/werun";
|
|
|
-import { useReady } from "@tarojs/taro";
|
|
|
+import { useDidShow, useReady } from "@tarojs/taro";
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
import { useEffect, useState } from "react";
|
|
|
import Taro from "@tarojs/taro";
|
|
|
import { activityCards, uploadSteps } from "@/services/trackSomething";
|
|
|
import { TimeFormatter } from "@/utils/time_format";
|
|
|
import MetricItem from "./MetricItem";
|
|
|
-import { ResultType, checkFail, checkStart, checkSuccess, setResult } from "@/store/action_results";
|
|
|
+import { ResultType, checkFail, checkRetry, checkStart, checkSuccess, setResult } from "@/store/action_results";
|
|
|
import { uploadSessionKey } from "@/services/common";
|
|
|
|
|
|
export default function Component(props: any) {
|
|
|
@@ -20,12 +20,14 @@ export default function Component(props: any) {
|
|
|
const [isCheking, setIsChecking] = useState(false)
|
|
|
const [list, setList] = useState([])
|
|
|
const dispatch = useDispatch();
|
|
|
- const [returnLoginData,setReturnLoginData] = useState(false)
|
|
|
+ const [returnLoginData, setReturnLoginData] = useState(false)
|
|
|
+ const [showErrorBadge, setShowErrorBadge] = useState(false)
|
|
|
// const [title, setTitle] = useState('打卡')
|
|
|
|
|
|
//未登录<->已登录 状态切换时,执行一次授权检查
|
|
|
useEffect(() => {
|
|
|
- checkAuth()
|
|
|
+ getCards();
|
|
|
+ checkAuth(false)
|
|
|
}, [user.isLogin])
|
|
|
|
|
|
useEffect(() => {
|
|
|
@@ -39,9 +41,13 @@ export default function Component(props: any) {
|
|
|
getCards();
|
|
|
})
|
|
|
|
|
|
+ useDidShow(() => {
|
|
|
+ checkAuth(false)
|
|
|
+ })
|
|
|
+
|
|
|
function getCards() {
|
|
|
activityCards().then(res => {
|
|
|
- if (user.isLogin){
|
|
|
+ if (user.isLogin) {
|
|
|
setReturnLoginData(true)
|
|
|
}
|
|
|
else {
|
|
|
@@ -51,7 +57,7 @@ export default function Component(props: any) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- function checkAuth() {
|
|
|
+ function checkAuth(autoCheck = true) {
|
|
|
console.log(new Date().getTime())
|
|
|
if (user.isLogin) {
|
|
|
// setAuth()
|
|
|
@@ -66,15 +72,19 @@ export default function Component(props: any) {
|
|
|
setAllowRun(true)
|
|
|
// setTitle('打卡')
|
|
|
//自动打卡流程
|
|
|
-
|
|
|
+ if (!autoCheck) {
|
|
|
+ return
|
|
|
+ }
|
|
|
var time = Taro.getStorageSync('lastUploadStepsTime')
|
|
|
if (time) {
|
|
|
var now = new Date().getTime()
|
|
|
if (now - time >= 30 * 24 * 3600 * 1000) {
|
|
|
+ console.log('a')
|
|
|
getWeRunData(true)
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
+ console.log('b')
|
|
|
getWeRunData(true)
|
|
|
}
|
|
|
}
|
|
|
@@ -106,6 +116,12 @@ export default function Component(props: any) {
|
|
|
}
|
|
|
|
|
|
function getWeRunData(autoCheck = false) {
|
|
|
+ if (autoCheck){
|
|
|
+ return
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ dispatch(checkStart());
|
|
|
+ }
|
|
|
|
|
|
// setTitle('打卡');
|
|
|
setAllowRun(true)
|
|
|
@@ -124,29 +140,31 @@ export default function Component(props: any) {
|
|
|
cloudID: res.cloudID,
|
|
|
}
|
|
|
uploadSteps(params).then(res => {
|
|
|
- if ((res as any).error_code=='WX_STEP_PARSE_FAIL'){
|
|
|
+ if ((res as any).error_code == 'WX_STEP_PARSE_FAIL') {
|
|
|
retry(params, autoCheck, time)
|
|
|
- if (!autoCheck){
|
|
|
- Taro.showToast({
|
|
|
- icon:'none',
|
|
|
- title:(res as any).error_message
|
|
|
- })
|
|
|
- }
|
|
|
+ dispatch(checkRetry())
|
|
|
+ // if (!autoCheck) {
|
|
|
+ // Taro.showToast({
|
|
|
+ // icon: 'none',
|
|
|
+ // title: (res as any).error_message
|
|
|
+ // })
|
|
|
+ // }
|
|
|
}
|
|
|
else {
|
|
|
+ setShowErrorBadge(false)
|
|
|
uploadSuccess(res, autoCheck, time)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}).catch(e => {
|
|
|
retry(params, autoCheck, time)
|
|
|
Taro.checkSession({
|
|
|
success: function () {
|
|
|
- console.log('打卡后获取session_key 未过期')
|
|
|
+ console.log('打卡后获取session_key 未过期')
|
|
|
},
|
|
|
fail: function () {
|
|
|
- console.log('打卡后获取session_key 已过期')
|
|
|
+ console.log('打卡后获取session_key 已过期')
|
|
|
}
|
|
|
- })
|
|
|
+ })
|
|
|
// if (!autoCheck)
|
|
|
// dispatch(setResult({ isSuccess: false }) as any);
|
|
|
});
|
|
|
@@ -163,7 +181,19 @@ export default function Component(props: any) {
|
|
|
params.iv = res2.iv
|
|
|
params.cloudID = res2.cloudID
|
|
|
uploadSteps(params).then(res => {
|
|
|
- uploadSuccess(res, autoCheck, time)
|
|
|
+ if ((res as any).error_code == 'WX_STEP_PARSE_FAIL') {
|
|
|
+ if (!autoCheck)
|
|
|
+ {
|
|
|
+ dispatch(setResult({ isSuccess: false }) as any);
|
|
|
+ setShowErrorBadge(true)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ setShowErrorBadge(false)
|
|
|
+ uploadSuccess(res, autoCheck, time)
|
|
|
+ }
|
|
|
+
|
|
|
}).catch(e => {
|
|
|
if (!autoCheck)
|
|
|
dispatch(setResult({ isSuccess: false }) as any);
|
|
|
@@ -191,6 +221,9 @@ export default function Component(props: any) {
|
|
|
|
|
|
function goDetail() {
|
|
|
if (user.isLogin) {
|
|
|
+ if (!allowRun){
|
|
|
+ return;
|
|
|
+ }
|
|
|
Taro.navigateTo({
|
|
|
url: '/pages/RecordsHistory?type=activity'
|
|
|
})
|
|
|
@@ -212,7 +245,6 @@ export default function Component(props: any) {
|
|
|
{
|
|
|
btnTitle = '打卡'
|
|
|
isEnable = true
|
|
|
-
|
|
|
}
|
|
|
break;
|
|
|
case ResultType.ing:
|
|
|
@@ -221,6 +253,12 @@ export default function Component(props: any) {
|
|
|
isEnable = false
|
|
|
}
|
|
|
break;
|
|
|
+ case ResultType.retry:
|
|
|
+ {
|
|
|
+ btnTitle = '仍在打卡中...'
|
|
|
+ isEnable = false
|
|
|
+ }
|
|
|
+ break;
|
|
|
case ResultType.success:
|
|
|
{
|
|
|
btnTitle = '打卡成功'
|
|
|
@@ -277,12 +315,18 @@ export default function Component(props: any) {
|
|
|
unit = '步'
|
|
|
}
|
|
|
|
|
|
- if (!user.isLogin || !returnLoginData){
|
|
|
+ if (!user.isLogin|| !returnLoginData) {
|
|
|
value = '未登录'
|
|
|
desc = '登录后可开启打卡'
|
|
|
unit = ''
|
|
|
}
|
|
|
|
|
|
+ // if (user.isLogin && !allowRun){
|
|
|
+ // value = '0'
|
|
|
+ // desc = '开启步数仅自己可见'
|
|
|
+ // unit = ''
|
|
|
+ // }
|
|
|
+
|
|
|
return <MetricItem title={item.name}
|
|
|
// value={allowRun ? stepInfo ? (stepInfo as any).step : '' : '未开启'}
|
|
|
value={value}
|
|
|
@@ -292,6 +336,7 @@ export default function Component(props: any) {
|
|
|
isDisabled={!isEnable}
|
|
|
themeColor={item.theme_color}
|
|
|
onClickDetail={goDetail}
|
|
|
+ showBadge={showErrorBadge && checkResult.type == 'idle'}
|
|
|
onClick={() => {
|
|
|
checkout()
|
|
|
}}
|