Leon 2 anni fa
parent
commit
ffbe9a4266

+ 17 - 0
src/components/SlidngScale.scss

@@ -2,6 +2,23 @@
     position: relative;
 }
 
+.number_bg{
+    display: flex;
+    align-items: center;
+    flex-direction: row;
+    justify-content: center;
+}
+
+.number{
+    text-align: center;
+    // flex-grow: 1;
+    // background-color: red;
+}
+
+.unit{
+    margin-left: 4px;
+}
+
 .value{
     width: 750px;
     display: flex;

+ 16 - 13
src/components/SlidngScale.tsx

@@ -3,12 +3,12 @@ import './SlidngScale.scss'
 import { useEffect, useRef, useState } from "react";
 import Taro from "@tarojs/taro";
 
-export default function Component(props:{step:number,min:number,max:number,default_value:number,changed:Function}) {
+export default function Component(props: { step: number, min: number, max: number, default_value: number, changed: Function, unit: string }) {
     const scrollViewRef = useRef<any>();
     const minNum: number = props.min;
     const maxNum: number = props.max;
-    const stepNum: number = props.step*10;
-    const jingdu: number = props.step<1?10:1;
+    const stepNum: number = props.step * 10;
+    const jingdu: number = props.step < 1 ? 10 : 1;
     const list: any[] = [];
 
     // console.log(750/Taro.getSystemInfoSync().screenWidth)
@@ -17,18 +17,18 @@ export default function Component(props:{step:number,min:number,max:number,defau
     const slidngWidth = 10
 
     const [current, setCurrent] = useState(props.default_value)
-    const [left, setLeft] = useState((props.default_value-props.min)*slidngWidth/props.step+1);
+    const [left, setLeft] = useState((props.default_value - props.min) * slidngWidth / props.step + 1);
     for (var i: number = props.min; i <= props.max; i += props.step) {
         var value: number = parseFloat(i.toFixed(1));
         var isBig: boolean = Math.round((value - minNum) * jingdu) % Math.round(stepNum * jingdu) == 0;
-        var isMiddle:boolean = (Math.round((value - minNum) * jingdu)*2) % Math.round(stepNum * jingdu) == 0;
+        var isMiddle: boolean = (Math.round((value - minNum) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0;
 
         list.push({
-            value: props.step<1?value:Math.round(value),
+            value: props.step < 1 ? value : Math.round(value),
             showBig: isBig && i >= minNum && i <= maxNum,
             showMiddle: isMiddle && !isBig && i >= minNum && i <= maxNum
         })
-        
+
     }
 
     useEffect(() => {
@@ -39,7 +39,7 @@ export default function Component(props:{step:number,min:number,max:number,defau
 
     // const dpr = Taro.pxTransform//Taro.getSystemInfoSync().pixelRatio; 
 
-    
+
     const handleScroll = (e) => {
         const { scrollLeft } = e.detail;
 
@@ -55,13 +55,13 @@ export default function Component(props:{step:number,min:number,max:number,defau
 
         if (parseFloat(strValue) != current) {
             var data = strValue as any;
-            if (props.step<1) {
+            if (props.step < 1) {
                 data = parseFloat(strValue).toFixed(1);
             }
             else {
                 data = Math.round(data);
             }
-            
+
             setCurrent(data);
 
             props.changed(data);
@@ -69,14 +69,17 @@ export default function Component(props:{step:number,min:number,max:number,defau
     };
 
     return <View className="slidng">
-        <Text className="value">{current}</Text>
+        <View className="number_bg">
+            <Text className="number">{current}</Text>
+            <Text className="unit">{props.unit}</Text>
+        </View>
         <ScrollView ref={scrollViewRef} scrollX scrollLeft={left} className="scroll" onScroll={handleScroll}>
             <View className="scrollContent">
                 <View className="scrollPadding" />
                 <View className="content">
                     {
                         list.map((item, index) => {
-                            return <View className={item.showBig ? 'slidng_item_big' : item.showMiddle?'slidng_item_middle':'slidng_item'} style={{width:1,marginRight:9}} key={index}>
+                            return <View className={item.showBig ? 'slidng_item_big' : item.showMiddle ? 'slidng_item_middle' : 'slidng_item'} style={{ width: 1, marginRight: 9 }} key={index}>
                                 {
                                     item.showBig ? <Text className="slidng_text">{item.value}</Text> : null
                                 }
@@ -90,6 +93,6 @@ export default function Component(props:{step:number,min:number,max:number,defau
 
         </ScrollView>
         <View className="center_line" />
-        
+
     </View>
 }

+ 66 - 21
src/features/trackSomething/components/Activity.tsx

@@ -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()
                     }}

+ 6 - 1
src/features/trackSomething/components/Metric.tsx

@@ -67,6 +67,9 @@ export default function Component(props: any) {
     }
     function goDetail(item) {
         if (user.isLogin) {
+            if (!item.latest_record) {
+                return
+            }
             Taro.navigateTo({
                 url: '/pages/RecordsHistory?type=metric&code=' + item.code
             })
@@ -274,7 +277,9 @@ export default function Component(props: any) {
                                     {
                                         (metricItem as any).schemas.length > 1 && <Text style={{ textAlign: 'center', width: '100%', display: 'flex', justifyContent: 'center' }}>{item.name}</Text>
                                     }
-                                    <SlidngScale step={item.step} min={item.min} max={item.max} default_value={item.default_value} changed={(e) => { item.tempValue = e }} />
+                                    <SlidngScale step={item.step} min={item.min} max={item.max} default_value={item.default_value}
+                                        unit={item.default_unit}
+                                        changed={(e) => { item.tempValue = e }} />
                                 </View>
                             })
                         }

+ 1 - 1
src/features/trackSomething/components/MetricHistory.tsx

@@ -57,7 +57,7 @@ export default function Component(props: { records: any[] }) {
                                 {
                                     record.items.length>2 && <Text className="value">/{record.items[2].value}</Text>
                                 }
-                                <Text className="unit"></Text>
+                                <Text className="unit">{record.items[0].unit}</Text>
                                 <View style={{ flex: 1 }} />
                                 <Text className="time">{formateHourMinutes(record.timestamp)}</Text>
                             </View>

+ 12 - 0
src/features/trackSomething/components/MetricItem.scss

@@ -65,4 +65,16 @@
     color: #000;
     line-height: 84px;
     text-align: center;
+    position: relative;
+}
+
+.badge{
+    position: absolute;
+    right: 80px;
+    top: 20px;
+    width: 8px;
+    height: 8px;
+    border-radius: 4px;
+    background-color: red;
+
 }

+ 7 - 2
src/features/trackSomething/components/MetricItem.tsx

@@ -10,7 +10,8 @@ export default function Component(props: {
     themeColor: string,
     isDisabled?: boolean,
     onClickDetail: Function,
-    onClick: Function
+    onClick: Function,
+    showBadge?: boolean
 }) {
 
     function clickBtn() {
@@ -29,6 +30,10 @@ export default function Component(props: {
             <Text className="mteric_desc">{props.desc}</Text>
         </View>
 
-        <View className="operate" style={{ backgroundColor: props.themeColor, opacity: props.isDisabled ? 0.5 : 1 }} onClick={clickBtn}>{props.btnText}</View>
+        <View className="operate" style={{ backgroundColor: props.themeColor, opacity: props.isDisabled ? 0.5 : 1 }} onClick={clickBtn}>{props.btnText}
+            {
+                props.showBadge && <View className="badge"></View>
+            }
+        </View>
     </View>
 }

+ 3 - 0
src/services/http/request.ts

@@ -68,6 +68,9 @@ export async function request<T>(param: RequestParam): Promise<T> {
                 else if (statusCode == 401) {
                     global.dispatch(logoutSuccess());
                 }
+                else if (statusCode == 500 && response.data.error_code == 'WX_STEP_PARSE_FAIL'){
+                    resolve(response.data);
+                }
                 else {
                     Taro.showToast({
                         icon: 'none',

+ 5 - 1
src/store/action_results.tsx

@@ -6,6 +6,7 @@ export enum ResultType {
     ing = 'ing',
     success = 'success',
     fail = 'fail',
+    retry = 'retry',
     countdown = 'countdown'
 
 }
@@ -37,6 +38,9 @@ const resultSlice = createSlice({
         checkFail(state) {
             state.type = ResultType.fail;
         },
+        checkRetry(state){
+            state.type = ResultType.retry;
+        },
         checkCountdown(state,action: PayloadAction<any>) {
             state.title = action.payload;
             state.type = ResultType.countdown;
@@ -53,7 +57,7 @@ const resultSlice = createSlice({
 }
 );
 
-export const { checkStart, checkSuccess, checkFail, checkCountdownEnd, checkCountdown, resetStatus } = resultSlice.actions;
+export const { checkStart, checkSuccess, checkFail, checkCountdownEnd, checkCountdown, resetStatus,checkRetry } = resultSlice.actions;
 export default resultSlice.reducer;
 
 export const setResult = createAsyncThunk('result/action', async (payload: {isSuccess:boolean}, { dispatch }) => {