|
@@ -8,8 +8,9 @@ import Taro from "@tarojs/taro";
|
|
|
import { activityCards, uploadSteps } from "@/services/trackSomething";
|
|
import { activityCards, uploadSteps } from "@/services/trackSomething";
|
|
|
import { TimeFormatter } from "@/utils/time_format";
|
|
import { TimeFormatter } from "@/utils/time_format";
|
|
|
import MetricItem from "./MetricItem";
|
|
import MetricItem from "./MetricItem";
|
|
|
-import { ResultType, checkFail, checkRetry, checkStart, checkSuccess, setResult } from "@/store/action_results";
|
|
|
|
|
|
|
+import { ResultType, checkFail, checkRetry, checkStart, checkSuccess, resetStatus, setResult } from "@/store/action_results";
|
|
|
import { uploadSessionKey } from "@/services/common";
|
|
import { uploadSessionKey } from "@/services/common";
|
|
|
|
|
+import RequestType, { thirdPartRequest } from "@/services/thirdPartRequest";
|
|
|
|
|
|
|
|
export default function Component(props: any) {
|
|
export default function Component(props: any) {
|
|
|
const user = useSelector((state: any) => state.user);
|
|
const user = useSelector((state: any) => state.user);
|
|
@@ -17,96 +18,96 @@ export default function Component(props: any) {
|
|
|
const [allowRun, setAllowRun] = useState(false)
|
|
const [allowRun, setAllowRun] = useState(false)
|
|
|
const [stepInfo, setStepInfo] = useState(null)
|
|
const [stepInfo, setStepInfo] = useState(null)
|
|
|
const [lastTime, setLastTime] = useState(new Date().getTime())
|
|
const [lastTime, setLastTime] = useState(new Date().getTime())
|
|
|
- const [isCheking, setIsChecking] = useState(false)
|
|
|
|
|
const [list, setList] = useState([])
|
|
const [list, setList] = useState([])
|
|
|
const dispatch = useDispatch();
|
|
const dispatch = useDispatch();
|
|
|
const [returnLoginData, setReturnLoginData] = useState(false)
|
|
const [returnLoginData, setReturnLoginData] = useState(false)
|
|
|
const [showErrorBadge, setShowErrorBadge] = useState(false)
|
|
const [showErrorBadge, setShowErrorBadge] = useState(false)
|
|
|
|
|
+ const [latestRecord, setLatestRecord] = useState(null)
|
|
|
// const [title, setTitle] = useState('打卡')
|
|
// const [title, setTitle] = useState('打卡')
|
|
|
|
|
|
|
|
- //未登录<->已登录 状态切换时,执行一次授权检查
|
|
|
|
|
|
|
+ //未登录<->已登录 状态切换时,重新拉取一次列表数据
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
|
|
+ dispatch(resetStatus())
|
|
|
getCards();
|
|
getCards();
|
|
|
- checkAuth(false)
|
|
|
|
|
|
|
+ // checkAuth(false)
|
|
|
}, [user.isLogin])
|
|
}, [user.isLogin])
|
|
|
|
|
|
|
|
|
|
+ // useEffect(() => {
|
|
|
|
|
+ // Taro.setStorage({key:'auth',data:allowRun})
|
|
|
|
|
+ // },[allowRun])
|
|
|
|
|
+
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
- console.log('checkResult.type:' + checkResult.type)
|
|
|
|
|
|
|
+ Taro.getStorage({
|
|
|
|
|
+ key: 'auth', success: function (res) {
|
|
|
|
|
+ setAllowRun(res.data ? res.data : false)
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}, [
|
|
}, [
|
|
|
- checkResult.type
|
|
|
|
|
])
|
|
])
|
|
|
|
|
|
|
|
//页面渲染完成后执行一次授权检查
|
|
//页面渲染完成后执行一次授权检查
|
|
|
useReady(() => {
|
|
useReady(() => {
|
|
|
- getCards();
|
|
|
|
|
|
|
+
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
useDidShow(() => {
|
|
useDidShow(() => {
|
|
|
- checkAuth(false)
|
|
|
|
|
|
|
+ if (list.length > 0) {
|
|
|
|
|
+ checkAuth()
|
|
|
|
|
+ }
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ global.refreshActivity = () => {
|
|
|
|
|
+ getCards()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
function getCards() {
|
|
function getCards() {
|
|
|
activityCards().then(res => {
|
|
activityCards().then(res => {
|
|
|
- if (user.isLogin) {
|
|
|
|
|
- setReturnLoginData(true)
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- setReturnLoginData(false)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ checkAuth()
|
|
|
setList((res as any).cards)
|
|
setList((res as any).cards)
|
|
|
|
|
+ for (var i = 0; i < (res as any).cards.length; i++) {
|
|
|
|
|
+ var obj = (res as any).cards[i];
|
|
|
|
|
+ if (obj.code == '_walk') {
|
|
|
|
|
+ setLatestRecord(obj.latest_record)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }).catch(e => {
|
|
|
|
|
+ console.log('请求超时')
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function checkAuth(autoCheck = true) {
|
|
|
|
|
|
|
+ global.activityCardsFunc = getCards
|
|
|
|
|
+
|
|
|
|
|
+ function checkAuth() {
|
|
|
console.log(new Date().getTime())
|
|
console.log(new Date().getTime())
|
|
|
if (user.isLogin) {
|
|
if (user.isLogin) {
|
|
|
- // setAuth()
|
|
|
|
|
Taro.getSetting({
|
|
Taro.getSetting({
|
|
|
success: res => {
|
|
success: res => {
|
|
|
//第一步,检测是否有授权 - 没有授权
|
|
//第一步,检测是否有授权 - 没有授权
|
|
|
if (!res.authSetting['scope.werun']) {
|
|
if (!res.authSetting['scope.werun']) {
|
|
|
- // setTitle('开启')
|
|
|
|
|
setAllowRun(false)
|
|
setAllowRun(false)
|
|
|
|
|
+ Taro.setStorage({ key: 'auth', data: false })
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
setAllowRun(true)
|
|
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)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ Taro.setStorage({ key: 'auth', data: true })
|
|
|
}
|
|
}
|
|
|
- console.log(new Date().getTime())
|
|
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- // Taro.setStorageSync('lastUploadStepsTime', time)
|
|
|
|
|
setAllowRun(false)
|
|
setAllowRun(false)
|
|
|
- // setTitle('开启');
|
|
|
|
|
|
|
+ Taro.setStorage({ key: 'auth', data: false })
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function checkout() {
|
|
|
|
|
- // console.log('开始获取步数的时间戳:' + new Date().getTime())
|
|
|
|
|
- if (allowRun) {
|
|
|
|
|
- setIsChecking(true)
|
|
|
|
|
- dispatch(checkStart());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ function tapBtn() {
|
|
|
if (user.isLogin) {
|
|
if (user.isLogin) {
|
|
|
- setAuth(getWeRunData, refuseAuth)
|
|
|
|
|
|
|
+ if (allowRun) {
|
|
|
|
|
+ checkout()
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ setAuth(successAuth, refuseAuth)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
Taro.navigateTo({
|
|
Taro.navigateTo({
|
|
@@ -115,11 +116,29 @@ export default function Component(props: any) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ function successAuth() {
|
|
|
|
|
+ Taro.setStorage({ key: 'auth', data: true })
|
|
|
|
|
+ setAllowRun(true)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function refuseAuth() {
|
|
|
|
|
+ // setTitle('开启');
|
|
|
|
|
+ Taro.setStorage({ key: 'auth', data: false })
|
|
|
|
|
+ setAllowRun(false)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function checkout() {
|
|
|
|
|
+ dispatch(checkStart());
|
|
|
|
|
+ getWeRunData(false)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
function getWeRunData(autoCheck = false) {
|
|
function getWeRunData(autoCheck = false) {
|
|
|
- if (autoCheck){
|
|
|
|
|
|
|
+ if (autoCheck) {
|
|
|
|
|
+ console.log('自动打卡')
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
|
|
+ console.log('开始打卡')
|
|
|
dispatch(checkStart());
|
|
dispatch(checkStart());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -128,48 +147,39 @@ export default function Component(props: any) {
|
|
|
var date = new Date();
|
|
var date = new Date();
|
|
|
var time = date.getTime()
|
|
var time = date.getTime()
|
|
|
var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
|
|
var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
|
|
|
- Taro.getWeRunData({
|
|
|
|
|
- success: res => {
|
|
|
|
|
- // console.log('已获取步数的时间戳:' + new Date().getTime())
|
|
|
|
|
- var params = {
|
|
|
|
|
- is_manual: autoCheck ? 0 : 1,
|
|
|
|
|
- timestamp: time,
|
|
|
|
|
- encryptedData: res.encryptedData,
|
|
|
|
|
- iv: res.iv,
|
|
|
|
|
- date: strDate,
|
|
|
|
|
- cloudID: res.cloudID,
|
|
|
|
|
- }
|
|
|
|
|
- uploadSteps(params).then(res => {
|
|
|
|
|
- if ((res as any).error_code == 'WX_STEP_PARSE_FAIL') {
|
|
|
|
|
- retry(params, autoCheck, time)
|
|
|
|
|
- dispatch(checkRetry())
|
|
|
|
|
- // if (!autoCheck) {
|
|
|
|
|
- // Taro.showToast({
|
|
|
|
|
- // icon: 'none',
|
|
|
|
|
- // title: (res as any).error_message
|
|
|
|
|
- // })
|
|
|
|
|
- // }
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- setShowErrorBadge(false)
|
|
|
|
|
- uploadSuccess(res, autoCheck, time)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ console.log('开始取步数的时间戳:' + new Date().getTime())
|
|
|
|
|
|
|
|
- }).catch(e => {
|
|
|
|
|
- retry(params, autoCheck, time)
|
|
|
|
|
- Taro.checkSession({
|
|
|
|
|
- success: function () {
|
|
|
|
|
- console.log('打卡后获取session_key 未过期')
|
|
|
|
|
- },
|
|
|
|
|
- fail: function () {
|
|
|
|
|
- console.log('打卡后获取session_key 已过期')
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- // if (!autoCheck)
|
|
|
|
|
- // dispatch(setResult({ isSuccess: false }) as any);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+
|
|
|
|
|
+ thirdPartRequest(RequestType.RequestTypeWXRunData).then(res => {
|
|
|
|
|
+ var params = {
|
|
|
|
|
+ is_manual: autoCheck ? 0 : 1,
|
|
|
|
|
+ timestamp: time,
|
|
|
|
|
+ encryptedData: (res as any).encryptedData,
|
|
|
|
|
+ iv: (res as any).iv,
|
|
|
|
|
+ date: strDate,
|
|
|
|
|
+ cloudID: (res as any).cloudID,
|
|
|
}
|
|
}
|
|
|
|
|
+ uploadSteps(params).then(res => {
|
|
|
|
|
+ if ((res as any).error_code == 'WX_STEP_PARSE_FAIL') {
|
|
|
|
|
+ retry(params, autoCheck, time)
|
|
|
|
|
+ dispatch(checkRetry())
|
|
|
|
|
+ console.log('上报失败 ')
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ console.log('上报成功 ')
|
|
|
|
|
+ setShowErrorBadge(false)
|
|
|
|
|
+ uploadSuccess(res, autoCheck, time)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }).catch(e => {
|
|
|
|
|
+ retry(params, autoCheck, time)
|
|
|
|
|
+ });
|
|
|
|
|
+ }).catch(_ => {
|
|
|
|
|
+ console.log('回调失败')
|
|
|
|
|
+ dispatch(setResult({ isSuccess: false }) as any)
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function retry(params, autoCheck, time) {
|
|
function retry(params, autoCheck, time) {
|
|
@@ -182,21 +192,24 @@ export default function Component(props: any) {
|
|
|
params.cloudID = res2.cloudID
|
|
params.cloudID = res2.cloudID
|
|
|
uploadSteps(params).then(res => {
|
|
uploadSteps(params).then(res => {
|
|
|
if ((res as any).error_code == 'WX_STEP_PARSE_FAIL') {
|
|
if ((res as any).error_code == 'WX_STEP_PARSE_FAIL') {
|
|
|
- if (!autoCheck)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (!autoCheck) {
|
|
|
dispatch(setResult({ isSuccess: false }) as any);
|
|
dispatch(setResult({ isSuccess: false }) as any);
|
|
|
setShowErrorBadge(true)
|
|
setShowErrorBadge(true)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
|
|
+
|
|
|
setShowErrorBadge(false)
|
|
setShowErrorBadge(false)
|
|
|
uploadSuccess(res, autoCheck, time)
|
|
uploadSuccess(res, autoCheck, time)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}).catch(e => {
|
|
}).catch(e => {
|
|
|
- if (!autoCheck)
|
|
|
|
|
|
|
+ if (!autoCheck) {
|
|
|
dispatch(setResult({ isSuccess: false }) as any);
|
|
dispatch(setResult({ isSuccess: false }) as any);
|
|
|
|
|
+ setShowErrorBadge(true)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
@@ -209,23 +222,26 @@ export default function Component(props: any) {
|
|
|
dispatch(setResult({ isSuccess: true }) as any)
|
|
dispatch(setResult({ isSuccess: true }) as any)
|
|
|
setStepInfo((res as any)[(res as any).length - 1])
|
|
setStepInfo((res as any)[(res as any).length - 1])
|
|
|
setLastTime(time)
|
|
setLastTime(time)
|
|
|
- setIsChecking(false)
|
|
|
|
|
- getCards();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ debugger
|
|
|
|
|
|
|
|
- function refuseAuth() {
|
|
|
|
|
- // setTitle('开启');
|
|
|
|
|
- setIsChecking(false)
|
|
|
|
|
- setAllowRun(false)
|
|
|
|
|
|
|
+ for (var i = 0; i < list.length; i++) {
|
|
|
|
|
+ var obj = list[i];
|
|
|
|
|
+ if ((obj as any).code == '_walk') {
|
|
|
|
|
+ (obj as any).latest_record = res
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // getCards();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
function goDetail() {
|
|
function goDetail() {
|
|
|
if (user.isLogin) {
|
|
if (user.isLogin) {
|
|
|
- if (!allowRun){
|
|
|
|
|
|
|
+ if (!allowRun) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
Taro.navigateTo({
|
|
Taro.navigateTo({
|
|
|
- url: '/pages/RecordsHistory?type=activity'
|
|
|
|
|
|
|
+ url: '/pages/RecordsHistory?type=activity&refreshList=getCards'
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
@@ -306,19 +322,29 @@ export default function Component(props: any) {
|
|
|
desc = '今天待打卡'
|
|
desc = '今天待打卡'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!allowRun) {
|
|
|
|
|
- value = '0'
|
|
|
|
|
- desc = '开启步数仅自己可见'
|
|
|
|
|
- unit = ''
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- unit = '步'
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- if (!user.isLogin|| !returnLoginData) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (!user.isLogin) {
|
|
|
value = '未登录'
|
|
value = '未登录'
|
|
|
desc = '登录后可开启打卡'
|
|
desc = '登录后可开启打卡'
|
|
|
- unit = ''
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ if (!allowRun) {
|
|
|
|
|
+ value = '未开启'
|
|
|
|
|
+ desc = '开启后可打卡步数'
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ // unit = '步'
|
|
|
|
|
+ if (item.latest_record) {
|
|
|
|
|
+ value = item.latest_record.items[0].value
|
|
|
|
|
+ desc = TimeFormatter.formatTimestamp(item.latest_record.timestamp)
|
|
|
|
|
+ unit = '步'
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ value = '今日待打卡'
|
|
|
|
|
+ desc = '查看历史记录'
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// if (user.isLogin && !allowRun){
|
|
// if (user.isLogin && !allowRun){
|
|
@@ -338,27 +364,11 @@ export default function Component(props: any) {
|
|
|
onClickDetail={goDetail}
|
|
onClickDetail={goDetail}
|
|
|
showBadge={showErrorBadge && checkResult.type == 'idle'}
|
|
showBadge={showErrorBadge && checkResult.type == 'idle'}
|
|
|
onClick={() => {
|
|
onClick={() => {
|
|
|
- checkout()
|
|
|
|
|
|
|
+ tapBtn()
|
|
|
|
|
+ // checkout()
|
|
|
}}
|
|
}}
|
|
|
/>
|
|
/>
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
</View>
|
|
</View>
|
|
|
-
|
|
|
|
|
- return <View className="metric_bg">
|
|
|
|
|
- <Text className="metric_title">行走</Text>
|
|
|
|
|
- {
|
|
|
|
|
- !allowRun && <Text className="metric_value">未开启</Text>
|
|
|
|
|
- }
|
|
|
|
|
- {
|
|
|
|
|
- !allowRun && <Text className="mteric_desc">开启步数仅自己可见</Text>
|
|
|
|
|
- }
|
|
|
|
|
- {
|
|
|
|
|
- allowRun && stepInfo && <Text className="metric_value">{(stepInfo as any).step}<Text className="metric_unit">步</Text></Text>
|
|
|
|
|
- }
|
|
|
|
|
- {
|
|
|
|
|
- allowRun && stepInfo && <Text className="mteric_desc">{TimeFormatter.formatTimestamp(lastTime)}</Text>
|
|
|
|
|
- }
|
|
|
|
|
- <View className="operate" onClick={checkout}>{allowRun ? '打卡' : '开启'}</View>
|
|
|
|
|
- </View>
|
|
|
|
|
}
|
|
}
|